-
Notifications
You must be signed in to change notification settings - Fork 4
/
node_helper.js
100 lines (79 loc) · 2.35 KB
/
node_helper.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
/* Magic Mirror
* Node Helper: MMM-temp-ds18b20
*
* npm : https://www.npmjs.com/package/ds18x20
* MIT Licensed.
*/
var NodeHelper = require("node_helper");
var Sensor = require('ds18x20');
var util = require('util');
module.exports = NodeHelper.create({
consolePrefix: 'MMM-temp-ds18b20 : ',
start: function function_name () {
"use strict";
this.initialized = false;
},
socketNotificationReceived: function(notification, payload){
"use strict";
var self = this;
// Get configuration
if(notification === 'DS18B20-INITIALIZE'){
this.config = payload;
}
if(!this.initialized){
// Check driver loaded (modprobe w1-gpio && modprobe w1-therm)
var isLoaded = Sensor.isDriverLoaded();
// If not loaded : error notification
if(!isLoaded){
console.error(self.consolePrefix + 'Error : DRIVER-NOT-LOADED');
self.sendSocketNotification('DS18B20-ERROR', 'DRIVER-NOT-LOADED');
}
else{
// If loaded : check sensor values
setInterval(function() {
self.sendTemperature();
}, self.config.refreshInterval * 1000);
}
this.initialized = true;
}
},
sendTemperature: function() {
"use strict";
var self = this;
var listSensors = Sensor.getAll();
var value;
// For each sensor referenced in config
for(var i in this.config.sensors){
value = null;
// If sensor id is plugged : ok : get value
if(listSensors[this.config.sensors[i].id]){
value = Sensor.get(this.config.sensors[i].id);
}
else{
// Sensor not found : maybe bad id
console.error(self.consolePrefix + 'Error: sensor ' + this.config.sensors[i].id + ' not found');
}
this.config.sensors[i].value = self.formatTempValue(value);
}
self.sendSocketNotification('DS18B20-VALUES', this.config.sensors);
},
formatTempValue: function(value){
"use strict";
var self = this;
switch(true){
case (value === null):
return 'N/A';
// Kelvin
case (self.config.units === 'default'):
return parseFloat(value + 273.15).toFixed(1) + " K";
// Fahrenheit
case (self.config.units === 'imperial'):
return parseFloat((value * 1.8) + 32).toFixed(1) + "°";
// Celsius
case (self.config.units === 'metric'):
return parseFloat(value).toFixed(1) + "°";
default :
return parseFloat(value).toFixed(1);
}
}
});