-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodbus-master.js
More file actions
130 lines (118 loc) · 4.25 KB
/
Copy pathmodbus-master.js
File metadata and controls
130 lines (118 loc) · 4.25 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
import {Coil, DiscreteInput, HoldingRegister, InputRegister} from './register.js'
import {monitoringRegisters, writeRegisters} from './registers-list.js'
import {client, connectClient} from "./slave-connection.js"
global.tcnt_t = 0;
global.SOC = 0;
let readNullCnt = -1;
//check
/*
export function monitor(slave, interval, callback) {
setInterval(() => readRegisters(slave, callback), interval)
}
*/
export function monitor(interval, callback) {
setInterval(() => readRegisters(callback), interval)
}
async function calculateSOC(ChargingCurrent, OpenCircuitVoltage, interval) {
const T_samp = 100 / Math.exp(1) - 6;
const BatteryCapacity = 288000;
const today = new Date();
tcnt_t += interval;
if(tcnt_t >= T_samp) {
try {
if(today.getHours() == 0 && today.getMinutes() == 0 && today.getSeconds < 6) {
var intergralCurrent = 0;
}
intergralCurrent += ChargingCurrent * T_samp;
SOC += intergralCurrent / BatteryCapacity;
tcnt_t -= T_samp;
} catch(e) {
// console.log(e);
}
}
return SOC;
}
//check function readRegisters(slave, callback){
function readRegisters(callback){
let regs = {
battery: [],
energyGeneration: [],
energyConsumption: []
};
let readValues = {
battery: {},
energyGeneration: {},
energyConsumption: {}
};
for (let module in monitoringRegisters){
for (let dataPoint in monitoringRegisters[module]){
//check regs[module].push(createRegisterObject(dataPoint, slave, monitoringRegisters[module][dataPoint]));
regs[module].push(createRegisterObject(dataPoint, client, monitoringRegisters[module][dataPoint]));
}
}
(async () => {
for (let module in regs) {
for (let reg of regs[module]){
try{
const {data} = await reg.read();
//console.log(module + ' ' + reg.getName() + ': ' + data);
readValues[module][reg.getName()] = (reg.scale == null) ? data[0] : data[0]/reg.scale //TODO considers only 1st register
if(module == "battery") {
readValues[module][level] = await calculateSOC(readValues[module][current], readValues[module][voltage], 6000);
}
} catch (e) {
console.log(e);
}
}
console.log('read ', module, ' : ' , readValues[module]);
if(JSON.stringify(readValues[module]) === '{}') {
console.log('NULL data cnt ', readNullCnt);
if(readNullCnt == 20) {
connectClient();
readNullCnt = -1;
} else {
readNullCnt++;
}
} else {
if(readNullCnt > -1) {
readNullCnt = -1;
}
}
}
return callback(readValues);
})();
}
function createRegisterObject(name, slave, {type, address, length, scale}) {
switch (type) {
case 1:
return new DiscreteInput(name, slave, address, length);
case 2:
return new Coil(name, slave, address, length);
case 3:
return new InputRegister(name, slave, address, length, scale);
case 4:
return new HoldingRegister(name, slave, address, length, scale);
default:
throw new Error('Not valid register type')
}
}
//check export async function writeCharging(slave, value) {
export async function writeCharging(value) {
//check const reg = createRegisterObject('charging', slave, writeRegisters.charging);
const reg = createRegisterObject('charging', client, writeRegisters.charging);
try {
return await reg.write(value);
} catch (e) {
console.log(e);
}
}
//check export async function writeDischarging(slave, value) {
export async function writeDischarging(value) {
//check const reg = createRegisterObject('discharging', slave, writeRegisters.discharging);
const reg = createRegisterObject('discharging', client, writeRegisters.discharging);
try {
return await reg.write(value);
} catch (e) {
console.log(e);
}
}