dbus-victron-virtual 0.1.33 → 0.1.35

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.
Files changed (3) hide show
  1. package/README.md +6 -6
  2. package/package.json +2 -2
  3. package/src/index.js +57 -11
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.33",
3
+ "version": "0.1.35",
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.8",
36
+ "dbus-native-victron": "^0.4.11",
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
 
@@ -421,8 +426,12 @@ function addVictronInterfaces(
421
426
  );
422
427
  return;
423
428
  }
424
- declaration["properties"]["Mgmt/Connection"] = { type: "s", readonly: true };
425
- definition["Mgmt/Connection"] = "Node-RED";
429
+ if (!declaration["properties"]["Mgmt/Connection"]) {
430
+ declaration["properties"]["Mgmt/Connection"] = { type: "s", readonly: true };
431
+ }
432
+ if (definition["Mgmt/Connection"] == null) {
433
+ definition["Mgmt/Connection"] = "Node-RED";
434
+ }
426
435
  declaration["properties"]["Mgmt/ProcessName"] = { type: "s", readonly: true };
427
436
  definition["Mgmt/ProcessName"] = packageJson.name;
428
437
  declaration["properties"]["Mgmt/ProcessVersion"] = { type: "s", readonly: true };
@@ -470,6 +479,25 @@ function addVictronInterfaces(
470
479
  }
471
480
  };
472
481
 
482
+ function processPropertyChanges({ changes: values }) {
483
+ const changes = {}
484
+ debug("processPropertyChanges called with values:", values);
485
+ for (const [k, value] of Object.entries(values)) {
486
+ changes[k] = validateNewValue(k, declaration.properties[k], value);
487
+ }
488
+ const changedProperties = onPropertiesChanged({ changes, instance: definition });
489
+
490
+ for (const k of Object.keys(changedProperties)) {
491
+ if (!declaration.properties || !declaration.properties[k]) {
492
+ throw new Error(`Property ${k} not found in properties.`);
493
+ }
494
+ // we allow readonly properties to be changed through onPropertiesChanged, but
495
+ // not through SetValue, so we don't check readonly here.
496
+ }
497
+
498
+ return changedProperties;
499
+ }
500
+
473
501
  // we use this for GetItems and ItemsChanged.
474
502
  function getProperties(limitToPropertyNames = [], prependSlash = false) {
475
503
  // Filter entries based on specificItem if provided
@@ -505,6 +533,7 @@ function addVictronInterfaces(
505
533
  },
506
534
  SetValues: function(values /* msg */) {
507
535
  debug(`SetValues called with values:`, values);
536
+ const changes = {}
508
537
  for (const [k, value] of values) {
509
538
  if (!declaration.properties || !declaration.properties[k]) {
510
539
  throw new Error(`Property ${k} not found in properties.`);
@@ -512,12 +541,17 @@ function addVictronInterfaces(
512
541
  if ((declaration.properties[k] || {}).readonly) {
513
542
  return -1;
514
543
  }
515
- definition[k] = validateNewValue(k, declaration.properties[k], unwrapValue(value));
544
+ changes[k] = validateNewValue(k, declaration.properties[k], unwrapValue(value));
545
+ }
546
+ const changedProperties = processPropertyChanges({ changes });
547
+
548
+ for (const k of Object.keys(changedProperties)) {
549
+ definition[k] = changedProperties[k];
516
550
  }
517
551
 
518
- debug(`SetValues updated definition:`, definition);
552
+ debug(`SetValues updated properties:`, changedProperties);
519
553
  // TODO: we must include changed values only.
520
- iface.emit("ItemsChanged", getProperties(Object.keys(values), true));
554
+ iface.emit("ItemsChanged", getProperties(Object.keys(changedProperties), true));
521
555
  return 0;
522
556
  },
523
557
  emit: function(name, args) {
@@ -552,11 +586,12 @@ function addVictronInterfaces(
552
586
  }
553
587
  }
554
588
 
555
- for (const k of Object.keys(sanitizedValues)) {
556
- definition[k] = validateNewValue(k, declaration.properties[k], sanitizedValues[k]);
589
+ const changedProperties = processPropertyChanges({ changes: sanitizedValues });
590
+ for (const k of Object.keys(changedProperties)) {
591
+ definition[k] = changedProperties[k];
557
592
  }
558
593
  debug(`setValuesLocally updated definition:`, definition);
559
- iface.emit("ItemsChanged", getProperties(Object.keys(sanitizedValues), true));
594
+ iface.emit("ItemsChanged", getProperties(Object.keys(changedProperties), true));
560
595
  }
561
596
 
562
597
  const ifaceDesc = {
@@ -783,8 +818,19 @@ function addVictronInterfaces(
783
818
  return -1;
784
819
  }
785
820
  try {
786
- definition[k] = validateNewValue(k, declaration.properties[k], unwrapValue(value));
787
- iface.emit("ItemsChanged", getProperties([k], true));
821
+
822
+ const changedProperties = processPropertyChanges({
823
+ changes: {
824
+ [k]: validateNewValue(k, declaration.properties[k], unwrapValue(value))
825
+ }
826
+ })
827
+
828
+ // validation done, update definition with all changed properties
829
+ for (const changedKey of Object.keys(changedProperties)) {
830
+ definition[changedKey] = validateNewValue(changedKey, declaration.properties[changedKey], changedProperties[changedKey]);
831
+ }
832
+
833
+ iface.emit("ItemsChanged", getProperties(Object.keys(changedProperties), true));
788
834
  return 0;
789
835
  } catch (e) {
790
836
  console.error(e);