-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_helper.js
91 lines (81 loc) · 3.86 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
/* MagicMirror²
* Module: Serial-Notifications
*
* By Tom Hirschberger
* MIT Licensed.
*/
const NodeHelper = require('node_helper')
const { ReadlineParser } = require('@serialport/parser-readline')
const { SerialPort } = require('serialport')
module.exports = NodeHelper.create({
start: function () {
this.started = false
this.currentProfile = ''
this.currentProfilePattern = new RegExp('.*')
},
socketNotificationReceived: function (notification, payload) {
const self = this
if (notification === 'CONFIG' && self.started === false) {
self.config = payload
for (var curUsbDev in self.config.devices){
(function(innerDev){
console.log("Creating port for usb dev: "+innerDev)
let curPort = new SerialPort({ path: innerDev, baudRate: 9600 })
curParser = curPort.pipe(new ReadlineParser({ delimiter: '\n' }))
curParser.on("data", function(data){
self.sendAllNotifications(innerDev, data)
});
})(curUsbDev)
}
self.started = true
} else if (notification === 'CHANGED_PROFILE'){
if(typeof payload.to !== 'undefined'){
self.currentProfile = payload.to
self.currentProfilePattern = new RegExp('\\b'+payload.to+'\\b')
}
}
},
sendAllNotifications: function (curDev, curData) {
const self = this
console.log("("+curDev+") Received: "+curData)
for(var curExptMessage in self.config.devices[curDev].messages){
if(curData.indexOf(curExptMessage) == 0){
for(var curNotification in self.config.devices[curDev].messages[curExptMessage]){
// console.log(self.currentProfilePattern.toString())
if(typeof self.config.devices[curDev].messages[curExptMessage][curNotification].profiles === 'undefined' ||
self.currentProfilePattern.test(self.config.devices[curDev].messages[curExptMessage][curNotification].profiles)){
if(typeof self.config.devices[curDev].messages[curExptMessage][curNotification].notification !== 'undefined'){
if(typeof self.config.devices[curDev].messages[curExptMessage][curNotification].payload === 'undefined'){
var payload = {}
} else {
var payload = self.config.devices[curDev].messages[curExptMessage][curNotification].payload
if(typeof self.config.devices[curDev].messages[curExptMessage][curNotification].payloadReplacement !== 'undefined'){
var payloadReplacementString = self.config.devices[curDev].messages[curExptMessage][curNotification].payloadReplacement
if(typeof self.config.devices[curDev].messages[curExptMessage][curNotification].replacementPrefix !== 'undefined'){
replacementPrefix = self.config.devices[curDev].messages[curExptMessage][curNotification].replacementPrefix
} else {
replacementPrefix = ""
}
var curReplacementValue = curData.substring(replacementPrefix.length-1,curData.length-1)
var newPayload = {}
for(var curPayloadKey in payload){
var curPayloadValue = payload[curPayloadKey]
if(typeof curPayloadValue === 'string'){
newPayload[curPayloadKey] = curPayloadValue.replace(payloadReplacementString,curReplacementValue)
} else {
newPayload[curPayloadKey] = curPayloadValue
}
}
payload = newPayload
}
}
self.sendSocketNotification(self.config.devices[curDev].messages[curExptMessage][curNotification].notification,payload)
}
} else {
console.log("Skipped because profile string is present but does not match")
}
}
}
}
},
})