apps-mqtt 1.0.2 → 2.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 +10 -3
- package/src/broker/emitters.ts +20 -0
- package/src/broker/index.ts +9 -0
- package/src/broker/listeners.ts +32 -0
- package/src/broker/subcribers.ts +40 -0
- package/src/constants.ts +6 -0
- package/src/index.ts +5 -0
- package/src/mqtt.ts +53 -0
- package/tsconfig.json +20 -0
- package/src/emitter.js +0 -42
- package/src/index.js +0 -9
- package/src/listeners.js +0 -23
- package/src/mqtt.js +0 -45
- package/src/subcribers.js +0 -30
package/package.json
CHANGED
|
@@ -1,14 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "apps-mqtt",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "",
|
|
5
|
-
"main": "src/index.
|
|
5
|
+
"main": "src/index.ts",
|
|
6
|
+
"types": "src/index.ts",
|
|
6
7
|
"scripts": {
|
|
7
|
-
"serve": "nodemon src/index.
|
|
8
|
+
"serve": "nodemon --exec ts-node src/index.ts",
|
|
9
|
+
"typecheck": "tsc --noEmit"
|
|
8
10
|
},
|
|
9
11
|
"dependencies": {
|
|
10
12
|
"log4js": "^6.1.0",
|
|
11
13
|
"mqtt": "^4.3.7"
|
|
12
14
|
},
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"@types/node": "^22.10.0",
|
|
17
|
+
"ts-node": "^10.9.2",
|
|
18
|
+
"typescript": "^5.9.3"
|
|
19
|
+
},
|
|
13
20
|
"license": "ISC"
|
|
14
21
|
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import * as log4js from 'log4js';
|
|
2
|
+
import type { MqttClient } from 'mqtt';
|
|
3
|
+
|
|
4
|
+
const logger = log4js.getLogger('emitter');
|
|
5
|
+
let mqtt_client: MqttClient | null = null;
|
|
6
|
+
|
|
7
|
+
export class Emitters {
|
|
8
|
+
|
|
9
|
+
static set_client(client: MqttClient): void {
|
|
10
|
+
mqtt_client = client;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
static emit(channel: string, data: any): void {
|
|
14
|
+
if (!channel) {
|
|
15
|
+
throw new Error('Channel is not specified');
|
|
16
|
+
}
|
|
17
|
+
logger.debug(`emit: ${channel} message: ${JSON.stringify(data)}`);
|
|
18
|
+
mqtt_client!.publish(channel, data);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import * as log4js from 'log4js';
|
|
2
|
+
import type { MqttClient } from 'mqtt';
|
|
3
|
+
import { TOPICS } from '../constants';
|
|
4
|
+
|
|
5
|
+
const logger = log4js.getLogger('listeners');
|
|
6
|
+
|
|
7
|
+
export class Listeners {
|
|
8
|
+
|
|
9
|
+
client: MqttClient;
|
|
10
|
+
|
|
11
|
+
constructor(client: MqttClient) {
|
|
12
|
+
this.client = client;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
start(): void {
|
|
16
|
+
this.client.subscribe(TOPICS.HEARTBEAT, (err) => {
|
|
17
|
+
if (err) {
|
|
18
|
+
logger.error(err);
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
this.client.subscribe(TOPICS.NEW_DEVICE, (err) => {
|
|
22
|
+
if (err) {
|
|
23
|
+
logger.error(err);
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
this.client.subscribe('inTopic', (err) => {
|
|
27
|
+
if (err) {
|
|
28
|
+
logger.error(err);
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import * as log4js from 'log4js';
|
|
2
|
+
import type { MqttClient } from 'mqtt';
|
|
3
|
+
import { TOPICS } from '../constants';
|
|
4
|
+
import { Emitters } from './emitters';
|
|
5
|
+
|
|
6
|
+
const logger = log4js.getLogger('subscriber');
|
|
7
|
+
|
|
8
|
+
export class Subscriber {
|
|
9
|
+
|
|
10
|
+
client: MqttClient;
|
|
11
|
+
devices: any[];
|
|
12
|
+
|
|
13
|
+
constructor(client: MqttClient, devices: any[]) {
|
|
14
|
+
this.client = client;
|
|
15
|
+
this.devices = devices;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
parse(topic: string, message: string): void {
|
|
19
|
+
const data = JSON.parse(message);
|
|
20
|
+
switch (topic) {
|
|
21
|
+
case TOPICS.NEW_DEVICE:
|
|
22
|
+
this.devices.push(data);
|
|
23
|
+
|
|
24
|
+
Emitters.emit(
|
|
25
|
+
TOPICS.START.replace('/+/', `/${data.d}/`), "{ r: 'wargos' }"
|
|
26
|
+
);
|
|
27
|
+
break;
|
|
28
|
+
case TOPICS.HEARTBEAT:
|
|
29
|
+
logger.info('heartbeat:', data);
|
|
30
|
+
break;
|
|
31
|
+
|
|
32
|
+
case 'inTopic':
|
|
33
|
+
logger.info('inTopic');
|
|
34
|
+
break;
|
|
35
|
+
|
|
36
|
+
default:
|
|
37
|
+
throw new Error(`topic does not exist: ${topic}`);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
package/src/constants.ts
ADDED
package/src/index.ts
ADDED
package/src/mqtt.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import * as mqtt from 'mqtt';
|
|
2
|
+
import type { MqttClient } from 'mqtt';
|
|
3
|
+
import * as log4js from 'log4js';
|
|
4
|
+
import { Subscriber } from './broker/subcribers';
|
|
5
|
+
|
|
6
|
+
export interface MqttConfig {
|
|
7
|
+
path: string;
|
|
8
|
+
username?: string;
|
|
9
|
+
password?: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export class Mqtt {
|
|
13
|
+
|
|
14
|
+
logger: log4js.Logger;
|
|
15
|
+
config: MqttConfig;
|
|
16
|
+
client: MqttClient;
|
|
17
|
+
devices: any[];
|
|
18
|
+
listener: any;
|
|
19
|
+
subscriber: Subscriber | null;
|
|
20
|
+
|
|
21
|
+
constructor(config: MqttConfig) {
|
|
22
|
+
this.logger = log4js.getLogger('mqtt');
|
|
23
|
+
this.config = config;
|
|
24
|
+
this.client = mqtt.connect(config.path, {
|
|
25
|
+
username: config.username,
|
|
26
|
+
password: config.password,
|
|
27
|
+
});
|
|
28
|
+
this.devices = [];
|
|
29
|
+
this.listener = null;
|
|
30
|
+
this.subscriber = null;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
start(): void {
|
|
34
|
+
this.client.on('connect', () => {
|
|
35
|
+
this.logger.info(`mqtt successful connection: ${this.config.path}`);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
this.client.on('error', (err) => {
|
|
39
|
+
console.log(err);
|
|
40
|
+
this.logger.error(err);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
this.client.on('message', (topic, message) => {
|
|
44
|
+
const data = message.toString();
|
|
45
|
+
this.logger.debug(`on: ${topic} message: ${data}`);
|
|
46
|
+
try {
|
|
47
|
+
this.subscriber!.parse(topic, data);
|
|
48
|
+
} catch (err) {
|
|
49
|
+
this.logger.error(err);
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2019",
|
|
4
|
+
"lib": ["ES2019"],
|
|
5
|
+
"module": "commonjs",
|
|
6
|
+
"moduleResolution": "node",
|
|
7
|
+
"esModuleInterop": true,
|
|
8
|
+
"forceConsistentCasingInFileNames": true,
|
|
9
|
+
"skipLibCheck": true,
|
|
10
|
+
"resolveJsonModule": true,
|
|
11
|
+
"strict": true,
|
|
12
|
+
"useUnknownInCatchVariables": false,
|
|
13
|
+
"noEmit": true,
|
|
14
|
+
"rootDir": "src"
|
|
15
|
+
},
|
|
16
|
+
"include": ["src/**/*.ts"],
|
|
17
|
+
"ts-node": {
|
|
18
|
+
"files": true
|
|
19
|
+
}
|
|
20
|
+
}
|
package/src/emitter.js
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
const log4js = require('log4js');
|
|
2
|
-
|
|
3
|
-
const logger = log4js.getLogger('emitter');
|
|
4
|
-
let mqtt_client = null;
|
|
5
|
-
|
|
6
|
-
class Emitter {
|
|
7
|
-
static set_client(client) {
|
|
8
|
-
mqtt_client = client;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
static emit(channel, data) {
|
|
12
|
-
if (!channel) {
|
|
13
|
-
throw new Error('channel is undefined');
|
|
14
|
-
}
|
|
15
|
-
if (!data) {
|
|
16
|
-
throw new Error('data is undefined');
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
logger.debug(`emit: ${channel} message: ${JSON.stringify(data)}`);
|
|
20
|
-
mqtt_client.publish(channel, JSON.stringify(data));
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
// setInterval(() => {
|
|
25
|
-
// Emitter.emit('/v1/emails', {})
|
|
26
|
-
// }, 2000);
|
|
27
|
-
|
|
28
|
-
// setTimeout(() => {
|
|
29
|
-
// Emitter.emit('/v1/emails', {
|
|
30
|
-
// from: {
|
|
31
|
-
// name: 'visitor',
|
|
32
|
-
// email: 'visitor@wargos-solutions.com',
|
|
33
|
-
// },
|
|
34
|
-
// to: {
|
|
35
|
-
// name: 'visitor',
|
|
36
|
-
// email: 'rolf.mtwo@outlook.com',
|
|
37
|
-
// },
|
|
38
|
-
// html: 'test generatir'
|
|
39
|
-
// })
|
|
40
|
-
// }, 5000);
|
|
41
|
-
|
|
42
|
-
module.exports = { Emitter };
|
package/src/index.js
DELETED
package/src/listeners.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
const log4js = require('log4js');
|
|
2
|
-
|
|
3
|
-
class Listeners {
|
|
4
|
-
start(db, client) {
|
|
5
|
-
this.db = db;
|
|
6
|
-
this.client = client;
|
|
7
|
-
this.logger = log4js.getLogger('listeners');
|
|
8
|
-
this.handlers = {};
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
subscribe(topic, callback) {
|
|
12
|
-
this.client.subscribe(topic, (err) => {
|
|
13
|
-
if (err) {
|
|
14
|
-
this.logger.error(err);
|
|
15
|
-
return;
|
|
16
|
-
}
|
|
17
|
-
this.logger.debug(`new topic subscription: ${topic}`);
|
|
18
|
-
callback();
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
module.exports = { Listeners };
|
package/src/mqtt.js
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
const mqtt = require('mqtt');
|
|
2
|
-
const log4js = require('log4js');
|
|
3
|
-
const { Emitter } = require('./emitter');
|
|
4
|
-
const { Listeners } = require('./listeners');
|
|
5
|
-
const { Subscribers } = require('./subcribers');
|
|
6
|
-
|
|
7
|
-
const listeners = new Listeners();
|
|
8
|
-
const subscribers = new Subscribers();
|
|
9
|
-
|
|
10
|
-
class Mqtt {
|
|
11
|
-
constructor(config, db) {
|
|
12
|
-
this.logger = log4js.getLogger('mqtt');
|
|
13
|
-
this.db = db;
|
|
14
|
-
this.config = config;
|
|
15
|
-
this.client = mqtt.connect(config.path, {
|
|
16
|
-
username: config.username,
|
|
17
|
-
password: config.password,
|
|
18
|
-
});
|
|
19
|
-
listeners.start(this.db, this.client);
|
|
20
|
-
subscribers.start(this.db, this.client);
|
|
21
|
-
Emitter.set_client(this.client);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
start() {
|
|
25
|
-
this.client.on('connect', () => {
|
|
26
|
-
this.logger.info(`mqtt successful connection: ${this.config.path}`);
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
this.client.on('error', (err) => {
|
|
30
|
-
this.logger.error(err);
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
this.client.on('message', (topic, message) => {
|
|
34
|
-
const data = message.toString();
|
|
35
|
-
this.logger.debug(`on: ${topic} message: ${data}`);
|
|
36
|
-
try {
|
|
37
|
-
subscribers.parse(topic, data);
|
|
38
|
-
} catch (err) {
|
|
39
|
-
this.logger.error(err);
|
|
40
|
-
}
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
module.exports = { Mqtt, listeners, subscribers };
|
package/src/subcribers.js
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
const log4js = require('log4js');
|
|
2
|
-
|
|
3
|
-
class Subscribers {
|
|
4
|
-
start(db, client) {
|
|
5
|
-
this.db = db;
|
|
6
|
-
this.client = client;
|
|
7
|
-
this.handlers = {};
|
|
8
|
-
this.logger = log4js.getLogger('subscriber');
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
parse(topic, message) {
|
|
12
|
-
const data = JSON.parse(message);
|
|
13
|
-
try {
|
|
14
|
-
const callback = this.handlers[topic];
|
|
15
|
-
if (callback) {
|
|
16
|
-
callback(data);
|
|
17
|
-
} else {
|
|
18
|
-
this.logger.debug(`no handler for this topic: ${topic}`);
|
|
19
|
-
}
|
|
20
|
-
} catch (err) {
|
|
21
|
-
this.logger.error(err);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
listen(topic, callback) {
|
|
26
|
-
this.handlers[topic] = callback;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
module.exports = { Subscribers };
|