-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
150 lines (132 loc) · 3.42 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const readline = require('readline');
const net = require('net');
const modbus = require('jsmodbus');
console.log('\n');
console.log('================================');
console.log('Nibe Modbus');
console.log('Tero Paananen 2023');
console.log('================================');
console.log('\n');
let deviceIP;
let devicePort = 502;
let userInterface;
let client;
let socket;
/**
* Asks user for Nibe device IP
*/
const askDeviceIP = async () => {
const resp = await ask('Give Nibe device IP address: ');
deviceIP = resp;
console.log('Using IP: ' + deviceIP);
console.log('Using port: ' + devicePort);
};
/**
* Node main function
*/
const main = async () => {
userInterface = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
try {
await askDeviceIP();
await connect();
await readingLoop();
} catch (error) {
console.log(error);
} finally {
userInterface.close();
socket && socket.end();
}
};
/**
* Connects to Nibe device using Modbus
*/
const connect = async () => {
socket = new net.Socket();
const option = {
host: deviceIP,
port: devicePort,
unitID: 1,
timeout: 5000,
autoReconnect: true,
logLabel: 'Nibe S-series',
logLevel: 'error',
logEnabled: true,
};
client = new modbus.client.TCP(socket);
const connectionPromise = new Promise((resolve, reject) => {
socket.on('connect', async () => {
console.log('Socket connected');
resolve(true);
});
socket.on('error', e => {
console.log('Socket error', e.message);
reject(e.message);
});
socket.on('close', () => {
console.log('Socket closed');
});
});
socket.connect(option);
return connectionPromise;
};
/**
* Reads registers loop until user aborts
*/
const readingLoop = async () => {
return new Promise(async (resolve, reject) => {
let reading = true;
try {
while (reading) {
const readingQuestion = await ask('Read registers? (y/n): ');
if (readingQuestion.length === 0 || readingQuestion === 'y') {
await readingRegisters();
} else {
throw new Error('User aborted');
}
}
} catch (error) {
reading = false;
reject(error.message);
}
});
};
/**
* Reads registers from Nibe device
*/
const readingRegisters = async () => {
console.log('\n');
console.log('Reading registers...');
const results = await Promise.all([
client.readInputRegisters(1, 1), // BT1 - outside temperature
client.readInputRegisters(140, 1), // kompressor Hz
]);
let tempOutside = results[0].response._body._valuesAsBuffer;
let kompressorHz = results[1].response._body._valuesAsBuffer;
// Convert HEX to Decimal and divide by the scalefactor
tempOutside = tempOutside.readInt16BE().toString() / 10; // scale factor is 10
kompressorHz = kompressorHz.readInt16BE().toString() / 1; // scale factor is 1
console.log('> Outside temperature: ' + tempOutside);
console.log('> Kompressor Hz: ' + kompressorHz);
console.log('\n');
};
/**
* Writes registers to Nibe device
*/
const writeRegister = async (register, value) => {
// NOT TESTED
console.log('\n');
console.log('Writing registers...');
await client.writeSingleRegister(register, value);
};
/**
* Helper function to ask user for input
*/
const ask = question => {
return new Promise(resolve => {
userInterface.question(question, input => resolve(input));
});
};
main();