homebridge-zaptec
Version:
Control your Zaptec EV chargers
81 lines • 3.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ZaptecAPI = void 0;
const zaptec_1 = require("./zaptec");
class ZaptecAPI {
constructor(username, password) {
this.username = username;
this.password = password;
this.contract = new zaptec_1.Api({
baseURL: "https://api.zaptec.com",
securityWorker: async () => {
var _a, _b, _c, _d;
const delta = (((_a = this.token) === null || _a === void 0 ? void 0 : _a.expiresAt) || 0) - Date.now();
console.log(this.token, { delta });
if (!((_b = this.token) === null || _b === void 0 ? void 0 : _b.accessToken) || delta <= 5) {
await this.login();
}
if ((_c = this.token) === null || _c === void 0 ? void 0 : _c.accessToken) {
return {
headers: {
Authorization: `Bearer ${(_d = this.token) === null || _d === void 0 ? void 0 : _d.accessToken}`,
},
};
}
return {};
},
});
this.getChargerState = async (id) => {
var _a, _b, _c, _d, _e, _f, _g, _h;
if (!this.constants) {
this.constants = await this.api.constantsList();
}
const res = await this.api.chargersStateDetail(id);
const keys = new Map([
...Object.entries((_b = (_a = this.constants.schema) === null || _a === void 0 ? void 0 : _a.smart) === null || _b === void 0 ? void 0 : _b.observationIds),
...Object.entries((_d = (_c = this.constants.schema) === null || _c === void 0 ? void 0 : _c.smart) === null || _d === void 0 ? void 0 : _d.settingIds),
...Object.entries((_f = (_e = this.constants.schema) === null || _e === void 0 ? void 0 : _e.apollo) === null || _f === void 0 ? void 0 : _f.observationIds),
...Object.entries((_h = (_g = this.constants.schema) === null || _g === void 0 ? void 0 : _g.apollo) === null || _h === void 0 ? void 0 : _h.settingIds),
].map(([value, key]) => [key, value]));
return res.reduce((acc, item) => {
const key = keys.get(item.stateId);
if (key) {
acc[key] = item.valueAsString;
}
else {
console.log('cannot parse', item);
}
return acc;
}, {});
};
// nope
}
get api() {
return this.contract.api;
}
async login() {
try {
const res = await this.contract.request({
path: "oauth/token",
method: "POST",
body: {
grant_type: "password",
username: this.username,
password: this.password,
},
});
console.log(res);
this.token = {
accessToken: res.access_token,
expiresAt: Date.now() + res.expires_in * 1000,
};
}
catch (err) {
console.log(err);
}
}
}
exports.ZaptecAPI = ZaptecAPI;
const { api, getChargerState } = new ZaptecAPI("birkir.gudjonsson@gmail.com", "Solid.2829");
getChargerState("1ebee72c-6b47-4652-a3ac-3a5faebd7f13").then(console.log);
//# sourceMappingURL=api.js.map