-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
executable file
·401 lines (364 loc) · 19.6 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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/**
*
* mieleathome adapter
*
*
* file io-package.json comments:
*
* {
* "common": {
* "name": "mieleathome", // name has to be set and has to be equal to adapters folder name and main file name excluding extension
* "version": "0.0.4", // use "Semantic Versioning"! see http://semver.org/
* "title": "Node.js mieleathome Adapter", // Adapter title shown in User Interfaces
* "authors": [ // Array of authord
* "name <[email protected]>"
* ]
* "desc": "mieleathome adapter", // Adapter description shown in User Interfaces. Can be a language object {de:"...",ru:"..."} or a string
* "platform": "Javascript/Node.js", // possible values "javascript", "javascript/Node.js" - more coming
* "mode": "daemon", // possible values "daemon", "schedule", "subscribe"
* "materialize": true, // support of admin3
* "schedule": "0 0 * * *" // cron-style schedule. Only needed if mode=schedule
* "loglevel": "info" // Adapters Log Level
* },
* "native": { // the native object is available via adapter.config in your adapters code - use it for configuration
* "test1": true,
* "test2": 42,
* "mySelect": "auto"
* }
* }
*
*/
/* jshint -W097 */// jshint strict:false
/*jslint node: true */
'use strict';
// you have to require the utils module and call adapter function
const utils = require('@iobroker/adapter-core'); // Get common adapter utils
// read the adapter name from package.json
const adapterName = require('./package.json').name.split('.').pop();
const mieleathome = require('./utils/mieleathome');
const mieledevice = require('./utils/devices.js');
const schedule = require('node-schedule');
// create adapter instance wich will be used for communication with controller
let adapter;
var miele;
var device = new mieledevice();
function startAdapter(options) {
options = options || {};
Object.assign(options, {
// name has to be set and has to be equal to adapters folder name and main file name excluding extension
name: adapterName,
// is called when adapter shuts down - callback has to be called under any circumstances!
unload: function (callback) {
try {
adapter.log.info('cleaned everything up...');
callback();
} catch (e) {
callback();
}
},
// is called if a subscribed object changes
objectChange: function (id, obj) {
// Warning, obj can be null if it was deleted
adapter.log.info('objectChange ' + id + ' ' + JSON.stringify(obj));
},
// is called if a subscribed state changes
// stateChange: function (id, state) {
// Warning, state can be null if it was deleted
// you can use the ack flag to detect if it is status (true) or command (false)
// if (state && !state.ack) {
// adapter.log.info('ack is not set!');
// }
// },
// Some message was sent to adapter instance over message box. Used by email, pushover, text2speech, ...
message: function (obj) {
if (typeof obj === 'object' && obj.message) {
if (obj.command === 'send') {
// e.g. send email or pushover or whatever
console.log('send command');
// Send response in callback if required
if (obj.callback) adapter.sendTo(obj.from, obj.command, 'Message received', obj.callback);
}
}
},
// is called when databases are connected and adapter received configuration.
// start here!
ready: () => main()
});
// you have to call the adapter function and pass a options object
// adapter will be restarted automatically every time as the configuration changed, e.g system.adapter.mieleathome.0
adapter = new utils.Adapter(options);
return adapter;
};
function GetDevices(data,Pfad){
var deviceType;
for (var ObjName in data) {
var New_Pfad=Pfad+'.'+ObjName;
var Type=typeof data[ObjName];
adapter.log.debug('Function GetDevices: ' + ObjName + ' ' + Pfad + ' ' + Type);
switch(Type){
case 'object':
adapter.log.debug('Ist Objekt '+ObjName+ ' ' + New_Pfad);
if (!adapter.getObject(New_Pfad)){
adapter.setObjectNotExists(New_Pfad, {type: 'state', common: { name: ObjName, type: Type, role: 'state'}, native: {} });
};
adapter.log.debug(ObjName + ' ' + Pfad + ' ' + New_Pfad + ' ' + Type);
if (New_Pfad.split('.')[0] === 'Devices' && !New_Pfad.split('.')[2]) {
// createExtendObject(New_Pfad + '.LightOn', { type: 'state', common: { name: 'LightOn',type: 'boolean',role: 'button' },native: {}});
// createExtendObject(New_Pfad + '.LightOff', { type: 'state', common: { name: 'LightOn',type: 'boolean',role: 'button' },native: {}});
addDevicefunction(New_Pfad,deviceType,data);
};
if (Pfad === 'Devices') {
createExtendObject(Pfad + '.GetDevices', { type: 'state', common: { name: 'GetDevices',type: 'boolean',role: 'button' },native: {}});
};
GetDevices(data[ObjName],New_Pfad);
break;
case 'boolean':
case 'string':
case 'number':
case 'none':
adapter.log.debug('Ist none ' + ObjName + ' ' + New_Pfad + ' ' + data[ObjName]);
if (String(New_Pfad).includes('state\.status\.value_raw')) {adapter.log.debug('DeviceType:' + New_Pfad + data[ObjName]);deviceType = data[ObjName];};
if (!adapter.getObject(New_Pfad)){
createExtendObject(New_Pfad, {type: 'state', common: { name: ObjName, type: Type, role: 'state' }, native: {} });
adapter.setState(New_Pfad,data[ObjName],true)
}
else{adapter.setState(New_Pfad,data[ObjName],true)}
break;
default:
adapter.log.debug('Ist '+Type+ ' '+ObjName+ ' ' + New_Pfad);
createExtendObject(New_Pfad, {type:Type, common: { role: 'state' }, native: {} });
if (Array.isArray(data[ObjName])===true){
adapter.log.debug('Ist Array'+ObjName);
for (i = 0; i < data[ObjName].length; i++) {
GetDevices(data[ObjName[i]]);
}
}
else{adapter.log.debug('Ist nix')}
break;
}
}
}//End of Function
function createExtendObject(id, objData, callback) {
adapter.getObject(id, function (err, oldObj) {
if (!err && oldObj) {
adapter.extendObject(id, objData, callback);
} else {
adapter.setObjectNotExists(id, objData, callback);
}
});
}
function addDevicefunction(Pfad,Type,data) {
var datajsonS = JSON.stringify(data);
var datajson = JSON.parse(datajsonS);
var devarray = Pfad.split('.');
var dev = devarray[devarray.length -1];
var Type = datajson[dev].state.status.value_raw;
adapter.log.debug('Function addDevicefunction: Dfunctions Call ' + Pfad + ' for Type ' + Type + ' ' + JSON.stringify(data));
var dfunctions = device.readDevice(parseFloat(Type));
for ( var i = 0; i<dfunctions.length; i++)
{
adapter.log.debug('Function:' + Pfad + dfunctions[i][0] + dfunctions[i][1] + dfunctions[i][2]);
createExtendObject(Pfad + '.' + dfunctions[i][0], { type: 'state', common: { name: dfunctions[i][0],type: 'boolean',role: 'button' },native: {}});
}
}
function main() {
// The adapters config (in the instance object everything under the attribute "native") is accessible via
// adapter.config:
adapter.log.info('config Client_ID: ' + adapter.config.Client_ID);
adapter.log.info('config Client_secret: ' + adapter.config.Client_secret);
adapter.log.info('config Miele_account: ' + adapter.config.Miele_account);
createExtendObject('Devices', {
type: 'channel',
common: {
name: 'Devices',
type: 'text',
role: 'value'
},
native: {}
});
createExtendObject('Authorization',{
type: 'channel',
common: {
name: 'Authorization',
type: 'text',
role: 'value'
},
native: {}
});
createExtendObject('Authorization.Authorized', {
type: 'state',
common: {
name: 'Authorized',
type: 'boolean',
role: 'value'
},
native: {}
});
createExtendObject('Authorization.Token', {
type: 'state',
common: {
name: 'Token',
type: 'text',
role: 'value'
},
native: {}
});
createExtendObject('Authorization.Refresh_Token', {
type: 'state',
common: {
name: 'Refresh_Token',
type: 'text',
role: 'value'
},
native: {}
});
if (adapter.config.Miele_account && adapter.config.Miele_pwd && adapter.config.Client_ID && adapter.config.Client_secret )
{
miele = new mieleathome(adapter.config.Miele_account, adapter.config.Miele_pwd, adapter.config.Client_ID,adapter.config.Client_secret);
adapter.getState('Authorization.Token', function (err, state) {
if (!err && state !== null ) {
var access_token = state.val ;
}
;
adapter.getState('Authorization.Refresh_Token', function (err, state) {
if (!err && state !== null ) {
var rrefresh_token = state.val ;
};
adapter.log.info('Authorization.Access_Token:' + access_token);
adapter.log.info('Authorization.Refresh_Token:' + rrefresh_token);
if ( !access_token || 0 === access_token.length) {
miele.NGetToken(
function(err,access_token,rrefresh_token){
if(!err){
adapter.setState('Authorization.Authorized',true,true);
adapter.setState('Authorization.Token',access_token,true);
adapter.setState('Authorization.Refresh_Token',rrefresh_token,true);
adapter.log.debug("Send GET Devices");
miele.NGetDevices(rrefresh_token, access_token,function(err,data,atoken,rtoken){
adapter.log.debug('NGetDevices Error: ' + err);
if(!err){GetDevices(data,'Devices')}
});
}
else{adapter.setState('Authorization.Authorized',false,true)}
});
}
else {
adapter.log.debug("Send GET Devices");
setTimeout(function(){
miele.NGetDevices(rrefresh_token, access_token,function(err,data,atoken,rtoken){
adapter.log.debug('NGetDevices Error: ' + err);
if(!err){GetDevices(data,'Devices')}
});
},8000);
};
var j = schedule.scheduleJob('*/10 * * * *', function(){
setTimeout(function(){
miele.NGetDevices(rrefresh_token, access_token,function(err,data,atoken,rtoken){
adapter.log.debug('NGetDevices Error: ' + err);
if(!err){GetDevices(data,'Devices')}
});
},8000);
});
});
});
miele.log("Test exports");
// in this mieleathome all states changes inside the adapters namespace are subscribed
adapter.subscribeStates('*');
}
}//Ende Functin main
// If started as allInOne/compact mode => return function to create instance
if (module && module.parent) {
module.exports = startAdapter;
} else {
// or start the instance directly
startAdapter();
}
adapter.on('stateChange', (id, state) => {
if (!id || !state || state.ack) {
// return;
}
const devices = id.split('.')[3];
adapter.log.debug('Function dapter.on: stateChange ' + devices + id + ' ' + JSON.stringify(state));
adapter.getState('Authorization.Token', function (err, state) {
if (!err) {adapter.log.debug("Tokenwert" + state.val);
var access_token = state.val ;
};
adapter.getState('Authorization.Refresh_Token', function (err, state) {
if (!err) {adapter.log.debug("Refresh_Tokenwert" + state.val);
var rrefresh_token = state.val ;
};
if ( !access_token || 0 === access_token.length) {}
else {
adapter.log.debug('Devices: ' + devices);
if (devices !== "GetDevices") {
id = id.split('.')[4];
adapter.log.debug('id' + id + 'Value');
switch (id)
{
case 'LightOn':
miele.NSetLightEnable(rrefresh_token, access_token,devices,function(err,data,atoken,rtoken){
if(err){adapter.log.error('NSetLightEnable: ' + err);}
if(!err){adapter.log.debug(devices + 'LightOn Set')}
});
adapter.log.debug('LightOn');
break;
case 'LightOff':
miele.NSetLightDisable(rrefresh_token, access_token,devices,function(err,data,atoken,rtoken){
if(err){adapter.log.error('NSetLightDisable: ' + err);}
if(!err){adapter.log.dbug(devices + 'LightOn Set')}
});
adapter.log.debug('LightOff');
break;
default:
adapter.getState('Devices.' + devices + '.ident.type.value_raw', function (err, state) {
if (!err) {adapter.log.debug('Device-typ:' + state.val);
var pAction = device.readProcessAction(parseFloat(state.val));
adapter.log.debug(pAction.toString());
adapter.log.debug('Info about change: ' + id);
if (pAction.includes(id)) {
adapter.log.info('Button:' + id);
miele.NSetProcessAction(rrefresh_token,access_token,id,devices,state.val,function(err,data,atoken,rtoken){
if(err){adapter.log.error('Function NSetProcessAction:' + err + ' ' + id)}
if(!err){adapter.log.debug(devices + id)}
});
}
}})
}
} else {
id = id.split('.')[3];
switch (id)
{
case 'GetDevices':
miele.NGetDevices(rrefresh_token, access_token,function(err,data,atoken,rtoken){
if(err){adapter.log.error('Function NGetDevices:' + err + ' ' + id)}
if(!err){GetDevices(data,'Devices')}
});
break;
}
}
}
});
});
});
/*
var mieleStates = {
1: 'STATE_OFF',
2: 'STATE_STAND_BY',
3: 'STATE_PROGRAMMED',
4: 'STATE_PROGRAMMED_WAITING_TO_START',
5: 'STATE_RUNNING',
6: 'STATE_PAUSE',
7: 'STATE_END_PROGRAMMED',
8: 'STATE_FAILURE',
9: 'STATE_PROGRAMME_INTERRUPTED',
10: 'STATE_IDLE',
11: 'STATE_RINSE_HOLD',
12: 'STATE_SERVICE',
13: 'STATE_SUPERFREEZING',
14: 'STATE_SUPERCOOLING',
15: 'STATE_SUPERHEATING',
144: 'STATE_DEFAULT',
145: 'STATE_LOCKED',
146: 'STATE_SUPERCOOLING_SUPERFREEZING'
};
*/