@lifefinder/vsm-mqtt-client-open-source 0.0.59 → 0.0.61

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.
@@ -1,3 +1,5 @@
1
+ // Copyright (C) 2026, Lifefinder Systems International AB, all rights reserved.
2
+ // ChirpStack v4 MQTT integration. Supports plain-password auth via CS4_* env vars.
1
3
  const mqtt = require('mqtt')
2
4
  const { isDate } = require('util/types');
3
5
 
@@ -50,7 +52,20 @@ module.exports.api = {
50
52
  connectAndSubscribe: async (args, devices, onUplinkDevicePortBufferDateLatLng) => {
51
53
  args.v && console.log("Trying to connect to " + args.s + " with application " + args.a);
52
54
  try {
53
- const client = mqtt.connect(args.s);
55
+ // Build connect options — auth required for mosquitto plain-password auth (CS4).
56
+ // Credentials read from env so the integration file stays config-free.
57
+ const mqttOpts = {};
58
+ if (process.env.CS4_USERNAME) {
59
+ mqttOpts.clientId = process.env.CS4_CLIENTID || 'vsm-mqtt-client-v4';
60
+ mqttOpts.username = process.env.CS4_USERNAME;
61
+ mqttOpts.password = process.env.CS4_PASSWORD;
62
+ }
63
+ const client = mqtt.connect(args.s, mqttOpts);
64
+
65
+ client.on('error', (e) => {
66
+ console.log("MQTT ChirpStack 4 Connection Error", e);
67
+ process.exit(1);
68
+ });
54
69
 
55
70
  client.on('connect', () => {
56
71
  args.v && console.log("Connected to chirpstack server");
@@ -93,9 +108,19 @@ module.exports.api = {
93
108
  // message is Buffer
94
109
  args.v && console.log(topic, message.toString());
95
110
 
96
- const obj = JSON.parse(message.toString('utf-8'));
111
+ let obj;
112
+ try {
113
+ obj = JSON.parse(message.toString('utf-8'));
114
+ } catch (e) {
115
+ console.log("Chirpstack: failed to parse message: " + e.message);
116
+ return;
117
+ }
97
118
  if (!obj.data)
98
119
  return;
120
+ if (!obj.deviceInfo || !obj.deviceInfo.devEui) {
121
+ console.log("Chirpstack: message missing deviceInfo.devEui, skipping");
122
+ return;
123
+ }
99
124
  const data = Buffer.from(obj.data, "base64");
100
125
  const port = obj.fPort;
101
126
  const id = obj.deviceInfo.devEui;
@@ -112,7 +137,7 @@ module.exports.api = {
112
137
  }
113
138
  date = new Date(gwinfo.time);
114
139
  }
115
- if (! (date && isDate(date)))
140
+ if (!(date && isDate(date) && !isNaN(date.getTime())))
116
141
  date = new Date()
117
142
 
118
143
  await onUplinkDevicePortBufferDateLatLng(client, id, port, data, date, lat, lng, maxSize, extractLoraInfo(obj));
@@ -132,7 +157,7 @@ module.exports.api = {
132
157
  devEui: devEUI,
133
158
  confirmed,
134
159
  fPort: port,
135
- payload: data.toString('base64'),
160
+ data: data.toString('base64'),
136
161
  };
137
162
  client.publish(topic, JSON.stringify(obj));
138
163
  args.v && console.log("Publish downlink on port " + port + " data: " + data.toString("hex"));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lifefinder/vsm-mqtt-client-open-source",
3
- "version": "0.0.59",
3
+ "version": "0.0.61",
4
4
  "description": "MQTT client for vsm sensors",
5
5
  "main": "vsm-mqtt-client.js",
6
6
  "author": "Lars Mats",