@kronos-integration/service-mqtt 1.0.0 → 1.0.1

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.
@@ -17,12 +17,12 @@ jobs:
17
17
  matrix:
18
18
  os:
19
19
  - ubuntu-latest
20
- - macos-latest
21
- - windows-latest
22
20
  node-version:
23
21
  - 22.17.0
24
22
  - 24.3.0
25
23
  steps:
24
+ - name: install
25
+ run: sudo apt-get -y install mosquitto
26
26
  - name: checkout
27
27
  uses: actions/checkout@v4.2.2
28
28
  - name: prepare node
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kronos-integration/service-mqtt",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "provenance": true
@@ -23,16 +23,16 @@ export class ServiceMQTT extends Service {
23
23
  static get configurationAttributes() {
24
24
  return mergeAttributes(
25
25
  createAttributes({
26
- "url": {
27
- description: "server listen definition",
28
-
29
- attributes: {
30
- url: {
31
- description: "url of the mqtt server",
32
- needsRestart: true,
33
- type: "url"
34
- }
35
- }
26
+ url: {
27
+ description: "url of the mqtt server",
28
+ needsRestart: true,
29
+ type: "url"
30
+ },
31
+ username: {
32
+ type: "string"
33
+ },
34
+ password: {
35
+ type: "string"
36
36
  }
37
37
  }),
38
38
  Service.configurationAttributes
@@ -46,6 +46,14 @@ export class ServiceMQTT extends Service {
46
46
  return `${this.name}(${this.url})`;
47
47
  }
48
48
 
49
+ get options() {
50
+ return { username: this.username, password: this.password };
51
+ }
52
+
53
+ get topics() {
54
+ return Object.keys(this.endpoints).filter(e=>e.topic);
55
+ }
56
+
49
57
  /**
50
58
  * On demand create TopicEndpoint.
51
59
  * @param {string} name
@@ -53,13 +61,7 @@ export class ServiceMQTT extends Service {
53
61
  * @return {Class} TopicEndpoint if path is present of name starts with '/'
54
62
  */
55
63
  endpointFactoryFromConfig(name, definition, ic) {
56
-
57
- if (
58
- definition.method ||
59
- definition.path ||
60
- name[0] === "/" ||
61
- name.match(/^\w+:\//)
62
- ) {
64
+ if (definition.topic) {
63
65
  return TopicEndpoint;
64
66
  }
65
67
 
@@ -69,7 +71,24 @@ export class ServiceMQTT extends Service {
69
71
  async _start() {
70
72
  await super._start();
71
73
 
72
- this.client = connect(this.url);
74
+ const client = connect(this.url, this.options);
75
+
76
+ this.client = client;
77
+
78
+ client.on("connect", err => {
79
+ client.subscribe(this.topics, (err, granted) => {
80
+ if (err) {
81
+ this.error(err);
82
+ }
83
+ });
84
+ });
85
+
86
+ client.on("message", (topic, message) => {
87
+ const ep = this.endpoints[topic];
88
+ if (ep) {
89
+ ep.receive(message);
90
+ }
91
+ });
73
92
  }
74
93
 
75
94
  async _stop() {
@@ -1,6 +1,16 @@
1
-
2
1
  import { SendReceiveEndpoint } from "@kronos-integration/endpoint";
3
2
  import { Service } from "@kronos-integration/service";
4
3
 
5
4
  export class TopicEndpoint extends SendReceiveEndpoint {
6
- }
5
+ constructor(name, owner, options) {
6
+ super(name, owner, options);
7
+
8
+ if (options.topic) {
9
+ this._topic = options.topic;
10
+ }
11
+ }
12
+
13
+ get topic() {
14
+ return this._topic || this.name;
15
+ }
16
+ }
@@ -1,13 +1,8 @@
1
1
  import test from "ava";
2
2
 
3
3
  import { ReceiveEndpoint } from "@kronos-integration/endpoint";
4
- import {
5
- StandaloneServiceProvider,
6
- } from "@kronos-integration/service";
7
- import {
8
- ServiceMQTT, TopicEndpoint
9
- } from "@kronos-integration/service-mqtt";
10
-
4
+ import { StandaloneServiceProvider } from "@kronos-integration/service";
5
+ import { ServiceMQTT, TopicEndpoint } from "@kronos-integration/service-mqtt";
11
6
 
12
7
  test("endpoint factory", async t => {
13
8
  const sp = new StandaloneServiceProvider();
@@ -17,14 +12,13 @@ test("endpoint factory", async t => {
17
12
 
18
13
  const mqtt = await sp.declareService({
19
14
  type: ServiceMQTT,
20
- listen: {
21
- socket: 1241
22
- },
23
15
  endpoints: {
24
- "/s1": { connected: r1,}
16
+ s1: { topic: "s1", connected: r1 }
25
17
  }
26
18
  });
27
19
 
28
- t.is(mqtt.endpoints["/s1"].name, "/s1");
29
- t.true(mqtt.endpoints["/s1"] instanceof TopicEndpoint);
20
+ t.is(mqtt.endpoints["s1"].name, "s1");
21
+ t.true(mqtt.endpoints["s1"] instanceof TopicEndpoint);
22
+
23
+ // t.deepEqual(mqtt.topics, ["s1"]);
30
24
  });
@@ -3,6 +3,11 @@ export { TopicEndpoint };
3
3
  * MQTT client.
4
4
  */
5
5
  export class ServiceMQTT extends Service {
6
+ get options(): {
7
+ username: any;
8
+ password: any;
9
+ };
10
+ get topics(): string[];
6
11
  /**
7
12
  * On demand create TopicEndpoint.
8
13
  * @param {string} name
@@ -1,3 +1,6 @@
1
1
  export class TopicEndpoint extends SendReceiveEndpoint {
2
+ constructor(name: any, owner: any, options: any);
3
+ _topic: any;
4
+ get topic(): any;
2
5
  }
3
6
  import { SendReceiveEndpoint } from "@kronos-integration/endpoint";