homebridge-cmd4 6.3.0 → 7.0.0-beta2
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.
- package/CHANGELOG.md +41 -32
- package/Cmd4Accessory.js +19 -86
- package/Cmd4Platform.js +0 -55
- package/Cmd4PriorityPollingQueue.js +246 -90
- package/Cmd4PriorityPollingQueue.js.1 +1148 -0
- package/Extras/config.json +110 -110
- package/Extras/config.min.json +111 -111
- package/RUNNING_CHANGELOG.md +48 -0
- package/cmd4Constants.js +56 -2
- package/docs/Developers.md +21 -23
- package/package.json +1 -1
|
@@ -0,0 +1,1148 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// 3rd Party includes
|
|
4
|
+
const exec = require( "child_process" ).exec;
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
// These would already be initialized by index.js
|
|
8
|
+
let CMD4_ACC_TYPE_ENUM = require( "./lib/CMD4_ACC_TYPE_ENUM" ).CMD4_ACC_TYPE_ENUM;
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
// Settings, Globals and Constants
|
|
12
|
+
let settings = require( "./cmd4Settings" );
|
|
13
|
+
const constants = require( "./cmd4Constants" );
|
|
14
|
+
|
|
15
|
+
// Pretty Colors
|
|
16
|
+
var chalk = require( "chalk" );
|
|
17
|
+
|
|
18
|
+
let trueTypeOf = require( "./utils/trueTypeOf" );
|
|
19
|
+
let lcFirst = require( "./utils/lcFirst" );
|
|
20
|
+
|
|
21
|
+
// For changing validValue Constants to Values and back again
|
|
22
|
+
var { transposeConstantToValidValue,
|
|
23
|
+
transposeValueToValidConstant,
|
|
24
|
+
transposeBoolToValue
|
|
25
|
+
} = require( "./utils/transposeCMD4Props" );
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
let HIGH_PRIORITY_SET = 0;
|
|
29
|
+
let HIGH_PRIORITY_GET = 1;
|
|
30
|
+
let LOW_PRIORITY_GET = 2;
|
|
31
|
+
|
|
32
|
+
class Cmd4PriorityPollingQueue
|
|
33
|
+
{
|
|
34
|
+
constructor( log, queueName, queueType = constants.DEFAULT_QUEUE_TYPE, queueRetryCount = constants.DEFAULT_WORM_QUEUE_RETRY_COUNT )
|
|
35
|
+
{
|
|
36
|
+
this.log = log;
|
|
37
|
+
|
|
38
|
+
// This works better for Unit testing
|
|
39
|
+
settings.cmd4Dbg = log.debugEnabled;
|
|
40
|
+
|
|
41
|
+
this.queueName = queueName;
|
|
42
|
+
this.queueType = queueType;
|
|
43
|
+
this.queueRetryCount = queueRetryCount;
|
|
44
|
+
this.queueStarted = false;
|
|
45
|
+
this.highPriorityQueue = [ ];
|
|
46
|
+
this.lowPriorityQueue = [ ];
|
|
47
|
+
this.lowPriorityQueueIndex = 0 ;
|
|
48
|
+
this.inProgressGets = 0;
|
|
49
|
+
this.inProgressSets = 0;
|
|
50
|
+
|
|
51
|
+
this.listOfRunningPolls = {};
|
|
52
|
+
|
|
53
|
+
// This is not a sanity timer.
|
|
54
|
+
// This controls when it is safe to do a "Get" of the Aircon
|
|
55
|
+
// after a failed condition. It does happen to fix the queue
|
|
56
|
+
// when something is wrong, but this is not the purpose of
|
|
57
|
+
// this timer.
|
|
58
|
+
this.pauseTimer = null;
|
|
59
|
+
this.lastGoodTransactionTime = Date.now( );
|
|
60
|
+
this.errorCountSinceLastGoodTransaction = 0;
|
|
61
|
+
|
|
62
|
+
// - Not a const so it can be manipulated during unit testing
|
|
63
|
+
this.pauseTimerTimeout = constants.DEFAULT_QUEUE_PAUSE_TIMEOUT;
|
|
64
|
+
|
|
65
|
+
// The WoRm queue needs error messages to be silenced as
|
|
66
|
+
// they are inevitable, but are handled through retries
|
|
67
|
+
// By default non WoRm queues are allowed to echo errors
|
|
68
|
+
|
|
69
|
+
if ( this.queueRetryCount == 0 || settings.debug )
|
|
70
|
+
this.echoE = true;
|
|
71
|
+
else
|
|
72
|
+
this.echoE = true;
|
|
73
|
+
|
|
74
|
+
this.changeQueueType( this, queueType );
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
echoRetryErrors( currentRetryCount )
|
|
78
|
+
{
|
|
79
|
+
// If debug then the default is true
|
|
80
|
+
if ( settings.cmd4Debug )
|
|
81
|
+
return true;
|
|
82
|
+
|
|
83
|
+
// Since this is the last retry, echo the error
|
|
84
|
+
if ( currentRetryCount == this.queueRetryCount )
|
|
85
|
+
return true;
|
|
86
|
+
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// This function is called by homebridge to *PUT AN ENTRY INTO THE HIGHEST PRIORITY SET QUEUE*.
|
|
91
|
+
// We immediately return success if the device is accessible. Either way the Set is attempted
|
|
92
|
+
// above everything else except other SetValue requests.
|
|
93
|
+
prioritySetValue( accTypeEnumIndex, characteristicString, timeout, stateChangeResponseTime, value, homebridgeCallback )
|
|
94
|
+
{
|
|
95
|
+
// this is Accessory
|
|
96
|
+
//
|
|
97
|
+
//if ( settings.cmd4Dbg ) this.log.debug(`prioritySetValue, asked to set: ${ characteristicString } to ${ value }`);
|
|
98
|
+
|
|
99
|
+
// Save the value to cache. The set will come later
|
|
100
|
+
// this.cmd4Storage.setStoredValueForIndex( accTypeEnumIndex, value );
|
|
101
|
+
|
|
102
|
+
if ( this.errorValue != 0 )
|
|
103
|
+
{
|
|
104
|
+
if ( settings.cmd4Dbg ) this.log.debug(`prioritySetValue for ${ this.displayName }, homebridgeCallback returning error ${ this.errorValue } ${ this.errorString }`);
|
|
105
|
+
homebridgeCallback( this.errorValue );
|
|
106
|
+
} else
|
|
107
|
+
{
|
|
108
|
+
if ( settings.cmd4Dbg ) this.log.debug(`prioritySetValue for ${ this.displayName }, homebridgeCallback returning default success 0`);
|
|
109
|
+
homebridgeCallback( 0 );
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
let newEntry = { [ constants.IS_SET_lv ]: true, [ constants.ACCESSORY_lv ]: this, [ constants.ACC_TYPE_ENUM_INDEX_lv ]: accTypeEnumIndex, [ constants.CHARACTERISTIC_STRING_lv ]: characteristicString, [ constants.TIMEOUT_lv ]: timeout, [ constants.STATE_CHANGE_RESPONSE_TIME_lv ]: stateChangeResponseTime, [ constants.CALLBACK_lv ]: homebridgeCallback, [ constants.VALUE_lv ]: value };
|
|
113
|
+
|
|
114
|
+
// Determine where to put the entry in the queue
|
|
115
|
+
if ( this.queue.highPriorityQueue.length == 0 )
|
|
116
|
+
{
|
|
117
|
+
// No entries, then it goes on top
|
|
118
|
+
this.queue.highPriorityQueue.push( newEntry );
|
|
119
|
+
|
|
120
|
+
} else {
|
|
121
|
+
|
|
122
|
+
// Make sure that this is the latest "Set" of this entry
|
|
123
|
+
let index = this.queue.highPriorityQueue.findIndex( ( entry ) => entry.accessory.uuid == this.uuid && entry.isSet == true && entry.accTypeEnumIndex == accTypeEnumIndex );
|
|
124
|
+
|
|
125
|
+
if ( index == -1 )
|
|
126
|
+
{
|
|
127
|
+
// It doesn't exist in the queue, It needs to be placed after any "Sets".
|
|
128
|
+
// First Determine the first "Get"
|
|
129
|
+
let getIndex = this.queue.highPriorityQueue.findIndex( ( entry ) => entry.isSet == false );
|
|
130
|
+
|
|
131
|
+
if ( getIndex == -1 )
|
|
132
|
+
{
|
|
133
|
+
// No "Get" entrys, it goes at the end after everything.
|
|
134
|
+
this.queue.highPriorityQueue.push( newEntry );
|
|
135
|
+
|
|
136
|
+
} else
|
|
137
|
+
{
|
|
138
|
+
// Insert before the first "Get" entry
|
|
139
|
+
this.queue.highPriorityQueue.splice( getIndex, 0, newEntry );
|
|
140
|
+
}
|
|
141
|
+
} else
|
|
142
|
+
{
|
|
143
|
+
this.queue.highPriorityQueue[ index ] = newEntry;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
this.queue.processQueueFunc( HIGH_PRIORITY_SET, this.queue );
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// This function is called by homebridge to *PUT AN ENTRY INTO THE HIGHEST PRIORITY GET QUEUE*.
|
|
150
|
+
// We immediately return with success and the last known value if the device is accessible, otherwise
|
|
151
|
+
// the last failure error code.
|
|
152
|
+
// The Get is attempted no matter the devices availability. This is done after every Set
|
|
153
|
+
// request and at the bottom of the hightPrioritySetValue queue, but above any polling.
|
|
154
|
+
priorityGetValue( accTypeEnumIndex, characteristicString, timeout, homebridgeCallback )
|
|
155
|
+
{
|
|
156
|
+
// this is Accessory
|
|
157
|
+
|
|
158
|
+
// if ( settings.cmd4Dbg ) this.log.debug(`priorityGetValue for ${ this.displayName }, asked to Get: ${ characteristicString }`);
|
|
159
|
+
|
|
160
|
+
if ( this.errorValue != 0 )
|
|
161
|
+
{
|
|
162
|
+
// if ( settings.cmd4Dbg ) this.log.debug(`priorityGetValue for ${ this.displayName }, homebridgeCallback returning error ${ this.errorValue } ${ this.errorString}`);
|
|
163
|
+
homebridgeCallback( this.errorValue );
|
|
164
|
+
} else
|
|
165
|
+
{
|
|
166
|
+
// return the cached value
|
|
167
|
+
let storedValue = this.cmd4Storage.getStoredValueForIndex( accTypeEnumIndex );
|
|
168
|
+
|
|
169
|
+
// if ( settings.cmd4Dbg ) this.log.debug(`priorityGetValue for ${ this.displayName }, homebridgeCallback returning storedValue: ${ storedValue }`);
|
|
170
|
+
homebridgeCallback( 0, storedValue );
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if ( this.queue.queueType != constants.QUEUETYPE_WORM2 )
|
|
174
|
+
{
|
|
175
|
+
// When the value is returned, it will update homebridge
|
|
176
|
+
this.queue.highPriorityQueue.push( { [ constants.IS_SET_lv ]: false, [ constants.QUEUE_GET_IS_UPDATE_lv ]: true, [ constants.ACCESSORY_lv ]: this, [ constants.ACC_TYPE_ENUM_INDEX_lv ]: accTypeEnumIndex, [ constants.CHARACTERISTIC_STRING_lv ]: characteristicString, [ constants.TIMEOUT_lv ]: timeout, [ constants.STATE_CHANGE_RESPONSE_TIME_lv ]: null, [ constants.VALUE_lv ]: null, [ constants.CALLBACK_lv ]: homebridgeCallback } );
|
|
177
|
+
|
|
178
|
+
this.queue.processQueueFunc( HIGH_PRIORITY_GET, this.queue );
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// This function is called by polling to *PUT AN ENTRY INTO THE LOW PRIORITY POLLING QUEUE*.
|
|
183
|
+
addLowPriorityGetPolledQueueEntry( accessory, accTypeEnumIndex, characteristicString, interval, timeout )
|
|
184
|
+
{
|
|
185
|
+
// These are all gets from polling
|
|
186
|
+
accessory.queue.lowPriorityQueue.push( { [ constants.ACCESSORY_lv ]: accessory, [ constants.ACC_TYPE_ENUM_INDEX_lv ]: accTypeEnumIndex, [ constants.CHARACTERISTIC_STRING_lv ]: characteristicString, [ constants.INTERVAL_lv ]: interval, [ constants.TIMEOUT_lv ]: timeout } );
|
|
187
|
+
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
processHighPrioritySetQueue( entry )
|
|
191
|
+
{
|
|
192
|
+
if ( settings.cmd4Dbg ) this.log.debug( `Processing high priority queue "Set" entry: ${ entry.accTypeEnumIndex } length: ${ this.highPriorityQueue.length }` );
|
|
193
|
+
|
|
194
|
+
this.inProgressSets ++;
|
|
195
|
+
this.qSetValue( entry.accessory, entry.accTypeEnumIndex, entry.characteristicString, entry.timeout, entry.value, function ( error )
|
|
196
|
+
{
|
|
197
|
+
|
|
198
|
+
let queue = entry.accessory.queue;
|
|
199
|
+
|
|
200
|
+
// Save the error code - Pass or fail
|
|
201
|
+
entry.accessory.errorValue = error;
|
|
202
|
+
|
|
203
|
+
if ( error == 0 )
|
|
204
|
+
{
|
|
205
|
+
// Now that the set was successful, store the value
|
|
206
|
+
entry.accessory.cmd4Storage.setStoredValueForIndex( entry.accTypeEnumIndex, entry.value );
|
|
207
|
+
|
|
208
|
+
// A set with no error means the queue is sane to do reading
|
|
209
|
+
queue.lastGoodTransactionTime = Date.now( );
|
|
210
|
+
queue.errorCountSinceLastGoodTransaction = 0;
|
|
211
|
+
|
|
212
|
+
// After the stateChangeResponseTime, do the related characteristic ( if any )
|
|
213
|
+
let relatedCurrentAccTypeEnumIndex = entry.accessory.getDevicesRelatedCurrentAccTypeEnumIndex( entry.accTypeEnumIndex );
|
|
214
|
+
|
|
215
|
+
// The "Set" is now complete
|
|
216
|
+
queue.inProgressSets --;
|
|
217
|
+
|
|
218
|
+
if ( relatedCurrentAccTypeEnumIndex == null )
|
|
219
|
+
{
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// Since the "Set" passed, do the relatedCurrentCharacteristic
|
|
224
|
+
let relatedCurrentCharacteristicString = CMD4_ACC_TYPE_ENUM.properties[ relatedCurrentAccTypeEnumIndex ].type;
|
|
225
|
+
// Change the entry to a get and set queueGetIsUpdate to true
|
|
226
|
+
// Use unshift to make it next in line
|
|
227
|
+
entry.isSet = false;
|
|
228
|
+
entry.accTypeEnumIndex = relatedCurrentAccTypeEnumIndex;
|
|
229
|
+
entry.characteristicString = relatedCurrentCharacteristicString;
|
|
230
|
+
entry.queueGetIsUpdate = true;
|
|
231
|
+
// Put the entry at the top of the queue
|
|
232
|
+
queue.highPriorityQueue.unshift( entry );
|
|
233
|
+
|
|
234
|
+
setTimeout( ( ) => {
|
|
235
|
+
queue.processQueueFunc( HIGH_PRIORITY_GET, queue );
|
|
236
|
+
}, entry.stateChangeResponseTime );
|
|
237
|
+
|
|
238
|
+
return;
|
|
239
|
+
|
|
240
|
+
} else // setValue failed
|
|
241
|
+
{
|
|
242
|
+
// The "Set" is complete, even if it failed.
|
|
243
|
+
queue.inProgressSets --;
|
|
244
|
+
|
|
245
|
+
let currentRetryCount = queue.errorCountSinceLastGoodTransaction;
|
|
246
|
+
|
|
247
|
+
if ( currentRetryCount >= queue.queueRetryCount )
|
|
248
|
+
{
|
|
249
|
+
if ( queue.echoRetryErrors( currentRetryCount ) )
|
|
250
|
+
{
|
|
251
|
+
// Counting starts from zero, i.e queueRetries = 0, so add 1
|
|
252
|
+
queue.log.warn( `*${ currentRetryCount + 1 }* error(s) were encountered for "${ entry.accessory.displayName }" getValue. Last error found Getting: "${ entry.characteristicString}". Perhaps you should run in debug mode to find out what the problem might be.` );
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// Convert the errorValue into an errorString
|
|
256
|
+
entry.accessory.errorString = new Error( constants.errorString( error ) );
|
|
257
|
+
|
|
258
|
+
// This does not work - Nothing happens to HomeKit !
|
|
259
|
+
// queue.log.warn( `START processHighPrioritySetQueue calling updateCharacteristic errorValue: ${ entry.accessory.errorValue} errorString: ${ entry.accessory.errorString }`);
|
|
260
|
+
// entry.accessory.service.getCharacteristic( CMD4_ACC_TYPE_ENUM.properties[ entry.accTypeEnumIndex ].characteristic ).updateValue( entry.accessory.errorString );
|
|
261
|
+
// queue.log.warn( `END processHighPrioritySetQueue calling updateCharacteristic errorValue: ${ entry.accessory.errorValue} errorString: ${ entry.accessory.errorString }`);
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
} else
|
|
265
|
+
{
|
|
266
|
+
// Increment the errorCount/currentRetryCount
|
|
267
|
+
queue.errorCountSinceLastGoodTransaction++;
|
|
268
|
+
|
|
269
|
+
// Set failed. We need to keep trying
|
|
270
|
+
queue.highPriorityQueue.push( entry );
|
|
271
|
+
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
entry.accessory.queue.pauseQueue( entry.accessory.queue );
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// Note 1.
|
|
278
|
+
// Do not call the callback as it was done when the "Set" entry was
|
|
279
|
+
// created.
|
|
280
|
+
|
|
281
|
+
// Note 2.
|
|
282
|
+
// We cannot release the queue for further processing as the
|
|
283
|
+
// statechangeResponseTime has not completed. This must be
|
|
284
|
+
// done first or any next "Get" or "Set" would interfere
|
|
285
|
+
// with the device
|
|
286
|
+
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
processHighPriorityGetQueue( entry )
|
|
291
|
+
{
|
|
292
|
+
if ( settings.cmd4Dbg ) this.log.debug( `Processing high priority queue "Get" entry: ${ entry.accTypeEnumIndex } isUpdate: ${ entry.queueGetIsUpdate } length: ${ this.highPriorityQueue.length }` );
|
|
293
|
+
|
|
294
|
+
this.inProgressGets ++;
|
|
295
|
+
|
|
296
|
+
this.qGetValue( entry.accessory, entry.accTypeEnumIndex, entry.characteristicString, entry.timeout, function ( error, properValue )
|
|
297
|
+
{
|
|
298
|
+
let queue = entry.accessory.queue;
|
|
299
|
+
|
|
300
|
+
// Save the error code - Pass or fail
|
|
301
|
+
entry.accessory.errorValue = error;
|
|
302
|
+
|
|
303
|
+
// Nothing special was done for casing on errors, so omit it.
|
|
304
|
+
if ( error == 0 )
|
|
305
|
+
{
|
|
306
|
+
// Save the new returned value
|
|
307
|
+
entry.accessory.cmd4Storage.setStoredValueForIndex( entry.accTypeEnumIndex, properValue );
|
|
308
|
+
|
|
309
|
+
// hmmm if ( entry.queueGetIsUpdate == true )
|
|
310
|
+
entry.accessory.service.getCharacteristic( CMD4_ACC_TYPE_ENUM.properties[ entry.accTypeEnumIndex ].characteristic ).updateValue( properValue );
|
|
311
|
+
|
|
312
|
+
// A good anything, updates the lastGoodTransactionTime
|
|
313
|
+
queue.lastGoodTransactionTime = Date.now( );
|
|
314
|
+
queue.errorCountSinceLastGoodTransaction = 0;
|
|
315
|
+
|
|
316
|
+
} else // highPriority getValue failed
|
|
317
|
+
{
|
|
318
|
+
|
|
319
|
+
let currentRetryCount = queue.errorCountSinceLastGoodTransaction;
|
|
320
|
+
|
|
321
|
+
if ( currentRetryCount >= queue.queueRetryCount )
|
|
322
|
+
{
|
|
323
|
+
if ( queue.echoRetryErrors( currentRetryCount ) )
|
|
324
|
+
queue.log.warn( `*${ currentRetryCount + 1}* error(s) were encountered for "${ entry.accessory.displayName }" getValue. Last error found Getting: "${ entry.characteristicString}". Perhaps you should run in debug mode to find out what the problem might be.` );
|
|
325
|
+
|
|
326
|
+
// Convert the errorValue into an errorString
|
|
327
|
+
entry.accessory.errorString = new Error( constants.errorString( error ) );
|
|
328
|
+
|
|
329
|
+
// This does not work - Nothing happens to HomeKit !
|
|
330
|
+
// queue.log.warn( `START processHighPriorityGetQueue calling updateCharacteristic errorValue: ${ entry.accessory.errorValue} errorString: ${ entry.accessory.errorString }`);
|
|
331
|
+
// entry.accessory.service.getCharacteristic( CMD4_ACC_TYPE_ENUM.properties[ entry.accTypeEnumIndex ].characteristic ).updateValue( entry.accessory.errorString );
|
|
332
|
+
// queue.log.warn( `END processHighPriorityGetQueue calling updateCharacteristic errorValue: ${ entry.accessory.errorValue} errorString: ${ entry.accessory.errorString }`);
|
|
333
|
+
|
|
334
|
+
} else
|
|
335
|
+
{
|
|
336
|
+
// Increment the errorCount/currentRetryCount
|
|
337
|
+
queue.errorCountSinceLastGoodTransaction++;
|
|
338
|
+
|
|
339
|
+
// High Priority Get failed. We keep retrying until the mod of
|
|
340
|
+
// queueRetryCount reaches zero, which is WoRm only as it has more than 1
|
|
341
|
+
// default retry count.
|
|
342
|
+
queue.highPriorityQueue.push( entry );
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
entry.accessory.queue.pauseQueue( entry.accessory.queue );
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
queue.inProgressGets --;
|
|
349
|
+
setTimeout( ( ) => { queue.processQueueFunc( HIGH_PRIORITY_GET, queue ); }, 0 );
|
|
350
|
+
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
// This is called from polling
|
|
355
|
+
processEntryFromLowPriorityQueue( entry )
|
|
356
|
+
{
|
|
357
|
+
if ( settings.cmd4Dbg ) this.log.debug( `Processing low priority queue entry: ${ entry.accTypeEnumIndex }` );
|
|
358
|
+
|
|
359
|
+
let queue = entry.accessory.queue;
|
|
360
|
+
|
|
361
|
+
queue.inProgressGets ++;
|
|
362
|
+
|
|
363
|
+
// isLowPriority is set to true,
|
|
364
|
+
queue.qGetValue( entry.accessory, entry.accTypeEnumIndex, entry.characteristicString, entry.timeout, function ( error, properValue )
|
|
365
|
+
{
|
|
366
|
+
// For the next one
|
|
367
|
+
queue.inProgressGets --;
|
|
368
|
+
|
|
369
|
+
// Save the error code - Pass or fail
|
|
370
|
+
entry.accessory.errorValue = error;
|
|
371
|
+
|
|
372
|
+
// Nothing special was done for casing on errors, so omit it.
|
|
373
|
+
if ( error == 0 )
|
|
374
|
+
{
|
|
375
|
+
// Save the new value
|
|
376
|
+
entry.accessory.cmd4Storage.setStoredValueForIndex( entry.accTypeEnumIndex, properValue );
|
|
377
|
+
|
|
378
|
+
if ( settings.cmd4Dbg ) entry.accessory.log.debug( `processEntryFromLowPriorityQueue calling updateValue properValue: ${ properValue }`);
|
|
379
|
+
|
|
380
|
+
// Update the new value in homebridge
|
|
381
|
+
entry.accessory.service.getCharacteristic( CMD4_ACC_TYPE_ENUM.properties[ entry.accTypeEnumIndex ].characteristic ).updateValue( properValue );
|
|
382
|
+
|
|
383
|
+
// A good anything, updates the lastGoodTransactionTime
|
|
384
|
+
queue.lastGoodTransactionTime = Date.now( );
|
|
385
|
+
queue.errorCountSinceLastGoodTransaction = 0;
|
|
386
|
+
|
|
387
|
+
} else { // LowPriority getValue failed
|
|
388
|
+
queue.errorCountSinceLastGoodTransaction++;
|
|
389
|
+
|
|
390
|
+
// Convert the errorValue into an errorString
|
|
391
|
+
entry.accessory.errorString = new Error( constants.errorString( error ));
|
|
392
|
+
|
|
393
|
+
// This does not work - Nothing happens to HomeKit !
|
|
394
|
+
// Call updateValue with new Error so device will become unavailable
|
|
395
|
+
// queue.log.warn( `START processEntryFromLowPriorityQueue calling updateCharacteristic errorValue: ${ entry.accessory.errorValue } errorString: ${ entry.accessory.errorString }`);
|
|
396
|
+
// entry.accessory.service.getCharacteristic( CMD4_ACC_TYPE_ENUM.properties[ entry.accTypeEnumIndex ].characteristic ).updateValue( entry.accessory.errorString );
|
|
397
|
+
// queue.log.warn( `END processEntryFromLowPriorityQueue calling updateCharacteristic errorValue: ${ entry.accessory.errorValue } errorString: ${ entry.accessory.errorString }`);
|
|
398
|
+
|
|
399
|
+
queue.pauseQueue( entry.accessory.queue );
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
// Now that this one has been processed, schedule it again for next time
|
|
403
|
+
queue.scheduleLowPriorityEntry( entry )
|
|
404
|
+
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
// ***********************************************
|
|
409
|
+
//
|
|
410
|
+
// qGetValue: Method to call an external script
|
|
411
|
+
// that returns an accessories status
|
|
412
|
+
// for a given characteristic.
|
|
413
|
+
//
|
|
414
|
+
// The script will be passed:
|
|
415
|
+
// Get <Device Name> <accTypeEnumIndex>
|
|
416
|
+
//
|
|
417
|
+
// Where:
|
|
418
|
+
// - Device name is the name in your
|
|
419
|
+
// config.json file.
|
|
420
|
+
// - accTypeEnumIndex represents
|
|
421
|
+
// the characteristic to get as in index into
|
|
422
|
+
// the CMD4_ACC_TYPE_ENUM.
|
|
423
|
+
//
|
|
424
|
+
// ***********************************************
|
|
425
|
+
qGetValue( accessory, accTypeEnumIndex, characteristicString, timeout, callback )
|
|
426
|
+
{
|
|
427
|
+
let self = accessory;
|
|
428
|
+
let queue = accessory.queue;
|
|
429
|
+
|
|
430
|
+
let cmd = self.state_cmd_prefix + self.state_cmd + " Get '" + self.displayName + "' '" + characteristicString + "'" + self.state_cmd_suffix;
|
|
431
|
+
|
|
432
|
+
if ( settings.cmd4Dbg ) self.log.debug( `getValue: accTypeEnumIndex:( ${ accTypeEnumIndex } )-"${ characteristicString }" function for: ${ self.displayName } cmd: ${ cmd } timeout: ${ timeout }` );
|
|
433
|
+
|
|
434
|
+
let reply = "NxN";
|
|
435
|
+
|
|
436
|
+
// Execute command to Get a characteristics value for an accessory
|
|
437
|
+
// exec( cmd, { timeout: timeout }, function ( error, stdout, stderr )
|
|
438
|
+
//let child = spawn( cmd, { shell:true } );
|
|
439
|
+
let child = exec( cmd, { timeout: timeout }, function ( error, stdout, stderr )
|
|
440
|
+
{
|
|
441
|
+
if ( stderr )
|
|
442
|
+
if ( queue.echoE ) self.log.error( `getValue: ${ characteristicString } function for ${ self.displayName } streamed to stderr: ${ stderr }` );
|
|
443
|
+
|
|
444
|
+
// Handle errors when process closes
|
|
445
|
+
if ( error )
|
|
446
|
+
if ( queue.echoE ) self.log.error( chalk.red( `getValue ${ characteristicString } function failed for ${ self.displayName } cmd: ${ cmd } Failed. Generated Error: ${ error }` ) );
|
|
447
|
+
|
|
448
|
+
reply = stdout;
|
|
449
|
+
|
|
450
|
+
}).on('close', ( code ) =>
|
|
451
|
+
{
|
|
452
|
+
// Was the return code successful ?
|
|
453
|
+
if ( code != 0 )
|
|
454
|
+
{
|
|
455
|
+
// Commands that time out have "null" return codes. So get the real one.
|
|
456
|
+
if ( child.killed == true )
|
|
457
|
+
{
|
|
458
|
+
if ( queue.echoE ) self.log.error( chalk.red( `getValue ${ characteristicString } function timed out ${ timeout }ms for ${ self.displayName } cmd: ${ cmd } Failed` ) );
|
|
459
|
+
callback( constants.ERROR_TIMER_EXPIRED );
|
|
460
|
+
|
|
461
|
+
return;
|
|
462
|
+
}
|
|
463
|
+
if ( queue.echoE ) self.log.error( chalk.red( `getValue ${ characteristicString } function failed for ${ self.displayName } cmd: ${ cmd } Failed. Error: ${ code }. ${ constants.DBUSY }` ) );
|
|
464
|
+
|
|
465
|
+
callback( code );
|
|
466
|
+
|
|
467
|
+
return;
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
if ( reply == "NxN" )
|
|
471
|
+
{
|
|
472
|
+
if ( queue.echoE ) self.log.error( `getValue: nothing returned from stdout for ${ characteristicString } ${ self.displayName }. ${ constants.DBUSY }` );
|
|
473
|
+
|
|
474
|
+
callback( constants.ERROR_NO_DATA_REPLY );
|
|
475
|
+
|
|
476
|
+
return;
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
if ( reply == null )
|
|
480
|
+
{
|
|
481
|
+
if ( queue.echoE ) self.log.error( `getValue: null returned from stdout for ${ characteristicString } ${ self.displayName }. ${ constants.DBUSY }` );
|
|
482
|
+
|
|
483
|
+
// We can call our callback though ;-)
|
|
484
|
+
callback( constants.ERROR_NULL_REPLY );
|
|
485
|
+
|
|
486
|
+
return;
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
// Coerce to string for manipulation
|
|
490
|
+
reply += '';
|
|
491
|
+
|
|
492
|
+
// Remove trailing newline or carriage return, then
|
|
493
|
+
// Remove leading and trailing spaces, carriage returns ...
|
|
494
|
+
let trimmedReply = reply.replace(/\n|\r$/,"").trim( );
|
|
495
|
+
|
|
496
|
+
// Theoretically not needed as this is caught below, but I wanted
|
|
497
|
+
// to catch this before much string manipulation was done.
|
|
498
|
+
if ( trimmedReply.toUpperCase( ) == "NULL" )
|
|
499
|
+
{
|
|
500
|
+
if ( queue.echoE ) self.log.error( `getValue: "${ trimmedReply }" returned from stdout for ${ characteristicString } ${ self.displayName }. ${ constants.DBUSY }` );
|
|
501
|
+
callback( constants.ERROR_NULL_STRING_REPLY );
|
|
502
|
+
|
|
503
|
+
return;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
// Handle beginning and ending matched single or double quotes. Previous version too heavy duty.
|
|
507
|
+
// - Remove matched double quotes at begining and end, then
|
|
508
|
+
// - Remove matched single quotes at beginning and end, then
|
|
509
|
+
// - remove leading and trailing spaces.
|
|
510
|
+
let unQuotedReply = trimmedReply.replace(/^"(.+)"$/,"$1").replace(/^'(.+)'$/,"$1").trim( );
|
|
511
|
+
|
|
512
|
+
if ( unQuotedReply == "" )
|
|
513
|
+
{
|
|
514
|
+
if ( queue.echoE ) self.log.error( `getValue: ${ characteristicString } function for: ${ self.displayName } returned an empty string "${ trimmedReply }". ${ constants.DBUSY }` );
|
|
515
|
+
|
|
516
|
+
callback( constants.ERROR_EMPTY_STRING_REPLY );
|
|
517
|
+
|
|
518
|
+
return;
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
// The above "null" checked could possibly have quotes around it.
|
|
522
|
+
// Now that the quotes are removed, I must check again. The
|
|
523
|
+
// things I must do for bad data ....
|
|
524
|
+
if ( unQuotedReply.toUpperCase( ) == "NULL" )
|
|
525
|
+
{
|
|
526
|
+
if ( queue.echoE ) self.log.error( `getValue: ${ characteristicString } function for ${ self.displayName } returned the string "${ trimmedReply }". ${ constants.DBUSY }` );
|
|
527
|
+
|
|
528
|
+
callback( constants.ERROR_2ND_NULL_STRING_REPLY );
|
|
529
|
+
|
|
530
|
+
return;
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
let words = unQuotedReply.split( " " ).length;
|
|
534
|
+
if ( words > 1 && CMD4_ACC_TYPE_ENUM.properties[ accTypeEnumIndex ].props.allowedWordCount == 1 )
|
|
535
|
+
{
|
|
536
|
+
self.log.warn( `getValue: Warning, Retrieving ${ characteristicString }, expected only one word value for: ${ self.displayName } of: ${ trimmedReply }` );
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
if ( settings.cmd4Dbg ) self.log.debug( `getValue: ${ characteristicString } function for: ${ self.displayName } returned: ${ unQuotedReply }` );
|
|
540
|
+
|
|
541
|
+
|
|
542
|
+
var transposed = transposeConstantToValidValue( CMD4_ACC_TYPE_ENUM.properties, accTypeEnumIndex, unQuotedReply )
|
|
543
|
+
if ( settings.cmd4Dbg && transposed != unQuotedReply ) self.log.debug( `getValue: ${ characteristicString } for: ${ self.displayName } transposed: ${ transposed }` );
|
|
544
|
+
|
|
545
|
+
|
|
546
|
+
// Return the appropriate type, by seeing what it is
|
|
547
|
+
// defined as in Homebridge,
|
|
548
|
+
let properValue = CMD4_ACC_TYPE_ENUM.properties[ accTypeEnumIndex ].stringConversionFunction( transposed );
|
|
549
|
+
if ( properValue == undefined )
|
|
550
|
+
{
|
|
551
|
+
self.log.warn( `${ self.displayName } ` + chalk.red( `Cannot convert value: ${ unQuotedReply } to ${ CMD4_ACC_TYPE_ENUM.properties[ accTypeEnumIndex ].props.format } for ${ characteristicString }` ) );
|
|
552
|
+
|
|
553
|
+
callback( constants.ERROR_NON_CONVERTABLE_REPLY );
|
|
554
|
+
|
|
555
|
+
return;
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
if ( settings.cmd4Dbg && properValue != transposed ) self.log.debug( `getValue: ${ characteristicString } for: ${ self.displayName } properValue: ${ properValue }` );
|
|
559
|
+
|
|
560
|
+
// Success !!!!
|
|
561
|
+
callback( 0, properValue );
|
|
562
|
+
|
|
563
|
+
// Store history using fakegato if set up
|
|
564
|
+
self.updateAccessoryAttribute( accTypeEnumIndex, properValue );
|
|
565
|
+
|
|
566
|
+
});
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
// ***********************************************
|
|
570
|
+
//
|
|
571
|
+
// qSetValue: Method to call an external script
|
|
572
|
+
// that sets an accessories status
|
|
573
|
+
// for a given characteristic.
|
|
574
|
+
//
|
|
575
|
+
//
|
|
576
|
+
// The script will be passed:
|
|
577
|
+
// Set < Device Name > < accTypeEnumIndex > < Value >
|
|
578
|
+
//
|
|
579
|
+
//
|
|
580
|
+
// Where:
|
|
581
|
+
// - Device name is the name in your
|
|
582
|
+
// config.json file.
|
|
583
|
+
// - accTypeEnumIndex represents
|
|
584
|
+
// the characteristic to get as in index into
|
|
585
|
+
// the CMD4_ACC_TYPE_ENUM.
|
|
586
|
+
// - Characteristic is the accTypeEnumIndex
|
|
587
|
+
// in HAP form.
|
|
588
|
+
// - Value is new characteristic value.
|
|
589
|
+
//
|
|
590
|
+
// Notes:
|
|
591
|
+
// ( 1 ) In the special TARGET set characteristics, getValue
|
|
592
|
+
// is called to update HomeKit.
|
|
593
|
+
// Example: Set My_Door < TargetDoorState > 1
|
|
594
|
+
// calls: Get My_Door < CurrentDoorState >
|
|
595
|
+
//
|
|
596
|
+
// - Where he value in <> is an one of CMD4_ACC_TYPE_ENUM
|
|
597
|
+
// ***********************************************
|
|
598
|
+
qSetValue( accessory, accTypeEnumIndex, characteristicString, timeout, value, queueCallback )
|
|
599
|
+
{
|
|
600
|
+
let self = accessory;
|
|
601
|
+
let queue = accessory.queue;
|
|
602
|
+
|
|
603
|
+
if ( self.hV.outputConstants == true )
|
|
604
|
+
{
|
|
605
|
+
value = transposeValueToValidConstant( CMD4_ACC_TYPE_ENUM.properties, accTypeEnumIndex, value );
|
|
606
|
+
|
|
607
|
+
} else
|
|
608
|
+
{
|
|
609
|
+
value = transposeBoolToValue( value );
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
let cmd = accessory.state_cmd_prefix + accessory.state_cmd + " Set '" + accessory.displayName + "' '" + characteristicString + "' '" + value + "'" + accessory.state_cmd_suffix;
|
|
613
|
+
|
|
614
|
+
if ( accessory.hV.statusMsg == "TRUE" )
|
|
615
|
+
self.log.info( chalk.blue( `Setting ${ self.displayName } ${ characteristicString }` ) + ` ${ value }` );
|
|
616
|
+
|
|
617
|
+
if ( settings.cmd4Dbg ) self.log.debug( `setValue: accTypeEnumIndex:( ${ accTypeEnumIndex } )-"${ characteristicString }" function for: ${ self.displayName } ${ value } cmd: ${ cmd } timeout: ${ timeout }` );
|
|
618
|
+
|
|
619
|
+
// Execute command to Set a characteristic value for an accessory
|
|
620
|
+
let child = exec( cmd, { timeout: timeout }, function ( error, stdout, stderr )
|
|
621
|
+
{
|
|
622
|
+
if ( stderr )
|
|
623
|
+
if ( queue.echoE ) self.log.error( `setValue: ${ characteristicString } function for ${ self.displayName } streamed to stderr: ${ stderr }` );
|
|
624
|
+
|
|
625
|
+
if ( error )
|
|
626
|
+
if ( queue.echoE ) self.log.error( chalk.red( `setValue ${ characteristicString } function failed for ${ self.displayName } cmd: ${ cmd } Failed. Error: ${ error.message }` ) );
|
|
627
|
+
|
|
628
|
+
}).on( "close", ( code ) =>
|
|
629
|
+
{
|
|
630
|
+
if ( code != 0 )
|
|
631
|
+
{
|
|
632
|
+
if ( child.killed == true )
|
|
633
|
+
{
|
|
634
|
+
if ( queue.echoE ) self.log.error( chalk.red( `setValue ${ characteristicString } function failed for ${ self.displayName } cmd: ${ cmd } Failed. Error: ${ code } ${ constants.DBUSY }` ) );
|
|
635
|
+
|
|
636
|
+
queueCallback( constants.ERROR_TIMER_EXPIRED );
|
|
637
|
+
|
|
638
|
+
return;
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
queueCallback( code );
|
|
642
|
+
|
|
643
|
+
return;
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
queueCallback( code );
|
|
647
|
+
|
|
648
|
+
});
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
// The queue is self maintaining, except for lowPriorityEntries
|
|
652
|
+
// which if passed in, must be rescheduled as they go by their own
|
|
653
|
+
// intervals and thus must handle the return code.
|
|
654
|
+
processWormQueue( lastTransactionType, queue, lowPriorityEntry = null )
|
|
655
|
+
{
|
|
656
|
+
// "WoRm", No matter what, only one "Set" allowed
|
|
657
|
+
if ( queue.inProgressSets > 0 )
|
|
658
|
+
{
|
|
659
|
+
// if ( settings.cmd4Dbg ) queue.log.debug(`processWormQueue queue.inProgressSets > 0 : ${queue.inProgressSets}`);
|
|
660
|
+
|
|
661
|
+
// We are *NOT* processing the low prioirity queue entry
|
|
662
|
+
return false;
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
// It is not a good time to do a anything, so skip it
|
|
666
|
+
if ( queue.lastGoodTransactionTime == 0 )
|
|
667
|
+
{
|
|
668
|
+
// if ( settings.cmd4Dbg ) queue.log.debug(`processWormQueue queue.lastGoodTransactionTime == 0`);
|
|
669
|
+
|
|
670
|
+
// We are *NOT* processing the low prioirity queue entry
|
|
671
|
+
return false;
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
if ( queue.highPriorityQueue.length > 0 )
|
|
675
|
+
{
|
|
676
|
+
let nextEntry = queue.highPriorityQueue[ 0 ];
|
|
677
|
+
|
|
678
|
+
if ( nextEntry.isSet == true )
|
|
679
|
+
{
|
|
680
|
+
// If already in progress, when they finish they will restart the queue
|
|
681
|
+
// Otherwise continuing will purge the next item from the queue as it
|
|
682
|
+
// cannot be run with an entry already in progress.
|
|
683
|
+
if ( nextEntry.accessory.queue.inProgressSets > 0 ||
|
|
684
|
+
nextEntry.accessory.queue.inProgressGets > 0 )
|
|
685
|
+
{
|
|
686
|
+
// Return as queue is busy.
|
|
687
|
+
// Return false as we are *NOT* processing the low prioirity queue entry
|
|
688
|
+
// if ( settings.cmd4Dbg ) queue.log.debug(`processWormQueue queue.inProgressSets> 0 ${ nextEntry.accessory.queue.inProgressSets } ${ nextEntry.accessory.queue.inProgressGets }`);
|
|
689
|
+
|
|
690
|
+
return false;
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
queue.processHighPrioritySetQueue( queue.highPriorityQueue.shift( ) );
|
|
694
|
+
|
|
695
|
+
// Return false as we are *NOT* processing the low prioirity queue entry
|
|
696
|
+
return false;
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
// This must be a "Get". Process them all.
|
|
700
|
+
let max = queue.highPriorityQueue.length;
|
|
701
|
+
|
|
702
|
+
while( queue.highPriorityQueue.length > 0 &&
|
|
703
|
+
nextEntry.isSet == false &&
|
|
704
|
+
max >= 1 )
|
|
705
|
+
{
|
|
706
|
+
queue.processHighPriorityGetQueue( queue.highPriorityQueue.shift( ) );
|
|
707
|
+
nextEntry = queue.highPriorityQueue[ 0 ];
|
|
708
|
+
max--;
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
// Return false as we are *NOT* processing the low prioirity queue entry
|
|
712
|
+
return false;
|
|
713
|
+
|
|
714
|
+
} else if ( lastTransactionType == HIGH_PRIORITY_SET ||
|
|
715
|
+
lastTransactionType == HIGH_PRIORITY_GET )
|
|
716
|
+
{
|
|
717
|
+
// Return false as we are *NOT* processing the low prioirity queue entry
|
|
718
|
+
return false;
|
|
719
|
+
}
|
|
720
|
+
// This is self evident, until their are other types of Prioritys
|
|
721
|
+
if ( lastTransactionType == LOW_PRIORITY_GET &&
|
|
722
|
+
lowPriorityEntry != null &&
|
|
723
|
+
queue.queueStarted == true )
|
|
724
|
+
{
|
|
725
|
+
queue.processEntryFromLowPriorityQueue( lowPriorityEntry );
|
|
726
|
+
|
|
727
|
+
// We are processing the low priority queue entry.
|
|
728
|
+
return true;
|
|
729
|
+
|
|
730
|
+
} else {
|
|
731
|
+
if ( lastTransactionType == LOW_PRIORITY_GET &&
|
|
732
|
+
queue.queueStarted == false )
|
|
733
|
+
{
|
|
734
|
+
// Return false as we are *NOT* processing the low prioirity queue entry
|
|
735
|
+
return false;
|
|
736
|
+
|
|
737
|
+
} if ( queue.inProgressGets == 0 &&
|
|
738
|
+
queue.inProgressSets == 0 )
|
|
739
|
+
{
|
|
740
|
+
// Return false as we are *NOT* processing the low prioirity queue entry
|
|
741
|
+
return false;
|
|
742
|
+
|
|
743
|
+
} else {
|
|
744
|
+
if ( settings.cmd4Dbg ) this.log.debug( `Unhandled lastTransactionType: ${ lastTransactionType } inProgressSets: ${ queue.inProgressSets } inProgressGets: ${ queue.inProgressGets } queueStarted: ${ queue.queueStarted } lowQueueLen: ${ queue.lowPriorityQueue.length } hiQueueLen: ${ queue.highPriorityQueue.length }` );
|
|
745
|
+
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
// The queue is self maintaining, except for lowPriorityEntries
|
|
751
|
+
// which if passed in, must be rescheduled as they go by their own
|
|
752
|
+
// intervals and thus must handle the return code.
|
|
753
|
+
processSequentialQueue( lastTransactionType, queue, lowPriorityEntry = null )
|
|
754
|
+
{
|
|
755
|
+
// Sequential, No matter what, only one transaction allowed
|
|
756
|
+
if ( queue.inProgressSets > 0 ||
|
|
757
|
+
queue.inProgressGets > 0 )
|
|
758
|
+
// Return false as we are *NOT* processing the low prioirity queue entry
|
|
759
|
+
return false;
|
|
760
|
+
|
|
761
|
+
// It is not a good time to do a anything, so skip it
|
|
762
|
+
if ( queue.lastGoodTransactionTime == 0 )
|
|
763
|
+
// Return false as we are *NOT* processing the low prioirity queue entry
|
|
764
|
+
return false;
|
|
765
|
+
|
|
766
|
+
if ( queue.highPriorityQueue.length > 0 )
|
|
767
|
+
{
|
|
768
|
+
let nextEntry = queue.highPriorityQueue[ 0 ];
|
|
769
|
+
|
|
770
|
+
if ( nextEntry.isSet == true )
|
|
771
|
+
{
|
|
772
|
+
queue.processHighPrioritySetQueue( queue.highPriorityQueue.shift( ) );
|
|
773
|
+
|
|
774
|
+
// Return false as we are *NOT* processing the low prioirity queue entry
|
|
775
|
+
return false;
|
|
776
|
+
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
// Has to be a High Priority "Get" entry. Process just this one.
|
|
780
|
+
queue.processHighPriorityGetQueue( queue.highPriorityQueue.shift( ) );
|
|
781
|
+
|
|
782
|
+
// Return false as we are *NOT* processing the low prioirity queue entry
|
|
783
|
+
return false;
|
|
784
|
+
|
|
785
|
+
} else if ( lastTransactionType == HIGH_PRIORITY_SET ||
|
|
786
|
+
lastTransactionType == HIGH_PRIORITY_GET )
|
|
787
|
+
{
|
|
788
|
+
// Return false as we are *NOT* processing the low prioirity queue entry
|
|
789
|
+
return false;
|
|
790
|
+
}
|
|
791
|
+
// This is self evident, until their are other types of Prioritys
|
|
792
|
+
if ( lastTransactionType == LOW_PRIORITY_GET &&
|
|
793
|
+
lowPriorityEntry != null &&
|
|
794
|
+
queue.queueStarted == true )
|
|
795
|
+
{
|
|
796
|
+
queue.processEntryFromLowPriorityQueue( lowPriorityEntry );
|
|
797
|
+
|
|
798
|
+
// We are processing the low priority queue entry.
|
|
799
|
+
return true;
|
|
800
|
+
|
|
801
|
+
} else {
|
|
802
|
+
if ( lastTransactionType == LOW_PRIORITY_GET &&
|
|
803
|
+
queue.queueStarted == false )
|
|
804
|
+
{
|
|
805
|
+
// Return false as we are *NOT* processing the low prioirity queue entry
|
|
806
|
+
return false;
|
|
807
|
+
|
|
808
|
+
} if ( queue.inProgressGets == 0 &&
|
|
809
|
+
queue.inProgressSets == 0 )
|
|
810
|
+
{
|
|
811
|
+
// Return false as we are *NOT* processing the low prioirity queue entry
|
|
812
|
+
return false;
|
|
813
|
+
|
|
814
|
+
} else {
|
|
815
|
+
if ( settings.cmd4Dbg ) this.log.debug( `Unhandled lastTransactionType: ${ lastTransactionType } inProgressSets: ${ queue.inProgressSets } inProgressGets: ${ queue.inProgressGets } queueStarted: ${ queue.queueStarted } lowQueueLen: ${ queue.lowPriorityQueue.length } hiQueueLen: ${ queue.highPriorityQueue.length }` );
|
|
816
|
+
|
|
817
|
+
}
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
// The standard queue is just free running, except if the queue has not
|
|
822
|
+
// been started yet.
|
|
823
|
+
processPassThruQueue( lastTransactionType, queue, lowPriorityEntry = null )
|
|
824
|
+
{
|
|
825
|
+
if ( lastTransactionType == LOW_PRIORITY_GET &&
|
|
826
|
+
lowPriorityEntry != null &&
|
|
827
|
+
queue.queueStarted == true )
|
|
828
|
+
{
|
|
829
|
+
queue.processEntryFromLowPriorityQueue( lowPriorityEntry );
|
|
830
|
+
}
|
|
831
|
+
|
|
832
|
+
if ( queue.highPriorityQueue.length > 0 )
|
|
833
|
+
{
|
|
834
|
+
let nextEntry = queue.highPriorityQueue[ 0 ];
|
|
835
|
+
|
|
836
|
+
if ( nextEntry.isSet == true )
|
|
837
|
+
{
|
|
838
|
+
|
|
839
|
+
queue.processHighPrioritySetQueue( queue.highPriorityQueue.shift( ) );
|
|
840
|
+
}
|
|
841
|
+
else
|
|
842
|
+
{
|
|
843
|
+
queue.processHighPriorityGetQueue( queue.highPriorityQueue.shift( ) );
|
|
844
|
+
}
|
|
845
|
+
}
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
scheduleLowPriorityEntry( entry )
|
|
849
|
+
{
|
|
850
|
+
let accessory = entry.accessory;
|
|
851
|
+
let queue = entry.accessory.queue;
|
|
852
|
+
|
|
853
|
+
if ( settings.cmd4Dbg ) accessory.log.debug( `Scheduling Poll of index: ${ entry.accTypeEnumIndex } characteristic: ${ entry.characteristicString } for: ${ accessory.displayName } timeout: ${ entry.timeout } interval: ${ entry.interval }` );
|
|
854
|
+
|
|
855
|
+
// Clear polling
|
|
856
|
+
if ( queue.listOfRunningPolls &&
|
|
857
|
+
queue.listOfRunningPolls[ accessory.displayName + entry.accTypeEnumIndex ] == undefined )
|
|
858
|
+
clearTimeout( queue.listOfRunningPolls[ accessory.displayName + entry.accTypeEnumIndex ] );
|
|
859
|
+
|
|
860
|
+
queue.listOfRunningPolls[ accessory.displayName + entry.accTypeEnumIndex ] = setTimeout( ( ) =>
|
|
861
|
+
{
|
|
862
|
+
// If the queue was busy/not available, schedule the entry at a later time
|
|
863
|
+
if ( queue.processQueueFunc( LOW_PRIORITY_GET, queue, entry ) == false )
|
|
864
|
+
{
|
|
865
|
+
if ( settings.cmd4Dbg ) accessory.log.debug( `processsQueue returned false` );
|
|
866
|
+
|
|
867
|
+
queue.scheduleLowPriorityEntry( entry );
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
}, entry.interval);
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
|
|
874
|
+
pauseQueue( queue )
|
|
875
|
+
{
|
|
876
|
+
if ( queue.queueType == constants.QUEUETYPE_STANDARD )
|
|
877
|
+
return;
|
|
878
|
+
|
|
879
|
+
queue.lastGoodTransactionTime = 0;
|
|
880
|
+
|
|
881
|
+
if ( queue.pauseTimer == null )
|
|
882
|
+
{
|
|
883
|
+
queue.pauseTimer = setTimeout( ( ) =>
|
|
884
|
+
{
|
|
885
|
+
// So we do not trip over this again immediately
|
|
886
|
+
queue.lastGoodTransactionTime = Date.now( );
|
|
887
|
+
queue.pauseTimer = null;
|
|
888
|
+
|
|
889
|
+
queue.processQueueFunc( HIGH_PRIORITY_GET, queue );
|
|
890
|
+
|
|
891
|
+
}, queue.pauseTimerTimeout );
|
|
892
|
+
}
|
|
893
|
+
}
|
|
894
|
+
|
|
895
|
+
printQueueStats( queue )
|
|
896
|
+
{
|
|
897
|
+
let line = `QUEUE "${ queue.queueName }" stats`;
|
|
898
|
+
this.log.info( line );
|
|
899
|
+
this.log.info( `${ "=".repeat( line.length ) }` );
|
|
900
|
+
this.log.info( "No longer applicable" );
|
|
901
|
+
}
|
|
902
|
+
dumpQueue( queue )
|
|
903
|
+
{
|
|
904
|
+
let line = `Low Priority Queue "${ queue.queueName }"`;
|
|
905
|
+
this.log.info( line );
|
|
906
|
+
this.log.info( `${ "=".repeat( line.length ) }` );
|
|
907
|
+
queue.lowPriorityQueue.forEach( ( entry, entryIndex ) =>
|
|
908
|
+
{
|
|
909
|
+
this.log.info( `${ entryIndex } ${ entry.accessory.displayName } characteristic: ${ entry.characteristicString } accTypeEnumIndex: ${ entry.accTypeEnumIndex } interval: ${ entry.interval } timeout: ${ entry.timeout }` );
|
|
910
|
+
} );
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
startQueue( queue, allDoneCallback )
|
|
914
|
+
{
|
|
915
|
+
queue.lowPriorityQueueIndex = 0 ;
|
|
916
|
+
|
|
917
|
+
let delay = 0;
|
|
918
|
+
let staggeredDelays = [ 3000, 6000, 9000, 12000 ];
|
|
919
|
+
let staggeredDelaysLength = staggeredDelays.length;
|
|
920
|
+
let staggeredDelayIndex = 0;
|
|
921
|
+
let lastAccessoryUUID = ""
|
|
922
|
+
let allDoneCount = 0;
|
|
923
|
+
|
|
924
|
+
if ( settings.cmd4Dbg ) this.log.debug( `enablePolling for the first time` );
|
|
925
|
+
|
|
926
|
+
// If there is nothing in the lowPriorityQueue, we are dome.
|
|
927
|
+
// Demo mode or Unit testing.
|
|
928
|
+
if ( queue.lowPriorityQueue.length == 0 )
|
|
929
|
+
{
|
|
930
|
+
|
|
931
|
+
allDoneCallback( allDoneCount );
|
|
932
|
+
setTimeout( ( ) => { queue.processQueueFunc( HIGH_PRIORITY_GET, queue ); }, 0 );
|
|
933
|
+
|
|
934
|
+
} else
|
|
935
|
+
{
|
|
936
|
+
queue.lowPriorityQueue.forEach( ( entry, entryIndex ) =>
|
|
937
|
+
{
|
|
938
|
+
allDoneCount ++;
|
|
939
|
+
setTimeout( ( ) =>
|
|
940
|
+
{
|
|
941
|
+
if ( entryIndex == 0 && settings.cmd4Dbg )
|
|
942
|
+
{
|
|
943
|
+
if ( queue.queueType == constants.QUEUETYPE_WORM ||
|
|
944
|
+
queue.queueType == constants.QUEUETYPE_WORM2
|
|
945
|
+
)
|
|
946
|
+
{
|
|
947
|
+
entry.accessory.log.debug( `Started staggered kick off of ${ queue.lowPriorityQueue.length } polled characteristics for queue: "${ entry.accessory.queue.queueName }"` );
|
|
948
|
+
} else
|
|
949
|
+
{
|
|
950
|
+
entry.accessory.log.debug( `Started staggered kick off of ${ queue.lowPriorityQueue.length } polled characteristics for "${ entry.accessory.displayName }"` );
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
|
|
954
|
+
if ( settings.cmd4Dbg ) entry.accessory.log.debug( `Kicking off polling for: ${ entry.accessory.displayName } ${ entry.characteristicString } interval:${ entry.interval }, staggered:${ staggeredDelays[ staggeredDelayIndex ] }` );
|
|
955
|
+
|
|
956
|
+
queue.scheduleLowPriorityEntry( entry );
|
|
957
|
+
|
|
958
|
+
if ( entryIndex == queue.lowPriorityQueue.length -1 )
|
|
959
|
+
{
|
|
960
|
+
if ( settings.cmd4Dbg )
|
|
961
|
+
{
|
|
962
|
+
if ( queue.queueType == constants.QUEUETYPE_WORM ||
|
|
963
|
+
queue.queueType == constants.QUEUETYPE_WORM2
|
|
964
|
+
)
|
|
965
|
+
{
|
|
966
|
+
entry.accessory.log.debug( `All characteristics are now being polled for queue: "${ queue.queueName }"` );
|
|
967
|
+
}
|
|
968
|
+
else
|
|
969
|
+
{
|
|
970
|
+
entry.accessory.log.debug( `All characteristics are now being polled for "${ entry.accessory.displayName }"` );
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
allDoneCallback( allDoneCount );
|
|
974
|
+
}
|
|
975
|
+
|
|
976
|
+
}, delay );
|
|
977
|
+
|
|
978
|
+
if ( staggeredDelayIndex++ >= staggeredDelaysLength )
|
|
979
|
+
staggeredDelayIndex = 0;
|
|
980
|
+
|
|
981
|
+
if ( lastAccessoryUUID != entry.accessory.uuid )
|
|
982
|
+
staggeredDelayIndex = 0;
|
|
983
|
+
|
|
984
|
+
lastAccessoryUUID = entry.accessory.uuid;
|
|
985
|
+
|
|
986
|
+
delay += staggeredDelays[ staggeredDelayIndex ];
|
|
987
|
+
|
|
988
|
+
});
|
|
989
|
+
}
|
|
990
|
+
|
|
991
|
+
queue.queueStarted = true;
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
changeQueueType( queue, queueType )
|
|
995
|
+
{
|
|
996
|
+
if ( queue.queueStarted )
|
|
997
|
+
throw new Error( `Cannot change queueType when queue is running` );
|
|
998
|
+
|
|
999
|
+
// The WoRm queue needs error messages to be silenced as
|
|
1000
|
+
// they are inevitable, but are handled through retries
|
|
1001
|
+
// By default non WoRm queus are allowed to echo errors
|
|
1002
|
+
this.echoE = true;
|
|
1003
|
+
|
|
1004
|
+
// Default
|
|
1005
|
+
this.processQueueFunc = this.processWormQueue;
|
|
1006
|
+
switch ( queueType )
|
|
1007
|
+
{
|
|
1008
|
+
case constants.QUEUETYPE_SEQUENTIAL:
|
|
1009
|
+
this.processQueueFunc = this.processSequentialQueue;
|
|
1010
|
+
break;
|
|
1011
|
+
case constants.QUEUETYPE_WORM:
|
|
1012
|
+
case constants.QUEUETYPE_WORM2:
|
|
1013
|
+
this.processQueueFunc = this.processWormQueue;
|
|
1014
|
+
// When not in debug mode, do not echo errors for the WoRm queue
|
|
1015
|
+
// as errors are handled through retries.
|
|
1016
|
+
if ( ! settings.cmd4Dbg )
|
|
1017
|
+
this.echoE = false;
|
|
1018
|
+
break;
|
|
1019
|
+
case constants.QUEUETYPE_STANDARD:
|
|
1020
|
+
// only polled entries go straight through the queue
|
|
1021
|
+
this.processQueueFunc = this.processPassThruQueue;
|
|
1022
|
+
break;
|
|
1023
|
+
case constants.QUEUETYPE_PASSTHRU:
|
|
1024
|
+
// entries go straight through the queue
|
|
1025
|
+
this.processQueueFunc = this.processPassThruQueue;
|
|
1026
|
+
break;
|
|
1027
|
+
default:
|
|
1028
|
+
this.log.error( `Error: Invalid queue type: ${ queueType }` );
|
|
1029
|
+
}
|
|
1030
|
+
}
|
|
1031
|
+
|
|
1032
|
+
isCharacteristicPolled( accTypeEnumIndex, queue, accessory )
|
|
1033
|
+
{
|
|
1034
|
+
if ( queue.lowPriorityQueue.filter(
|
|
1035
|
+
entry => entry.accessory.uuid == accessory.uuid &&
|
|
1036
|
+
entry.accTypeEnumIndex == accTypeEnumIndex
|
|
1037
|
+
).length == 0 )
|
|
1038
|
+
{
|
|
1039
|
+
return false;
|
|
1040
|
+
}
|
|
1041
|
+
|
|
1042
|
+
return true;
|
|
1043
|
+
}
|
|
1044
|
+
}
|
|
1045
|
+
|
|
1046
|
+
var queueExists = function( queueName )
|
|
1047
|
+
{
|
|
1048
|
+
return settings.listOfCreatedPriorityQueues[ queueName ];
|
|
1049
|
+
}
|
|
1050
|
+
|
|
1051
|
+
var addQueue = function( log, queueName, queueType = constants.DEFAULT_QUEUE_TYPE, queueRetryCount = constants.DEFAULT_WORM_QUEUE_RETRY_COUNT )
|
|
1052
|
+
{
|
|
1053
|
+
let queue = queueExists( queueName );
|
|
1054
|
+
if ( queue != undefined )
|
|
1055
|
+
return queue;
|
|
1056
|
+
|
|
1057
|
+
log.debug( `Creating new Priority Polled Queue "${ queueName }" with QueueType of: "${ queueType }" retryCount: ${queueRetryCount}` );
|
|
1058
|
+
|
|
1059
|
+
queue = new Cmd4PriorityPollingQueue( log, queueName, queueType, queueRetryCount );
|
|
1060
|
+
settings.listOfCreatedPriorityQueues[ queueName ] = queue;
|
|
1061
|
+
|
|
1062
|
+
return queue;
|
|
1063
|
+
|
|
1064
|
+
}
|
|
1065
|
+
|
|
1066
|
+
|
|
1067
|
+
|
|
1068
|
+
var parseAddQueueTypes = function ( log, entrys )
|
|
1069
|
+
{
|
|
1070
|
+
if ( trueTypeOf( entrys ) != Array )
|
|
1071
|
+
throw new Error( `${ constants.QUEUETYPES } is not an Array of { "Queue Name": "QueueType" }. found: ${ entrys }` );
|
|
1072
|
+
|
|
1073
|
+
entrys.forEach( ( entry, entryIndex ) =>
|
|
1074
|
+
{
|
|
1075
|
+
let queueName = null;
|
|
1076
|
+
let queueType = constants.DEFAULT_QUEUE_TYPE;
|
|
1077
|
+
let queueRetryCount = constants.DEFAULT_WORM_QUEUE_RETRY_COUNT;
|
|
1078
|
+
|
|
1079
|
+
for ( let key in entry )
|
|
1080
|
+
{
|
|
1081
|
+
let lcKey = lcFirst( key );
|
|
1082
|
+
|
|
1083
|
+
// warn now
|
|
1084
|
+
if ( key.charAt( 0 ) === key.charAt( 0 ).toUpperCase( ) )
|
|
1085
|
+
{
|
|
1086
|
+
log.warn( `The config.json queueTypes key: ${ key } is Capitalized. All keys in the near future will ALWAYS start with a lower case character for homebridge-ui integration.\nTo remove this Warning, Please fix your config.json.` );
|
|
1087
|
+
}
|
|
1088
|
+
|
|
1089
|
+
let value = entry[ key ];
|
|
1090
|
+
|
|
1091
|
+
switch( lcKey )
|
|
1092
|
+
{
|
|
1093
|
+
case constants.QUEUE:
|
|
1094
|
+
if ( settings.listOfCreatedPriorityQueues[ entry.queue ] )
|
|
1095
|
+
throw new Error( `QueueName: ${ entry.queue } was added twice` );
|
|
1096
|
+
|
|
1097
|
+
queueName = value;
|
|
1098
|
+
|
|
1099
|
+
break;
|
|
1100
|
+
case constants.QUEUETYPE:
|
|
1101
|
+
// Set the default Queue Retry Count based on QueueType
|
|
1102
|
+
switch ( value )
|
|
1103
|
+
{
|
|
1104
|
+
case constants.QUEUETYPE_WORM:
|
|
1105
|
+
queueRetryCount = constants.DEFAULT_WORM_QUEUE_RETRY_COUNT;
|
|
1106
|
+
queueType = value;
|
|
1107
|
+
break;
|
|
1108
|
+
case constants.QUEUETYPE_WORM2:
|
|
1109
|
+
queueRetryCount = constants.DEFAULT_WORM_QUEUE_RETRY_COUNT;
|
|
1110
|
+
queueType = value;
|
|
1111
|
+
break;
|
|
1112
|
+
case constants.QUEUETYPE_SEQUENTIAL:
|
|
1113
|
+
queueRetryCount = constants.DEFAULT_STANDARD_QUEUE_RETRY_COUNT;
|
|
1114
|
+
queueType = value;
|
|
1115
|
+
break;
|
|
1116
|
+
case constants.QUEUETYPE_STANDARD:
|
|
1117
|
+
queueRetryCount = constants.DEFAULT_STANDARD_QUEUE_RETRY_COUNT;
|
|
1118
|
+
queueType = value;
|
|
1119
|
+
break;
|
|
1120
|
+
default:
|
|
1121
|
+
throw new Error( `QueueType: ${ entry.queueType } is not valid at index: ${ entryIndex }. Expected: ${ constants.QUEUETYPE_WORM }, ${ constants.QUEUETYPE_WORM2 } or ${ constants.QUEUETYPE_SEQUENTIAL }` );
|
|
1122
|
+
}
|
|
1123
|
+
|
|
1124
|
+
break;
|
|
1125
|
+
case constants.QUEUE_RETRIES:
|
|
1126
|
+
queueRetryCount = value;
|
|
1127
|
+
break;
|
|
1128
|
+
default:
|
|
1129
|
+
throw new Error( `Unknown Queue option"${ key } not provided at index ${ entryIndex }` );
|
|
1130
|
+
}
|
|
1131
|
+
}
|
|
1132
|
+
|
|
1133
|
+
// At least a Queue name must be defined, the rest are defaulted
|
|
1134
|
+
if ( queueName == null )
|
|
1135
|
+
throw new Error( `"${ constants.QUEUE }" not provided at index ${ entryIndex }` );
|
|
1136
|
+
|
|
1137
|
+
if ( settings.cmd4Dbg ) log.debug( `calling addQueue: ${ queueName } type: ${ queueType } retryCount: ${ queueRetryCount }` );
|
|
1138
|
+
|
|
1139
|
+
addQueue( log, queueName, queueType, queueRetryCount );
|
|
1140
|
+
} );
|
|
1141
|
+
}
|
|
1142
|
+
|
|
1143
|
+
|
|
1144
|
+
module.exports = { addQueue,
|
|
1145
|
+
parseAddQueueTypes,
|
|
1146
|
+
queueExists,
|
|
1147
|
+
Cmd4PriorityPollingQueue
|
|
1148
|
+
}
|