v2c-any 0.1.2 → 0.1.4
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/dist/index.js
CHANGED
|
@@ -28,24 +28,28 @@ async function main() {
|
|
|
28
28
|
break;
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
|
-
await service.start();
|
|
32
31
|
const shutdown = async () => {
|
|
33
32
|
try {
|
|
34
33
|
logger.info('Shutting down...');
|
|
35
34
|
if (service)
|
|
36
35
|
await service.stop();
|
|
36
|
+
logger.info('Shutdown complete');
|
|
37
|
+
}
|
|
38
|
+
catch (err) {
|
|
39
|
+
logger.error(err, 'Error during shutdown');
|
|
37
40
|
}
|
|
38
41
|
finally {
|
|
39
42
|
process.exit(0);
|
|
40
43
|
}
|
|
41
44
|
};
|
|
42
|
-
|
|
43
|
-
process.on('SIGINT', () => {
|
|
45
|
+
process.once('SIGINT', () => {
|
|
44
46
|
shutdown().catch((err) => logger.error(err, 'Error during shutdown'));
|
|
45
47
|
});
|
|
46
|
-
process.
|
|
48
|
+
process.once('SIGTERM', () => {
|
|
47
49
|
shutdown().catch((err) => logger.error(err, 'Error during shutdown'));
|
|
48
50
|
});
|
|
51
|
+
await service.start();
|
|
52
|
+
logger.info('Application started successfully');
|
|
49
53
|
}
|
|
50
54
|
main().catch((err) => {
|
|
51
55
|
logger.error(err, 'Fatal error occurred');
|
|
@@ -26,6 +26,7 @@ export class MqttBridgeService {
|
|
|
26
26
|
* @returns A promise that resolves when the subscription is active
|
|
27
27
|
*/
|
|
28
28
|
async start() {
|
|
29
|
+
logger.info('Starting MQTT bridge service');
|
|
29
30
|
this.client = await createMqttClient(this.properties.url);
|
|
30
31
|
this.client.on('message', (topic, message) => {
|
|
31
32
|
if (topic === this.properties.topic) {
|
|
@@ -40,16 +41,20 @@ export class MqttBridgeService {
|
|
|
40
41
|
});
|
|
41
42
|
}
|
|
42
43
|
});
|
|
44
|
+
logger.info({ topic: this.properties.topic }, 'Subscribing to MQTT topic');
|
|
43
45
|
await this.client.subscribeAsync(this.properties.topic);
|
|
46
|
+
logger.info('MQTT bridge service started');
|
|
44
47
|
}
|
|
45
48
|
/**
|
|
46
49
|
* Stops the bridge: disconnects the MQTT client and clears resources.
|
|
47
50
|
* @returns A promise that resolves when the client has disconnected
|
|
48
51
|
*/
|
|
49
52
|
async stop() {
|
|
53
|
+
logger.info('Stopping MQTT bridge service');
|
|
50
54
|
if (this.client) {
|
|
51
55
|
await this.client.endAsync();
|
|
52
56
|
this.client = null;
|
|
53
57
|
}
|
|
58
|
+
logger.info('MQTT bridge service stopped');
|
|
54
59
|
}
|
|
55
60
|
}
|
|
@@ -60,6 +60,7 @@ export class MqttService {
|
|
|
60
60
|
throw new Error('MQTT client already started');
|
|
61
61
|
}
|
|
62
62
|
this.client = await createMqttClient(this.properties.url);
|
|
63
|
+
logger.info('MQTT client started');
|
|
63
64
|
}
|
|
64
65
|
/**
|
|
65
66
|
* Stops the MQTT client connection.
|
|
@@ -67,6 +68,7 @@ export class MqttService {
|
|
|
67
68
|
* @throws {Error} If the client is not started
|
|
68
69
|
*/
|
|
69
70
|
async stop() {
|
|
71
|
+
logger.info('Stopping MQTT mode');
|
|
70
72
|
const client = this.client;
|
|
71
73
|
this.client = null;
|
|
72
74
|
if (!client) {
|
package/dist/utils/mqtt.js
CHANGED
|
@@ -10,12 +10,6 @@ import { logger } from './logger.js';
|
|
|
10
10
|
*/
|
|
11
11
|
export async function createMqttClient(url) {
|
|
12
12
|
const client = await mqtt.connectAsync(url);
|
|
13
|
-
client.on('connect', () => {
|
|
14
|
-
logger.info({ url }, 'Connected to MQTT broker');
|
|
15
|
-
});
|
|
16
|
-
client.on('error', (err) => {
|
|
17
|
-
logger.error(err, 'MQTT client error');
|
|
18
|
-
});
|
|
19
13
|
client.on('reconnect', () => logger.info('Reconnecting...'));
|
|
20
14
|
return client;
|
|
21
15
|
}
|