# HG changeset patch # User Luke Hoersten # Date 1516384464 21600 # Node ID bc6a0f25a2293f873e4f0eee1afa4b11ca711d25 # Parent 30d8bcb1ebb47ede4d996c7f4982ffcfba858e8a Pulled out src files. diff -r 30d8bcb1ebb4 -r bc6a0f25a229 roles/hap-nodejs/files/Door_accessory.js --- a/roles/hap-nodejs/files/Door_accessory.js Fri Jan 19 11:48:25 2018 -0600 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,115 +0,0 @@ -var Accessory = require('../').Accessory; -var Service = require('../').Service; -var Characteristic = require('../').Characteristic; -var uuid = require('../').uuid; -var PythonShell = require('python-shell'); - -var door = exports.accessory = new Accessory('Door', uuid.generate('hap-nodejs:accessories:door')); -door.username = 'C1:5D:3A:EA:54:AB'; -door.pincode = '031-45-154'; - -door.getService(Service.AccessoryInformation) - .setCharacteristic(Characteristic.Manufacturer, 'Raspberry Pi') - .setCharacteristic(Characteristic.Model, 'Zero W') - .setCharacteristic(Characteristic.SerialNumber, 'A1S2NASF88EW'); - -var DOOR = { - lockTimeout: 10000, // milliseconds - 10 sec - - pyshell: new PythonShell('doord.py', { - mode: 'text', - pythonPath: '/usr/bin/python3', - pythonOptions: ['-u'], - scriptPath: 'python/' - }), - - lock: function() { - console.log('locking door'); - this.pyshell.send('lock'); - }, - - unlock: function() { - console.log('unlocking door'); - this.pyshell.send('unlock'); - }, - - identify: function() { - console.log('identify door'); - }, - - listenDoorbell: function(doorbellOnCallback, doorbellOffCallback) { - this.pyshell.on('message', function (message) { - console.log(message); - switch(message) { - case 'doorbell_on': - doorbellOnCallback(); - break; - case 'doorbell_off': - doorbellOffCallback(); - break; - } - }); - } -}; - -door.on('identify', function(paired, callback) { - DOOR.identify(); - callback(); -}); - -door.addService(Service.Doorbell, 'Doorbell'); -door.addService(Service.CameraRTPStreamManagement, 'Psudo-Camera'); -door.addService(Service.Speaker, 'Psudo-Speaker'); -door.addService(Service.Microphone, 'Psudo-Microphone'); - -door.addService(Service.LockMechanism, 'Door') - .setCharacteristic(Characteristic.LockTargetState, Characteristic.LockTargetState.SECURED) // force initial state - .setCharacteristic(Characteristic.LockCurrentState, Characteristic.LockCurrentState.SECURED) - .getCharacteristic(Characteristic.LockTargetState) - .on('set', function(value, callback) { - setDoorTargetState(value); - callback(); - }); - -function setDoorTargetState(value) { - switch(value) { - case Characteristic.LockTargetState.UNSECURED: - unlockDoor(); - break; - case Characteristic.LockTargetState.SECURED: - lockDoor(); - break; - } -} - -function unlockDoor() { - DOOR.unlock(); - door.getService(Service.LockMechanism) - .setCharacteristic(Characteristic.LockCurrentState, - Characteristic.LockCurrentState.UNSECURED); - scheduleUnlockTimeout(); -} - -function lockDoor() { - DOOR.lock(); - door.getService(Service.LockMechanism) - .setCharacteristic(Characteristic.LockCurrentState, - Characteristic.LockCurrentState.SECURED); -} - -function scheduleUnlockTimeout() { - setTimeout(function() { - console.log('unlock timeout door'); - door.getService(Service.LockMechanism) - .setCharacteristic(Characteristic.LockTargetState, - Characteristic.LockTargetState.SECURED); - }, DOOR.lockTimeout); -} - -DOOR.listenDoorbell( - function() { - door.getService(Service.Doorbell) - .setCharacteristic(Characteristic.ProgrammableSwitchEvent, - Characteristic.ProgrammableSwitchEvent.SINGLE_PRESS); - }, - function() {}); diff -r 30d8bcb1ebb4 -r bc6a0f25a229 roles/hap-nodejs/files/doord.py --- a/roles/hap-nodejs/files/doord.py Fri Jan 19 11:48:25 2018 -0600 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,51 +0,0 @@ -#!/usr/bin/env python3 - -import time -import automationhat -import sys - -import queue -import threading - - -def main(): - command_queue = queue.LifoQueue() - read_thread = threading.Thread(target=read_loop, args=[command_queue]) - read_thread.start() - run_loop(command_queue) - - -def read_loop(command_queue): - while True: - command_queue.put_nowait(sys.stdin.readline().rstrip('\n')) - - -def run_loop(command_queue): - thread_local = threading.local() - thread_local.doorbell_on_state = False - - while True: - run_command(command_queue) - read_doorbell(thread_local) - - -def run_command(command_queue): - try: - command = command_queue.get(timeout=0.5) - except queue.Empty: - pass - else: - automationhat.relay.on() if command == "unlock" else automationhat.relay.off() - - -def read_doorbell(thread_local): - analog_value = automationhat.analog.one.read() - doorbell_on_state = 6.0 < analog_value and analog_value < 6.3 - - if doorbell_on_state != thread_local.doorbell_on_state: - thread_local.doorbell_on_state = doorbell_on_state - print("doorbell_on") if doorbell_on_state else print("doorbell_off") - - -if __name__ == "__main__": - main() diff -r 30d8bcb1ebb4 -r bc6a0f25a229 roles/hap-nodejs/tasks/main.yaml --- a/roles/hap-nodejs/tasks/main.yaml Fri Jan 19 11:48:25 2018 -0600 +++ b/roles/hap-nodejs/tasks/main.yaml Fri Jan 19 11:54:24 2018 -0600 @@ -22,11 +22,11 @@ file: path="{{hap_dest}}/python" state="directory" - name: install doord.py - copy: src="doord.py" dest="{{hap_dest}}/python/doord.py" + copy: src="../../../src/doord.py" dest="{{hap_dest}}/python/doord.py" notify: restart hap-nodejs service - name: install door accessory - copy: src="Door_accessory.js" dest="{{hap_dest}}/accessories/Door_accessory.js" + copy: src="../../../src/Door_accessory.js" dest="{{hap_dest}}/accessories/Door_accessory.js" notify: restart hap-nodejs service - name: build HAP-NodeJS diff -r 30d8bcb1ebb4 -r bc6a0f25a229 src/Door_accessory.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/Door_accessory.js Fri Jan 19 11:54:24 2018 -0600 @@ -0,0 +1,115 @@ +var Accessory = require('../').Accessory; +var Service = require('../').Service; +var Characteristic = require('../').Characteristic; +var uuid = require('../').uuid; +var PythonShell = require('python-shell'); + +var door = exports.accessory = new Accessory('Door', uuid.generate('hap-nodejs:accessories:door')); +door.username = 'C1:5D:3A:EA:54:AB'; +door.pincode = '031-45-154'; + +door.getService(Service.AccessoryInformation) + .setCharacteristic(Characteristic.Manufacturer, 'Raspberry Pi') + .setCharacteristic(Characteristic.Model, 'Zero W') + .setCharacteristic(Characteristic.SerialNumber, 'A1S2NASF88EW'); + +var DOOR = { + lockTimeout: 10000, // milliseconds - 10 sec + + pyshell: new PythonShell('doord.py', { + mode: 'text', + pythonPath: '/usr/bin/python3', + pythonOptions: ['-u'], + scriptPath: 'python/' + }), + + lock: function() { + console.log('locking door'); + this.pyshell.send('lock'); + }, + + unlock: function() { + console.log('unlocking door'); + this.pyshell.send('unlock'); + }, + + identify: function() { + console.log('identify door'); + }, + + listenDoorbell: function(doorbellOnCallback, doorbellOffCallback) { + this.pyshell.on('message', function (message) { + console.log(message); + switch(message) { + case 'doorbell_on': + doorbellOnCallback(); + break; + case 'doorbell_off': + doorbellOffCallback(); + break; + } + }); + } +}; + +door.on('identify', function(paired, callback) { + DOOR.identify(); + callback(); +}); + +door.addService(Service.Doorbell, 'Doorbell'); +door.addService(Service.CameraRTPStreamManagement, 'Psudo-Camera'); +door.addService(Service.Speaker, 'Psudo-Speaker'); +door.addService(Service.Microphone, 'Psudo-Microphone'); + +door.addService(Service.LockMechanism, 'Door') + .setCharacteristic(Characteristic.LockTargetState, Characteristic.LockTargetState.SECURED) // force initial state + .setCharacteristic(Characteristic.LockCurrentState, Characteristic.LockCurrentState.SECURED) + .getCharacteristic(Characteristic.LockTargetState) + .on('set', function(value, callback) { + setDoorTargetState(value); + callback(); + }); + +function setDoorTargetState(value) { + switch(value) { + case Characteristic.LockTargetState.UNSECURED: + unlockDoor(); + break; + case Characteristic.LockTargetState.SECURED: + lockDoor(); + break; + } +} + +function unlockDoor() { + DOOR.unlock(); + door.getService(Service.LockMechanism) + .setCharacteristic(Characteristic.LockCurrentState, + Characteristic.LockCurrentState.UNSECURED); + scheduleUnlockTimeout(); +} + +function lockDoor() { + DOOR.lock(); + door.getService(Service.LockMechanism) + .setCharacteristic(Characteristic.LockCurrentState, + Characteristic.LockCurrentState.SECURED); +} + +function scheduleUnlockTimeout() { + setTimeout(function() { + console.log('unlock timeout door'); + door.getService(Service.LockMechanism) + .setCharacteristic(Characteristic.LockTargetState, + Characteristic.LockTargetState.SECURED); + }, DOOR.lockTimeout); +} + +DOOR.listenDoorbell( + function() { + door.getService(Service.Doorbell) + .setCharacteristic(Characteristic.ProgrammableSwitchEvent, + Characteristic.ProgrammableSwitchEvent.SINGLE_PRESS); + }, + function() {}); diff -r 30d8bcb1ebb4 -r bc6a0f25a229 src/doord.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/doord.py Fri Jan 19 11:54:24 2018 -0600 @@ -0,0 +1,51 @@ +#!/usr/bin/env python3 + +import time +import automationhat +import sys + +import queue +import threading + + +def main(): + command_queue = queue.LifoQueue() + read_thread = threading.Thread(target=read_loop, args=[command_queue]) + read_thread.start() + run_loop(command_queue) + + +def read_loop(command_queue): + while True: + command_queue.put_nowait(sys.stdin.readline().rstrip('\n')) + + +def run_loop(command_queue): + thread_local = threading.local() + thread_local.doorbell_on_state = False + + while True: + run_command(command_queue) + read_doorbell(thread_local) + + +def run_command(command_queue): + try: + command = command_queue.get(timeout=0.5) + except queue.Empty: + pass + else: + automationhat.relay.on() if command == "unlock" else automationhat.relay.off() + + +def read_doorbell(thread_local): + analog_value = automationhat.analog.one.read() + doorbell_on_state = 6.0 < analog_value and analog_value < 6.3 + + if doorbell_on_state != thread_local.doorbell_on_state: + thread_local.doorbell_on_state = doorbell_on_state + print("doorbell_on") if doorbell_on_state else print("doorbell_off") + + +if __name__ == "__main__": + main()