UNPKG

lavva.exalushome

Version:

Library implementing communication and abstraction layers for ExalusHome system

152 lines 7.5 kB
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()); }); }; import { Api } from "../../Api"; import { AsyncSemaphore } from "../../AsyncSemaphore"; import { DataFrame, Method, Status } from "../../DataFrame"; import { DependencyContainer } from "../../DependencyContainer"; import { ExalusConnectionService } from "../ExalusConnectionService"; export class ControllerExtensionsService { constructor() { this._extensionInfoCache = new Map(); this._protocolInfoCache = new Map(); this._isExtensionInfoInitialized = false; this._semaphore = new AsyncSemaphore(1); } GetServiceName() { return ControllerExtensionsService.ServiceName; } GetExtensionsInfoAsync(extensionGuid_1) { return __awaiter(this, arguments, void 0, function* (extensionGuid, forceUpdate = false) { var _a; (_a = DependencyContainer.Log) === null || _a === void 0 ? void 0 : _a.Debug(ControllerExtensionsService.ServiceName, `Getting ExtensionsInfo, data loaded from cache: ${!forceUpdate}`); var lock = yield this._semaphore.AcquireAsync(); if (!this._isExtensionInfoInitialized || forceUpdate) { yield this.GetExtensionInfoFromControllerAsync(); } lock.Release(); if ([...this._extensionInfoCache.values()].length == 0) { return []; //throw new CannotGetExtensions("Cannot get ExtensionsInfo, controler may return empty list of Extensions or other unknown error occurs!"); } if (extensionGuid != null) { const extInfo = this._extensionInfoCache.get(extensionGuid); if (extInfo == null) throw new CannotGetExtensions("Cannot get ExtensionsInfo, extension not found."); return [extInfo]; } else return [...this._extensionInfoCache.values()]; }); } GetProtocolInfoAsync(protocolGuid_1) { return __awaiter(this, arguments, void 0, function* (protocolGuid, forceUpdate = false, retrying = false) { var _a; (_a = DependencyContainer.Log) === null || _a === void 0 ? void 0 : _a.Debug(ControllerExtensionsService.ServiceName, `Getting ProtocolInfo, data loaded from cache: ${!forceUpdate}`); if (forceUpdate) yield this.GetProtocolExtensionInfoFromControllerAsync(); if ([...this._protocolInfoCache.values()].length == 0 && !retrying) { yield this.GetProtocolInfoAsync(protocolGuid, true, true); } else if ([...this._protocolInfoCache.values()].length == 0) { throw new CannotGetExtensions("Cannot get ProtocolInfo, controler may return empty list of Extensions or other unknown error occurs!"); } if (protocolGuid != null) { const extInfo = this._protocolInfoCache.get(protocolGuid); if (extInfo == null) throw new CannotGetExtensions("Cannot get ProtocolInfo, extension not found."); return [extInfo]; } else return [...this._protocolInfoCache.values()]; }); } GetExtensionInfoFromControllerAsync() { return __awaiter(this, void 0, void 0, function* () { const request = new ExtensionInfoRequest(); const result = yield Api.Get(ExalusConnectionService.ServiceName).SendAndWaitForResponseAsync(request, 16000, true); if (result == null || result.Status == null) throw new CannotGetExtensions(`Cannot get ExtensionsInfo from controller, controller response does not contain data!`); if (result.Status != Status.OK) throw new CannotGetExtensions(`Cannot get ExtensionsInfo from controller! Response status: ${result.Status}`); if (result.Data == null) throw new CannotGetExtensions(`Cannot get ExtensionsInfo from controller, controller response does not contain data!`); result.Data.forEach(res => { this._extensionInfoCache.set(res.ExtensionGuid, res); }); if ([...this._extensionInfoCache.values()].length != 0) this._isExtensionInfoInitialized = true; }); } GetProtocolExtensionInfoFromControllerAsync() { return __awaiter(this, void 0, void 0, function* () { var _a; const request = new ProtocolInfoRequest(); const result = yield Api.Get(ExalusConnectionService.ServiceName).SendAndWaitForResponseAsync(request, 16000, true); if (result == null || result.Status == null) throw new CannotGetExtensions(`Cannot get ProtocolInfo from controller, controller response does not contain data!`); switch (result.Status) { case Status.OK: if (result.Data == null) throw new CannotGetExtensions(`Cannot get ProtocolInfo from controller, controller response does not contain data!`); result.Data.forEach(res => { this._protocolInfoCache.set(res.PublicGuid, res); }); break; case Status.ResourceDoesNotExists: (_a = DependencyContainer.Log) === null || _a === void 0 ? void 0 : _a.Debug(ControllerExtensionsService.ServiceName, `Cannot get ProtocolInfo from controller, controller software does not implement this yet, update controller to the newest software!`); break; default: throw new CannotGetExtensions(`Cannot get ProtocolInfo from controller! Response status: ${result.Status}`); } }); } } ControllerExtensionsService.ServiceName = "StatisticsInfoService"; export class ExtensionInfo { constructor() { this.ExtensionGuid = ""; this.APIVersion = ""; this.ExtensionVersion = ""; this.Name = ""; this.PublisherGuid = ""; this.ExtensionSystemGuid = ""; } } export class ProtocolInfo { constructor() { this.Guid = ""; this.PublicGuid = ""; this.Name = ""; } } //Requests class ExtensionInfoRequest extends DataFrame { constructor() { super(); this.Resource = "/controller/extensions/"; this.Method = Method.Get; } } class ProtocolInfoRequest extends DataFrame { constructor() { super(); this.Resource = "/controller/protocols/"; this.Method = Method.Get; } } //Errors class CannotGetExtensions extends Error { constructor(message) { super(message); this.message = message; this.name = "CannotGetExtensions"; } } //# sourceMappingURL=ControllerExtensionsService.js.map