-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.js
More file actions
211 lines (194 loc) · 6.19 KB
/
Copy pathmain.js
File metadata and controls
211 lines (194 loc) · 6.19 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
'use strict';
/*
* Created with @iobroker/create-adapter v2.1.1
*/
// The adapter-core module gives you access to the core ioBroker functions
// you need to create an adapter
const utils = require('@iobroker/adapter-core');
const axios = require('axios').default;
class Voltoplus extends utils.Adapter {
/**
* @param {Partial<utils.AdapterOptions>} [options]
*/
constructor(options) {
super({
...options,
name: 'voltoplus',
});
this.on('ready', this.onReady.bind(this));
// this.on("stateChange", this.onStateChange.bind(this));
// this.on("objectChange", this.onObjectChange.bind(this));
this.on('unload', this.onUnload.bind(this));
this.connectionError = false;
this.connectionErrorCount = 0;
}
/**
* Is called when databases are connected and adapter received configuration.
*/
async onReady() {
await this.collectAndSetValues();
}
/**
* Is called when adapter shuts down - callback has to be called under any circumstances!
*
* @param {() => void} callback
*/
onUnload(callback) {
try {
clearTimeout(this.timeout);
callback();
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (e) {
callback();
}
}
// If you need to react to object changes, uncomment the following block and the corresponding line in the constructor.
// You also need to subscribe to the objects with `this.subscribeObjects`, similar to `this.subscribeStates`.
// /**
// * Is called if a subscribed object changes
// * @param {string} id
// * @param {ioBroker.Object | null | undefined} obj
// */
// onObjectChange(id, obj) {
// if (obj) {
// // The object was changed
// this.log.info(`object ${id} changed: ${JSON.stringify(obj)}`);
// } else {
// // The object was deleted
// this.log.info(`object ${id} deleted`);
// }
// }
// /**
// * Is called if a subscribed state changes
// * @param {string} id
// * @param {ioBroker.State | null | undefined} state
// */
// onStateChange(id, state) {
// if (state) {
// // The state was changed
// this.log.info(`state ${id} changed: ${state.val} (ack = ${state.ack})`);
// } else {
// // The state was deleted
// this.log.info(`state ${id} deleted`);
// }
// }
async collectAndSetValues() {
const values = await this.getValues();
if (values !== null) {
const idValueMap = this.createIdValueMap(values);
await this.setValues(idValueMap);
}
this.timeout = setTimeout(() => {
this.collectAndSetValues();
}, 1000);
}
/**
* Get the json with values from the Voltoplus
*
* @returns {Promise<null|object>}
*/
async getValues() {
const url = `http://${this.config.ip}/api/v1/values`;
try {
const response = await axios.get(url);
this.connectionError = false;
this.setStateChanged('info.connection', {
val: true,
ack: true,
});
if (this.connectionErrorCount > 0) {
this.connectionErrorCount = 0;
}
return response.data['json_values'];
} catch (error) {
this.connectionErrorCount += 1;
if (this.connectionErrorCount === 30) {
this.log.error(
`There was an error while connecting, please check the device and network connection: ${error}`,
);
this.connectionError = true;
this.setStateChanged('info.connection', {
val: false,
ack: true,
});
}
return null;
}
}
async setValues(values) {
await this.setState('phase_1.voltage', {
val: this.getFixedNumber(values.U1 / 100, 2),
ack: true,
});
await this.setState('phase_1.current', {
val: this.getFixedNumber(values.I1 / 1000, 3),
ack: true,
});
await this.setState('phase_1.power', {
val: parseInt(values.P1),
ack: true,
});
await this.setState('phase_2.voltage', {
val: this.getFixedNumber(values.U2 / 100, 2),
ack: true,
});
await this.setState('phase_2.current', {
val: this.getFixedNumber(values.I2 / 1000, 3),
ack: true,
});
await this.setState('phase_2.power', {
val: parseInt(values.P2),
ack: true,
});
await this.setState('phase_3.voltage', {
val: this.getFixedNumber(values.U3 / 100, 2),
ack: true,
});
await this.setState('phase_3.current', {
val: this.getFixedNumber(values.I3 / 1000, 3),
ack: true,
});
await this.setState('phase_3.power', {
val: parseInt(values.P3),
ack: true,
});
await this.setState('power', {
val: parseInt(values.P),
ack: true,
});
await this.setState('energy_purchased', {
val: values.fwdEn / 10,
ack: true,
});
await this.setState('energy_supplied', {
val: values.rvsEn / 10,
ack: true,
});
}
createIdValueMap(data) {
return data.reduce((acc, item) => {
acc[item.id] = item.value;
return acc;
}, {});
}
/**
* Get back number with fixed number of decimals
*
* @param {number} number
* @param {number} decimals
* @returns {number}
*/
getFixedNumber(number, decimals) {
return parseFloat(number.toFixed(decimals));
}
}
if (require.main !== module) {
// Export the constructor in compact mode
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
module.exports = options => new Voltoplus(options);
} else {
// otherwise start the instance directly
new Voltoplus();
}