Repository navigation
Expand file tree
/
Copy pathnode_helper.js
More file actions
105 lines (89 loc) · 2.63 KB
/
Copy pathnode_helper.js
File metadata and controls
105 lines (89 loc) · 2.63 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
'use strict';
/* Magic Mirror
* Module: MMM-FHEM
*
* By Benjamin Roesner http://benjaminroesner.com
* MIT Licensed.
*/
const NodeHelper = require('node_helper');
var request = require('request');
var _ = require('underscore');
module.exports = NodeHelper.create({
start: function() {
this.config = {};
},
buildFhemUrl: function (config) {
var deviceString = '';
config.devices.forEach(function(element, index, array) {
deviceString += element.deviceName + ',';
});
if (config.https) {
return 'https://' + config.host + ':' + config.port +
'/fhem?cmd=jsonlist2%20NAME=' + deviceString + '&XHR=1';
} else {
return 'http://' + config.host + ':' + config.port +
'/fhem?cmd=jsonlist2%20NAME=' + deviceString + '&XHR=1';
}
},
getFhemJson: function(config, callback) {
request({
url: this.buildFhemUrl(config),
json: true,
}, function(error, response, body) {
if (!error && response.statusCode == 200) {
// console.log(body);
callback(body);
}
});
},
/**
* use fhem alias as label if it is set
* @param {object} device fhem device object
* @return {string}
*/
getDeviceName: function (device) {
if (device.Attributes.alias) {
return device.Attributes.alias;
} else {
return device.Name;
}
},
getReadingsValue: function (readingsName, device) {
var values = [];
readingsName.forEach(function(element, index, array) {
var readingName = element;
if (device.Readings[readingName]) {
values.push(device.Readings[readingName].Value);
} else {
values.push('Reading not exist');
}
});
return values;
},
parseJson: function (json) {
var self = this;
var parsedJson = [];
var resultsArray = json.Results;
resultsArray.forEach(function(element, index, array) {
var device = {};
// save value of property 'name' an array
var readingsName = _.pluck(self.config.devices[index].deviceReadings, 'name');
device.name = self.getDeviceName(element);
device.values = self.getReadingsValue(readingsName, element);
parsedJson.push(device);
});
return parsedJson;
},
// Subclass socketNotificationReceived received.
socketNotificationReceived: function(notification, payload) {
if (notification === 'GETDATA') {
var self = this;
self.config = payload;
this.getFhemJson(this.config ,function(data) {
// console.log(data);
var structuredData = self.parseJson(data);
self.sendSocketNotification('DATARECEIVED', structuredData);
});
}
}
});