dbus-victron-virtual 0.1.32 → 0.1.34

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/README.md CHANGED
@@ -66,12 +66,12 @@ Test coverage stats for unit and integration tests (append ` -- --coverage` to t
66
66
 
67
67
  ## Test Coverage
68
68
 
69
- Coverage as per v0.1.23:
69
+ Coverage as per v0.1.33:
70
70
 
71
- ----------|---------|----------|---------|---------|------------------------------------------------------------------------------------------------------------------------
71
+ ----------|---------|----------|---------|---------|---------------------------------------------------------------------------------
72
72
  File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
73
- ----------|---------|----------|---------|---------|------------------------------------------------------------------------------------------------------------------------
74
- All files | 87.3 | 80.23 | 86.36 | 87.22 |
75
- index.js | 87.3 | 80.23 | 86.36 | 87.22 | 73,85,91-93,98,110,120,171,178,182,229,253,264,309-340,374-377,381-385,423,472,570-573,629,643-646,708,713-716,787-789
76
- ----------|---------|----------|---------|---------|------------------------------------------------------------------------------------------------------------------------
73
+ ----------|---------|----------|---------|---------|---------------------------------------------------------------------------------
74
+ All files | 91.59 | 85.81 | 86.56 | 91.54 |
75
+ index.js | 91.59 | 85.81 | 86.56 | 91.54 | 266,290,301,346-377,411-414,418-422,461,619-622,678,692-695,750,755-758,832-834
76
+ ----------|---------|----------|---------|---------|---------------------------------------------------------------------------------
77
77
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dbus-victron-virtual",
3
- "version": "0.1.32",
3
+ "version": "0.1.34",
4
4
  "description": "Add interoperability with victron dbus to a given dbus interface",
5
5
  "main": "src/index.js",
6
6
  "type": "commonjs",
@@ -33,7 +33,7 @@
33
33
  "lint-staged": "^15.2.7"
34
34
  },
35
35
  "dependencies": {
36
- "dbus-native-victron": "^0.4.7",
36
+ "dbus-native-victron": "^0.4.9",
37
37
  "debug": "^4.3.7"
38
38
  },
39
39
  "files": [
package/src/index.js CHANGED
@@ -380,12 +380,17 @@ async function getMax(bus, { path, interface_, destination }) {
380
380
  });
381
381
  }
382
382
 
383
+ function defaultOnPropertiesChanged({ changes }) {
384
+ return changes; // NOOP
385
+ }
386
+
383
387
  function addVictronInterfaces(
384
388
  bus,
385
389
  declaration,
386
390
  definition,
387
391
  add_defaults = true,
388
- emitCallback = null
392
+ emitCallback = null,
393
+ onPropertiesChanged = defaultOnPropertiesChanged
389
394
  ) {
390
395
  const warnings = [];
391
396
 
@@ -470,6 +475,25 @@ function addVictronInterfaces(
470
475
  }
471
476
  };
472
477
 
478
+ function processPropertyChanges({ changes: values }) {
479
+ const changes = {}
480
+ debug("processPropertyChanges called with values:", values);
481
+ for (const [k, value] of Object.entries(values)) {
482
+ changes[k] = validateNewValue(k, declaration.properties[k], value);
483
+ }
484
+ const changedProperties = onPropertiesChanged({ changes, instance: definition });
485
+
486
+ for (const k of Object.keys(changedProperties)) {
487
+ if (!declaration.properties || !declaration.properties[k]) {
488
+ throw new Error(`Property ${k} not found in properties.`);
489
+ }
490
+ // we allow readonly properties to be changed through onPropertiesChanged, but
491
+ // not through SetValue, so we don't check readonly here.
492
+ }
493
+
494
+ return changedProperties;
495
+ }
496
+
473
497
  // we use this for GetItems and ItemsChanged.
474
498
  function getProperties(limitToPropertyNames = [], prependSlash = false) {
475
499
  // Filter entries based on specificItem if provided
@@ -505,6 +529,7 @@ function addVictronInterfaces(
505
529
  },
506
530
  SetValues: function(values /* msg */) {
507
531
  debug(`SetValues called with values:`, values);
532
+ const changes = {}
508
533
  for (const [k, value] of values) {
509
534
  if (!declaration.properties || !declaration.properties[k]) {
510
535
  throw new Error(`Property ${k} not found in properties.`);
@@ -512,12 +537,17 @@ function addVictronInterfaces(
512
537
  if ((declaration.properties[k] || {}).readonly) {
513
538
  return -1;
514
539
  }
515
- definition[k] = validateNewValue(k, declaration.properties[k], unwrapValue(value));
540
+ changes[k] = validateNewValue(k, declaration.properties[k], unwrapValue(value));
541
+ }
542
+ const changedProperties = processPropertyChanges({ changes });
543
+
544
+ for (const k of Object.keys(changedProperties)) {
545
+ definition[k] = changedProperties[k];
516
546
  }
517
547
 
518
- debug(`SetValues updated definition:`, definition);
548
+ debug(`SetValues updated properties:`, changedProperties);
519
549
  // TODO: we must include changed values only.
520
- iface.emit("ItemsChanged", getProperties(Object.keys(values), true));
550
+ iface.emit("ItemsChanged", getProperties(Object.keys(changedProperties), true));
521
551
  return 0;
522
552
  },
523
553
  emit: function(name, args) {
@@ -552,11 +582,12 @@ function addVictronInterfaces(
552
582
  }
553
583
  }
554
584
 
555
- for (const k of Object.keys(sanitizedValues)) {
556
- definition[k] = validateNewValue(k, declaration.properties[k], sanitizedValues[k]);
585
+ const changedProperties = processPropertyChanges({ changes: sanitizedValues });
586
+ for (const k of Object.keys(changedProperties)) {
587
+ definition[k] = changedProperties[k];
557
588
  }
558
589
  debug(`setValuesLocally updated definition:`, definition);
559
- iface.emit("ItemsChanged", getProperties(Object.keys(sanitizedValues), true));
590
+ iface.emit("ItemsChanged", getProperties(Object.keys(changedProperties), true));
560
591
  }
561
592
 
562
593
  const ifaceDesc = {
@@ -783,8 +814,19 @@ function addVictronInterfaces(
783
814
  return -1;
784
815
  }
785
816
  try {
786
- definition[k] = validateNewValue(k, declaration.properties[k], unwrapValue(value));
787
- iface.emit("ItemsChanged", getProperties([k], true));
817
+
818
+ const changedProperties = processPropertyChanges({
819
+ changes: {
820
+ [k]: validateNewValue(k, declaration.properties[k], unwrapValue(value))
821
+ }
822
+ })
823
+
824
+ // validation done, update definition with all changed properties
825
+ for (const changedKey of Object.keys(changedProperties)) {
826
+ definition[changedKey] = validateNewValue(changedKey, declaration.properties[changedKey], changedProperties[changedKey]);
827
+ }
828
+
829
+ iface.emit("ItemsChanged", getProperties(Object.keys(changedProperties), true));
788
830
  return 0;
789
831
  } catch (e) {
790
832
  console.error(e);