apps-mqtt 1.0.0

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/package.json ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "name": "apps-mqtt",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "src/index.js",
6
+ "scripts": {
7
+ "serve": "nodemon src/index.js"
8
+ },
9
+ "dependencies": {
10
+ "log4js": "^6.1.0"
11
+ },
12
+ "license": "ISC"
13
+ }
@@ -0,0 +1,23 @@
1
+ const log4js = require('log4js');
2
+
3
+ const logger = log4js.getLogger('emitter');
4
+ let mqtt_client = null;
5
+
6
+ class Emitters {
7
+
8
+ static set_client(client) {
9
+ mqtt_client = client;
10
+ }
11
+
12
+ static emit(channel, data) {
13
+ if (!channel) {
14
+ throw new Error('Channel is not specified');
15
+ }
16
+ logger.debug(`emit: ${channel} message: ${JSON.stringify(data)}`);
17
+ mqtt_client.publish(channel, data);
18
+ }
19
+ }
20
+
21
+ module.exports = {
22
+ Emitters
23
+ };
@@ -0,0 +1,9 @@
1
+ const { Listeners } = require('./listeners');
2
+ const { Subscriber } = require('./subcribers');
3
+ const { Emitters } = require('./emitters');
4
+
5
+ module.exports = {
6
+ Listeners,
7
+ Subscriber,
8
+ Emitters,
9
+ };
@@ -0,0 +1,32 @@
1
+ const log4js = require('log4js');
2
+ const { TOPICS } = require('../constants');
3
+
4
+ const logger = log4js.getLogger('listeners');
5
+
6
+ class Listeners {
7
+ constructor(client) {
8
+ this.client = client;
9
+ }
10
+
11
+ start() {
12
+ this.client.subscribe(TOPICS.HEARTBEAT, (err) => {
13
+ if (err) {
14
+ logger.error(err);
15
+ }
16
+ });
17
+ this.client.subscribe(TOPICS.NEW_DEVICE, (err) => {
18
+ if (err) {
19
+ logger.error(err);
20
+ }
21
+ });
22
+ this.client.subscribe('inTopic', (err) => {
23
+ if (err) {
24
+ logger.error(err);
25
+ }
26
+ });
27
+ }
28
+ }
29
+
30
+ module.exports = {
31
+ Listeners
32
+ };
@@ -0,0 +1,39 @@
1
+ const log4js = require('log4js');
2
+ const { TOPICS } = require('../constants');
3
+ const { Emitters } = require('./emitters');
4
+
5
+ const logger = log4js.getLogger('subscriber');
6
+
7
+ class Subscriber {
8
+ constructor(client, devices) {
9
+ this.client = client;
10
+ this.devices = devices;
11
+ }
12
+
13
+ parse(topic, message) {
14
+ const data = JSON.parse(message);
15
+ switch (topic) {
16
+ case TOPICS.NEW_DEVICE:
17
+ this.devices.push(data);
18
+
19
+ Emitters.emit(
20
+ TOPICS.START.replace('/+/', `/${data.d}/`), "{ r: 'wargos' }"
21
+ );
22
+ break;
23
+ case TOPICS.HEARTBEAT:
24
+ logger.info('heartbeat:', data);
25
+ break;
26
+
27
+ case 'inTopic':
28
+ logger.info('inTopic');
29
+ break;
30
+
31
+ default:
32
+ throw new Error(`topic does not exist: ${topic}`, data);
33
+ }
34
+ }
35
+ }
36
+
37
+ module.exports = {
38
+ Subscriber
39
+ };
@@ -0,0 +1,10 @@
1
+ const TOPICS = {
2
+ NEW_DEVICE: '/v1/device',
3
+ HEARTBEAT: '/v1/heartbeat',
4
+ START: '/v1/+/start',
5
+ STOP: '/v1/+/stop',
6
+ };
7
+
8
+ module.exports = {
9
+ TOPICS
10
+ }
package/src/index.js ADDED
@@ -0,0 +1,5 @@
1
+ const { Mqtt } = require('./mqtt');
2
+
3
+ module.exports = {
4
+ Mqtt,
5
+ };
package/src/mqtt.js ADDED
@@ -0,0 +1,39 @@
1
+ var mqtt = require('mqtt');
2
+ const log4js = require('log4js');
3
+
4
+ class Mqtt {
5
+ constructor(config) {
6
+ this.logger = log4js.getLogger('mqtt');
7
+ this.config = config;
8
+ this.client = mqtt.connect(config.path, {
9
+ username: config.username,
10
+ password: config.password,
11
+ });
12
+ this.devices = [];
13
+ this.listener = null;
14
+ this.subscriber = null;
15
+ }
16
+
17
+ start() {
18
+ this.client.on('connect', () => {
19
+ this.logger.info(`mqtt successful connection: ${this.config.path}`);
20
+ });
21
+
22
+ this.client.on('error', (err) => {
23
+ console.log(err);
24
+ this.logger.error(err);
25
+ });
26
+
27
+ this.client.on('message', (topic, message) => {
28
+ const data = message.toString();
29
+ this.logger.debug(`on: ${topic} message: ${data}`);
30
+ try {
31
+ subscriber.parse(topic, data);
32
+ } catch (err) {
33
+ this.logger.error(err);
34
+ }
35
+ });
36
+ }
37
+ }
38
+
39
+ module.exports = { Mqtt };