@golemio/energetics
Version:
Golemio Energetics Module
110 lines • 4.35 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.UnimonitorCemApiHelper = void 0;
const golemio_errors_1 = require("@golemio/core/dist/shared/golemio-errors");
/**
* Helper class for requesting additional data from Unimonitor CEM API
*/
class UnimonitorCemApiHelper {
constructor(url, config) {
this.url = url;
this.config = config;
/**
* Create API session and return authorization cookie
*/
this.createSession = async () => {
try {
const { authcookiename, user, pass } = this.config;
const params = new URLSearchParams({
id: UnimonitorCemApiHelper.resourceType.UserLogin,
pass,
user,
});
const absoluteUrl = `${this.url}?${params}`;
const result = await fetch(absoluteUrl, {
method: "get",
signal: AbortSignal.timeout(10000),
});
const cookieHeader = this.getAuthCookie(result.headers);
return UnimonitorCemApiHelper.processAndFilterAuthCookie(cookieHeader, authcookiename);
}
catch (err) {
throw new golemio_errors_1.GeneralError("Cannot retrieve Unimonitor CEM API authorization token " + err.message, UnimonitorCemApiHelper.name, err);
}
};
/**
* Terminate current session/invalidate auth cookie
*/
this.terminateSession = async (authCookie) => {
const url = this.url;
const params = new URLSearchParams({
id: UnimonitorCemApiHelper.resourceType.UserLogout,
});
const absoluteUrl = `${url}?${params}`;
try {
await fetch(absoluteUrl, {
headers: {
Cookie: authCookie,
},
method: "GET",
signal: AbortSignal.timeout(10000),
});
}
catch (err) {
throw new golemio_errors_1.GeneralError("Cannot terminate Unimonitor CEM API session", UnimonitorCemApiHelper.name, err);
}
};
}
getAuthCookie(headers) {
// method getSetCookie is missing in types see https://developer.mozilla.org/en-US/docs/Web/API/Headers/getSetCookie
// it should be possible to remove any with next version of typescript / node
const cookieHeader = headers.getSetCookie();
if (cookieHeader.length === 0) {
throw new golemio_errors_1.GeneralError("Cannot retrieve Unimonitor CEM API authorization cookie", UnimonitorCemApiHelper.name);
}
return cookieHeader[0];
}
/**
* Return resource types/identifiers
*/
static get resourceType() {
return {
Devices: "46",
Counters: "45",
MeasurementsByCounter: "3",
Measurement: "20",
MeasuringEquipment: "6",
MeterType: "14",
TypeMeasuringEquipment: "11",
Units: "7",
UserLogin: "4",
UserLogout: "5",
};
}
/** Return counter types/identifiers */
static get counterType() {
return {
ElHighTariff: 1,
ElLowTariff: 2,
};
}
}
exports.UnimonitorCemApiHelper = UnimonitorCemApiHelper;
UnimonitorCemApiHelper.API_DATE_FORMAT = "yyyy-LL-dd"; // Luxon token format: https://moment.github.io/luxon/#/formatting
UnimonitorCemApiHelper.API_DATE_TZ = "Europe/Prague";
UnimonitorCemApiHelper.COOKIE_KV_SEPARATOR = "=";
/**
* Process and filter auth cookie from the original cookie header
*/
UnimonitorCemApiHelper.processAndFilterAuthCookie = (cookieHeader, authCookieName) => {
const rawCookies = cookieHeader?.split(";") ?? [];
for (const rawCookie of rawCookies) {
const rawCookieArray = rawCookie.split(UnimonitorCemApiHelper.COOKIE_KV_SEPARATOR).map((prop) => prop.trim());
const [cookieName, cookieValue] = rawCookieArray;
if (cookieName === authCookieName && !!cookieValue) {
return rawCookieArray.join(UnimonitorCemApiHelper.COOKIE_KV_SEPARATOR);
}
}
return "";
};
//# sourceMappingURL=UnimonitorCemApiHelper.js.map
;