I am experimenting with ewelink API, but I am totally new at node.js and I am proceeding by copy, past, try, fail,....
It also looks like not so many examples are available around, so I have to start from scratch, using as a basis just this basic script (which probably is online erroneously, given that it shows credentials in plain text...).
Here follows the complete script I prepared; I can login, I can retrieve the list of devices, but script fails on retrieving data of a specific device.
I experimented with this and this login method, but I cannot understand which one I should actually use.
My devices have standard firmware and configuration "out of the box", I just connected them to my wifi, and they work fine.
You can access my script using your ewelink credentials:
https://3u6vx.sse.codesandbox.io/index.html?user=xxx&pass=yyy&bypass=false
Or you can use the "default public" credentials:
https://3u6vx.sse.codesandbox.io/index.html?bypass=true
var http = require("http");
const ewelink = require("ewelink-api");
const url = require("url");
var devices;
var connection;
var newConnection;
var auth;
var onlineDevice;
//var device;
console.log("====================");
console.log(Date());
console.log("====================");
//create a server object:
http
.createServer(
function (req, res) {
res.write(timestampLC() + " - Ewelink - " + Date() + "\n");
const queryObject = url.parse(req.url, true).query;
console.log(timestampLC() + " - Executing function inside Server...");
eweRetrieve(res, queryObject);
} // function in createserver
) // createserver
.listen(8080); //the server object listens on port 8080
function printOfflineDevice(res, device) {
res.write(timestampLC() + " - Name: " + device.name + "\n");
res.write(timestampLC() + " - Type: " + device.type + "\n");
res.write(timestampLC() + " - Brand name:" + device.brandName + "\n");
res.write(timestampLC() + " - Model: " + device.productModel + "\n");
res.write(timestampLC() + " - Url: " + device.deviceUrl + "\n");
}
////////////////////
async function eweRetrieve(res, queryObject) {
res.write(timestampLC() + " - Attempting connection of user ");
console.log(timestampLC() + " - Attempting user connection...");
if (queryObject.bypass === true) {
res.write("tugsbayar.g@gmail.com'\n");
try {
res.write(timestampLC() + " - Trying...\n");
connection = new ewelink({
email: "tugsbayar.g@gmail.com",
password: "mdk06tgs6",
region: "as"
});
res.write(timestampLC() + " - Ewelink - user DEBUG connected\n");
} catch {
res.write(
timestampLC() + " - Fail 001 - no connection available for DEBUG user\n"
);
return 1;
}
} else {
res.write("'" + queryObject.user + "'\n");
try {
res.write(timestampLC() + " - Trying...\n");
connection = new ewelink({
email: queryObject.user,
password: queryObject.pass
//region: "it"
});
res.write(
timestampLC() +
" - >>> Initial login SUCCESS: user " +
connection.email +
" connected\n"
);
res.write(timestampLC() + " - ID: " + connection.APP_ID + "\n");
res.write(timestampLC() + " - SEC: " + connection.APP_SECRET + "\n");
//console.log(timestampLC() + "*** connection data ****", connection);
} catch {
res.write(
timestampLC() + " - Fail 002 - no connecion available for real user\n"
);
return 2;
}
//credentialsLogin(res, connection);
try {
console.log(timestampLC() +" - Credentials...\n");
res.write(timestampLC() +" - Credentials...");
auth = await connection.getCredentials();
res.write("SUCCESS! <<<<<<<<<<\n");
console.log("access token: ", auth.at);
console.log("api key: ", auth.user.apikey);
console.log("region: ", auth.region);
} catch {
res.write(timestampLC() + "***ERROR*** - cannot login\n");
console.log(timestampLC() + "***ERROR*** - cannot login\n");
}
}
//listAllDevices(res, connection);
/* get all devices */
try {
res.write(timestampLC() + " - Connected. Reading devices list...\n");
devices = await connection.getDevices();
} catch {
res.write(timestampLC() + " - Fail 003 - no devices\n");
console.log(timestampLC() + " - No devices found.");
return 3;
}
//console.log("=== Begin devices list ===");
//console.log(devices);
//console.log("=== End devices list ===");
res.write(timestampLC() + " - Found n." + devices.length + " devices\n\n");
console.log(timestampLC() + " - Found n. " + devices.length + " devices\n\n");
console.log(timestampLC() + " - Reading info...");
/* get specific device info */
for (var x = 0; x < devices.length; x++) {
res.write(timestampLC() + " - Device n. " + (x + 1) + "\n");
var element = devices[x];
res.write(timestampLC() + " - Id: " + element.deviceid + "\n");
res.write(timestampLC() + " - Online: " + element.online + "\n");
// console.log(element);
if (element.online === true) {
res.write(timestampLC() + " - Device '" +
element.name +
"' is online, fetching data...\n" );
try {
console.log("CREDENTIALS:\n", auth.at);
console.log(auth.region);
newConnection = new ewelink({
at: auth.at,
region: auth.region
});
console.log(timestampLC() + ">>>>>>>>>>>New connection ok! >>>>>>>>>>>>> Scarico dati online di", element.name , "\n")
} catch {
res.write(timestampLC() + " - *****ERROR: cannot read*****\n");
console.log(
timestampLC() +
"!!!!!!!!!!!!! NON RIUSCITO per " +
element.name +
" !!!!!!!!!!!!!!!"
);
}
try {
console.log("provo.");
/*var deviceConnection = new ewelink({
at: auth.at,
region: auth.region
}); */
onlineDevice = await connection.getDevice(element.id);
res.write(timestampLC() + " - DATA:\n");
console.log(timestampLC() + "***********************\n", onlineDevice , "<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
} catch {
res.write(timestampLC() + " - ***** ONLINE ERROR: cannot retrieve *****\n");
console.log(
timestampLC() +
"!!!!!!!!!!!!! Impossibile scaricare dati per " +
element.name +
" !!!!!!!!!!!!!!!"
);
}
//printDevice(res, device);
} else {
printOfflineDevice(res, element);
}
res.write(timestampLC() + " - ------------\n\n");
}
/* toggle device */
//await connection.toggleDevice('1000aea4dd');
res.end(); //end the response
}
eweRetrieve().catch(function () {
console.log(timestampLC() + " - retrieve failed");
});
function timestampLC() {
let d = new Date();
return d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
}
