Skip to content

Commit

Permalink
new methods for turning on and off the smart pug
Browse files Browse the repository at this point in the history
code refactoring
  • Loading branch information
bikerp committed Jan 1, 2016
1 parent 1a3ca36 commit a0e6770
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 40 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@ Tested with hardware version B1 and firmware version 2.20.

Usage: fill your login credentials into app.js, modify device IP address.
Run: node app.js

Supported operations:
soapclient.consumption() - read current power consumption
soapclient.temperature() - read current temperature
soapclient.on() - turn on
soapclient.off() - turn off
20 changes: 15 additions & 5 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,30 @@ var fs = require('fs');

var OUTPUT_FILE = "result.txt";
var LOGIN_USER = "admin";
var LOGIN_PWD = "<PIN CODE>";
var LOGIN_PWD = "PIN CODE";
var HNAP_URL = "http://192.168.1.128/HNAP1";
var POLLING_INTERVAL = 10000;
var POLLING_INTERVAL = 60000;

soapclient.login(LOGIN_USER, LOGIN_PWD, HNAP_URL).done(function (status) {
if (!status) {
throw "Login failed!";
}
read();
if (status!="success") {
throw "Login failed!";
}
start();
});

function start(){
soapclient.on().done(function (result){
console.log(result);
read();
})
}

function read() {
soapclient.sendCommand("GetCurrentPowerConsumption", "CurrentConsumption", 1).done(function (power) {
soapclient.sendCommand("GetCurrentTemperature", "CurrentTemperature", 2).done(function (temperature) {
soapclient.consumption().done(function (power) {
soapclient.temperature().done(function (temperature) {
console.log(new Date().toLocaleString(), power, temperature);
save(power, temperature);
setTimeout(function () {
Expand Down
71 changes: 37 additions & 34 deletions js/soapclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,12 @@ exports.login = function (user, password, url) {
body: requestBody(HNAP_LOGIN_METHOD, loginRequest())
}).then(function (response) {
save_login_result(response.getBody(HNAP_BODY_ENCODING));
return processLogin(HNAP_LOGIN_METHOD);
return soapAction(HNAP_LOGIN_METHOD, "LoginResult", requestBody(HNAP_LOGIN_METHOD, loginParameters()));
}).catch(function (err) {
console.log("error:", err);
});
};

function processLogin(method) {
return request(HNAP_METHOD, HNAP_AUTH.URL,
{
headers: {
"Content-Type": "text/xml; charset=utf-8",
"SOAPAction": '"' + HNAP1_XMLNS + method + '"',
"HNAP_AUTH": getHnapAuth('"' + HNAP1_XMLNS + method + '"', HNAP_AUTH.PrivateKey),
"Cookie": "uid=" + HNAP_AUTH.Cookie
},
body: requestBody(method, loginParameters())
}).then(function (response) {
return response.statusCode;
}).catch(function (err) {
console.log("error:", err);
})
}
function save_login_result(body) {
var doc = new DOMParser().parseFromString(body);
HNAP_AUTH.Result = doc.getElementsByTagName(HNAP_LOGIN_METHOD + "Result").item(0).firstChild.nodeValue;
Expand All @@ -56,28 +40,34 @@ function save_login_result(body) {
}

function requestBody(method, parameters) {
var str = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
return "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<soap:Envelope " +
"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " +
"xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
"<soap:Body>" +
"<" + method + " xmlns=\"";

str += HNAP1_XMLNS;

str += "\">" +
"<" + method + " xmlns=\"" + HNAP1_XMLNS + "\">" +
parameters +
"</" + method + "></soap:Body></soap:Envelope>";
return str;
"</" + method + ">" +
"</soap:Body></soap:Envelope>";
}

function commandParameters(module) {
var param = "<ModuleID>" + module + "</ModuleID>";
return param;
function moduleParameters(module) {
return "<ModuleID>" + module + "</ModuleID>";
}

function controlParameters(module, status) {
return moduleParameters(module) +
"<NickName>Socket 1</NickName><Description>Socket 1</Description>" +
"<OPStatus>" + status + "</OPStatus><Controller>1</Controller>";

}

exports.sendCommand = function (method, responseElement, module) {
return soapAction(method, responseElement, requestBody(method, moduleParameters(module)));
};

function soapAction(method, responseElement, body) {
return request(HNAP_METHOD, HNAP_AUTH.URL,
{
headers: {
Expand All @@ -86,30 +76,43 @@ exports.sendCommand = function (method, responseElement, module) {
"HNAP_AUTH": getHnapAuth('"' + HNAP1_XMLNS + method + '"', HNAP_AUTH.PrivateKey),
"Cookie": "uid=" + HNAP_AUTH.Cookie
},
body: requestBody(method, commandParameters(module))
body: body
}).then(function (response) {
return readResponseValue(response.getBody(HNAP_BODY_ENCODING), responseElement);
}).catch(function (err) {
console.log("error:", err);
});
}

exports.on = function () {
return soapAction("SetSocketSettings", "SetSocketSettingsResult", requestBody("SetSocketSettings", controlParameters(1, true)));
};

exports.off = function () {
return soapAction("SetSocketSettings", "SetSocketSettingsResult", requestBody("SetSocketSettings", controlParameters(1, false)));
};

exports.consumption = function () {
return soapAction("GetCurrentPowerConsumption", "CurrentConsumption", requestBody("GetCurrentPowerConsumption", controlParameters(1, false)));
};

exports.temperature = function () {
return soapAction("GetCurrentTemperature", "CurrentTemperature", requestBody("GetCurrentTemperature", controlParameters(2, false)));
};

function loginRequest() {
var para = "<Action>request</Action>"
return "<Action>request</Action>"
+ "<Username>" + HNAP_AUTH.User + "</Username>"
+ "<LoginPassword></LoginPassword>"
+ "<Captcha></Captcha>";

return para;
}

function loginParameters() {
var login_pwd = md5.hex_hmac_md5(HNAP_AUTH.PrivateKey, HNAP_AUTH.Challenge);
var para = "<Action>login</Action>"
return "<Action>login</Action>"
+ "<Username>" + HNAP_AUTH.User + "</Username>"
+ "<LoginPassword>" + login_pwd.toUpperCase() + "</LoginPassword>"
+ "<Captcha></Captcha>";
return para;
}

function getHnapAuth(SoapAction, privateKey) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hnap",
"version": "1.0.0",
"version": "2.0.0",
"description": "Reads power consumption and temperature from D-Link DSP-W215",
"main": "app.js",
"dependencies": {
Expand Down

0 comments on commit a0e6770

Please sign in to comment.