forked from bitpool/edge-modbus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bp-modbus-io-config.js
executable file
·92 lines (73 loc) · 3.03 KB
/
bp-modbus-io-config.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
/**
Copyright (c) 2016,2017,2018,2019,2020,2021 Klaus Landsdorf (https://bianco-royal.space/)
All rights reserved.
node-red-contrib-modbus
node-red-contrib-modbusio
@author <a href="mailto:[email protected]">Klaus Landsdorf</a> (Bianco Royal)
*/
module.exports = function (RED) {
'use strict'
// SOURCE-MAP-REQUIRED
const coreIO = require('./core/modbus-io-core')
function ModbusIOConfigNode (config) {
// const fs = require('fs-extra')
const UNLIMITED_LISTENERS = 0
RED.nodes.createNode(this, config)
this.name = config.name
this.path = config.path
this.format = config.format
this.addressOffset = config.addressOffset
const node = this
node.setMaxListeners(UNLIMITED_LISTENERS)
node.lastUpdatedAt = null
const lineReader = new coreIO.LineByLineReader(node.path)
coreIO.internalDebug('Read IO File ' + node.path)
node.configData = []
lineReader.on('error', function (err) {
coreIO.internalDebug(err.message)
})
lineReader.on('line', function (line) {
if (line) {
node.configData.push(JSON.parse(line))
}
})
lineReader.on('end', function () {
node.lastUpdatedAt = Date.now()
coreIO.internalDebug('Read IO Done From File ' + node.path)
node.warn({ payload: coreIO.allValueNamesFromIOFile(node), name: 'Modbus Value Names From IO File', path: node.path })
node.emit('updatedConfig', node.configData)
})
coreIO.internalDebug('Loading IO File Started For ' + node.path)
// node.watcher = fs.watchFile(node.path, (curr, prev) => {
// coreIO.internalDebug(`the current mtime is: ${curr.mtime}`)
// coreIO.internalDebug(`the previous mtime was: ${prev.mtime}`)
// if (curr.mtime !== prev.mtime) {
// coreIO.internalDebug('Reload IO File ' + node.path)
// node.configData = []
// delete node.lastUpdatedAt
// const lineReader = new coreIO.LineByLineReader(node.path)
// lineReader.on('error', function (err) {
// coreIO.internalDebug(err.message)
// })
// lineReader.on('line', function (line) {
// if (line) {
// node.configData.push(JSON.parse(line))
// }
// })
// lineReader.on('end', function () {
// node.lastUpdatedAt = Date.now()
// coreIO.internalDebug('Reload IO Done From File ' + node.path)
// node.warn({ payload: coreIO.allValueNamesFromIOFile(node), name: 'Modbus Value Names From IO File', path: node.path })
// node.emit('updatedConfig', node.configData)
// })
// coreIO.internalDebug('Reloading IO File Started For ' + node.path)
// }
// })
// node.on('close', function (done) {
// fs.unwatchFile(node.path)
// node.watcher.close()
// done()
// })
}
RED.nodes.registerType('bp-modbus-io-config', ModbusIOConfigNode)
}