generated from homebridge/homebridge-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgaragestate.ts
91 lines (73 loc) · 2.3 KB
/
garagestate.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
import { API, Characteristic, Logger } from 'homebridge';
import { EventEmitter } from 'events';
export class GarageState extends EventEmitter {
private current: number;
private target: number;
private readonly Characteristic: typeof Characteristic;
private readonly emitter: EventEmitter;
private readonly log: Logger;
constructor(api: API, log: Logger) {
super();
this.Characteristic = api.hap.Characteristic;
this.emitter = new EventEmitter();
this.current = -1;
this.target = -1;
this.log = log;
}
public updateCurrentState(current: number, emit: boolean = true) {
if (this.current === current) {
return;
}
this.current = current;
if (emit === true) {
this.emit('current', current);
}
}
public updateTargetState(target: number, emit: boolean = true) {
if (this.target === target) {
return;
}
this.target = target;
if (emit === true) {
this.emit('target', target);
}
}
public getCurrentState(): number {
return this.current;
}
public getTargetState(): number {
return this.target;
}
public getCurrentDescription(): string {
switch (this.current) {
case this.Characteristic.CurrentDoorState.OPEN:
return 'Open';
case this.Characteristic.CurrentDoorState.CLOSED:
return 'Closed';
case this.Characteristic.CurrentDoorState.OPENING:
return 'Opening';
case this.Characteristic.CurrentDoorState.CLOSING:
return 'Closing';
case this.Characteristic.CurrentDoorState.STOPPED:
return 'Stopped';
}
return 'unknown';
}
public targetDoorStateForCurrent(value: number): number {
switch (value) {
case this.Characteristic.CurrentDoorState.OPEN:
return this.Characteristic.TargetDoorState.OPEN;
case this.Characteristic.CurrentDoorState.CLOSED:
return this.Characteristic.TargetDoorState.CLOSED;
case this.Characteristic.CurrentDoorState.OPENING:
return this.Characteristic.TargetDoorState.OPEN;
case this.Characteristic.CurrentDoorState.CLOSING:
return this.Characteristic.TargetDoorState.CLOSED;
default:
return -1;
}
}
public description(): string {
return 'current: ' + this.getCurrentDescription() + ', target: ' + this.target;
}
}