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