-
Notifications
You must be signed in to change notification settings - Fork 3
/
eda-modbus-bridge.mjs
204 lines (182 loc) · 7.62 KB
/
eda-modbus-bridge.mjs
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import express from 'express'
import expressWinston from 'express-winston'
import mqtt from 'mqtt'
import yargs from 'yargs'
import ModbusRTU from 'modbus-serial'
import { configureRoutes } from './app/http.mjs'
import {
publishValues,
subscribeToChanges,
handleMessage,
publishDeviceInformation,
validateBrokerUrl,
} from './app/mqtt.mjs'
import { configureMqttDiscovery } from './app/homeassistant.mjs'
import { createLogger, setLogLevel } from './app/logger.mjs'
import { MODBUS_DEVICE_TYPE, parseDevice, validateDevice } from './app/modbus.mjs'
const MQTT_INITIAL_RECONNECT_RETRY_INTERVAL_SECONDS = 5
const argv = yargs(process.argv.slice(2))
.usage('node $0 [options]')
.options({
'device': {
description:
'The Modbus device to use, e.g. /dev/ttyUSB0 for Modbus RTU or tcp://192.168.1.40:502 for Modbus TCP',
demand: true,
alias: 'd',
},
'modbusSlave': {
description: 'The Modbus slave address',
default: 1,
alias: 's',
},
'http': {
description: 'Whether to enable the HTTP server or not',
type: 'boolean',
default: true,
},
'httpListenAddress': {
description: 'The address to listen (HTTP)',
default: '0.0.0.0',
alias: 'a',
},
'httpPort': {
description: 'The port to listen on (HTTP)',
default: 8080,
alias: 'p',
},
'mqttBrokerUrl': {
description: 'The URL to the MQTT broker, e.g. mqtt://localhost:1883. Omit to disable MQTT support.',
default: undefined,
alias: 'm',
},
'mqttUsername': {
description: 'The username to use when connecting to the MQTT broker. Omit to disable authentication.',
default: undefined,
},
'mqttPassword': {
description:
'The password to use when connecting to the MQTT broker. Required when mqttUsername is defined. Omit to disable authentication.',
default: undefined,
},
'mqttPublishInterval': {
description: 'How often messages should be published over MQTT (in seconds)',
default: 10,
alias: 'i',
},
'mqttDiscovery': {
description:
'Whether to enable Home Assistant MQTT discovery support. Only effective when mqttBrokerUrl is defined.',
type: 'boolean',
default: true,
},
'debug': {
description: 'Enable debug logging',
type: 'boolean',
default: false,
alias: 'v',
},
}).argv
;(async () => {
// Create logger(s)
const logger = createLogger('main')
if (argv.debug) {
setLogLevel(logger, 'debug')
}
const httpLogger = createLogger('http')
// Create Modbus client. Abort if a malformed device is specified.
if (!validateDevice(argv.device)) {
logger.error(`Malformed Modbus device ${argv.device} specified, exiting`)
process.exit(1)
}
logger.info(`Opening Modbus connection to ${argv.device}, slave ID ${argv.modbusSlave}`)
const modbusDevice = parseDevice(argv.device)
const modbusClient = new ModbusRTU()
modbusClient.setID(argv.modbusSlave)
modbusClient.setTimeout(5000) // 5 seconds
// Use buffered RTU or TCP depending on device type
if (modbusDevice.type === MODBUS_DEVICE_TYPE.RTU) {
await modbusClient.connectRTUBuffered(modbusDevice.path, {
baudRate: 19200,
dataBits: 8,
parity: 'none',
stopBits: 1,
})
} else if (modbusDevice.type === MODBUS_DEVICE_TYPE.TCP) {
await modbusClient.connectTCP(modbusDevice.hostname, {
port: modbusDevice.port,
})
}
// Optionally create HTTP server
if (argv.http) {
// Define middleware
const httpServer = express()
httpServer.use(expressWinston.logger({ winstonInstance: httpLogger }))
httpServer.use(express.json())
// Define routes
configureRoutes(httpServer, modbusClient)
httpServer.listen(argv.httpPort, argv.httpListenAddress, () => {
httpLogger.info(`Listening on http://${argv.httpListenAddress}:${argv.httpPort}`)
})
}
// Optionally create MQTT client
if (argv.mqttBrokerUrl !== undefined) {
if (!validateBrokerUrl(argv.mqttBrokerUrl)) {
logger.error(`Malformed MQTT broker URL: ${argv.mqttBrokerUrl}. Should be e.g. mqtt://localhost:1883.`)
} else {
logger.info(`Connecting to MQTT broker at ${argv.mqttBrokerUrl}`)
try {
// Handle authentication
let clientOptions = {}
if (argv.mqttUsername && argv.mqttPassword) {
logger.info('Using MQTT broker authentication')
clientOptions = {
'username': argv.mqttUsername,
'password': argv.mqttPassword,
}
}
// The MQTT client handles reconnections automatically, but only after it has connected successfully once.
// Retry manually until we get an initial connection.
let mqttClient
let connectedOnce = false
const retryIntervalMs = MQTT_INITIAL_RECONNECT_RETRY_INTERVAL_SECONDS * 1000
do {
try {
mqttClient = await mqtt.connectAsync(argv.mqttBrokerUrl, clientOptions)
connectedOnce = true
logger.info(`Successfully connected to MQTT broker at ${argv.mqttBrokerUrl}`)
} catch (e) {
logger.error(
`Failed to connect to MQTT broker: ${e.message}. Retrying in ${retryIntervalMs} milliseconds`
)
await new Promise((resolve) => setTimeout(resolve, retryIntervalMs))
}
} while (!connectedOnce)
// Publish device information once only (since it doesn't change)
await publishDeviceInformation(modbusClient, mqttClient)
// Publish readings/settings/modes/alarms once immediately, then regularly according to the configured
// interval.
await publishValues(modbusClient, mqttClient)
setInterval(async () => {
await publishValues(modbusClient, mqttClient)
}, argv.mqttPublishInterval * 1000)
logger.info(`MQTT scheduler started, will publish readings every ${argv.mqttPublishInterval} seconds`)
// Subscribe to changes and register a handler
await subscribeToChanges(mqttClient)
mqttClient.on('message', async (topicName, payload) => {
await handleMessage(modbusClient, mqttClient, topicName, payload)
})
// Optionally configure Home Assistant MQTT discovery
if (argv.mqttDiscovery) {
await configureMqttDiscovery(modbusClient, mqttClient)
logger.info('Finished configuration Home Assistant MQTT discovery')
}
// Log reconnection attempts
mqttClient.on('reconnect', () => {
logger.info(`Attempting to reconnect to ${argv.mqttBrokerUrl}`)
})
} catch (e) {
logger.error(`An exception occurred: ${e.name}: ${e.message}`, e.stack)
}
}
}
})()