author | Luke Hoersten <luke@hoersten.org> |
Sat, 11 Jan 2020 18:09:32 -0600 | |
changeset 25 | bc6c646f1814 |
parent 22 | 00185499ed12 |
permissions | -rw-r--r-- |
0 | 1 |
var Accessory = require('../').Accessory; |
2 |
var Service = require('../').Service; |
|
3 |
var Characteristic = require('../').Characteristic; |
|
4 |
var uuid = require('../').uuid; |
|
5 |
var PythonShell = require('python-shell'); |
|
6 |
||
7 |
var door = exports.accessory = new Accessory('Door', uuid.generate('hap-nodejs:accessories:door')); |
|
8 |
door.username = 'C1:5D:3A:EA:54:AB'; |
|
9 |
door.pincode = '031-45-154'; |
|
10 |
||
11 |
door.getService(Service.AccessoryInformation) |
|
12 |
.setCharacteristic(Characteristic.Manufacturer, 'Raspberry Pi') |
|
13 |
.setCharacteristic(Characteristic.Model, 'Zero W') |
|
14 |
.setCharacteristic(Characteristic.SerialNumber, 'A1S2NASF88EW'); |
|
15 |
||
16 |
var DOOR = { |
|
17 |
lockTimeout: 10000, // milliseconds - 10 sec |
|
18 |
||
19 |
pyshell: new PythonShell('doord.py', { |
|
20 |
mode: 'text', |
|
21 |
pythonPath: '/usr/bin/python3', |
|
22 |
pythonOptions: ['-u'], |
|
23 |
scriptPath: 'python/' |
|
24 |
}), |
|
25 |
||
26 |
lock: function() { |
|
27 |
console.log('locking door'); |
|
28 |
this.pyshell.send('lock'); |
|
29 |
}, |
|
30 |
||
31 |
unlock: function() { |
|
32 |
console.log('unlocking door'); |
|
33 |
this.pyshell.send('unlock'); |
|
34 |
}, |
|
35 |
||
36 |
identify: function() { |
|
37 |
console.log('identify door'); |
|
38 |
}, |
|
39 |
||
40 |
listenDoorbell: function(doorbellOnCallback, doorbellOffCallback) { |
|
41 |
this.pyshell.on('message', function (message) { |
|
42 |
console.log(message); |
|
43 |
switch(message) { |
|
11
a70d948427d3
More doorbell debug range tweaks.
Luke Hoersten <luke@hoersten.org>
parents:
3
diff
changeset
|
44 |
case 'doorbell on': |
0 | 45 |
doorbellOnCallback(); |
46 |
break; |
|
11
a70d948427d3
More doorbell debug range tweaks.
Luke Hoersten <luke@hoersten.org>
parents:
3
diff
changeset
|
47 |
case 'doorbell off': |
0 | 48 |
doorbellOffCallback(); |
49 |
break; |
|
50 |
} |
|
51 |
}); |
|
52 |
} |
|
53 |
}; |
|
54 |
||
55 |
door.on('identify', function(paired, callback) { |
|
56 |
DOOR.identify(); |
|
57 |
callback(); |
|
58 |
}); |
|
59 |
||
60 |
door.addService(Service.Doorbell, 'Doorbell'); |
|
61 |
||
22
00185499ed12
Removed psudo services. Not needed anymore!
Luke Hoersten <luke@hoersten.org>
parents:
11
diff
changeset
|
62 |
door.addService(Service.LockMechanism, 'Door lock') |
0 | 63 |
.setCharacteristic(Characteristic.LockTargetState, Characteristic.LockTargetState.SECURED) // force initial state |
64 |
.setCharacteristic(Characteristic.LockCurrentState, Characteristic.LockCurrentState.SECURED) |
|
65 |
.getCharacteristic(Characteristic.LockTargetState) |
|
66 |
.on('set', function(value, callback) { |
|
67 |
setDoorTargetState(value); |
|
68 |
callback(); |
|
69 |
}); |
|
70 |
||
71 |
function setDoorTargetState(value) { |
|
72 |
switch(value) { |
|
73 |
case Characteristic.LockTargetState.UNSECURED: |
|
74 |
unlockDoor(); |
|
75 |
break; |
|
76 |
case Characteristic.LockTargetState.SECURED: |
|
77 |
lockDoor(); |
|
78 |
break; |
|
79 |
} |
|
80 |
} |
|
81 |
||
82 |
function unlockDoor() { |
|
83 |
DOOR.unlock(); |
|
84 |
door.getService(Service.LockMechanism) |
|
85 |
.setCharacteristic(Characteristic.LockCurrentState, |
|
86 |
Characteristic.LockCurrentState.UNSECURED); |
|
87 |
scheduleUnlockTimeout(); |
|
88 |
} |
|
89 |
||
90 |
function lockDoor() { |
|
91 |
DOOR.lock(); |
|
92 |
door.getService(Service.LockMechanism) |
|
93 |
.setCharacteristic(Characteristic.LockCurrentState, |
|
94 |
Characteristic.LockCurrentState.SECURED); |
|
95 |
} |
|
96 |
||
97 |
function scheduleUnlockTimeout() { |
|
98 |
setTimeout(function() { |
|
99 |
console.log('unlock timeout door'); |
|
100 |
door.getService(Service.LockMechanism) |
|
101 |
.setCharacteristic(Characteristic.LockTargetState, |
|
102 |
Characteristic.LockTargetState.SECURED); |
|
103 |
}, DOOR.lockTimeout); |
|
104 |
} |
|
105 |
||
106 |
DOOR.listenDoorbell( |
|
107 |
function() { |
|
108 |
door.getService(Service.Doorbell) |
|
109 |
.setCharacteristic(Characteristic.ProgrammableSwitchEvent, |
|
110 |
Characteristic.ProgrammableSwitchEvent.SINGLE_PRESS); |
|
111 |
}, |
|
112 |
function() {}); |