alphaess-api-js
Version:
Reverse engineered API for AlphaESS batteries
122 lines (121 loc) • 5.09 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AlphaEssApi = void 0;
const fetch = require("node-fetch");
class AlphaEssApi {
static init(username, pw, serialNumber, proxyIp) {
return __awaiter(this, void 0, void 0, function* () {
if (proxyIp !== undefined)
this.proxyIp = proxyIp;
this.setSerialNumber(serialNumber);
yield this.setLoginData(username, pw);
});
}
static setLoginData(username, pw) {
return __awaiter(this, void 0, void 0, function* () {
yield this.login(username, pw).then((json) => {
const resp = json;
if (resp.info === "Success") {
this.bearer = resp.data.AccessToken;
this.uname = username;
this.pw = pw;
this.bearerValidationTime = new Date().getTime();
}
});
console.log("Reset login data");
});
}
static setSerialNumber(sNo) {
this.serialNumber = sNo;
}
static login(name, pw) {
return __awaiter(this, void 0, void 0, function* () {
// Login
return fetch(this.proxyIp, {
method: "POST",
body: JSON.stringify({
url: "https://www.alphaess.com/api/Account/Login",
headers: { "Content-Type": "application/json" },
body: { username: name, password: pw }
})
}).then(response => response.json());
});
}
static getPeriodData(beginDay, endDay) {
return __awaiter(this, void 0, void 0, function* () {
yield this.checkBearer();
// Period-data
if (this.serialNumber === "") {
return null;
}
return yield fetch(this.proxyIp, {
method: "POST",
body: JSON.stringify({
url: "https://www.alphaess.com/api/Power/SticsByPeriod",
headers: { "Content-Type": "application/json", "Authorization": "Bearer " + this.bearer },
body: { beginDay, endDay, SN: this.serialNumber }
}),
}).then(res => {
const res2 = res.json();
return res2;
});
});
}
static getDetailedDayData(day) {
return __awaiter(this, void 0, void 0, function* () {
yield this.checkBearer();
// Day and current data
if (this.serialNumber === "") {
return null;
}
const sDate = day;
return yield fetch(this.proxyIp, {
method: "POST",
body: JSON.stringify({
url: "https://www.alphaess.com/api/Power/SticsByDay",
headers: { "Content-Type": "application/json", "Authorization": "Bearer " + this.bearer },
body: { szDay: day, sDate, SN: this.serialNumber }
}),
}).then(res => {
const res2 = res.json();
return res2;
});
});
}
static getRealtimeData() {
return __awaiter(this, void 0, void 0, function* () {
yield this.checkBearer();
return yield fetch(this.proxyIp, {
method: "POST",
body: JSON.stringify({
url: "https://www.alphaess.com/api/ESS/GetSecondDataBySn?sys_sn=" + this.serialNumber + "&noLoading=true",
headers: { "Content-Type": "application/json", "Authorization": "Bearer " + this.bearer }
})
}).then(res => {
const res2 = res.json();
return res2;
});
});
}
static checkBearer() {
return __awaiter(this, void 0, void 0, function* () {
if (new Date().getTime() - this.bearerValidationTime > 30000000) {
yield this.setLoginData(this.uname, this.pw);
}
});
}
}
exports.AlphaEssApi = AlphaEssApi;
AlphaEssApi.serialNumber = "";
AlphaEssApi.bearer = "";
AlphaEssApi.proxyIp = "http://127.0.0.1:3000";
AlphaEssApi.bearerValidationTime = 0;