forked from BartTK/node-p1-reader
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
158 lines (126 loc) · 5.42 KB
/
main.js
File metadata and controls
158 lines (126 loc) · 5.42 KB
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
const EventEmitter = require('events');
const util = require('util');
let SerialPort = require('serialport');
let connectedToSmartMeter = false;
let constructor;
let crcCheckRequired = false;
const checkCrc = require('./lib/checkCrc');
const parsePacket = require('./lib/parsePacket');
const debug = require('./lib/debug');
const config = require('./config/config.json');
function P1Reader(options) {
if (typeof options !== 'object' || options.port == "" || options.baudRate == "" || options.parity == "" || options.dataBits == "" || options.stopBits == "") {
console.error("Parameters 'port', 'baudRate', 'parity', 'dataBits' and 'stopBit' are required since version 2.x.x to instantiate the module");
}
if (options.debug) {
debug.enableDebugMode();
}
// Overwrite serialport module when emulator mode is set
if (options.emulator) {
SerialPort = require('./lib/emulateSerialport');
SerialPort.setEmulatorOverrides(options.emulatorOverrides);
}
if (options.crcCheckRequired) {
crcCheckRequired = options.crcCheckRequired;
}
constructor = this; // TODO???????????????????????????????
EventEmitter.call(this);
_setupSerialConnection(options.port, options.baudRate, options.parity, options.dataBits, options.stopBits);
//
// // Either force a specific port (with specific configuration) or automatically discover it
// if (options && options.serialPort) {
//
//
// _setupSerialConnection();
// } else {
// SerialPort.list()
// .then(ports => {
// // Create the auto discovery list with each of the possible serialport configurations per port found
// for (let i = 0; i < ports.length; i++) {
// for (let j = 0; j < config.serialPort.length; j++) {
// autodiscoverList.push({
// port: ports[i].comName,
// baudRate: config.serialPort[j].baudRate,
// parity: config.serialPort[j].parity,
// dataBits: config.serialPort[j].dataBits,
// stopBits: config.serialPort[j].stopBits
// });
// }
// }
//
// debug.logAutodiscoverList(autodiscoverList);
//
// _setupSerialConnection();
// })
// .catch(err => {
// console.error('Serialports could not be listed: ' + err);
// });
// }
}
util.inherits(P1Reader, EventEmitter);
module.exports = P1Reader;
/**
* Setup serial port connection
*/
function _setupSerialConnection(port, baudRate, parity, dataBits, stopBits) {
debug.log('Trying to connect to Smart Meter via port: ' + port + ' (BaudRate: ' + baudRate + ', Parity: ' + parity + ', Databits: ' + dataBits + ', Stopbits: ' + stopBits + ')');
// Open serial port connection
const sp = new SerialPort(port, {
baudRate: baudRate,
parity: parity,
dataBits: dataBits,
stopBits: stopBits
});
let received = '';
sp.on('open', () => {
debug.log('Serial connection established');
sp.on('data', data => {
received += data.toString();
let startCharPos = received.indexOf(config.startCharacter);
let endCharPos = received.indexOf(config.stopCharacter);
if (endCharPos >= 0 && endCharPos < startCharPos) {
received = received.substr(endCharPos + 1);
startCharPos = -1;
endCharPos = -1;
}
// Package is complete if the start- and stop character are received
const crcReceived = endCharPos >= 0 && received.length > endCharPos + 4;
if (startCharPos >= 0 && endCharPos >= 0 && crcReceived) {
const packet = received.substr(startCharPos, endCharPos - startCharPos);
const expectedCrc = parseInt(received.substr(endCharPos + 1, 4), 16);
received = received.substr(endCharPos + 5);
var crcOk = true;
if (crcCheckRequired) {
crcOk = checkCrc(packet + '!', expectedCrc);
}
if (crcOk) {
const parsedPacket = parsePacket(packet);
received = '';
// Emit a 'connected' event when we have actually successfully parsed our first data
if (!connectedToSmartMeter && parsedPacket.timestamp !== null) {
debug.log('Connection with Smart Meter established');
constructor.emit('connected');
connectedToSmartMeter = true;
}
debug.writeToLogFile(packet, parsedPacket);
constructor.emit('reading-raw', packet);
if (parsedPacket.timestamp !== null) {
constructor.emit('reading', parsedPacket);
} else {
constructor.emit('error', 'Invalid reading');
}
} else {
constructor.emit('error', 'Invalid CRC');
}
}
});
});
sp.on('error', (error) => {
debug.log('Error emitted: ' + error);
constructor.emit('error', error);
});
sp.on('close', () => {
debug.log('Connection closed');
constructor.emit('close');
});
}