-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathnode_helper.js
70 lines (63 loc) · 2.22 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
/**
* Magic Mirror
* Node Helper: MMM-LocalTemperature
*
* By David Dearden
* MIT Licensed.
*/
/**
* Load resources required by this module.
*/
var NodeHelper = require("node_helper");
const exec = require("child_process").exec;
/**
* Use NodeHelper to create a module.
*/
module.exports = NodeHelper.create({
/**
* The minimum version of magic mirror that is required for this module to run.
*/
requiresVersion: "2.2.1",
/**
* Override the start function to run when the module is started up.
* Used to provide initialization information to the console.
*/
start: function () {
var self = this;
console.log(self.name + ": module loaded! Path: " + this.path);
},
/**
* Override the socketNotificationReceived function to handle notifications sent from the client script.
*
* @param notification (string) The type of notification sent
* @param payload (any) The data sent with the notification
*/
socketNotificationReceived: function(notification, payload) {
var self = this;
if (payload.developerMode) { console.log(self.name + ': Socket Notification Received: "' + notification + '".'); }
if (notification === "GET_DATA") {
self.getSensorData(payload);
} else if (notification === "INIT") {
self.sendSocketNotification("LOG", { original: payload, message: ("INIT received from: " + payload.instanceID + "."), messageType: "dev" } );
self.sendSocketNotification("LOG", { original: payload, message: ("node_helper.js loaded successfully."), messageType: "dev" } );
}
},
/**
* The getSensorData queries the sensor for data usin the provided script
*
* @param payload (object) Contains the data required for querying the sensor
*/
getSensorData: function(payload) {
const self = this;
const sudo = payload.useSudo ? "sudo " : '';
exec(sudo + payload.scriptPath + " " + payload.sensorPin + " -m j -a 3", {}, function(error, stdout, stderr){
var result;
if (!error) {
result = { original: payload, isSuccessful: true, data: JSON.parse(stdout) };
} else {
result = { original: payload, isSuccessful: false, data: stderr, error: error };
}
if (typeof payload.notification === "string") { self.sendSocketNotification(payload.notification, result); }
});
},
});