@golemio/energetics
Version:
Golemio Energetics Module
79 lines • 3.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PpasAveApiHelper = void 0;
const golemio_errors_1 = require("@golemio/core/dist/shared/golemio-errors");
/**
* Helper class for requesting additional data from PPAS InternetAVE API
*/
class PpasAveApiHelper {
constructor(baseUrl, config) {
this.baseUrl = baseUrl;
this.config = config;
/**
* Create API session and return session ID
*/
this.createSession = async () => {
const { user, pass } = this.config;
try {
const url = new URL(this.baseUrl);
url.pathname += "/" + PpasAveApiHelper.resourceType.Logon;
const result = await fetch(url.toString(), {
method: "post",
headers: new Headers({
Accept: "application/json",
"Content-Type": "application/x-www-form-urlencoded",
}),
body: new URLSearchParams({
name: user,
password: pass,
}),
signal: AbortSignal.timeout(10000),
});
const data = await result.json();
if (data?.success && typeof data?.sessionId === "string") {
return data.sessionId;
}
throw new golemio_errors_1.GeneralError("Cannot parse PPAS AVE Session ID from the server response", PpasAveApiHelper.name, new Error(data?.message));
}
catch (err) {
if (err instanceof golemio_errors_1.AbstractGolemioError) {
throw err;
}
throw new golemio_errors_1.GeneralError("Cannot retrieve PPAS AVE Session ID " + err.message, PpasAveApiHelper.name, err);
}
};
/**
* Terminate current session/invalidate session ID
*/
this.terminateSession = async (sessionId) => {
try {
const url = new URL(this.baseUrl);
url.pathname += "/" + PpasAveApiHelper.resourceType.LogOff;
url.searchParams.append("sessionId", sessionId);
await fetch(url.toString(), {
method: "get",
signal: AbortSignal.timeout(10000),
});
}
catch (err) {
throw new golemio_errors_1.GeneralError("Cannot terminate PPAS AVE API session", PpasAveApiHelper.name, err);
}
};
}
/**
* Return resource types/identifiers
*/
static get resourceType() {
return {
Logon: "logon.rails",
LogOff: "LogOff.rails",
Places: "GetPlaces.rails",
DeviceData: "GetDeviceData.rails",
};
}
}
exports.PpasAveApiHelper = PpasAveApiHelper;
// based on analysis it seems API PPAS is handling timezones incorrectly, we deciced to follow PPAS UI approach to timezones.
// https://gitlab.com/operator-ict/golemio/projekty/energetika/p0262.prevzeti-energeticke-databaze/-/issues/37#note_2010344938
PpasAveApiHelper.API_DATE_TZ = "UTC+1";
//# sourceMappingURL=PpasAveApiHelper.js.map