This repository has been archived by the owner on Nov 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
garage.hap.js
124 lines (106 loc) · 3.88 KB
/
garage.hap.js
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const
Accessory = require('./hap/').Accessory,
Service = require('./hap').Service,
Characteristic = require('./hap').Characteristic,
uuid = require('./hap').uuid,
EventEmitter = require('events');
class GarageHap extends EventEmitter {
constructor(config) {
super();
//config settings
this.username = config.username;
this.pincode = config.pincode;
this.port = config.port;
this.accessory = undefined;
if (!this.username)
throw new Error('Username is not provided');
if (!this.pincode)
throw new Error('Pincode is not provided');
}
closed() {
this.doorState = Characteristic.CurrentDoorState.CLOSED;
}
opened() {
this.doorState = Characteristic.CurrentDoorState.OPEN;
}
opening() {
this.doorState = Characteristic.CurrentDoorState.OPENING;
}
closing() {
this.doorState = Characteristic.CurrentDoorState.CLOSING;
}
stopped() {
this.doorState = Characteristic.CurrentDoorState.STOPPED;
}
set doorState(val) {
this.accessory
.getService(Service.GarageDoorOpener)
.setCharacteristic(Characteristic.CurrentDoorState, val);
}
createAccessory() {
if (this.accessory) {
return this.accessory;
}
const garageUUID = uuid.generate('hap-nodejs:accessories:' + 'GarageDoor'),
accessory = this.accessory = new Accessory('Garage Door', garageUUID);
accessory.username = this.username;
accessory.pincode = this.pincode;
accessory
.getService(Service.AccessoryInformation)
.setCharacteristic(Characteristic.Manufacturer, "Liftmaster")
.setCharacteristic(Characteristic.Model, "Rev-1")
.setCharacteristic(Characteristic.SerialNumber, "TW000165");
accessory.on('identify', (paired, callback) => {
console.log('GarageHap.identify');
this.emit('identify');
callback();
});
accessory
.addService(Service.GarageDoorOpener, "Garage Door")
.setCharacteristic(Characteristic.TargetDoorState, Characteristic.TargetDoorState.CLOSED)
.getCharacteristic(Characteristic.TargetDoorState)
.on('set', (value, callback) => {
if (value == Characteristic.TargetDoorState.CLOSED) {
console.log('GarageHap.close');
this.emit('close');
callback();
}
else if (value == Characteristic.TargetDoorState.OPEN) {
console.log('GarageHap.open');
this.emit('open');
callback();
}
});
accessory
.getService(Service.GarageDoorOpener)
.getCharacteristic(Characteristic.CurrentDoorState)
.on('get', (callback) => {
console.log('GarageHap.get');
var state = {
isClosed: undefined
};
this.emit('status', state);
var err = null;
if (state.isClosed === true) {
console.log("Query: Is Garage Open? Yes");
callback(err, Characteristic.CurrentDoorState.CLOSED);
}
else if (state.isClosed === false) {
console.log("Query: Is Garage Open? No");
callback(err, Characteristic.CurrentDoorState.OPEN);
}
else {
console.log("Query: Is Garage Open? Unknown");
}
});
return accessory;
}
start() {
this.createAccessory().publish({
port: this.port,
username: this.username,
pincode: this.pincode
});
}
}
module.exports = GarageHap;