Skip to content

Commit 891e599

Browse files
fixed additional parameters like timeout beeing ignored during call of the scripts;
the commands now will be called with working dir set to the "MMM-CommandToNotification/scripts"; added a option to send a custom payload instead of the output of the script; changed version to 0.0.3
1 parent 15571d5 commit 891e599

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

README.md

+17-2
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,23 @@ Add the following code to your ~/MagicMirror/config/config.js:
4242
| script | Either a absolute path or the path of a script within "scripts" directory | true | String | null |
4343
| args | Arguments which should be passed to the script | false | String | "" |
4444
| timeout | Should the script be killed if it does not return within a specific amount of sedonds? | false | Integer | infinity |
45-
| notifications | A array containing names of the notifications to send if script returns output. If not present the script gets called but no notification will be send | false | Array | [] |
45+
| notifications | A array containing names of the notifications to send if script returns output. If not present the script gets called but no notification will be send. If you want to override the payload instead of using the output please look at the notification section. | false | Array | [] |
4646
| conditions | A map containing conditions that need to match to send the notifications | false | Map | null |
4747

48+
### Notifications
49+
50+
The notifications array contains all notifications that should be send if a command is called (and the conditions matched).
51+
There may be situations where you want send a notification with a specific payload instead of the output of the script. You can do so if you specify a array instead of the string identifiying the notification.
52+
Lets see the following example:
53+
54+
```json
55+
notifications: [
56+
["TEST1","MY_NEW_PAYLOAD"],
57+
"TEST2",
58+
],
59+
```
60+
61+
In this example the notification "TEST1" will have "MY_NEW_PAYLOAD" as output while "TEST2" contains the output of the command.
4862

4963
### Conditions
5064

@@ -67,7 +81,7 @@ Add the following example to produce the following result:
6781
* the script "scripts/randomNumberJson.js" will be called every fourth iteration because a three skips are configured
6882
* the script calculates a random number between -50 and 20 and produces a json object containing two values ("integer" and "float"). The float value is the random number the integer value the random number rounded as integer.
6983
* the timeout of the script is set to 10 seconds
70-
* the result of the script (JSON object as string) will be send as payload of notification TEST3
84+
* the result of the script (JSON object as string) will be send as payload of notification TEST3 while TEST4 will be send with payload "true"
7185

7286
```json5
7387
{
@@ -92,6 +106,7 @@ Add the following example to produce the following result:
92106
timeout: 10,
93107
notifications: [
94108
"TEST3",
109+
["TEST4", true]
95110
],
96111
}
97112
]

node_helper.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,10 @@ module.exports = NodeHelper.create({
9696
try {
9797
if(typeof curCmdConfig["timeout"] !== "undefined"){
9898
// console.log(self.name+": Calling "+fullScriptPath+" with timeout of "+curCmdConfig["timeout"])
99-
output = execSync(curCommand, encoding="utf8", timeout=curCmdConfig["timeout"]).toString()
99+
output = execSync(curCommand, {encoding:"utf8", timeout:curCmdConfig["timeout"], cwd:scriptsDir}).toString()
100100
} else {
101101
// console.log(self.name+": Calling "+fullScriptPath+" without timeout")
102-
output = execSync(curCommand, encoding="utf8").toString()
102+
output = execSync(curCommand, {encoding:"utf8", cwd:scriptsDir}).toString()
103103
}
104104
}
105105
catch (error) {
@@ -111,7 +111,15 @@ module.exports = NodeHelper.create({
111111
if (typeof curNotifications !== "undefined"){
112112
if (self.validateConditions(curCmdConfig, output, returnCode)){
113113
for (let curNotiIdx = 0; curNotiIdx < curNotifications.length; curNotiIdx++){
114-
self.sendSocketNotification("RESULT_"+curNotifications[curNotiIdx], output)
114+
if (Array.isArray(curNotifications[curNotiIdx])){
115+
if (curNotifications[curNotiIdx].length > 1){
116+
self.sendSocketNotification("RESULT_"+curNotifications[curNotiIdx][0], curNotifications[curNotiIdx][1])
117+
} else {
118+
self.sendSocketNotification("RESULT_"+curNotifications[curNotiIdx][0], output)
119+
}
120+
} else {
121+
self.sendSocketNotification("RESULT_"+curNotifications[curNotiIdx], output)
122+
}
115123
}
116124
}
117125
} else {

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "MMM-CommandToNotification",
3-
"version": "0.0.2",
3+
"version": "0.0.3",
44
"description": "",
55
"main": "MMM-CommandToNotification",
66
"dependencies": {

0 commit comments

Comments
 (0)