1
1
const merge = require ( 'deepmerge' )
2
2
const deepEql = require ( 'deep-eql' )
3
+ const RateLimiter = require ( './RateLimiter' )
3
4
const {
4
5
getValidators,
5
6
getDecorator,
@@ -23,6 +24,19 @@ module.exports = function (RED) {
23
24
const validators = getValidators ( config . template )
24
25
const decorator = getDecorator ( config . template )
25
26
27
+ let isIncomingMsgProcessingAllowed = true
28
+
29
+ const rater = new RateLimiter ( {
30
+ highWaterMark : 15 ,
31
+ intervalInSec : 60 ,
32
+ onExhaustionCb : ( ) => {
33
+ isIncomingMsgProcessingAllowed = false
34
+ console . log (
35
+ 'Blocking device state sync to Alexa from now on! Quota exhausted!'
36
+ )
37
+ }
38
+ } )
39
+
26
40
const getLocalState = function ( ) {
27
41
return { ...localState }
28
42
}
@@ -77,10 +91,15 @@ module.exports = function (RED) {
77
91
const mergedState = merge ( oldLocalState , approvedState )
78
92
const newLocalState = { ...mergedState , source : 'device' }
79
93
80
- if ( ! deepEql ( oldLocalState , newLocalState ) ) {
94
+ if (
95
+ isIncomingMsgProcessingAllowed &&
96
+ ! deepEql ( oldLocalState , newLocalState )
97
+ ) {
81
98
setLocalState ( newLocalState )
82
99
83
- connectionNode . updateShadow ( { nodeId, type : 'desired' } )
100
+ rater . execute ( ( ) =>
101
+ connectionNode . updateShadow ( { nodeId, type : 'desired' } )
102
+ )
84
103
}
85
104
86
105
if ( config . passthrough && Object . keys ( approvedState ) . length > 0 ) {
0 commit comments