|
| 1 | +var types = require("../lib/HAP-NodeJS/accessories/types.js"); |
| 2 | +var AD2USB = require('ad2usb'); |
| 3 | +var CUSTOM_PANEL_LCD_TEXT_CTYPE = "A3E7B8F9-216E-42C1-A21C-97D4E3BE52C8"; |
| 4 | + |
| 5 | +function AD2USBAccessory(log, config) { |
| 6 | + |
| 7 | + this.log = log; |
| 8 | + this.name = config["name"]; |
| 9 | + this.host = config["host"]; |
| 10 | + this.port = config["port"]; |
| 11 | + this.pin = config["pin"]; |
| 12 | + var that = this; |
| 13 | + this.currentArmState = 2; |
| 14 | + this.currentStateCharacteristic = undefined; |
| 15 | + this.targetStateCharacteristic = undefined; |
| 16 | + this.lcdCharacteristic = undefined; |
| 17 | + |
| 18 | + var alarm = AD2USB.connect(this.host, this.port, function() { |
| 19 | + |
| 20 | + // Send an initial empty character to get status |
| 21 | + alarm.send(''); |
| 22 | + |
| 23 | + // Armed Away |
| 24 | + alarm.on('armedAway', function() { |
| 25 | + |
| 26 | + that.log("Armed to AWAY"); |
| 27 | + if (that.currentStateCharacteristic) { |
| 28 | + that.currentStateCharacteristic.updateValue(0, null); |
| 29 | + } |
| 30 | + if (that.targetStateCharacteristic) { |
| 31 | + that.targetStateCharacteristic.updateValue(1, null); |
| 32 | + } |
| 33 | + |
| 34 | + }); |
| 35 | + |
| 36 | + // Armed Stay |
| 37 | + alarm.on('armedStay', function() { |
| 38 | + |
| 39 | + that.log("Armed to STAY"); |
| 40 | + if (that.currentStateCharacteristic) { |
| 41 | + that.currentStateCharacteristic.updateValue(0, null); |
| 42 | + } |
| 43 | + if (that.targetStateCharacteristic) { |
| 44 | + that.targetStateCharacteristic.updateValue(0, null); |
| 45 | + } |
| 46 | + |
| 47 | + }); |
| 48 | + |
| 49 | + // Armed Night |
| 50 | + alarm.on('armedNight', function() { |
| 51 | + |
| 52 | + that.log("Armed to NIGHT"); |
| 53 | + if (that.currentStateCharacteristic) { |
| 54 | + that.currentStateCharacteristic.updateValue(0, null); |
| 55 | + } |
| 56 | + if (that.targetStateCharacteristic) { |
| 57 | + that.targetStateCharacteristic.updateValue(2, null); |
| 58 | + } |
| 59 | + |
| 60 | + }); |
| 61 | + |
| 62 | + // Disarmed |
| 63 | + alarm.on('disarmed', function() { |
| 64 | + |
| 65 | + that.log("Disarmed"); |
| 66 | + if (that.currentStateCharacteristic) { |
| 67 | + that.currentStateCharacteristic.updateValue(1, null); |
| 68 | + } |
| 69 | + if (that.targetStateCharacteristic) { |
| 70 | + that.targetStateCharacteristic.updateValue(3, null); |
| 71 | + } |
| 72 | + |
| 73 | + }); |
| 74 | + |
| 75 | + // Text Change |
| 76 | + alarm.on('lcdtext', function(newText) { |
| 77 | + |
| 78 | + that.log("LCD: " + newText); |
| 79 | + if (that.lcdCharacteristic) { |
| 80 | + that.lcdCharacteristic.updateValue(newText, null); |
| 81 | + } |
| 82 | + |
| 83 | + }); |
| 84 | + |
| 85 | + |
| 86 | + }); |
| 87 | + this.alarm = alarm; |
| 88 | + |
| 89 | +} |
| 90 | + |
| 91 | +AD2USBAccessory.prototype = { |
| 92 | + |
| 93 | + setArmState: function(targetArmState) { |
| 94 | + |
| 95 | + var that = this; |
| 96 | + that.log("Desired target arm state: " + targetArmState); |
| 97 | + |
| 98 | + // TARGET |
| 99 | + // 0 - Stay |
| 100 | + // 1 - Away |
| 101 | + // 2 - Night |
| 102 | + // 3 - Disarm |
| 103 | + if (targetArmState == 0) { |
| 104 | + that.alarm.armStay(that.pin); |
| 105 | + } |
| 106 | + else if (targetArmState == 1) { |
| 107 | + that.alarm.armAway(that.pin); |
| 108 | + } |
| 109 | + else if (targetArmState == 2) { |
| 110 | + that.alarm.armNight(that.pin); |
| 111 | + } |
| 112 | + else if (targetArmState == 3) { |
| 113 | + that.alarm.disarm(that.pin); |
| 114 | + } |
| 115 | + |
| 116 | + |
| 117 | + // CURRENT |
| 118 | + // 0 - Armed |
| 119 | + // 1 - Disarmed |
| 120 | + // 2 - Hold |
| 121 | + |
| 122 | + }, |
| 123 | + |
| 124 | + getServices: function() { |
| 125 | + var that = this; |
| 126 | + return [{ |
| 127 | + sType: types.ACCESSORY_INFORMATION_STYPE, |
| 128 | + characteristics: [{ |
| 129 | + cType: types.NAME_CTYPE, |
| 130 | + onUpdate: null, |
| 131 | + perms: ["pr"], |
| 132 | + format: "string", |
| 133 | + initialValue: this.name, |
| 134 | + supportEvents: false, |
| 135 | + supportBonjour: false, |
| 136 | + manfDescription: "Name of the accessory", |
| 137 | + designedMaxLength: 255 |
| 138 | + },{ |
| 139 | + cType: types.MANUFACTURER_CTYPE, |
| 140 | + onUpdate: null, |
| 141 | + perms: ["pr"], |
| 142 | + format: "string", |
| 143 | + initialValue: "Nutech", |
| 144 | + supportEvents: false, |
| 145 | + supportBonjour: false, |
| 146 | + manfDescription: "Manufacturer", |
| 147 | + designedMaxLength: 255 |
| 148 | + },{ |
| 149 | + cType: types.MODEL_CTYPE, |
| 150 | + onUpdate: null, |
| 151 | + perms: ["pr"], |
| 152 | + format: "string", |
| 153 | + initialValue: "AD2USB", |
| 154 | + supportEvents: false, |
| 155 | + supportBonjour: false, |
| 156 | + manfDescription: "Model", |
| 157 | + designedMaxLength: 255 |
| 158 | + },{ |
| 159 | + cType: types.SERIAL_NUMBER_CTYPE, |
| 160 | + onUpdate: null, |
| 161 | + perms: ["pr"], |
| 162 | + format: "string", |
| 163 | + initialValue: "AD2USBIF", |
| 164 | + supportEvents: false, |
| 165 | + supportBonjour: false, |
| 166 | + manfDescription: "SN", |
| 167 | + designedMaxLength: 255 |
| 168 | + },{ |
| 169 | + cType: types.IDENTIFY_CTYPE, |
| 170 | + onUpdate: null, |
| 171 | + perms: ["pw"], |
| 172 | + format: "bool", |
| 173 | + initialValue: false, |
| 174 | + supportEvents: false, |
| 175 | + supportBonjour: false, |
| 176 | + manfDescription: "Identify Accessory", |
| 177 | + designedMaxLength: 1 |
| 178 | + }] |
| 179 | + },{ |
| 180 | + sType: types.ALARM_STYPE, |
| 181 | + characteristics: [{ |
| 182 | + cType: types.NAME_CTYPE, |
| 183 | + onUpdate: null, |
| 184 | + perms: ["pr"], |
| 185 | + format: "string", |
| 186 | + initialValue: this.name, |
| 187 | + supportEvents: false, |
| 188 | + supportBonjour: false, |
| 189 | + manfDescription: "Name of service", |
| 190 | + designedMaxLength: 255 |
| 191 | + },{ |
| 192 | + cType: types.ALARM_CURRENT_STATE_CTYPE, |
| 193 | + onUpdate: null, |
| 194 | + onRegister: function(characteristic) { |
| 195 | + |
| 196 | + that.currentStateCharacteristic = characteristic; |
| 197 | + characteristic.eventEnabled = true; |
| 198 | + |
| 199 | + }, |
| 200 | + perms: ["pr","ev"], |
| 201 | + format: "int", |
| 202 | + initialValue: 2, |
| 203 | + supportEvents: true, |
| 204 | + supportBonjour: false, |
| 205 | + manfDescription: "Alarm current arm state", |
| 206 | + designedMaxLength: 1 |
| 207 | + },{ |
| 208 | + cType: types.ALARM_TARGET_STATE_CTYPE, |
| 209 | + onUpdate: function(value) { that.setArmState(value); }, |
| 210 | + onRegister: function(characteristic) { |
| 211 | + |
| 212 | + that.targetStateCharacteristic = characteristic; |
| 213 | + characteristic.eventEnabled = true; |
| 214 | + |
| 215 | + }, |
| 216 | + perms: ["pw","pr","ev"], |
| 217 | + format: "int", |
| 218 | + initialValue: 1, |
| 219 | + supportEvents: true, |
| 220 | + supportBonjour: false, |
| 221 | + manfDescription: "Alarm target arm state", |
| 222 | + designedMaxLength: 1 |
| 223 | + }, |
| 224 | + { |
| 225 | + cType: CUSTOM_PANEL_LCD_TEXT_CTYPE, |
| 226 | + onUpdate: null, |
| 227 | + onRegister: function(characteristic) { |
| 228 | + |
| 229 | + that.lcdCharacteristic = characteristic; |
| 230 | + characteristic.eventEnabled = true; |
| 231 | + |
| 232 | + }, |
| 233 | + perms: ["pr","ev"], |
| 234 | + format: "string", |
| 235 | + initialValue: "Unknown", |
| 236 | + supportEvents: false, |
| 237 | + supportBonjour: false, |
| 238 | + manfDescription: "Keypad Text", |
| 239 | + designedMaxLength: 64 |
| 240 | + }] |
| 241 | + }]; |
| 242 | + } |
| 243 | +}; |
| 244 | + |
| 245 | +module.exports.accessory = AD2USBAccessory; |
0 commit comments