dbus-victron-virtual 0.1.0 → 0.1.2

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
@@ -1,52 +1,62 @@
1
1
  # About
2
2
 
3
+ > [!WARNING]
4
+ > This package is under active development. I believe it is ready for "bleeding
5
+ > edge" use, but its API may have to change, based on findings and bugs.
6
+ > Please use with caution.
7
+
3
8
  `dbus-victron-virtual` is a wrapper around
4
9
  [dbus-native](https://www.npmjs.com/package/dbus-native), which allows you to
5
10
  connect to [dbus](https://www.freedesktop.org/wiki/Software/dbus/), and
6
11
  simplify integrating with the [Victron](https://www.victronenergy.com/)
7
12
  infrastructure: To do this, `dbus-victron-virtual` provides functions to
8
13
 
9
- * expose your dbus interface as a Victron service, by implementing the dbus interface `com.victronenergy.BusItem`,
10
- * emit the Victron-specific event `ItemsChanged`, and
11
- * define and modify settings which are then available through Victron's settings interface.
14
+ - expose your dbus interface as a Victron service, by implementing the dbus interface `com.victronenergy.BusItem`,
15
+ - emit the Victron-specific event `ItemsChanged`, and
16
+ - define and modify settings which are then available through Victron's settings interface.
12
17
 
13
18
  See `dbus-victron-virtual` in action [here](https://github.com/Chris927/dbus-victron-virtual-test).
14
19
 
15
-
16
20
  This package may be for you if
17
21
 
18
- * you want to define virtual devices for testing on a Victron device, like a [Victron Cerbo GX](https://www.victronenergy.com/media/pg/Cerbo_GX/en/index-en.html), e.g. to use it in [Node-RED](https://www.victronenergy.com/live/venus-os:large), or
19
- * you need to integrate a device via dbus that is not (yet) supported by Victron natively.
20
-
22
+ - you want to define virtual devices for testing on a Victron device, like a [Victron Cerbo GX](https://www.victronenergy.com/media/pg/Cerbo_GX/en/index-en.html), e.g. to use it in [Node-RED](https://www.victronenergy.com/live/venus-os:large), or
23
+ - you need to integrate a device via dbus that is not (yet) supported by Victron natively.
21
24
 
22
25
  # Usage
23
26
 
24
- (TODO)
27
+ ## Prerequisites
25
28
 
29
+ You have a NodeJS project you want to run on [Venus
30
+ OS](https://github.com/victronenergy/venus), and you want to communicate with
31
+ the local dbus.
26
32
 
27
- # Development
33
+ ## Setup Up
28
34
 
35
+ - Add this package as a dependency: `npm add dbus-victron-virtual`.
36
+ - Use this package, by importing or requiring it: `const { addVictronInterfaces } = require('dbus-victron-virtual');`
37
+ - Make sure you understand how to use this package by studying the [example](https://github.com/Chris927/dbus-victron-virtual-test).
38
+ - Have fun.
29
39
 
30
- ## Prerequisites
40
+ # Development
31
41
 
32
- You can develop on a device that runs [Venus OS](https://github.com/victronenergy/venus). This way, the dbus environment as required by this package will be available. Alternatively, you can develop in any environment that support node 18 or higher, but you won't be able to run integration tests.
42
+ ## Prerequisites
33
43
 
44
+ You can develop on a device that runs [Venus OS](https:/.com/victronenergy/venus). This way, the dbus environment as required by this package will be available. Alternatively, you can develop in any environment that support node 18 or higher, but you won't be able to run integration tests.
34
45
 
35
46
  ## Steps
36
47
 
37
- * clone the repository
38
- * `npm install`
39
- * `npm run test`
40
- * run integration tests with `DBUS_SESSION_BUS_ADDRESS=unix:socket=/var/run/dbus/system_bus_socket TEST_INTEGRATION=1 npm run test`
48
+ - clone the repository
49
+ - `npm install`
50
+ - `npm run test`
51
+ - run tests and include running integration tests with `DBUS_SESSION_BUS_ADDRESS=unix:socket=/var/run/dbus/system_bus_socket TEST_INTEGRATION=1 npm run test`
41
52
 
42
53
  The implementation is in `./src/index.js`, tests are in `./src/__tests__`.
43
54
 
44
- Test coverage stats, when run with the integration `./src/__tests__/integrationTest.js`:
45
-
46
- ----------|---------|----------|---------|---------|---------------------------
47
- File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
48
- ----------|---------|----------|---------|---------|---------------------------
49
- All files | 85.18 | 69.23 | 100 | 84.9 |
50
- index.js | 85.18 | 69.23 | 100 | 84.9 | 28,35,39-41,94-95,130,170
51
- ----------|---------|----------|---------|---------|---------------------------
55
+ Test coverage stats for unit and integration tests (append ` -- --coverage` to the test command):
52
56
 
57
+ ----------|---------|----------|---------|---------|-----------------------------
58
+ File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
59
+ ----------|---------|----------|---------|---------|-----------------------------
60
+ All files | 87.32 | 73.91 | 100 | 86.76 |
61
+ index.js | 87.32 | 73.91 | 100 | 86.76 | 26,35,39-43,107-108,145,169
62
+ ----------|---------|----------|---------|---------|-----------------------------
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dbus-victron-virtual",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "Add interoperability with victron dbus to a given dbus interface",
5
5
  "main": "src/index.js",
6
6
  "type": "commonjs",
@@ -33,6 +33,7 @@
33
33
  "lint-staged": "^15.2.7"
34
34
  },
35
35
  "dependencies": {
36
- "dbus-native": "^0.4.0"
36
+ "dbus-native-victron": "^0.4.2",
37
+ "debug": "^4.3.7"
37
38
  }
38
39
  }
@@ -1,31 +1,47 @@
1
1
  /* eslint-env node */
2
- const { addVictronInterfaces } = require('../index');
2
+ const { addVictronInterfaces } = require("../index");
3
3
 
4
- describe('victron-dbus-virtual, emitItemsChanged tests', () => {
4
+ describe("victron-dbus-virtual, emitItemsChanged tests", () => {
5
+ const noopBus = { exportInterface: () => {} };
5
6
 
6
- const noopBus = { exportInterface: () => { } };
7
-
8
- it('works for the case without props', () => {
9
- const declaration = { name: 'foo' };
7
+ it("works for the case without props", () => {
8
+ const declaration = { name: "foo" };
10
9
  const definition = {};
11
- const { emitItemsChanged } = addVictronInterfaces(noopBus, declaration, definition);
10
+ const { emitItemsChanged } = addVictronInterfaces(
11
+ noopBus,
12
+ declaration,
13
+ definition,
14
+ false,
15
+ );
12
16
  emitItemsChanged();
13
17
  });
14
18
 
15
- it('works for the happy case', () => {
16
- const declaration = { name: 'foo', properties: { StringProp: 's' } };
17
- const definition = { StringProp: 'hello' };
19
+ it("works for the happy case", () => {
20
+ const declaration = { name: "foo", properties: { StringProp: "s" } };
21
+ const definition = { StringProp: "hello" };
18
22
  const emit = jest.fn();
19
23
  const bus = {
20
24
  exportInterface: (iface /* , _path, _ifaceDesc */) => {
21
25
  iface.emit = emit;
22
- }
23
- }
24
- const { emitItemsChanged } = addVictronInterfaces(bus, declaration, definition);
26
+ },
27
+ };
28
+ const { emitItemsChanged } = addVictronInterfaces(
29
+ bus,
30
+ declaration,
31
+ definition,
32
+ false,
33
+ );
25
34
  emitItemsChanged();
26
35
  expect(emit.mock.calls.length).toBe(1);
27
- expect(emit.mock.calls[0][0]).toBe('ItemsChanged');
28
- expect(emit.mock.calls[0][1]).toEqual([['StringProp', [['Value', ['s', 'hello']], ['Text', ['s', 'hello']]]]]);
36
+ expect(emit.mock.calls[0][0]).toBe("ItemsChanged");
37
+ expect(emit.mock.calls[0][1]).toEqual([
38
+ [
39
+ "StringProp",
40
+ [
41
+ ["Value", ["s", "hello"]],
42
+ ["Text", ["s", "hello"]],
43
+ ],
44
+ ],
45
+ ]);
29
46
  });
30
-
31
47
  });
@@ -0,0 +1,44 @@
1
+ const { addVictronInterfaces } = require("../index");
2
+
3
+ describe("victron-dbus-virtual, GetText being called on us", () => {
4
+ it("works for the happy case", () => {
5
+ const declaration = { name: "foo", properties: { IntProp: "i" } };
6
+ const definition = { IntProp: 42 };
7
+ const bus = {
8
+ exportInterface: jest.fn(),
9
+ };
10
+
11
+ addVictronInterfaces(bus, declaration, definition, false);
12
+
13
+ expect(bus.exportInterface.mock.calls.length).toBe(2);
14
+ expect(bus.exportInterface.mock.calls[1][0].GetText).toBeDefined();
15
+ expect(bus.exportInterface.mock.calls[1][1]).toBe("/IntProp");
16
+
17
+ const result = bus.exportInterface.mock.calls[1][0].GetText();
18
+ expect(result).toEqual("42");
19
+ });
20
+ it("works with our format, if we specify one", () => {
21
+ const declaration = {
22
+ name: "foo",
23
+ properties: {
24
+ IntProp: {
25
+ type: "i",
26
+ format: (v) => (v === 42 ? "fourty-two" : "not fourty-two"),
27
+ },
28
+ },
29
+ };
30
+ const definition = { IntProp: 42 };
31
+ const bus = {
32
+ exportInterface: jest.fn(),
33
+ };
34
+
35
+ addVictronInterfaces(bus, declaration, definition, false);
36
+
37
+ expect(bus.exportInterface.mock.calls.length).toBe(2);
38
+ expect(bus.exportInterface.mock.calls[1][0].GetText).toBeDefined();
39
+ expect(bus.exportInterface.mock.calls[1][1]).toBe("/IntProp");
40
+
41
+ const result = bus.exportInterface.mock.calls[1][0].GetText();
42
+ expect(result).toEqual("fourty-two");
43
+ });
44
+ });
@@ -0,0 +1,38 @@
1
+ const { addVictronInterfaces } = require("../index");
2
+
3
+ describe("victron-dbus-virtual, GetValue and GetItems being called on us", () => {
4
+ it("works for the happy case", () => {
5
+ const declaration = { name: "foo", properties: { IntProp: "i" } };
6
+ const definition = { IntProp: 42 };
7
+ const bus = {
8
+ exportInterface: jest.fn(),
9
+ };
10
+
11
+ addVictronInterfaces(bus, declaration, definition, false);
12
+
13
+ const calls = bus.exportInterface.mock.calls;
14
+ expect(calls.length).toBe(2);
15
+ expect(calls[1][0].GetValue).toBeDefined();
16
+ expect(calls[1][1]).toBe("/IntProp");
17
+
18
+ // GetItems on path / returns the item with value and text
19
+ const rootItemsResult = calls[0][0].GetItems();
20
+ expect(rootItemsResult).toEqual([
21
+ [
22
+ "/IntProp",
23
+ [
24
+ ["Value", ["i", 42]],
25
+ ["Text", ["s", "42"]],
26
+ ],
27
+ ],
28
+ ]);
29
+
30
+ // GetValue on path / returns all props (therefore nested array)
31
+ const rootValueResult = calls[0][0].GetValue();
32
+ expect(rootValueResult).toEqual([["/IntProp", ["i", 42]]]);
33
+
34
+ // GetValue on path /IntProp returns the value as a variant
35
+ const valueResult = calls[1][0].GetValue();
36
+ expect(valueResult).toEqual(["i", 42]);
37
+ });
38
+ });
@@ -0,0 +1,51 @@
1
+ const { addVictronInterfaces } = require("../index");
2
+
3
+ describe("GetValue and SetValue called on us", () => {
4
+ it("returns the value if not null, returns empty int variant array in case of null", () => {
5
+ const declaration = { name: "foo", properties: { SomeProp: "s" } };
6
+ const definition = { SomeProp: "some text" };
7
+ const bus = {
8
+ exportInterface: jest.fn(),
9
+ };
10
+
11
+ addVictronInterfaces(bus, declaration, definition, false);
12
+
13
+ const calls = bus.exportInterface.mock.calls;
14
+ expect(calls.length).toBe(2);
15
+ expect(calls[1][0].GetValue).toBeDefined();
16
+ expect(calls[1][1]).toBe("/SomeProp");
17
+
18
+ // GetValue on path /SomeProp returns the value as a variant
19
+ const valueResult = calls[1][0].GetValue();
20
+ expect(valueResult).toEqual(["s", "some text"]);
21
+
22
+ definition.SomeProp = null;
23
+ const valueResultNull = calls[1][0].GetValue();
24
+ expect(valueResultNull).toEqual(["ai", []]);
25
+ });
26
+ it("sets the value given on SetValue, and sets the value to null when empty int array given", () => {
27
+ const declaration = { name: "foo", properties: { SomeProp: "s" } };
28
+ const definition = { SomeProp: "some text" };
29
+ const bus = {
30
+ exportInterface: jest.fn(),
31
+ };
32
+
33
+ addVictronInterfaces(bus, declaration, definition, false);
34
+
35
+ const calls = bus.exportInterface.mock.calls;
36
+ expect(calls.length).toBe(2);
37
+ expect(calls[1][0].SetValue).toBeDefined();
38
+ expect(calls[1][1]).toBe("/SomeProp");
39
+
40
+ // SetValue on path /SomeProp sets the value on us
41
+ const result = calls[1][0].SetValue([[{ type: "s" }], ["new text"]]);
42
+ expect(definition.SomeProp).toEqual("new text");
43
+ expect(result).toBe(0);
44
+
45
+ // SetValue on path /SomeProp with an empty int array sets the value on us to null
46
+ // TODO
47
+ const nullResult = calls[1][0].SetValue([[{ type: "ai" }], [[]]]);
48
+ expect(definition.SomeProp).toEqual(null);
49
+ expect(nullResult).toBe(0);
50
+ });
51
+ });
@@ -0,0 +1,64 @@
1
+ const { addVictronInterfaces } = require("../index");
2
+
3
+ describe("victron-dbus-virtual, getValue for settings tests", () => {
4
+ it("works for the happy case", async () => {
5
+ const declaration = { name: "foo" };
6
+ const definition = {};
7
+ const bus = {
8
+ exportInterface: () => {},
9
+ invoke: function (args, cb) {
10
+ process.nextTick(() => cb(null, args));
11
+ },
12
+ };
13
+ const { getValue } = addVictronInterfaces(
14
+ bus,
15
+ declaration,
16
+ definition,
17
+ false,
18
+ );
19
+
20
+ // NOTE: calling getValue() is useful to retrieve the value of a setting.
21
+ // See https://github.com/Chris927/dbus-victron-virtual-test/blob/master/index.js for an example.
22
+
23
+ const result = await getValue({
24
+ path: "/StringProp",
25
+ interface_: "foo",
26
+ destination: "foo",
27
+ });
28
+ expect(result.member).toBe("GetValue");
29
+ expect(result.path).toBe("/StringProp");
30
+ expect(result.interface).toBe("foo");
31
+ expect(result.destination).toBe("foo");
32
+ });
33
+
34
+ it("fails if invoke fails", async () => {
35
+ const declaration = { name: "foo" };
36
+ const definition = {};
37
+ const bus = {
38
+ exportInterface: () => {},
39
+ invoke: function (args, cb) {
40
+ process.nextTick(() => cb(new Error("oops")));
41
+ },
42
+ };
43
+ const { getValue } = addVictronInterfaces(
44
+ bus,
45
+ declaration,
46
+ definition,
47
+ false,
48
+ );
49
+
50
+ // NOTE: calling getValue() is useful to retrieve the value of a setting.
51
+ // See https://github.com/Chris927/dbus-victron-virtual-test/blob/master/index.js for an example.
52
+
53
+ try {
54
+ await getValue({
55
+ path: "/StringProp",
56
+ interface_: "foo",
57
+ destination: "foo",
58
+ });
59
+ expect(false, "should have thrown");
60
+ } catch (err) {
61
+ expect(err.message).toBe("oops");
62
+ }
63
+ });
64
+ });
@@ -1,94 +1,108 @@
1
1
  /* eslint-env node */
2
- const { addVictronInterfaces } = require('../index');
3
-
4
- describe('victron-dbus-virtual, input parameters tests', () => {
2
+ const { addVictronInterfaces } = require("../index");
5
3
 
4
+ describe("victron-dbus-virtual, input parameters tests", () => {
6
5
  const noopBus = { exportInterface: () => { } };
7
6
 
8
- it('works for the trivial case', () => {
9
- const declaration = { name: 'foo' };
7
+ it("works for the trivial case", () => {
8
+ const declaration = { name: "foo" };
10
9
  const definition = {};
11
- const result = addVictronInterfaces(noopBus, declaration, definition);
10
+ const result = addVictronInterfaces(
11
+ noopBus,
12
+ declaration,
13
+ definition,
14
+ false,
15
+ );
12
16
  expect(!!result).toBe(true);
13
17
  });
14
18
 
15
- it('fails in some scenarios', () => {
19
+ it("fails in some scenarios", () => {
16
20
  try {
17
21
  addVictronInterfaces(noopBus, {}, {});
18
22
  } catch (e) {
19
- expect(e.message.includes('Interface name')).toBe(true);
23
+ expect(e.message.includes("Interface name")).toBe(true);
20
24
  }
21
25
  try {
22
- addVictronInterfaces(noopBus, { name: '' }, {});
26
+ addVictronInterfaces(noopBus, { name: "" }, {});
23
27
  } catch (e) {
24
- expect(e.message.includes('Interface name')).toBe(true);
28
+ expect(e.message.includes("Interface name")).toBe(true);
25
29
  }
26
30
  });
27
31
 
28
- it('works for an example with properties', () => {
32
+ it("works for an example with properties", () => {
29
33
  const declaration = {
30
- name: 'com.victronenergy.myservice',
34
+ name: "com.victronenergy.myservice",
31
35
  properties: {
32
- 'foo': 'i',
33
- 'bar': 's',
34
- }
36
+ foo: "i",
37
+ bar: "s",
38
+ },
35
39
  };
36
40
  const definition = {
37
41
  foo: 42,
38
- bar: 'hello',
39
- emit: function() { }
42
+ bar: "hello",
43
+ emit: function () { },
40
44
  };
41
- const result = addVictronInterfaces(noopBus, declaration, definition);
45
+ const result = addVictronInterfaces(
46
+ noopBus,
47
+ declaration,
48
+ definition,
49
+ false,
50
+ );
42
51
  expect(!!result).toBe(true);
43
52
  expect(result.warnings.length).toBe(0);
44
53
  });
45
54
 
46
- it('provides a warning if the interface name contains problematic characters', () => {
55
+ it("provides a warning if the interface name contains problematic characters", () => {
47
56
  const declaration = {
48
- name: 'com.victronenergy.my-service-with-dashes',
57
+ name: "com.victronenergy.my-service-with-dashes",
49
58
  };
50
- const { warnings } = addVictronInterfaces(noopBus, declaration, {});
59
+ const { warnings } = addVictronInterfaces(noopBus, declaration, {}, false);
51
60
  expect(warnings.length).toBe(1);
52
- expect(warnings[0].includes('problematic characters')).toBe(true);
61
+ expect(warnings[0].includes("problematic characters")).toBe(true);
53
62
  });
54
63
 
55
- it('provides a warning if the interface name does not start with com.victronenergy', () => {
64
+ it("provides a warning if the interface name does not start with com.victronenergy", () => {
56
65
  const declaration = {
57
- name: 'com.example.my_service',
66
+ name: "com.example.my_service",
58
67
  };
59
- const { warnings } = addVictronInterfaces(noopBus, declaration, {});
68
+ const { warnings } = addVictronInterfaces(noopBus, declaration, {}, false);
60
69
  expect(warnings.length).toBe(1);
61
- expect(warnings[0].includes('start with com.victronenergy')).toBe(true);
70
+ expect(warnings[0].includes("start with com.victronenergy")).toBe(true);
62
71
  });
63
72
 
64
- it('exports GetItems, and SetValue for each property', () => {
73
+ it("exports GetItems, GetValue, GetText, and SetValue for each property", () => {
65
74
  const declaration = {
66
- name: 'some_name',
75
+ name: "some_name",
67
76
  properties: {
68
- 'foo': 'i',
69
- 'bar': 's',
70
- 'baz': 'b',
71
- }
72
- }
77
+ foo: "i",
78
+ bar: "s",
79
+ baz: "b",
80
+ },
81
+ };
73
82
  const bus = {
74
- exportInterface: jest.fn()
83
+ exportInterface: jest.fn(),
75
84
  };
76
85
 
77
- addVictronInterfaces(bus, declaration, {});
86
+ addVictronInterfaces(bus, declaration, {}, false);
78
87
  expect(bus.exportInterface.mock.calls.length).toBe(4);
79
88
 
80
89
  const call0 = bus.exportInterface.mock.calls[0];
81
- expect(Object.keys(call0[0])).toStrictEqual(['GetItems', 'emit']);
90
+ expect(Object.keys(call0[0])).toStrictEqual([
91
+ "GetItems",
92
+ "GetValue",
93
+ "emit",
94
+ ]);
82
95
 
83
96
  const call1 = bus.exportInterface.mock.calls[1];
84
- expect(Object.keys(call1[0])).toStrictEqual(['SetValue']);
85
- expect(call1[1]).toStrictEqual('/foo');
86
- expect(call1[2].name).toBe('com.victronenergy.BusItem');
97
+ expect(Object.keys(call1[0])).toStrictEqual([
98
+ "GetValue",
99
+ "GetText",
100
+ "SetValue",
101
+ ]);
102
+ expect(call1[1]).toStrictEqual("/foo");
103
+ expect(call1[2].name).toBe("com.victronenergy.BusItem");
87
104
 
88
105
  const call3 = bus.exportInterface.mock.calls[3];
89
- expect(call3[1]).toStrictEqual('/baz');
90
-
106
+ expect(call3[1]).toStrictEqual("/baz");
91
107
  });
92
-
93
-
94
108
  });