signalk-automation-plugin 0.0.9 → 0.0.10
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/lib/engine.js +39 -0
- package/lib/runtime.js +2 -8
- package/package.json +1 -1
package/lib/engine.js
CHANGED
|
@@ -74,6 +74,23 @@ function evalClause (clause, ctx) {
|
|
|
74
74
|
clause: { not: true }
|
|
75
75
|
}
|
|
76
76
|
}
|
|
77
|
+
if (clause.triggered) {
|
|
78
|
+
const want = clause.triggered
|
|
79
|
+
const fired = ctx.trigger && ctx.trigger.path
|
|
80
|
+
let expected = null
|
|
81
|
+
if (want && typeof want === 'object') {
|
|
82
|
+
if (want.helper) expected = helperPath(want.helper)
|
|
83
|
+
else if (want.path) expected = want.path
|
|
84
|
+
} else if (typeof want === 'string') {
|
|
85
|
+
expected = want
|
|
86
|
+
}
|
|
87
|
+
return {
|
|
88
|
+
pass: expected != null && fired === expected,
|
|
89
|
+
error: expected == null ? 'triggered needs helper or path' : null,
|
|
90
|
+
actual: fired || null,
|
|
91
|
+
expected
|
|
92
|
+
}
|
|
93
|
+
}
|
|
77
94
|
if (Array.isArray(clause.all)) {
|
|
78
95
|
const parts = clause.all.map((c) => withLabel(c, evalClause(c, ctx)))
|
|
79
96
|
return {
|
|
@@ -212,6 +229,12 @@ function fmtVal (value) {
|
|
|
212
229
|
function clauseLabel (clause, result) {
|
|
213
230
|
if (!clause || typeof clause !== 'object') return ''
|
|
214
231
|
if (clause.not) return 'not ' + clauseLabel(clause.not, result)
|
|
232
|
+
if (clause.triggered) {
|
|
233
|
+
const want = clause.triggered
|
|
234
|
+
if (want && want.helper) return 'triggered helper ' + want.helper
|
|
235
|
+
if (want && want.path) return 'triggered path ' + want.path
|
|
236
|
+
return 'triggered ' + fmtVal(want)
|
|
237
|
+
}
|
|
215
238
|
if (Array.isArray(clause.all)) return 'all'
|
|
216
239
|
if (Array.isArray(clause.any)) return 'any'
|
|
217
240
|
if (clause.zone) {
|
|
@@ -636,6 +659,20 @@ function sameValue (a, b) {
|
|
|
636
659
|
return false
|
|
637
660
|
}
|
|
638
661
|
|
|
662
|
+
function asSwitch (v) {
|
|
663
|
+
if (v === true || v === 1 || v === '1' || v === 'on' || v === 'ON') return 1
|
|
664
|
+
if (v === false || v === 0 || v === '0' || v === 'off' || v === 'OFF') return 0
|
|
665
|
+
return null
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
function valueUnchanged (previous, next) {
|
|
669
|
+
if (previous === undefined) return false
|
|
670
|
+
if (sameValue(previous, next)) return true
|
|
671
|
+
const a = asSwitch(previous)
|
|
672
|
+
const b = asSwitch(next)
|
|
673
|
+
return a !== null && a === b
|
|
674
|
+
}
|
|
675
|
+
|
|
639
676
|
function makeRunScript (opts) {
|
|
640
677
|
return function (spec) {
|
|
641
678
|
return runScript({
|
|
@@ -664,6 +701,8 @@ module.exports = {
|
|
|
664
701
|
makeRunScript,
|
|
665
702
|
parseDuration,
|
|
666
703
|
sameValue,
|
|
704
|
+
asSwitch,
|
|
705
|
+
valueUnchanged,
|
|
667
706
|
clauseLabel,
|
|
668
707
|
triggerFiredLabel,
|
|
669
708
|
triggerListMatched,
|
package/lib/runtime.js
CHANGED
|
@@ -151,6 +151,7 @@ class Runtime {
|
|
|
151
151
|
: undefined
|
|
152
152
|
this.setPathValue(p, value, { $source: source })
|
|
153
153
|
await this.applyLatches(p, source, previous)
|
|
154
|
+
if (engine.valueUnchanged(previous, value)) return
|
|
154
155
|
await this.kickPath(p, value)
|
|
155
156
|
}
|
|
156
157
|
|
|
@@ -159,8 +160,7 @@ class Runtime {
|
|
|
159
160
|
if (fromSelf) return
|
|
160
161
|
if (previous === undefined) return
|
|
161
162
|
const next = this.values[p]
|
|
162
|
-
if (engine.
|
|
163
|
-
if (asSwitch(previous) !== null && asSwitch(previous) === asSwitch(next)) return
|
|
163
|
+
if (engine.valueUnchanged(previous, next)) return
|
|
164
164
|
for (const def of Object.values(this.doc.helpers)) {
|
|
165
165
|
if (def.latch_on_external_put === p && def.type === 'boolean') {
|
|
166
166
|
await this.setHelper(def.id, true)
|
|
@@ -507,12 +507,6 @@ function newAbort () {
|
|
|
507
507
|
return abort
|
|
508
508
|
}
|
|
509
509
|
|
|
510
|
-
function asSwitch (v) {
|
|
511
|
-
if (v === true || v === 1 || v === '1' || v === 'on' || v === 'ON') return 1
|
|
512
|
-
if (v === false || v === 0 || v === '0' || v === 'off' || v === 'OFF') return 0
|
|
513
|
-
return null
|
|
514
|
-
}
|
|
515
|
-
|
|
516
510
|
function holdMs (auto) {
|
|
517
511
|
for (const t of auto.trigger || []) {
|
|
518
512
|
if (t.for) {
|