-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathapp.js
50 lines (45 loc) · 1.32 KB
/
app.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
/**
* Tool for reading data from D-Link DSP-W215 Home Smart Plug.
*
* Usage: enter your PIN code to LOGIN_PWD, change value of HNAP_URL according to your device settings.
*
* @type {exports|module.exports}
*/
var soapclient = require('./js/soapclient');
var fs = require('fs');
var OUTPUT_FILE = "result.txt";
var LOGIN_USER = "admin";
var LOGIN_PWD = "<PIN CODE>";
var HNAP_URL = "http://192.168.1.128/HNAP1";
var POLLING_INTERVAL = 60000;
soapclient.login(LOGIN_USER, LOGIN_PWD, HNAP_URL).done(function (status) {
if (!status) {
throw "Login failed!";
}
if (status != "success") {
throw "Login failed!";
}
start();
});
function start(){
soapclient.on().done(function (result){
console.log(result);
read();
})
};
function read() {
soapclient.consumption().done(function (power) {
soapclient.temperature().done(function (temperature) {
console.log(new Date().toLocaleString(), power, temperature);
save(power, temperature);
setTimeout(function () {
read();
}, POLLING_INTERVAL);
});
})
}
function save(power, temperature) {
fs.writeFile(OUTPUT_FILE, new Date().toLocaleString() + ";" + power + ";" + temperature + "\r\n", {flag: "a"}, function (err) {
if (err) throw err;
})
}