-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.js
349 lines (297 loc) · 10.7 KB
/
main.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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/**
* Adapter for integration of Gardena Smart System to ioBroker
* based on official GARDENA smart system API (https://developer.1689.cloud/)
* Support: https://forum.iobroker.net/...
* Autor: jpgorganizer (ioBroker) | jpgorganizer (github)
* SVN: $Rev: 3209 $ $Date: 2024-05-25 12:16:16 +0200 (Sa, 25 Mai 2024) $
* contains some functions available at forum.iobroker.net, see function header
*/
'use strict';
/*
* Created with @iobroker/create-adapter v1.17.0
*/
const mainrev ='$Rev: 3209 $';
const adapterversion = '2.0.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 ju = require('@jpgorganizer/utils').utils;
// Load your modules here, e.g.:
const gardena_api = require(__dirname + '/lib/api');
let configUseMowerHistory;
function updateAdapter010004(adapter, previousAdapterVersion, currentAdapterVersion, dotfactor) {
// delete smartgarden.0.testVariable
let updateAdapterVersion = '1.0.4';
let arr = updateAdapterVersion.split('.');
let relevantAdapterVersion = arr[0] * dotfactor * dotfactor + arr[1] * dotfactor + arr[2];
if (previousAdapterVersion < relevantAdapterVersion) {
let id = adapter.name + '.' + adapter.instance + '.testVariable'
adapter.getState(id, function(err, state) {
if (!err && state) {
adapter.delState(id, function (err) {
if (!err) {
adapter.delObject(id);
ju.adapterloginfo(1, 'update: ' + id + ' removed');
ju.adapterloginfo(1, 'update for version ' + updateAdapterVersion + ' done with ' + adapterversion);
}
});
}
});
}
}
function updateAdapter020000(adapter, previousAdapterVersion, currentAdapterVersion, dotfactor, callback) {
// change data type for some data points
// just delete the data points, they will get recreated in normal process
let updateAdapterVersion = '2.0.0';
let arr = updateAdapterVersion.split('.');
let relevantAdapterVersion = arr[0] * dotfactor * dotfactor + arr[1] * dotfactor + arr[2];
if (previousAdapterVersion < relevantAdapterVersion) {
// - for all devices: `rfLinkLevel_value`
// - for mower: `batteryLevel_value`, `operatingHours_value`
// - for sensor: `batteryLevel_value`, `soilHumidity_value`, `soilTemperature_value`, `lightIntensity_value`, `ambientTemperature_value`
let arrPattern = [];
arrPattern.push('*.name_value');
arrPattern.push('*.batteryLevel_value');
arrPattern.push('*.batteryLevel_timestamp');
arrPattern.push('*.batteryState_value');
arrPattern.push('*.batteryState_timestamp');
arrPattern.push('*.rfLinkLevel_value');
arrPattern.push('*.rfLinkLevel_timestamp');
arrPattern.push('*.serial_value');
arrPattern.push('*.modelType_value');
arrPattern.push('*.rfLinkState_value');
arrPattern.push('*.rfLinkState_timestamp');
arrPattern.push('*.state_value');
arrPattern.push('*.state_timestamp');
arrPattern.push('*.activity_value');
arrPattern.push('*.activity_timestamp');
arrPattern.push('*.lastErrorCode_value');
arrPattern.push('*.lastErrorCode_timestamp');
arrPattern.push('*.operatingHours_value');
arrPattern.push('*.duration_value');
arrPattern.push('*.duration_timestamp');
arrPattern.push('*.soilHumidity_value');
arrPattern.push('*.soilHumidity_timestamp');
arrPattern.push('*.soilTemperature_value');
arrPattern.push('*.soilTemperature_timestamp');
arrPattern.push('*.lightIntensity_value');
arrPattern.push('*.lightIntensity_timestamp');
arrPattern.push('*.ambientTemperature_value');
arrPattern.push('*.ambientTemperature_timestamp');
arrPattern.push('*.name');
deleteStateWPatternList(adapter, arrPattern, 0, function(err) {
ju.adapterloginfo(1, 'update for version ' + updateAdapterVersion + ' done with ' + adapterversion);
if (callback) callback(err);
});
} else {
if (callback) callback(0);
}
}
function updateAdapter(adapter, callback) {
let dotfactor = 100;
let id = adapter.name + '.' + adapter.instance + '.info.adapterversion'
let previousAdapterVersion = 0;
let arr = adapterversion.split('.');
let currentAdapterVersion = arr[0] * dotfactor * dotfactor + arr[1] * dotfactor + arr[2];
adapter.getState(id, function(err, state) {
if (!err) {
if (state) {
let arr = state.val.split('.');
previousAdapterVersion = arr[0] * dotfactor * dotfactor + arr[1] * dotfactor + arr[2];
}
updateAdapter010004(adapter, previousAdapterVersion, currentAdapterVersion, dotfactor);
updateAdapter020000(adapter, previousAdapterVersion, currentAdapterVersion, dotfactor, function (err) {
if (callback) callback(0);
});
}
adapter.setObjectNotExists(id, {
type: 'state',
common: {
name: 'adapterversion',
type: 'string',
role: 'text',
read: true,
write: false,
},
native: {},
}, function (err) {
if (err) {
ju.adapterloginfo(1, 'ERROR updateAdapter: creating state info.adapterversion failed');
} else {
adapter.setState('info.adapterversion', adapterversion, true);
}
});
});
}
function delStatesWList(adapter, arrId, idx, callback) {
if (idx < arrId.length) {
let id = arrId[idx];
adapter.delState(id, function (err) {
if (!err) {
adapter.delObject(id, function (err) {
ju.adapterloginfo(1, 'delStatesWList: ' + gardena_api.beautifyStateIdName(id) + ' removed');
if (idx + 1 < arrId.length) {
delStatesWList(adapter, arrId, idx + 1, function(err) {
if (callback) callback(0);
});
} else {
if (callback) callback(0);
}
});
} else {
if (callback) callback(0);
}
});
} else {
if (callback) callback(0);
}
}
function deleteStateWPattern(adapter, namepattern, callback) {
let pattern = adapter.name + '.' + adapter.instance + '.' + namepattern;
let arrId = [];
let arrIdBeautified = [];
adapter.getStates(pattern, function(err, states) {
if (!err && states) {
for (let id in states) {
arrId.push(id);
arrIdBeautified.push(gardena_api.beautifyStateIdName(id));
}
ju.adapterloginfo(1, 'deleteStateWPattern: ' + namepattern + ' > found states=' + JSON.stringify(arrIdBeautified));
if (arrId.length > 0) {
delStatesWList(adapter, arrId, 0, function(err) {
if (callback) callback(0);
});
} else {
if (callback) callback(0);
}
} else {
ju.adapterloginfo(1, 'deleteStateWPattern: ' + namepattern + ' > no states found');
if (callback) callback(0);
}
});
}
function deleteStateWPatternList(adapter, patternlist, idx, callback) {
let pattern = patternlist[idx];
deleteStateWPattern(adapter, pattern, function(err) {
if (idx + 1 < patternlist.length) {
deleteStateWPatternList(adapter, patternlist, idx + 1, function(err) {
if (callback) callback(0);
});
} else {
if (callback) callback(0);
}
});
}
function main(adapter) {
// Initialize your adapter here
// Reset the connection indicator during startup
adapter.setState('info.connection', false, true);
// The adapters config (in the instance object everything under the attribute "native") is accessible via
// this.config:
ju.adapterloginfo(1, 'config authenticaton_host: ' + adapter.config.gardena_authentication_host);
ju.adapterloginfo(1, 'config smart_host: ' + adapter.config.smart_host);
configUseMowerHistory = adapter.config.useMowerHistory;
gardena_api.setAdapter(adapter);
updateAdapter(adapter, function(err) {
gardena_api.setVer(mainrev);
gardena_api.getConnection();
if (configUseMowerHistory === true) {
adapter.getState('info.saveMowingHistory', function (err, state) {
if (!err && state) {
if (state.val.length > 0) {
let mowHistory = JSON.parse(state.val);
gardena_api.setMowingHistory(mowHistory);
}
}
});
adapter.getState('info.saveChargingHistory', function (err, state) {
if (!err && state) {
if (state.val.length > 0) {
let chargeHistory = JSON.parse(state.val);
gardena_api.setChargingHistory(chargeHistory);
}
}
});
}
// all states changes inside the adapters namespace are subscribed
adapter.subscribeStates('*');
});
}
class Smartgarden extends utils.Adapter {
/**
* @param {Partial<ioBroker.AdapterOptions>} [options={}]
*/
constructor(options) {
super({
...options,
name: 'smartgarden',
});
this.on('ready', this.onReady.bind(this));
this.on('stateChange', this.onStateChange.bind(this));
this.on('unload', this.onUnload.bind(this));
}
/**
* Is called when databases are connected and adapter received configuration.
*/
async onReady() {
ju.adapterloginfo(1, "onReady...");
main(this);
}
/**
* Is called when adapter shuts down - callback has to be called under any circumstances!
* @param {() => void} callback
*/
onUnload(callback) {
try {
ju.adapterloginfo(1, 'cleaned everything up...');
gardena_api.stopAllTimer();
// Reset the connection indicator during shutdown
this.setState('info.connection', false, true, function (err) {
if (err) this.log.error(err);
callback();
});
} catch (e) {
ju.adapterloginfo(1, 'onUnload error catched');
callback();
}
}
/**
* Is called if a subscribed state changes
* @param {string} id
* @param {ioBroker.State | null | undefined} state
*/
onStateChange(id, state) {
if (state !== null && state !== undefined) {
if (state.ack === false) {
// The state was changed by user
ju.adapterloginfo(3, '---> Command should be sent to device: state '+ gardena_api.beautifyStateIdName(id) + ' changed, ' + state.val + ' (ack = ' + state.ack + ')');
gardena_api.sendCommand(id, state);
} else {
// The state was changed by system
let arr = id.split('.');
// print some logs, but for some large values it's not helpful to write the value itself
switch (arr[arr.length - 1]) {
case 'RateLimitCounter':
case 'saveChargingHistory':
case 'saveMowingHistory':
ju.adapterloginfo(3, '---> State change by device: state ' + gardena_api.beautifyStateIdName(id) + ' changed, ' + ' (ack = ' + state.ack + ')');
break;
default:
ju.adapterloginfo(3, '---> State change by device: state ' + gardena_api.beautifyStateIdName(id) + ' changed: ' + state.val + ' (ack = ' + state.ack + ')');
break;
}
}
}
}
}
// @ts-ignore parent is a valid property on module
if (module.parent) {
// Export the constructor in compact mode
/**
* @param {Partial<ioBroker.AdapterOptions>} [options={}]
*/
module.exports = (options) => new Smartgarden(options);
} else {
// otherwise start the instance directly
new Smartgarden();
}