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