UNPKG

lavva.exalushome

Version:

Library implementing communication and abstraction layers for ExalusHome system

222 lines 11.8 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 { TimeoutError } from "@microsoft/signalr"; import { Api } from "../../Api"; import { AsyncSemaphore } from "../../AsyncSemaphore"; import { DataFrame, Method, Status } from "../../DataFrame"; import { ExalusConnectionService } from "../ExalusConnectionService"; import { UpdatesProviderNotFound } from "./IUpdatesProvider"; import { DependencyContainer } from "../../DependencyContainer"; export class UpdatesProvider { constructor() { this._services = new Map(); this._runtimeInfo = new SoftwareRuntimeInfo(); this._hardwareInfo = new HardwareInfo(); this._semaphore = new AsyncSemaphore(1); this._isRuntimeInfoInitialized = false; this._isHardwareInfoInitialized = false; } GetServiceName() { return UpdatesProvider.ServiceName; } RegisterUpdatesProvider(service) { var _a; (_a = DependencyContainer.Log) === null || _a === void 0 ? void 0 : _a.Debug(UpdatesProvider.ServiceName, `Registering new update provider: ${service.GetUpdateProviderName()}`); this._services.set(service.GetUpdateProviderName(), service); } GetUpdatesProviderAsync(serviceName) { return __awaiter(this, void 0, void 0, function* () { var _a; (_a = DependencyContainer.Log) === null || _a === void 0 ? void 0 : _a.Debug(UpdatesProvider.ServiceName, `Getting update provider [${serviceName}] by GetUpdateProviderAsync<T>()`); const service = this._services.get(serviceName); if (service == null) throw new UpdatesProviderNotFound("Cannot get update provider! Requested update provider has not been registered."); return service; }); } GetUpdatesProvidersAsync(updateProviderType) { return __awaiter(this, void 0, void 0, function* () { var _a; (_a = DependencyContainer.Log) === null || _a === void 0 ? void 0 : _a.Debug(UpdatesProvider.ServiceName, `Getting update providers with type ${updateProviderType} by GetUpdateProvidersAsync<T>()`); let services = []; services = [...this._services.values()].filter(provider => provider.ProviderType == updateProviderType); if (services.length == 0) throw new UpdatesProviderNotFound("Cannot get update providers! Requested update providers has not been registered."); return services; }); } GetUpdatesProvidersByProtocolAsync(protocolGuid, updateProviderType, apiVersion) { return __awaiter(this, void 0, void 0, function* () { var _a; const regGuid = new RegExp("^[{]?[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}[}]?$"); if (regGuid.test(protocolGuid)) { (_a = DependencyContainer.Log) === null || _a === void 0 ? void 0 : _a.Debug(UpdatesProvider.ServiceName, `Getting update providers with protocol guid: [${protocolGuid}] by GetUpdateProvidersByProtocolAsync<T>()`); let services = []; if (apiVersion != null) services = [...this._services.values()].filter(provider => provider.ProtocolGuid == protocolGuid && provider.ProviderType == updateProviderType && provider.AvailableExtensionVersion == apiVersion); else services = [...this._services.values()].filter(provider => provider.ProtocolGuid == protocolGuid && provider.ProviderType == updateProviderType); if (services.length == 0) throw new UpdatesProviderNotFound("Cannot get update providers! Requested update providers has not been registered."); return services; } else throw new UpdatesProviderNotFound("Cannot get update providers! Bad parameters."); }); } GetUpdatesProvidersByExtensionAsync(extensionGuid, updateProviderType, apiVersion) { return __awaiter(this, void 0, void 0, function* () { var _a; const regGuid = new RegExp("^[{]?[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}[}]?$"); if (regGuid.test(extensionGuid)) { (_a = DependencyContainer.Log) === null || _a === void 0 ? void 0 : _a.Debug(UpdatesProvider.ServiceName, `Getting update providers with extension guid: [${extensionGuid}] by GetUpdateProvidersByExtensionAsync<T>()`); let services = []; if (apiVersion != null) services = [...this._services.values()].filter(provider => provider.ExtensionGuid == extensionGuid && provider.ProviderType == updateProviderType && provider.AvailableExtensionVersion == apiVersion); else services = [...this._services.values()].filter(provider => provider.ExtensionGuid == extensionGuid && provider.ProviderType == updateProviderType); if (services.length == 0) throw new UpdatesProviderNotFound("Cannot get update providers! Requested update providers has not been registered."); return services; } else throw new UpdatesProviderNotFound("Cannot get update providers! Bad parameters."); }); } GetSoftwareRuntimeInfoAsync() { return __awaiter(this, arguments, void 0, function* (reloadCachedData = false) { var lock = yield this._semaphore.AcquireAsync(); try { if (!this._isRuntimeInfoInitialized || reloadCachedData) { yield this.GetRuntimeControllerInfoAsync(); } return this._runtimeInfo; } finally { lock.Release(); } }); } GetRuntimeControllerInfoAsync() { return __awaiter(this, void 0, void 0, function* () { var _a; (_a = DependencyContainer.Log) === null || _a === void 0 ? void 0 : _a.Debug(UpdatesProvider.ServiceName, `Getting runtime version info from controller.`); try { const result = yield Api.Get(ExalusConnectionService.ServiceName).SendAndWaitForResponseAsync(new GetControllerRuntimeInfoRequest(), 16000, false); if (result == null || result.Status == null) throw new CannotGetRuntimeInfo(`Cannot get runtime info, controller response result is empty."`, CannotGetRuntimeErrorCode.EmptyResponse); if (result.Status != Status.OK) throw new CannotGetRuntimeInfo(`Cannot get runtime info, controller responded with status ${result.Status}.`, CannotGetRuntimeErrorCode.WrongResponseStatus); if (result.Data == null) throw new CannotGetRuntimeInfo(`Cannot get runtime info, controller responded with status OK but response does not contains data.`, CannotGetRuntimeErrorCode.NoDataInResponse); this._runtimeInfo = result.Data; this._isRuntimeInfoInitialized = true; } catch (error) { if (error instanceof TimeoutError) throw new CannotGetRuntimeInfo(`Cannot get runtime info, controller response timeout.`, CannotGetRuntimeErrorCode.Timeout); else throw error; } }); } GetHardwareInfoAsync() { return __awaiter(this, arguments, void 0, function* (reloadCachedData = false) { if (!this._isHardwareInfoInitialized || reloadCachedData) { yield this.GetHardwareControllerInfoAsync(); return this._hardwareInfo; } return this._hardwareInfo; }); } GetHardwareControllerInfoAsync() { return __awaiter(this, void 0, void 0, function* () { var _a; (_a = DependencyContainer.Log) === null || _a === void 0 ? void 0 : _a.Debug(UpdatesProvider.ServiceName, `Getting hardware info from controller.`); const result = yield Api.Get(ExalusConnectionService.ServiceName).SendAndWaitForResponseAsync(new GetControllerHardwareInfoRequest(), 8000, false); if (result == null || result.Status == null) throw new CannotGetRuntimeInfo(`Cannot get hardware info, controller response result is empty."`, CannotGetRuntimeErrorCode.EmptyResponse); if (result.Status != Status.OK) throw new CannotGetRuntimeInfo(`Cannot get hardware info, controller responded with status ${result.Status}.`, CannotGetRuntimeErrorCode.WrongResponseStatus); if (result.Data == null) throw new CannotGetRuntimeInfo(`Cannot get hardware info, controller responded with status OK but response does not contains data.`, CannotGetRuntimeErrorCode.NoDataInResponse); this._hardwareInfo = result.Data; this._isHardwareInfoInitialized = true; }); } } UpdatesProvider.ServiceName = "UpdateProvider"; //RuntimeInfo export class SoftwareRuntimeInfo { constructor() { this.BaseLinuxVersion = ""; this.BaseMonoVersion = ""; this.RuntimeVersion = ""; this.UpdateChannel = ""; this.SoftwareVersion = ""; } } class GetControllerRuntimeInfoRequest extends DataFrame { constructor() { super(); this.Resource = "/controller/software/info"; this.Method = Method.Get; } } //HardwareInfo export class HardwareInfo { constructor() { this.GetControllerHardwareVersion = 0; this.DevicePIN = ""; this.GetControllerSerialNumber = ""; this.HostnamePrefix = ""; this.DefaultStaticIpAddress = ""; this.GetAvailableHardware = []; this.ControllerSecret = ""; } } export class Hardware { constructor() { this.HardwareGUID = ""; this.DevicePath = ""; this.DisplayName = ""; this.HardwareSerialNumber = ""; this.HardwareVersion = 0; this.SoftwareVersion = 0; this.Configuration = ""; this.ControllerSecret = ""; } } class GetControllerHardwareInfoRequest extends DataFrame { constructor() { super(); this.Resource = "/controller/hardware/info"; this.Method = Method.Get; } } //Errors export class CannotGetRuntimeInfo extends Error { constructor(message, code) { super(message); this.message = message; this.code = 0; this.name = "CannotGetRuntimeInfo"; this.code = code; } } export var CannotGetRuntimeErrorCode; (function (CannotGetRuntimeErrorCode) { CannotGetRuntimeErrorCode[CannotGetRuntimeErrorCode["Unknown"] = 0] = "Unknown"; CannotGetRuntimeErrorCode[CannotGetRuntimeErrorCode["EmptyResponse"] = 1] = "EmptyResponse"; CannotGetRuntimeErrorCode[CannotGetRuntimeErrorCode["NoDataInResponse"] = 2] = "NoDataInResponse"; CannotGetRuntimeErrorCode[CannotGetRuntimeErrorCode["WrongResponseStatus"] = 3] = "WrongResponseStatus"; CannotGetRuntimeErrorCode[CannotGetRuntimeErrorCode["Timeout"] = 4] = "Timeout"; })(CannotGetRuntimeErrorCode || (CannotGetRuntimeErrorCode = {})); //# sourceMappingURL=UpdatesProvider.js.map