src/Door_accessory.js
changeset 1 bc6a0f25a229
parent 0 30d8bcb1ebb4
child 3 67f6a1e7c40a
equal deleted inserted replaced
0:30d8bcb1ebb4 1:bc6a0f25a229
       
     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) {
       
    44             case 'doorbell_on':
       
    45                 doorbellOnCallback();
       
    46                 break;
       
    47             case 'doorbell_off':
       
    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 door.addService(Service.CameraRTPStreamManagement, 'Psudo-Camera');
       
    62 door.addService(Service.Speaker, 'Psudo-Speaker');
       
    63 door.addService(Service.Microphone, 'Psudo-Microphone');
       
    64 
       
    65 door.addService(Service.LockMechanism, 'Door')
       
    66     .setCharacteristic(Characteristic.LockTargetState, Characteristic.LockTargetState.SECURED) // force initial state
       
    67     .setCharacteristic(Characteristic.LockCurrentState, Characteristic.LockCurrentState.SECURED)
       
    68     .getCharacteristic(Characteristic.LockTargetState)
       
    69     .on('set', function(value, callback) {
       
    70         setDoorTargetState(value);
       
    71         callback();
       
    72     });
       
    73 
       
    74 function setDoorTargetState(value) {
       
    75     switch(value) {
       
    76     case Characteristic.LockTargetState.UNSECURED:
       
    77         unlockDoor();
       
    78         break;
       
    79     case Characteristic.LockTargetState.SECURED:
       
    80         lockDoor();
       
    81         break;
       
    82     }
       
    83 }
       
    84 
       
    85 function unlockDoor() {
       
    86     DOOR.unlock();
       
    87     door.getService(Service.LockMechanism)
       
    88         .setCharacteristic(Characteristic.LockCurrentState,
       
    89                            Characteristic.LockCurrentState.UNSECURED);
       
    90     scheduleUnlockTimeout();
       
    91 }
       
    92 
       
    93 function lockDoor() {
       
    94     DOOR.lock();
       
    95     door.getService(Service.LockMechanism)
       
    96         .setCharacteristic(Characteristic.LockCurrentState,
       
    97                            Characteristic.LockCurrentState.SECURED);
       
    98 }
       
    99 
       
   100 function scheduleUnlockTimeout() {
       
   101     setTimeout(function() {
       
   102         console.log('unlock timeout door');
       
   103         door.getService(Service.LockMechanism)
       
   104             .setCharacteristic(Characteristic.LockTargetState,
       
   105                                Characteristic.LockTargetState.SECURED);
       
   106     }, DOOR.lockTimeout);
       
   107 }
       
   108 
       
   109 DOOR.listenDoorbell(
       
   110     function() {
       
   111         door.getService(Service.Doorbell)
       
   112             .setCharacteristic(Characteristic.ProgrammableSwitchEvent,
       
   113                                Characteristic.ProgrammableSwitchEvent.SINGLE_PRESS);
       
   114     },
       
   115     function() {});