-
Notifications
You must be signed in to change notification settings - Fork 7
/
node_helper.js
139 lines (103 loc) · 4.38 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* Magic Mirror
* Module: MMM-Astrology
*
* By Cowboysdude //revision RDR
*
*/
const NodeHelper = require('node_helper');
const request = require('request');
const exec = require("child_process").exec;
const wget = require('wget');
module.exports = NodeHelper.create({
start: function() {
console.log("Starting module: " + this.name);
var config = null;
},
translation_text: "***",
thehoroscope: "",
getAstrology: function(url) {
self = this;
try{
request({
url: url,
method: 'GET'
}, (error, response, body) => {
if (!error && response.statusCode == 200) {
var result = JSON.parse(body);
if (this.config.translation_languages[0] != "" && this.config.translation_languages[0] != null){
if(self.thehoroscope != result.horoscope){
self.thehoroscope = result.horoscope;
var cmd = "trans "+'-b'+" "+":"+this.config.translation_languages[0]+' "'+result.horoscope+'"';
//thereturnvalue = self.execShellCommand(cmd);
self.execShell(cmd,result,function(thereturnvalue,theresult){
//console.log(self.name+" inside thereturnvalue == "+thereturnvalue );
theresult = result;
if(thereturnvalue==""){
//means too many requests
}else{
theresult.horoscope = thereturnvalue;
}
self.sendSocketNotification('ASTRO_RESULTS', theresult);
});
}
}else{
self.sendSocketNotification('ASTRO_RESULTS', result);
}
}
});
} catch(error) {
}
},
execShell: function(cmd,theresult,cb){
exec(cmd, (error, stdout, stderr) => {
if (error) {
console.log(error);
}
thereturnvalue = stdout? stdout : stderr;
if(typeof thereturnvalue != 'undefined'){
cb(thereturnvalue,theresult); // If thereturnvalue is already define, I don't wait.
} else {
callback = cb;
}
});
},
execShellCommand: function(cmd) {
return new Promise((resolve, reject) => {
exec(cmd, (error, stdout, stderr) => {
if (error) {
console.log(error);
}
resolve(stdout? stdout : stderr);
});
});
},
//Subclass socketNotificationReceived received.
socketNotificationReceived: function(notification, payload) {
if (notification === 'START_ASTROLOGY') {
this.config = payload;
this.sendSocketNotification('ASTRO_STARTED');
return
}
if (notification === 'GET_ASTROLOGY') {
this.getAstrology(payload);
return
}
if (notification === 'GET_ASTROLOGY_TRANSLATION') {
//console.log(this.name+" payload[0] "+payload[0]+":"+" payload[1] "+ payload[1]);
this.getTranslation(payload[0],payload[1]);
console.log(this.name+" thetranslationIs "+payload[0]+":"+this.translation_text);
if (this.translation_text==''){
this,translation_text = payload[1];
}
this.sendSocketNotification('ASTRO_RESULTS_TRANSLATION',this.translation_text);
return
}
},
sleep: function(milliseconds) {
const date = Date.now();
let currentDate = null;
do {
currentDate = Date.now();
} while (currentDate - date < milliseconds);
}
});