-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
91 lines (76 loc) · 2.25 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
'use strict';
/* Magic Mirror
* Module: MMM-qnapDownloadStation
*
* By Stuart McNally
* MIT Licensed.
*/
const NodeHelper = require('node_helper');
var request = require('request');
var U = require("./getSid.js");
module.exports = NodeHelper.create({
start: function() {
this.started = false;
this.config = null;
},
getData: function() {
var self = this;
var qnap = this.config.qnapServer;
var lUrl = this.config.loginUrl;
var qUrl = this.config.queryUrl;
var sessionID;
var fromNo = this.config.fromNo;
var limit = this.config.limit;
var type = this.config.type;
var status = this.config.status;
var username = this.config.username;
var password = U.ezEncode(U.utf16to8(this.config.password));
request({
url: qnap + lUrl,
method: 'POST',
formData: {
user: username,
pass: password
},
}, function (error, response, body) {
if (!error && response.statusCode == 200 && body.error != 1) {
var responseJson = JSON.parse(body);
sessionID = responseJson.sid;
}
else {
self.sendSocketNotification("ERROR", body);
}
request({
url: qnap + qUrl,
method: 'POST',
formData: {
from: fromNo,
limit: limit,
type: type,
status: status,
sid: sessionID
},
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
self.sendSocketNotification("DATA", body);
}
else if (response.statusCode == 404) {
self.sendSocketNotification("ERROR404", body);
}
else {
self.sendSocketNotification("ERROR", body);
}
})
});
setTimeout(function() { self.getData(); }, this.config.refreshInterval);
},
socketNotificationReceived: function(notification, payload) {
var self = this;
if (notification === 'CONFIG' && self.started == false) {
self.config = payload;
self.sendSocketNotification("STARTED", true);
self.getData();
self.started = true;
}
}
});