-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMM-SensorGateway.js
188 lines (159 loc) · 5.82 KB
/
MMM-SensorGateway.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
/* global Module */
/* Magic Mirror
* Module: MMM-SensorGateway
*
* By Matti Lehtinen
* MIT Licensed.
*/
Module.register("MMM-SensorGateway", {
// Override start method.
start: function () {
if (this.config.envsensors.length > 0) {
this.envsensors = new Array({
MAC: this.config.envsensors[0].MAC, location: this.config.envsensors[0].location,
pressure: NaN, temperature: NaN, humidity: NaN
});
for (var i = 1; i < this.config.envsensors.length; i++) {
this.envsensors.push({
MAC: this.config.envsensors[i].MAC, location: this.config.envsensors[i].location, temperature: this.config.envsensors[i].temperature,
pressure: NaN, temperature: NaN, humidity: NaN
});
}
}
this.gLocationText = "No data"
this.gState = 1,
this.gTemp = NaN,
this.gPressure = NaN,
this.gHumidity = NaN
},
// Define required translations.
getTranslations: function () {
return {
en: "translations/en.json",
fi: "translations/fi.json",
sv: "translations/sv.json",
};
},
sendInitNotification: function () {
//From module developer documentation:
//https://github.com/MichMich/MagicMirror/tree/develop/modules
//Note 2: The socket connection is established as soon as the module sends its first message using sendSocketNotification.
this.sendSocketNotification("SENSORGATEWAY_START", {});
},
// Override dom generator.
getDom: function () {
var wrapper = document.createElement("div");
wrapper.className = "thin small bright pre-line";
var places = document.createElement('table');
places.className = 'small';
var title = document.createElement('header');
title.innerHTML = this.translate("HOME");
title.className = 'bold medium bright';
wrapper.appendChild(title);
var TR_element = document.createElement('tr');
var locationIcon = document.createElement('th');
locationIcon.innerHTML = this.translate("LOCATION");
var team_logo_image = document.createElement('img');
team_logo_image.src = "modules/MMM-SensorGateway/images/home.png";
team_logo_image.width = 40;
team_logo_image.height = 40;
locationIcon.appendChild(team_logo_image);
TR_element.appendChild(locationIcon);
var temperatureIcon = document.createElement('th');
temperatureIcon.innerHTML = this.translate("TEMPERATURE");
var logoimage2 = document.createElement('img');
logoimage2.src = "modules/MMM-SensorGateway/images/temperature.png";
logoimage2.width = 40;
logoimage2.height = 40;
temperatureIcon.appendChild(logoimage2);
TR_element.appendChild(temperatureIcon);
var pressureIcon = document.createElement('th');
pressureIcon.innerHTML = this.translate("AIR_PRESSURE");
var image3 = document.createElement('img');
image3.src = "modules/MMM-SensorGateway/images/pressure.png";
image3.width = 40;
image3.height = 40;
pressureIcon.appendChild(image3);
TR_element.appendChild(pressureIcon);
var humidityIcon = document.createElement('th');
humidityIcon.innerHTML = this.translate("HUMIDITY");
var image4 = document.createElement('img');
image4.src = "modules/MMM-SensorGateway/images/humidity.png";
image4.width = 40;
image4.height = 40;
humidityIcon.appendChild(image4);
TR_element.appendChild(humidityIcon);
places.appendChild(TR_element);
for (var i = 0; i < this.envsensors.length; i++) {
var valueRow = document.createElement('tr');
var tmpLocation = document.createElement('td');
tmpLocation.innerHTML = this.envsensors[i].location;
valueRow.appendChild(tmpLocation);
var valTemperature2 = document.createElement('td');
valTemperature2.innerHTML = this.envsensors[i].temperature + " C";
var degreeLabel = "°";
if (config.units === "metric") {
valTemperature2.innerHTML = this.envsensors[i].temperature + degreeLabel + " C";
} else {
if (this.envsensors[i].temperature != NaN) {
var farenheit = this.envsensors[i].temperature * 9 / 5 + 32;
valTemperature2.innerHTML = farenheit.toFixed(2) + degreeLabel + " F";
} else {
valTemperature2.innerHTML = this.envsensors[i].temperature + "F";
}
}
valueRow.appendChild(valTemperature2);
var valPressure2 = document.createElement('td');
valPressure2.innerHTML = this.envsensors[i].pressure + " hPa";
valueRow.appendChild(valPressure2);
var valHumidity2 = document.createElement('td');
valHumidity2.innerHTML = this.envsensors[i].humidity + " %";
valueRow.appendChild(valHumidity2);
places.appendChild(valueRow);
// Log.log(this.envsensors[i].location + ' adding location');
}
wrapper.appendChild(places);
return wrapper;
},
// Override socket notification handler.
notificationReceived: function (notification, payload, sender) {
if (notification == 'MODULE_DOM_CREATED') {
//we are ready to receive sensor data
this.sendInitNotification();
}
},
// Override socket notification handler.
socketNotificationReceived: function (notification, payload) {
var obj = JSON.parse(payload);
var self = this;
if (notification === 'MOVESENSE_CONTROL_PACKET') {
if (this.config.controlsensor && this.config.controlsensor.MAC === obj.MAC) {
if (obj.state === 1) {
if (self.gState != 1) {
console.log("Movesense control - initiate state change to ON");
self.gState = 1;
this.sendSocketNotification("MOVESENSE_ON", {});
}
}
else if (obj.state === 2) {
if (self.gState != 2) {
console.log("Movesense control - initiate state change to OFF");
self.gState = 2;
this.sendSocketNotification("MOVESENSE_OFF", {});
}
}
}
} //control package
else if (notification === 'RUUVI_ENVIRONMENT_PACKET') {
for (var i = 0; i < this.envsensors.length; i++) {
var sensor = this.envsensors[i];
if (sensor.MAC === obj.MAC) {
sensor.temperature = obj.temperature;
sensor.pressure = obj.pressure;
sensor.humidity = obj.humidity;
self.updateDom();
}
}
}
}, //function
});