homebridge-cmd4 5.1.0 → 6.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,368 @@
1
+ #!/usr/bin/env node
2
+ // SecuritySystem.js
3
+ //
4
+ // Description:
5
+ // This script *CAN* be called by the HomeBridge plugin Cmd4 as defined in your config.json
6
+ // file. The purpose is to fake the existance of an Security System as best it can.
7
+ //
8
+ // Note: Security Systems are tricky to fake since the securitySysytemCurrentState
9
+ // Does not align with the SecuritySystemTargetState. The only way to trigger
10
+ // the alarm would be:
11
+ // node ./SecuritySystem.js Set My_System SecuritySystemCurrentState 4
12
+ //
13
+ // Likewise to disarm
14
+ // node ./SecuritySystem.js Set My_System SecuritySystemCurrentState 3
15
+ //
16
+ // Note 1: The Hap spec is quoted throughout, but this is not the spec so
17
+ // this is not gauranteed to be correct.
18
+ //
19
+ // How it works:
20
+ // A characteristic value is stored in the $HOME/.homebridge/Cmd4Scripts/Cmd4States in
21
+ // the file named "Status + <Accessory Name> _ <Characteristic>" and is returned or
22
+ // written to based on the <Get|Set> option.
23
+ //
24
+ // For example: node SecuritySystem.js Set My_System SecuritySystemCurrentState 0
25
+ // will create the file:
26
+ // .homebridge/Cmd4Scripts/Cmd4States/Status_My_System_SecuritySystemCurrentState
27
+ // with the contents of "0"
28
+ //
29
+ // Environmental Variables Used:
30
+ // $HOME - to create path to .homebridge
31
+ //
32
+ // The following is an example config.json entry for this script.
33
+ /*
34
+ {
35
+ "platform": "Cmd4",
36
+ "name": "Cmd4",
37
+ "debug": false,
38
+ "statusMsg": true,
39
+ "accessories": [
40
+ {
41
+ "type": "SecuritySystem",
42
+ "displayName": "My_SecuritySystem",
43
+ "securitySystemCurrentState": "AWAY_ARM",
44
+ "securitySystemTargetState": "AWAY_ARM",
45
+ "interval": 30,
46
+ "timeout": 4900,
47
+ "polling": [
48
+ { "characteristic": "securitySystemCurrentState" },
49
+ { "characteristic": "securitySystemTargetState" }
50
+ ],
51
+ "stateChangeResponseTime": 1,
52
+ "state_cmd": "node ./homebridge/Cmd4Scripts/SecuritySystem.js"
53
+ }
54
+ }
55
+ */
56
+
57
+ 'use strict';
58
+
59
+ // FileSystem requirements.
60
+ var fs = require('fs');
61
+ var path = require('path');
62
+ var os = require('os');
63
+
64
+ // The files created within Cmd4StatesPath contain just a value of the accessories last state,
65
+ // so they are very small in size.
66
+ var Cmd4StatesPath = path.join(os.homedir(), ".homebridge/Cmd4Scripts/Cmd4States");
67
+
68
+ var length = process.argv.length;
69
+ var device = "";
70
+ var io = "";
71
+ var characteristic = "";
72
+ var option = "";
73
+
74
+ if ( length == 2 ) process.exit( 0 );
75
+
76
+ if ( length <= 2 ) {
77
+ process.stdout.write( `Usage: ${ process.argv[ 0 ] } <Get> <AccessoryName> <Characteristic>` );
78
+ process.stdout.write( ` ${ process.argv[ 0 ] } <Set> <AccessoryName> <Characteristic> <Value>` );
79
+ process.exit(-1);
80
+ }
81
+
82
+ if ( length >= 2 ) io = process.argv[ 2 ];
83
+ if ( length >= 3 ) device = process.argv[ 3 ];
84
+ if ( length >= 4 ) characteristic = process.argv[ 4 ];
85
+ if ( length >= 5 ) option = process.argv[ 5 ];
86
+
87
+ // Placing the states in a subdirectory makes things look cleaner.
88
+ // Some platforms require an exception handler
89
+ const mkdirSync = function( dirPath )
90
+ {
91
+ try {
92
+ fs.mkdirSync( dirPath )
93
+ } catch ( err ) {
94
+ if ( err.code !== 'EEXIST' )
95
+ {
96
+ process.stdout.write( `mkdir failed: ${ dirPath }`);
97
+ throw err;
98
+ } else {
99
+ // directory already exists - OK
100
+ }
101
+ }
102
+ }
103
+
104
+ mkdirSync( Cmd4StatesPath );
105
+
106
+
107
+ // Such a simple way to store state information that is small and fast!
108
+ // Put exception handling here too. Just in case!
109
+ function writeData( a, b,c )
110
+ {
111
+ var fn = Cmd4StatesPath + "/Status_" + a + "_" + b;
112
+
113
+ try {
114
+ fs.writeFileSync( fn,c );
115
+ } catch (err) {
116
+ if ( err.code !== 'EEXIST' )
117
+ {
118
+ process.stdout.write( `write data failed: ${ fn } data: ${ c }` );
119
+ throw err;
120
+ } else {
121
+ // file already exists - OK
122
+ }
123
+ }
124
+ }
125
+
126
+ // Read the state information. If there is none, just return what
127
+ // was expected.
128
+ // Put exception handling here too. Just in case!
129
+ function readData( a, b )
130
+ {
131
+ var fn = Cmd4StatesPath + "/Status_" + a + "_" + b;
132
+ c = "";
133
+
134
+ try {
135
+ c = String( fs.readFileSync( fn, 'utf8' ) );
136
+ } catch (err) {
137
+ if ( err.code === 'ENOENT' ) {
138
+ // This is OK. just return what was expected.
139
+ return c;
140
+ } else
141
+ {
142
+ if ( err.code !== 'EEXIST' )
143
+ {
144
+ process.stdout.write( `read data failed: ${ fn }` );
145
+ throw err;
146
+ } else {
147
+ // file already exists - OK
148
+ }
149
+ }
150
+ }
151
+
152
+ return c;
153
+ }
154
+
155
+ var c = "";
156
+
157
+ switch( io )
158
+ {
159
+ case "Get":
160
+ {
161
+ switch( characteristic )
162
+ {
163
+ case "Name": // 97 Optional Characteristic
164
+ {
165
+ c = readData( device, characteristic );
166
+
167
+ if ( c == "" ) process.stdout.write( `"${ device }"` ); else process.stdout.write( `"${ c }"` );
168
+
169
+ // UUID: 00000023-0000-1000-8000-0026BB765291
170
+ // Type: public.hap.characteristic.name
171
+ // Permissions: Paired Read, Notify
172
+
173
+ // Format: string
174
+
175
+ // Maximum Length: 64
176
+
177
+ break;
178
+ }
179
+ case "SecuritySystemCurrentState": // 143
180
+ {
181
+ c = readData( device, characteristic );
182
+
183
+ if ( c == "" ) process.stdout.write( "1" ); else process.stdout.write( `"${ c }"` );
184
+
185
+ // UUID: 00000066-0000-1000-8000-0026BB765291
186
+ // Type: public.hap.characteristic.security-system-state.current
187
+ // Permissions: Paired Read, Notify
188
+
189
+ // Format: uint8
190
+
191
+ // Minimum Value: 0
192
+ // Maximum Value: 4
193
+ // Step Value: 1
194
+
195
+ // Valid Values:
196
+ // 0 - "STAY_ARM" - The home is occupied and the residents are
197
+ // active. e.g. morning or evenings"
198
+ // 1 - "AWAY_ARM" - The home is unoccupied"
199
+ // 2 - "NIGHT ARM" - The home is occupied and the residents are sleeping"
200
+ // 3 - "DISARMED"
201
+ // 4 - "ALARM_TRIGGERED"
202
+ // 5 - 255 - Reserved
203
+
204
+ break;
205
+ }
206
+ case "SecuritySystemTargetState": // 144
207
+ {
208
+ c = readData( device, characteristic );
209
+
210
+ if ( c == "" ) process.stdout.write( "1" ); else process.stdout.write( `"${ c }"` );
211
+
212
+ // UUID: 00000067-0000-1000-8000-0026BB765291
213
+ // Type: public.hap.characteristic.security-system-state.target
214
+ // Permissions: Paired Read, Paired Write, Notify
215
+
216
+ // Format: uint8
217
+
218
+ // Minimum Value: 0
219
+ // Maximum Value: 3
220
+ // Step Value: 1
221
+
222
+ // Valid Values:
223
+ // 0 - "STAY_ARM" - The home is occupied and the residents
224
+ // are active. e.g. morning or evenings
225
+ // 1 - "AWAY_ARM" - The home is unoccupied
226
+ // 2 - "NIGHT_ARM" - The home is occupied and the residents are sleeping
227
+ //
228
+ // 3 - 255 - Reserved
229
+
230
+ break;
231
+ }
232
+ case "SecuritySystemAlarmType": // 142 Optional Characteristic
233
+ {
234
+ c = readData( device, characteristic );
235
+
236
+ if ( c == "" ) process.stdout.write( "0" ); else process.stdout.write( `"${ c }"` );
237
+
238
+ // UUID: 0000008E-0000-1000-8000-0026BB765291
239
+ // Type: public.hap.characteristic.security-system.alarm-type
240
+ // Permissions: Paired Read, Notify
241
+
242
+ // Format: uint8
243
+
244
+ // Minimum Value: 0
245
+ // Maximum Value: 1
246
+ // Step Value: 1
247
+
248
+ // Valid Values:
249
+ // 0 - When alert cleared
250
+ // 1 - Unknown Cause
251
+
252
+ break;
253
+ }
254
+
255
+ case "StatusFault": // 163 Optional Characteristic
256
+ {
257
+ c = readData( device, characteristic );
258
+
259
+ if ( c == "" ) process.stdout.write( "0" ); else process.stdout.write( `"${ c }"` );
260
+
261
+ // UUID: 00000077-0000-1000-8000-0026BB765291
262
+ // Type: public.hap.characteristic.status-fault
263
+ // Permissions: Paired Read, Notify
264
+ // Format: uint8
265
+ // Minimum Value: 0
266
+ // Maximum Value: 1
267
+ // Step Value: 1
268
+
269
+ // Valid Values:
270
+ // 0 - "NO_FAULT"
271
+ // 1 - "GENERAL_FAULT"
272
+
273
+ break;
274
+ }
275
+ case "StatusTampered": // 166 Optional Characteristic
276
+ {
277
+ c = readData( device, characteristic );
278
+
279
+ if ( c == "" ) process.stdout.write( "0" ); else process.stdout.write( `"${ c }"` );
280
+
281
+ // UUID: 0000007A-0000-1000-8000-0026BB765291
282
+ // Type: public.hap.characteristic.status-tampered
283
+ // Permissions: Paired Read, Notify
284
+
285
+ // Format: uint8
286
+
287
+ // Valid Values:
288
+ // 0 - "NOT_TAMPERED" - Accessory is not tampered"
289
+ // 1 - "TAMPERED" - Accessory is tampered with
290
+
291
+ break;
292
+ }
293
+
294
+ default:
295
+ process.stderr.write( `Unknown Characteristic for: ${ io } Device: ${ device } Characteristic: ${ characteristic }` );
296
+ process.exit( -1 );
297
+ }
298
+
299
+ break;
300
+
301
+ } // End of Switch for "Get"
302
+ case "Set":
303
+ {
304
+ switch( characteristic )
305
+ {
306
+ case "Name": // 97 Optional Characteristic
307
+ {
308
+ writeData( device, characteristic, option );
309
+
310
+ // Not settable in Hap Spec, here for debugging.
311
+ break;
312
+ }
313
+ case "SecuritySystemAlarmType": // 142
314
+ {
315
+ writeData( device, characteristic, option );
316
+
317
+ // Not settable in Hap Spec, here for debugging.
318
+ break;
319
+ }
320
+ case "SecuritySystemCurrentState": // 143
321
+ {
322
+ writeData( device, characteristic, option );
323
+
324
+ // Not settable in Hap Spec, here for debugging.
325
+ break;
326
+ }
327
+ case "SecuritySystemTargetState": // 144
328
+ {
329
+ writeData( device, characteristic, option );
330
+
331
+ // Set to done
332
+ writeData(device,"SecuritySystemCurrentState", option);
333
+
334
+ break;
335
+ }
336
+ case "StatusFault": // 163 Optional Characteristic
337
+ {
338
+ writeData( device, characteristic, option );
339
+
340
+ // Not settable in Hap Spec, here for debugging.
341
+ break;
342
+ }
343
+ case "StatusTampered": // 166 Optional Characteristic
344
+ {
345
+ writeData( device, characteristic, option );
346
+
347
+ // Not settable in Hap Spec, here for debugging.
348
+ break;
349
+ }
350
+
351
+
352
+ default:
353
+ process.stderr.write( `Unknown Characteristic for: ${ io } Device:${ device } Characteristic:${ characteristic }` );
354
+ process.exit( -1 );
355
+ }
356
+
357
+ break;
358
+ } // End of Switch Device for "Set"
359
+ default:
360
+ process.stderr.write( `Unknown IO ${ io }` );
361
+ process.exit( -1 );
362
+ }
363
+
364
+ //process.stdout.write( `Say What Device: ${ device } Characteristic: ${ characteristic } Option: ${ option }` );
365
+
366
+ process.exit( 0 );
367
+
368
+