@iobroker/modbus 7.0.2
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/LICENSE +21 -0
- package/README.md +40 -0
- package/build/convert.d.ts +2 -0
- package/build/convert.js +102 -0
- package/build/convert.js.map +1 -0
- package/build/index.d.ts +88 -0
- package/build/index.js +1005 -0
- package/build/index.js.map +1 -0
- package/build/lib/Master.d.ts +27 -0
- package/build/lib/Master.js +811 -0
- package/build/lib/Master.js.map +1 -0
- package/build/lib/Put.d.ts +19 -0
- package/build/lib/Put.js +113 -0
- package/build/lib/Put.js.map +1 -0
- package/build/lib/Slave.d.ts +15 -0
- package/build/lib/Slave.js +545 -0
- package/build/lib/Slave.js.map +1 -0
- package/build/lib/common.d.ts +3 -0
- package/build/lib/common.js +371 -0
- package/build/lib/common.js.map +1 -0
- package/build/lib/crc16modbus.d.ts +1 -0
- package/build/lib/crc16modbus.js +33 -0
- package/build/lib/crc16modbus.js.map +1 -0
- package/build/lib/jsmodbus/modbus-client-core.d.ts +78 -0
- package/build/lib/jsmodbus/modbus-client-core.js +528 -0
- package/build/lib/jsmodbus/modbus-client-core.js.map +1 -0
- package/build/lib/jsmodbus/modbus-server-core.d.ts +33 -0
- package/build/lib/jsmodbus/modbus-server-core.js +363 -0
- package/build/lib/jsmodbus/modbus-server-core.js.map +1 -0
- package/build/lib/jsmodbus/transports/errors.d.ts +7 -0
- package/build/lib/jsmodbus/transports/errors.js +40 -0
- package/build/lib/jsmodbus/transports/errors.js.map +1 -0
- package/build/lib/jsmodbus/transports/modbus-client-serial.d.ts +23 -0
- package/build/lib/jsmodbus/transports/modbus-client-serial.js +154 -0
- package/build/lib/jsmodbus/transports/modbus-client-serial.js.map +1 -0
- package/build/lib/jsmodbus/transports/modbus-client-tcp-rtu.d.ts +24 -0
- package/build/lib/jsmodbus/transports/modbus-client-tcp-rtu.js +166 -0
- package/build/lib/jsmodbus/transports/modbus-client-tcp-rtu.js.map +1 -0
- package/build/lib/jsmodbus/transports/modbus-client-tcp-ssl.d.ts +34 -0
- package/build/lib/jsmodbus/transports/modbus-client-tcp-ssl.js +138 -0
- package/build/lib/jsmodbus/transports/modbus-client-tcp-ssl.js.map +1 -0
- package/build/lib/jsmodbus/transports/modbus-client-tcp.d.ts +27 -0
- package/build/lib/jsmodbus/transports/modbus-client-tcp.js +123 -0
- package/build/lib/jsmodbus/transports/modbus-client-tcp.js.map +1 -0
- package/build/lib/jsmodbus/transports/modbus-server-serial.d.ts +29 -0
- package/build/lib/jsmodbus/transports/modbus-server-serial.js +206 -0
- package/build/lib/jsmodbus/transports/modbus-server-serial.js.map +1 -0
- package/build/lib/jsmodbus/transports/modbus-server-tcp.d.ts +25 -0
- package/build/lib/jsmodbus/transports/modbus-server-tcp.js +112 -0
- package/build/lib/jsmodbus/transports/modbus-server-tcp.js.map +1 -0
- package/build/lib/loggingUtils.d.ts +11 -0
- package/build/lib/loggingUtils.js +37 -0
- package/build/lib/loggingUtils.js.map +1 -0
- package/package.json +68 -0
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const modbus_server_core_1 = __importDefault(require("../modbus-server-core"));
|
|
7
|
+
const crc16modbus_1 = __importDefault(require("../../crc16modbus"));
|
|
8
|
+
let SerialPortClass;
|
|
9
|
+
class ModbusServerSerial extends modbus_server_core_1.default {
|
|
10
|
+
buffer = Buffer.alloc(0);
|
|
11
|
+
fifo = [];
|
|
12
|
+
serialPort = null;
|
|
13
|
+
deviceId = 1;
|
|
14
|
+
serial;
|
|
15
|
+
constructor(options) {
|
|
16
|
+
super(options);
|
|
17
|
+
this.serial = options.serial;
|
|
18
|
+
this.deviceId = options.deviceId || 1;
|
|
19
|
+
if (!this.serial.portName) {
|
|
20
|
+
throw new Error('No portname specified for serial server.');
|
|
21
|
+
}
|
|
22
|
+
this.serial.baudRate ||= 9600;
|
|
23
|
+
this.serial.dataBits ||= 8;
|
|
24
|
+
this.serial.stopBits ||= 1;
|
|
25
|
+
this.serial.parity ||= 'none';
|
|
26
|
+
this.log.debug(`Starting serial server on port ${this.serial.portName} with settings`);
|
|
27
|
+
try {
|
|
28
|
+
import('serialport')
|
|
29
|
+
.then(sp => {
|
|
30
|
+
SerialPortClass = sp.SerialPort;
|
|
31
|
+
this.serialPort = new SerialPortClass({
|
|
32
|
+
path: this.serial.portName,
|
|
33
|
+
baudRate: this.serial.baudRate,
|
|
34
|
+
parity: this.serial.parity,
|
|
35
|
+
dataBits: this.serial.dataBits,
|
|
36
|
+
stopBits: this.serial.stopBits,
|
|
37
|
+
});
|
|
38
|
+
this.serialPort.on('open', this.#onSerialOpen);
|
|
39
|
+
this.serialPort.on('data', this.#onSerialData);
|
|
40
|
+
this.serialPort.on('error', this.#onSerialError);
|
|
41
|
+
this.serialPort.on('close', this.#onSerialClose);
|
|
42
|
+
})
|
|
43
|
+
.catch(e => this.log.error(`Error importing serialPort module: ${e}`));
|
|
44
|
+
}
|
|
45
|
+
catch (err) {
|
|
46
|
+
this.log.error(`Failed to create serial port: ${err.message}`);
|
|
47
|
+
this.emit('error', err);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
this.on('newState_ready', this.#flush);
|
|
51
|
+
}
|
|
52
|
+
#onSerialOpen = () => {
|
|
53
|
+
this.log.debug('Serial port opened successfully');
|
|
54
|
+
this.setState('ready');
|
|
55
|
+
this.emit('connection', { port: this.serial.portName });
|
|
56
|
+
};
|
|
57
|
+
#onSerialClose = () => {
|
|
58
|
+
this.log.debug('Serial port closed');
|
|
59
|
+
this.emit('close');
|
|
60
|
+
};
|
|
61
|
+
#onSerialError = (err) => {
|
|
62
|
+
this.log.error(`Serial port error: ${err}`);
|
|
63
|
+
this.emit('error', err);
|
|
64
|
+
};
|
|
65
|
+
#onSerialData = (data) => {
|
|
66
|
+
this.buffer = Buffer.concat([this.buffer, data]);
|
|
67
|
+
// Process complete RTU frames
|
|
68
|
+
while (this.buffer.length >= 4) {
|
|
69
|
+
// Minimum RTU frame: 1 byte address + 1 byte function + 2 bytes CRC
|
|
70
|
+
// Look for a valid RTU frame
|
|
71
|
+
let frameLength = 0;
|
|
72
|
+
// RTU frame structure: [Address][Function Code][Data][CRC16]
|
|
73
|
+
// We need to determine frame length based on function code
|
|
74
|
+
if (this.buffer.length >= 2) {
|
|
75
|
+
const address = this.buffer.readUInt8(0);
|
|
76
|
+
const functionCode = this.buffer.readUInt8(1);
|
|
77
|
+
// Skip frames not for our address (0 = broadcast, our deviceId)
|
|
78
|
+
const deviceId = this.deviceId || 1;
|
|
79
|
+
if (address !== 0 && address !== deviceId) {
|
|
80
|
+
// Remove first byte and continue looking
|
|
81
|
+
this.buffer = this.buffer.slice(1);
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
frameLength = this.getExpectedFrameLength(functionCode, this.buffer);
|
|
85
|
+
if (frameLength > 0 && this.buffer.length >= frameLength) {
|
|
86
|
+
// Check CRC
|
|
87
|
+
const frameData = this.buffer.slice(0, frameLength - 2);
|
|
88
|
+
const receivedCrc = this.buffer.readUInt16LE(frameLength - 2);
|
|
89
|
+
const calculatedCrc = (0, crc16modbus_1.default)(frameData);
|
|
90
|
+
if (receivedCrc === calculatedCrc) {
|
|
91
|
+
// Valid frame found
|
|
92
|
+
const pdu = frameData.slice(1); // Remove address byte to get PDU
|
|
93
|
+
this.fifo.push({
|
|
94
|
+
address,
|
|
95
|
+
pdu,
|
|
96
|
+
originalFrame: frameData,
|
|
97
|
+
});
|
|
98
|
+
this.buffer = this.buffer.slice(frameLength);
|
|
99
|
+
this.#flush();
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
// Invalid CRC, remove first byte and continue
|
|
103
|
+
this.log.debug(`Invalid CRC for RTU frame, expected: ${calculatedCrc.toString(16)}received: ${receivedCrc.toString(16)}`);
|
|
104
|
+
this.buffer = this.buffer.slice(1);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
else if (frameLength === 0) {
|
|
108
|
+
// Unknown function code, remove first byte
|
|
109
|
+
this.log.debug(`Unknown function code: ${functionCode}`);
|
|
110
|
+
this.buffer = this.buffer.slice(1);
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
// Not enough data yet, wait for more
|
|
114
|
+
break;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
// Not enough data for address and function code
|
|
119
|
+
break;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
getExpectedFrameLength = (functionCode, buffer) => {
|
|
124
|
+
// Return expected frame length based on function code
|
|
125
|
+
// Returns 0 if function code is unknown or not enough data
|
|
126
|
+
switch (functionCode) {
|
|
127
|
+
case 0x01: // Read Coils
|
|
128
|
+
case 0x02: // Read Discrete Inputs
|
|
129
|
+
case 0x03: // Read Holding Registers
|
|
130
|
+
case 0x04: // Read Input Registers
|
|
131
|
+
return buffer.length >= 6 ? 8 : 0; // Address + FC + Start + Count + CRC = 8 bytes
|
|
132
|
+
case 0x05: // Write Single Coil
|
|
133
|
+
case 0x06: // Write Single Register
|
|
134
|
+
return buffer.length >= 6 ? 8 : 0; // Address + FC + Address + Value + CRC = 8 bytes
|
|
135
|
+
case 0x0f: // Write Multiple Coils
|
|
136
|
+
case 0x10: // Write Multiple Registers
|
|
137
|
+
if (buffer.length >= 7) {
|
|
138
|
+
const byteCount = buffer.readUInt8(6);
|
|
139
|
+
return 9 + byteCount; // Address + FC + Start + Count + ByteCount + Data + CRC
|
|
140
|
+
}
|
|
141
|
+
return 0;
|
|
142
|
+
default:
|
|
143
|
+
return 0; // Unknown function code
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
#flush = () => {
|
|
147
|
+
if (this.inState('processing')) {
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
if (!this.fifo.length) {
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
this.setState('processing');
|
|
154
|
+
const current = this.fifo.shift();
|
|
155
|
+
this.onData(current.pdu, response => {
|
|
156
|
+
this.log.debug('Sending RTU response');
|
|
157
|
+
// Build RTU response frame: Address + Response PDU + CRC
|
|
158
|
+
const address = current.address;
|
|
159
|
+
const responseFrame = Buffer.concat([Buffer.from([address]), response]);
|
|
160
|
+
// Calculate and append CRC
|
|
161
|
+
const crcValue = (0, crc16modbus_1.default)(responseFrame);
|
|
162
|
+
const responseWithCrc = Buffer.concat([
|
|
163
|
+
responseFrame,
|
|
164
|
+
Buffer.from([crcValue & 0xff, (crcValue >> 8) & 0xff]), // Little endian CRC
|
|
165
|
+
]);
|
|
166
|
+
if (this.serialPort?.isOpen) {
|
|
167
|
+
this.serialPort.write(responseWithCrc, err => {
|
|
168
|
+
if (err) {
|
|
169
|
+
this.log.error(`Error writing to serial port: ${err}`);
|
|
170
|
+
}
|
|
171
|
+
else {
|
|
172
|
+
this.log.debug(`RTU response sent, length: ${responseWithCrc.length}`);
|
|
173
|
+
}
|
|
174
|
+
this.setState('ready');
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
178
|
+
this.log.error('Serial port not open, cannot send response');
|
|
179
|
+
this.setState('ready');
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
};
|
|
183
|
+
close(cb) {
|
|
184
|
+
if (this.serialPort?.isOpen) {
|
|
185
|
+
this.serialPort.close(err => {
|
|
186
|
+
if (err) {
|
|
187
|
+
this.log.error(`Error closing serial port: ${err}`);
|
|
188
|
+
}
|
|
189
|
+
else {
|
|
190
|
+
this.log.debug('Serial port closed');
|
|
191
|
+
}
|
|
192
|
+
this.serialPort = null;
|
|
193
|
+
cb?.(err);
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
cb?.();
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
getClients() {
|
|
201
|
+
// For serial, we return info about the serial port connection
|
|
202
|
+
return this.serialPort?.isOpen ? [this.serial.portName] : [];
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
exports.default = ModbusServerSerial;
|
|
206
|
+
//# sourceMappingURL=modbus-server-serial.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"modbus-server-serial.js","sourceRoot":"","sources":["../../../../src/lib/jsmodbus/transports/modbus-server-serial.ts"],"names":[],"mappings":";;;;;AAAA,+EAAqD;AACrD,oEAA4C;AAG5C,IAAI,eAAoB,CAAC;AAEzB,MAAqB,kBAAmB,SAAQ,4BAAgB;IACpD,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IACzB,IAAI,GAA8D,EAAE,CAAC;IACrE,UAAU,GAAiC,IAAI,CAAC;IACvC,QAAQ,GAAW,CAAC,CAAC;IAE9B,MAAM,CAMZ;IAEF,YAAY,OAgBX;QACG,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;QAC7B,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC,QAAQ,IAAI,CAAC,CAAC;QAEtC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC;YACxB,MAAM,IAAI,KAAK,CAAC,0CAA0C,CAAC,CAAC;QAChE,CAAC;QAED,IAAI,CAAC,MAAM,CAAC,QAAQ,KAAK,IAAI,CAAC;QAC9B,IAAI,CAAC,MAAM,CAAC,QAAQ,KAAK,CAAC,CAAC;QAC3B,IAAI,CAAC,MAAM,CAAC,QAAQ,KAAK,CAAC,CAAC;QAC3B,IAAI,CAAC,MAAM,CAAC,MAAM,KAAK,MAAM,CAAC;QAE9B,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,kCAAkC,IAAI,CAAC,MAAM,CAAC,QAAQ,gBAAgB,CAAC,CAAC;QAEv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,CAAC;CACJ;AAvPD,qCAuPC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import ModbusServerCore from '../modbus-server-core';
|
|
2
|
+
export default class ModbusServerTcp extends ModbusServerCore {
|
|
3
|
+
#private;
|
|
4
|
+
private server;
|
|
5
|
+
private socketCount;
|
|
6
|
+
private fifo;
|
|
7
|
+
private clients;
|
|
8
|
+
private buffer;
|
|
9
|
+
private tcp;
|
|
10
|
+
constructor(options: {
|
|
11
|
+
tcp: {
|
|
12
|
+
port?: number;
|
|
13
|
+
hostname?: string;
|
|
14
|
+
};
|
|
15
|
+
logger: ioBroker.Logger;
|
|
16
|
+
timeout?: number;
|
|
17
|
+
responseDelay?: number;
|
|
18
|
+
coils?: Buffer;
|
|
19
|
+
holding?: Buffer;
|
|
20
|
+
input?: Buffer;
|
|
21
|
+
discrete?: Buffer;
|
|
22
|
+
});
|
|
23
|
+
close(cb?: () => void): void;
|
|
24
|
+
getClients(): string[];
|
|
25
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const modbus_server_core_1 = __importDefault(require("../modbus-server-core"));
|
|
7
|
+
const Put_1 = __importDefault(require("../../Put"));
|
|
8
|
+
const node_net_1 = require("node:net");
|
|
9
|
+
class ModbusServerTcp extends modbus_server_core_1.default {
|
|
10
|
+
server;
|
|
11
|
+
socketCount = 0;
|
|
12
|
+
fifo = [];
|
|
13
|
+
clients = [];
|
|
14
|
+
buffer = Buffer.alloc(0);
|
|
15
|
+
tcp;
|
|
16
|
+
constructor(options) {
|
|
17
|
+
super(options);
|
|
18
|
+
this.tcp = {
|
|
19
|
+
port: options.tcp.port || 502,
|
|
20
|
+
hostname: options.tcp.hostname || '0.0.0.0',
|
|
21
|
+
};
|
|
22
|
+
this.server = (0, node_net_1.createServer)();
|
|
23
|
+
this.server.on('connection', s => {
|
|
24
|
+
this.log.debug(`new connection ${JSON.stringify(s.address())}`);
|
|
25
|
+
this.clients.push(s);
|
|
26
|
+
this.#initiateSocket(s);
|
|
27
|
+
this.emit('connection', s.address());
|
|
28
|
+
});
|
|
29
|
+
this.server.on('disconnect', s => this.emit('close', s.address()));
|
|
30
|
+
this.server.listen(this.tcp.port, this.tcp.hostname, () => {
|
|
31
|
+
this.log.debug(`server is listening on port ${this.tcp.hostname}:${this.tcp.port}`);
|
|
32
|
+
});
|
|
33
|
+
this.on('newState_ready', this.#flush);
|
|
34
|
+
this.setState('ready');
|
|
35
|
+
}
|
|
36
|
+
#onSocketData = (socket) => {
|
|
37
|
+
return (data) => {
|
|
38
|
+
this.buffer = Buffer.concat([this.buffer, data]);
|
|
39
|
+
while (this.buffer.length > 8) {
|
|
40
|
+
// 1. extract mbap
|
|
41
|
+
const len = this.buffer.readUInt16BE(4);
|
|
42
|
+
const request = {
|
|
43
|
+
transId: this.buffer.readUInt16BE(0),
|
|
44
|
+
protocolVer: this.buffer.readUInt16BE(2),
|
|
45
|
+
unitId: this.buffer.readUInt8(6),
|
|
46
|
+
};
|
|
47
|
+
// 2. extract pdu
|
|
48
|
+
if (this.buffer.length < 7 + len - 1) {
|
|
49
|
+
break; // wait for next bytes
|
|
50
|
+
}
|
|
51
|
+
const pdu = this.buffer.slice(7, 7 + len - 1);
|
|
52
|
+
// emit data event and let the
|
|
53
|
+
// listener handle the pdu
|
|
54
|
+
this.fifo.push({ request, pdu, socket });
|
|
55
|
+
this.#flush();
|
|
56
|
+
this.buffer = this.buffer.slice(pdu.length + 7, this.buffer.length);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
};
|
|
60
|
+
#flush = () => {
|
|
61
|
+
if (this.inState('processing')) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
if (!this.fifo.length) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
this.setState('processing');
|
|
68
|
+
const current = this.fifo.shift();
|
|
69
|
+
this.onData(current.pdu, response => {
|
|
70
|
+
this.log.debug('sending tcp data');
|
|
71
|
+
const pkt = new Put_1.default()
|
|
72
|
+
.word16be(current.request.transId) // transaction id
|
|
73
|
+
.word16be(current.request.protocolVer) // protocol version
|
|
74
|
+
.word16be(response.length + 1) // pdu length
|
|
75
|
+
.word8(current.request.unitId) // unit id
|
|
76
|
+
.put(response) // the actual pdu
|
|
77
|
+
.buffer();
|
|
78
|
+
current.socket.write(pkt);
|
|
79
|
+
this.setState('ready');
|
|
80
|
+
});
|
|
81
|
+
};
|
|
82
|
+
#initiateSocket = (socket) => {
|
|
83
|
+
this.socketCount += 1;
|
|
84
|
+
socket.on('end', () => {
|
|
85
|
+
this.emit('close');
|
|
86
|
+
this.log.debug(`connection closed, socket ${JSON.stringify(socket.address())}`);
|
|
87
|
+
const pos = this.clients.indexOf(socket);
|
|
88
|
+
if (pos !== -1) {
|
|
89
|
+
this.clients.splice(pos, 1);
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
socket.on('data', this.#onSocketData(socket));
|
|
93
|
+
socket.on('error', e => {
|
|
94
|
+
this.emit('error', e);
|
|
95
|
+
this.log.error(`Socket error ${e}`);
|
|
96
|
+
});
|
|
97
|
+
};
|
|
98
|
+
close(cb) {
|
|
99
|
+
for (const socket of this.clients) {
|
|
100
|
+
socket?.destroy();
|
|
101
|
+
}
|
|
102
|
+
this.server.close(() => {
|
|
103
|
+
this.server.unref();
|
|
104
|
+
cb?.();
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
getClients() {
|
|
108
|
+
return this.clients?.map(it => it?.address()?.address).filter(it => it) || [];
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
exports.default = ModbusServerTcp;
|
|
112
|
+
//# sourceMappingURL=modbus-server-tcp.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"modb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,CAAC;YACpB,EAAE,EAAE,EAAE,CAAC;QACX,CAAC,CAAC,CAAC;IACP,CAAC;IAED,UAAU;QACN,OAAO,IAAI,CAAC,OAAO,EAAE,GAAG,CAAC,EAAE,CAAC,EAAE,CAAE,EAAE,EAAE,OAAO,EAAkB,EAAE,OAAO,CAAC,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,IAAI,EAAE,CAAC;IACnG,CAAC;CACJ;AAtJD,kCAsJC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility functions for logging management
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Create a logging wrapper that can filter connection error messages
|
|
6
|
+
*
|
|
7
|
+
* @param originalLog - The original logger object
|
|
8
|
+
* @param disableLogging - Whether to disable connection error logging
|
|
9
|
+
* @returns The wrapped logger or original logger
|
|
10
|
+
*/
|
|
11
|
+
export declare function createLoggingWrapper(originalLog: ioBroker.Logger, disableLogging?: boolean): ioBroker.Logger;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Utility functions for logging management
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createLoggingWrapper = createLoggingWrapper;
|
|
7
|
+
/**
|
|
8
|
+
* Create a logging wrapper that can filter connection error messages
|
|
9
|
+
*
|
|
10
|
+
* @param originalLog - The original logger object
|
|
11
|
+
* @param disableLogging - Whether to disable connection error logging
|
|
12
|
+
* @returns The wrapped logger or original logger
|
|
13
|
+
*/
|
|
14
|
+
function createLoggingWrapper(originalLog, disableLogging) {
|
|
15
|
+
if (!disableLogging) {
|
|
16
|
+
return originalLog;
|
|
17
|
+
}
|
|
18
|
+
// List of connection error messages that should be suppressed when logging is disabled
|
|
19
|
+
const suppressedErrorMessages = ['Socket Error', 'Client in error state'];
|
|
20
|
+
return {
|
|
21
|
+
silly: originalLog.silly.bind(originalLog),
|
|
22
|
+
level: originalLog.level,
|
|
23
|
+
debug: originalLog.debug.bind(originalLog),
|
|
24
|
+
info: originalLog.info.bind(originalLog),
|
|
25
|
+
warn: originalLog.warn.bind(originalLog),
|
|
26
|
+
error: (msg, ...args) => {
|
|
27
|
+
// Check if this is a connection error message that should be suppressed
|
|
28
|
+
if (typeof msg === 'string' && suppressedErrorMessages.some(pattern => msg.includes(pattern))) {
|
|
29
|
+
// Suppress this error message when logging is disabled
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
// Allow all other error messages through
|
|
33
|
+
originalLog.error(msg.toString() + (args.length > 0 ? ` ${args.join(', ')}` : ''));
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=loggingUtils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"loggingUtils.js","sourceRoot":"","sources":["../../src/lib/loggingUtils.ts"],"names":[],"mappings":";AAAA;;GAEG;;AASH,oDAwBC;AA/BD;;;;;;GAMG;AACH,SAAgB,oBAAoB,CAAC,WAA4B,EAAE,cAAwB;IACvF,IAAI,CAAC,cAAc,EAAE,CAAC;QAClB,OAAO,WAAW,CAAC;IACvB,CAAC;IAED,uFAAuF;IACvF,MAAM,uBAAuB,GAAG,CAAC,cAAc,EAAE,uBAAuB,CAAC,CAAC;IAE1E,OAAO;QACH,KAAK,EAAE,WAAW,CAAC,KAAK,CAAC,IAAI,CAAC,WAAW,CAAC;QAC1C,KAAK,EAAE,WAAW,CAAC,KAAK;QACxB,KAAK,EAAE,WAAW,CAAC,KAAK,CAAC,IAAI,CAAC,WAAW,CAAC;QAC1C,IAAI,EAAE,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC;QACxC,IAAI,EAAE,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC;QACxC,KAAK,EAAE,CAAC,GAAmB,EAAE,GAAG,IAAW,EAAQ,EAAE;YACjD,wEAAwE;YACxE,IAAI,OAAO,GAAG,KAAK,QAAQ,IAAI,uBAAuB,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,GAAG,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,EAAE,CAAC;gBAC5F,uDAAuD;gBACvD,OAAO;YACX,CAAC;YACD,yCAAyC;YACzC,WAAW,CAAC,KAAK,CAAC,GAAG,CAAC,QAAQ,EAAE,GAAG,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;QACvF,CAAC;KACJ,CAAC;AACN,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@iobroker/modbus",
|
|
3
|
+
"version": "7.0.2",
|
|
4
|
+
"description": "Template for Modbus protocol in ioBroker",
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "public"
|
|
7
|
+
},
|
|
8
|
+
"author": {
|
|
9
|
+
"name": "bluefox",
|
|
10
|
+
"email": "dogafox@gmail.com"
|
|
11
|
+
},
|
|
12
|
+
"contributors": [
|
|
13
|
+
{
|
|
14
|
+
"name": "bluefox",
|
|
15
|
+
"email": "dogafox@gmail.com"
|
|
16
|
+
}
|
|
17
|
+
],
|
|
18
|
+
"homepage": "https://github.com/ioBroker/modbus",
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"keywords": [
|
|
21
|
+
"ioBroker",
|
|
22
|
+
"modbus",
|
|
23
|
+
"Smart Home",
|
|
24
|
+
"home automation"
|
|
25
|
+
],
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "https://github.com/ioBroker/modbus"
|
|
29
|
+
},
|
|
30
|
+
"engines": {
|
|
31
|
+
"node": ">=18"
|
|
32
|
+
},
|
|
33
|
+
"optionalDependencies": {
|
|
34
|
+
"serialport": "^13.0.0"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@iobroker/adapter-core": "^3.3.2",
|
|
38
|
+
"tsv-json": "^2.0.0"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@alcalzone/release-script": "^3.8.0",
|
|
42
|
+
"@alcalzone/release-script-plugin-license": "^3.7.0",
|
|
43
|
+
"@iobroker/build-tools": "^2.0.15",
|
|
44
|
+
"@iobroker/eslint-config": "^2.2.0",
|
|
45
|
+
"@types/node": "^24.6.2",
|
|
46
|
+
"@types/serialport": "^10.2.0"
|
|
47
|
+
},
|
|
48
|
+
"main": "build/index.js",
|
|
49
|
+
"files": [
|
|
50
|
+
"build/",
|
|
51
|
+
"LICENSE"
|
|
52
|
+
],
|
|
53
|
+
"scripts": {
|
|
54
|
+
"prepublishOnly": "npm run build",
|
|
55
|
+
"lint": "eslint -c eslint.config.mjs",
|
|
56
|
+
"build": "tsc -p tsconfig.build.json",
|
|
57
|
+
"release": "release-script",
|
|
58
|
+
"release-patch": "release-script patch --yes",
|
|
59
|
+
"release-minor": "release-script minor --yes",
|
|
60
|
+
"release-major": "release-script major --yes",
|
|
61
|
+
"update-packages": "npx -y npm-check-updates --upgrade",
|
|
62
|
+
"npm": "npm i"
|
|
63
|
+
},
|
|
64
|
+
"bugs": {
|
|
65
|
+
"url": "https://github.com/ioBroker/modbus/issues"
|
|
66
|
+
},
|
|
67
|
+
"readmeFilename": "README.md"
|
|
68
|
+
}
|