-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
132 lines (119 loc) Β· 3.7 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const _ = require('lodash');
const debug = require('debug')('gate-addon-http');
const EventEmitter = require('events');
const mqtt = require('mqtt');
const async = require('async');
const findDevices = require('./findDevices');
class GateAddOnHTTP extends EventEmitter {
constructor(id, type, allDevices, options = {}) {
/**
* options : {
* ip_address
* }
*/
super();
this.id = id;
this.data = {};
const http = type.protocols[0];
this.knownDevices = allDevices[http] || [];
this.deviceType = type;
this.client = mqtt.connect('mqtt://eclipse-mosquitto', {
username: process.env.MQTT_USERNAME,
password: process.env.MQTT_PASSWORD,
});
this.options = options;
}
setKnownDevices(knownDevices) {
this.knownDevices = knownDevices;
}
subscribe(callback) {
return this.client.subscribe('http2mqtt/bridge/log', (err) => {
if (err) {
return callback(err);
}
return callback();
});
}
init() {
this.on('internalNewDeviceTimeout', () => {
const payload = {
status: {
eventType: 'not_paired',
},
deviceId: this.id,
};
this.emit('timeoutDiscovering', payload);
});
this.on('internalNewDevice', (newDevice) => {
this.client.removeAllListeners('message');
this.client.unsubscribe('http2mqtt/bridge/log');
this.emit('newDevice', newDevice);
this.start(newDevice);
});
this.client.on('connect', () => {
debug('Connected');
async.waterfall([
this.subscribe.bind(this),
findDevices.bind(this),
]);
});
}
start(device) {
const { ieeeAddr } = device;
const topic = `http2mqtt/${ieeeAddr}`;
if (this.deviceType.type === 'sensor') {
this.client.on('message', (topic, message) => {
const parsed = JSON.parse(message.toString());
this.emit('data', parsed);
});
this.client.subscribe(topic);
}
}
stop() { }
control(message, action) {
const httpDevice = this.knownDevices.find((httpDeviceFilter) => httpDeviceFilter.id === this.id);
const friendlyName = _.get(httpDevice, 'ieeeAddr');
const topic = `http2mqtt/${friendlyName}/${action}`;
if (action === 'get') {
const responseTopic = `http2mqtt/${friendlyName}`;
this.client.once('message', (topic, responseMessage) => {
const parsed = JSON.parse(responseMessage.toString());
this.client.unsubscribe(responseTopic, (err) => { });
const response = {
payload: parsed,
requestId: message.requestId,
deviceId: message.deviceId,
projectId: process.env.PROJECT_ID,
};
this.emit('status', response);
});
this.client.subscribe(responseTopic, (err) => { });
}
this.client.publish(topic, JSON.stringify(message));
}
remove() {
const device = this.knownDevices.find((modbusDevice) => modbusDevice.id === this.id);
const friendlyName = _.get(device, 'ieeeAddr');
this.subscribe((err) => {
this.client.on('message', (topic, message) => {
if (topic !== 'http2mqtt/bridge/log') {
return;
}
const logMessage = JSON.parse(message.toString());
const messageType = logMessage.type;
if (messageType !== 'device_force_removed') {
return;
}
const friendlyNameRemoved = logMessage.message;
if (friendlyNameRemoved === friendlyName) {
this.emit('deviceRemoved', this.id);
this.removeAllListeners();
this.client.end();
}
});
});
const topic = 'http2mqtt/bridge/config/force_remove';
this.client.publish(topic, friendlyName);
}
}
module.exports = GateAddOnHTTP;