generated from homebridge/homebridge-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
motor.ts
107 lines (87 loc) · 4.1 KB
/
motor.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { CharacteristicValue, PlatformAccessory, Service } from 'homebridge';
import { LoadState } from './model/loadstate';
import { FellerWiserPlatform } from './platform';
export class Motor {
protected service : Service;
// new model
protected positionState : CharacteristicValue = 0;
protected currentPosition : CharacteristicValue = 0;
protected targetPosition : CharacteristicValue = 0;
// end new model
constructor (
protected readonly platform : FellerWiserPlatform,
protected readonly accessory: PlatformAccessory,
){
this.accessory.getService(this.platform.Service.AccessoryInformation)!
.setCharacteristic(this.platform.Characteristic.Manufacturer, 'Feller AG')
.setCharacteristic(this.platform.Characteristic.Model, 'undefined')
.setCharacteristic(this.platform.Characteristic.SerialNumber, accessory.UUID);
this.service = this.accessory.getService(this.platform.Service.WindowCovering)
|| this.accessory.addService(this.platform.Service.WindowCovering);
this.service.setCharacteristic(this.platform.Characteristic.Name, accessory.context.load.name);
this.service.getCharacteristic(this.platform.Characteristic.CurrentPosition)
.onGet(this.getCurrentPosition.bind(this));
this.service.getCharacteristic(this.platform.Characteristic.PositionState)
.onGet(this.getPositionState.bind(this));
this.service.getCharacteristic(this.platform.Characteristic.TargetPosition)
.onGet(this.getTargetPosition.bind(this))
.onSet(this.setTargetPosition.bind(this));
this.service.getCharacteristic(this.platform.Characteristic.HoldPosition)
.onSet(this.setHoldPosition.bind(this));
this.platform.fellerClient.loadStateChange.on(
this.accessory.context.load.id.toString(),
(loadState) => this.updateState(loadState));
}
async getCurrentPosition() : Promise<CharacteristicValue> {
return this.currentPosition;
}
async getPositionState() : Promise<CharacteristicValue> {
return this.positionState;
}
async getTargetPosition() : Promise<CharacteristicValue>{
return this.targetPosition;
}
async setTargetPosition(value: CharacteristicValue): Promise<void> {
this.platform.log.debug('setTargetPosition ', value);
if ('number' === typeof value){
this.targetPosition = value;
return this.platform.fellerClient.setLoadState(this.accessory.context.load.id,
{
'level' : (100-value) * 100,
})
.then(() => {
this.service.updateCharacteristic(this.platform.Characteristic.TargetPosition, (100-value * 100));
return;
});
}
}
async updateState (loadState: LoadState){
this.platform.log.debug('updateState', loadState);
switch(loadState.moving){
case 'stop':
this.positionState = this.platform.Characteristic.PositionState.STOPPED;
this.service.updateCharacteristic(this.platform.Characteristic.TargetPosition, (10000-loadState.level!) / 100);
this.targetPosition = (10000-loadState.level!) / 100;
break;
case 'down':
this.positionState = this.platform.Characteristic.PositionState.DECREASING;
break;
case 'up':
this.positionState = this.platform.Characteristic.PositionState.INCREASING;
break;
}
this.service.updateCharacteristic(this.platform.Characteristic.CurrentPosition, (10000-loadState.level!) / 100);
this.service.updateCharacteristic( this.platform.Characteristic.PositionState, this.positionState);
this.currentPosition = (10000-loadState.level!) / 100;
this.service.updateCharacteristic(this.platform.Characteristic.CurrentPosition, (10000-loadState.level!) / 100);
this.platform.log.debug('position state', this.positionState);
this.platform.log.debug('current position', this.currentPosition);
this.platform.log.debug('target position', this.targetPosition);
}
async setHoldPosition() : Promise<void> {
this.platform.fellerClient.ctrlLoad(this.accessory.context.load.id, {
button: 'stop',
event: 'click',
});
}
}