forked from Z-Wave-Me/home-automation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
updateBackendConfig.js
413 lines (367 loc) · 14.4 KB
/
updateBackendConfig.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
402
403
404
405
406
407
408
409
410
411
412
413
// This script transforms old formats to new
(function () {
var config = loadObject("config.json");
oldConfigJSON = JSON.stringify(config);
if (config) {
// Change profiles data
if (config.hasOwnProperty('profiles') && Array.isArray(config.profiles) && config.profiles.length > 0) {
config.profiles.forEach(function (profile) {
if (profile.hasOwnProperty('groups')) {
delete profile.groups;
}
if (profile.hasOwnProperty('active')) {
delete profile.active;
}
if (Array.isArray(profile.positions)) {
profile.positions = profile.positions.filter(function (position) {
return typeof position === 'string';
});
} else {
profile.positions = [];
}
});
} else {
// default profile
config.profiles = [{
id: 1,
role: 1,
login: 'admin',
password: 'admin',
name: 'Administrator',
lang:'en',
color:'#dddddd',
dashboard: [],
interval: 2000,
rooms:[0],
expert_view: false,
hide_all_device_events: false,
hide_system_events: false,
hide_single_device_events: []
},
{
id: 2,
role: 3,
login: 'local',
password: 'local',
name: 'Local User',
lang:'en',
color:'#dddddd',
dashboard: [],
interval: 2000,
rooms:[0],
expert_view: false,
hide_all_device_events: false,
hide_system_events: false,
hide_single_device_events: []
}];
}
// add global location if not present
if (config.hasOwnProperty('locations') && Array.isArray(config.locations) && config.locations.filter(function (location) { return location.id === 0;}).length === 0) {
config.locations.push({
id : 0,
title: "globalRoom",
user_img: "",
default_img: "",
img_type: ""
});
}
// Change instances data
if (config.hasOwnProperty('instances')) {
if (config.instances.length > 0) {
config.instances.forEach(function (instance) {
// move title and description params
if (instance.params) {
if (instance.params.hasOwnProperty('title') || instance.params.hasOwnProperty('description')) {
instance.title = instance.params.title;
instance.description = instance.params.description;
delete instance.params.title;
delete instance.params.description;
}
// move status
if (instance.params.hasOwnProperty('status')) {
instance.active = instance.params.status === 'enable';
delete instance.params.status;
} else if (!instance.hasOwnProperty('active')) {
instance.active = true;
}
}
/*
// update/add instance title/module/state to show it correct in instances overview
if(instance.moduleId && (!instance.state || !instance.title || !instance.module)){
var moduleMeta,
moduleLang,
modulesMetaPath = 'modules/'+ instance.moduleId + '/module.json',
modulesLangPath = 'modules/'+ instance.moduleId + '/lang/en.json',
userModulesMetaPath = 'userModules/' + instance.moduleId + '/module.json',
userModulesLangPath = 'userModules/'+ instance.moduleId + '/lang/en.json';
try{
// load meta data from module.json - returns null if error
moduleMeta = !!fs.loadJSON(modulesMetaPath)? fs.loadJSON(modulesMetaPath) : fs.loadJSON(userModulesMetaPath);
// load en.json - returns null if error
moduleLang = !!fs.loadJSON(modulesLangPath)? fs.loadJSON(modulesLangPath) : fs.loadJSON(userModulesLangPath);
if(!!moduleMeta) {
// replace language keys
if (!!moduleLang) {
metaStringify = JSON.stringify(moduleMeta);
Object.keys(moduleLang).forEach(function (key) {
var regExp = new RegExp('__' + key + '__', 'g');
if (moduleLang[key]) {
metaStringify = metaStringify.replace(regExp, moduleLang[key]);
}
});
moduleMeta = JSON.parse(metaStringify);
}
// update title / module / state
// title ... instance title
// module ... module title, usually added by creating new instance - is not the same as className or moduleId
// state ... could be 'hidden', 'camera' or null - will be used to hide core modules or to mark as camera module e.g.
instance.state = moduleMeta.state? moduleMeta.state : null;
instance.title = !instance.title? moduleMeta.defaults.title : instance.title;
instance.module = !instance.module? moduleMeta.defaults.title : instance.module;
}
}catch (e){
console.log('Could not set state/title/module value of instance ' + instance.id + '. ERROR: ' + e);
}
}
*/
// delete userView
if (instance.hasOwnProperty('userView')) {
delete instance.userView;
}
// delete state
if (instance.hasOwnProperty('state')) {
delete instance.state;
}
// delete module
if (instance.hasOwnProperty('module')) {
delete instance.module;
}
});
}
// Remove Z-Wave Gate, Z-Wave Dead detection and add Z-Wave Binding
if (config.instances.length > 0) {
var toDelete=[];
config.instances.forEach(function(el, indx) {
if (el.moduleId && (el.moduleId === "ZWaveDeadDetection" || el.moduleId === "ZWaveGate")) {
console.log("Removing module " + el.moduleId);
toDelete.push(indx)
}
});
toDelete.reverse().forEach(function(el) {
config.instances.splice(el, 1)
});
var maxInstanceId = Math.max.apply(null, config.instances.map(function(el) {
return el.id;
}));
if (toDelete.length) {
console.log("Adding module ZWave");
config.instances.push({
"params": {
"name": "zway",
"port": "/dev/ttyAMA0",
"enableAPI": true,
"publicAPI": false,
"createVDev": true,
"config": "config",
"translations": "translations",
"ZDDX": "ZDDX",
},
"active": true,
"moduleId": "ZWave",
"module":"Z-Wave Network Access",
"state":"hidden",
"title": "Z-Wave Network Access",
"description": "Allows accessing Z-Wave devices from attached Z-Wave transceiver.\n(Added by backend updater script)",
"id": maxInstanceId + 1
});
}
}
}
// Add permanently_hidden, h, visibility, hasHistory properties
Object.keys(config.vdevInfo).forEach(function(id) {
if (!config.vdevInfo[id].hasOwnProperty('permanently_hidden')) {
console.log("Adding to VDev " + id + " new property permanently_hidden");
config.vdevInfo[id].permanently_hidden = false;
}
if (!config.vdevInfo[id].hasOwnProperty('h')) {
var hashCode = function(str) {
var hash = 0, i, chr, len;
if (this.length === 0) {
return hash;
}
for (i = 0, len = str.length; i < len; i++) {
chr = str.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash = hash & hash; // Convert to 32bit integer
}
return hash;
};
config.vdevInfo[id].h = hashCode(id);
}
if (!config.vdevInfo[id].hasOwnProperty('hasHistory')) {
config.vdevInfo[id].hasHistory = false;
}
if (!config.vdevInfo[id].hasOwnProperty('visibility')) {
config.vdevInfo[id].visibility = true;
}
});
// Change IDs to new notation
{
function getNewID(id) {
var pattern1 = /^ZWayVDev_([0-9]+(:[0-9]+)+)$/,
pattern2 = /^Remote_[0-9]+_([0-9]+(:[0-9]+)+)$/;
if (id.match(pattern1)) {
return id.replace(pattern1, "ZWayVDev_zway_$1").replace(/:/g, "-");
} else if (id.match(pattern2)) {
return id.replace(pattern2, "ZWayVDev_zway_Remote_$1").replace(/:/g, "-");
} else {
return id;
}
}
// Update IDs of devices created by SwitchControlGenerator and ZWaveGate
Object.keys(config.vdevInfo).forEach(function(id) {
var _id = getNewID(id);
if (id !== _id) {
console.log("Changing VDev ID from " + id + " to " + _id);
config.vdevInfo[_id] = config.vdevInfo[id];
delete config.vdevInfo[id];
}
});
// Update IDs in profiles
config.profiles && config.profiles.forEach(function(profile) {
profile.positions && profile.positions.forEach(function(id, index) {
var _id = getNewID(id);
if (id !== _id) {
console.log("Changing widget ID from " + id + " to " + _id);
profile.positions[index] = _id;
}
});
});
// Update IDs in modules params
function fixArray(arr) {
arr.forEach(function(element, index) {
if (typeof element === "string") {
if (element != getNewID(element)) {
console.log("Changing ID in params (array) from " + element + " to " + getNewID(element));
arr[index] = getNewID(element);
}
} else if (typeof element === "object" && element && element.constructor && element.constructor === Array) {
fixArray(element);
} else if (typeof element === "object" && element) {
fixObject(element);
}
});
}
function fixObject(obj) {
for (var key in obj) {
if (typeof obj[key] === "string") {
if (obj[key] != getNewID(obj[key])) {
console.log("Changing ID in params (object) from " + obj[key] + " to " + getNewID(obj[key]));
obj[key] = getNewID(obj[key]);
}
} else if (typeof obj[key] === "object" && obj[key] && obj[key].constructor && obj[key].constructor === Array) {
fixArray(obj[key]);
} else if (typeof obj[key] === "object" && obj[key]) {
fixObject(obj[key]);
}
}
}
for (var indx in config.instances) {
fixObject(config.instances[indx].params);
}
}
// Transform profile to user profile
{
var counter = 0;
config.profiles && config.profiles.forEach(function(profile) {
if (!profile.login) {
profile.login = "admin" + (counter++ ? counter.toString(10) : "");
profile.password = "admin";
profile.role = 1;
profile.lang = "en";
profile.color = "#dddddd";
profile.default_ui = 1;
profile.dashboard = profile.positions;
profile.interval = 2000;
profile.rooms = [0];
expert_view: false,
profile.hide_all_device_events = false;
profile.hide_system_events = false;
profile.hide_single_device_events = [];
delete profile.description;
delete profile.widgets;
delete profile.positions;
}
// delete profile.sid
if(profile.sid){
delete profile.sid;
}
// change MD5 hashed passwords back to string
// replace it with profile.login or 'admin' as fallback
// affects versions below rc39
if(profile.password && /^[a-f0-9]{32}$/.test(profile.password)){
profile.password = profile.login? profile.login : 'admin';
}
// add room 0 if no rooms exists
if(!profile.rooms){
profile.rooms = [0];
}
// transform room if it is no array
if(profile.rooms && !Array.isArray(profile.rooms)){
profile.rooms = !isNaN(profile.rooms) && profile.rooms % 1 === 0? [profile.rooms] : [0];
}
// add room 0 if rooms exists but room 0 is missing
if(profile.rooms && Array.isArray(profile.rooms)){
if(profile.rooms.indexOf(0) === -1){
profile.rooms.push(0);
}
}
// transform positions into dashboard
if(Array.isArray(profile.positions)){
var unique = function (array) {
var a = array.concat();
for(var i=0; i<a.length; ++i) {
for(var j=i+1; j<a.length; ++j) {
if(a[i] === a[j])
a.splice(j--, 1);
}
}
return a;
};
profile.dashboard = profile.dashboard? unique(profile.dashboard.concat(profile.positions)) : (!profile.dashboard ? profile.positions : []);
delete profile.positions;
}
if(!profile.expert_view && (profile.role === 1 || profile.role === 3)){
profile.expert_view = false;
}
});
// add local user if he not exists
if (config.profiles && config.profiles.filter(function(profile){ return profile.login === 'local';}).length === 0) {
config.profiles.push({
id: config.profiles.length + 1,
role: 3,
login: 'local',
password: 'local',
name: 'Local User',
lang:'en',
color:'#dddddd',
dashboard: [],
interval: 2000,
rooms:[0],
expert_view: false,
hide_all_device_events: false,
hide_system_events: false,
hide_single_device_events: []
});
}
// Save changes
if (oldConfigJSON !== JSON.stringify(config)) { // do we need to update the config?
try {
saveObject("config.json", config);
} catch (e) {
console.log("Error: can not write back config.json to storage: ", e);
}
}
}
}
})();