mirror of
https://github.com/skydiver/ewelink-api.git
synced 2025-12-27 07:31:41 +01:00
* helper method to login into ewelink if no auth credentials found * return websocket reponse as JSON * created function to get raw consumption data * created function to parse raw consumption data and return daily usage * renamed property * created function to get current month power usage * created function to get raw power usage * added new test cases * catch websocket connection errors * power usage enhancements * added new test case * removed unused code * updated credentials file * version bump * updated dependencies * tests reorganized
39 lines
837 B
JavaScript
39 lines
837 B
JavaScript
/**
|
|
* Return daily power usage
|
|
*
|
|
* @param hundredDaysKwhData
|
|
*
|
|
* @returns {{daily: *, monthly: *}}
|
|
*/
|
|
const currentMonthPowerUsage = ({ hundredDaysKwhData }) => {
|
|
const today = new Date();
|
|
const days = today.getDate();
|
|
|
|
let monthlyUsage = 0;
|
|
const dailyUsage = [];
|
|
|
|
for (let day = 0; day < days; day += 1) {
|
|
const s = hundredDaysKwhData.substr(6 * day, 2);
|
|
const c = hundredDaysKwhData.substr(6 * day + 2, 2);
|
|
const f = hundredDaysKwhData.substr(6 * day + 4, 2);
|
|
const h = parseInt(s, 16);
|
|
const y = parseInt(c, 16);
|
|
const I = parseInt(f, 16);
|
|
const E = parseFloat(`${h}.${y}${I}`);
|
|
|
|
dailyUsage.push({
|
|
day: days - day,
|
|
usage: E,
|
|
});
|
|
|
|
monthlyUsage += E;
|
|
}
|
|
|
|
return {
|
|
monthly: monthlyUsage,
|
|
daily: dailyUsage,
|
|
};
|
|
};
|
|
|
|
module.exports = currentMonthPowerUsage;
|