-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathnode_helper.js
executable file
·45 lines (41 loc) · 1.12 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
"use strict";
/* Magic Mirror
* Module: MMM-DHT-Sensor
*
* By Ricardo Gonzalez http://www.github.com/ryck/MMM-DHT-Sensor
* MIT Licensed.
*/
const NodeHelper = require("node_helper");
const sensor = require("node-dht-sensor");
module.exports = NodeHelper.create({
start: function () {
console.log("MMM-DHT-Sensor helper started ...");
},
/**
* readSensor()
* Requests sensor data.
*/
readSensor: function (sensorPin, sensorType) {
var self = this;
sensor.read(sensorType, sensorPin, function (err, temperature, humidity) {
if (!err) {
self.sendSocketNotification("SENSOR_DATA", {
temperature: temperature.toFixed(2),
humidity: humidity.toFixed(2),
});
} else {
self.sendSocketNotification("SENSOR_DATA", {
temperature: null,
humidity: null,
});
console.log(err);
}
});
},
//Subclass socketNotificationReceived received.
socketNotificationReceived: function (notification, payload) {
if (notification === "GET_SENSOR_DATA") {
this.readSensor(payload.sensorPin, payload.sensorType);
}
},
});