UNPKG

lavva.exalushome

Version:

Library implementing communication and abstraction layers for ExalusHome system

304 lines 11.9 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 { TypedEvent } from "../../TypedEvent"; import { FieldChangeResultType, FieldChangeResult } from "../FieldChangeResult"; import { DevicesService } from "./DevicesService"; import { ChannelConfigurations } from './IDeviceChannel'; import { DeviceTaskExecutionResult } from "./TaskExecutionResult"; import { ExalusConnectionService } from "../ExalusConnectionService"; import { DataFrame, Method, Status } from "../../DataFrame"; import { DependencyContainer } from '../../DependencyContainer'; import { Device } from "./Device"; import { LoggerService } from "../Logging/LoggerService"; import { ControllerConfigurationService } from "../Controller/ControllerConfigurationService"; export class DeviceChannel { constructor() { this._device = new Device(); this._number = 0; this._name = ""; this._iconName = ""; this._channelId = ""; this._mode = ChannelConfigurations.Default; this._channelGroups = []; this._isHidden = false; this._availableTasksTypes = []; this._availableResponseTypes = []; this._customData = {}; this._roles = []; this._configurations = {}; this.CustomDataAndRolesSupported = false; this._onTasksExecutionChangedEvent = new TypedEvent(); this._onChannelStateChangedEvent = new TypedEvent(); this._onChannelStateRefreshedOrChangedEvent = new TypedEvent(); if (DeviceChannel._devicesService == null) DeviceChannel._devicesService = Api.Get(DevicesService.ServiceName); } OnChannelStateChangedEvent() { return this._onChannelStateChangedEvent; } OnChannelStateRefreshedOrChangedEvent() { return this._onChannelStateRefreshedOrChangedEvent; } get Configurations() { return this._configurations; } set Configurations(value) { this._configurations = value; } IsCustomDataAndRolesSupported() { return this.CustomDataAndRolesSupported; } get CustomData() { return this._customData; } set CustomData(value) { this._customData = value; } get Roles() { return this._roles; } set Roles(value) { this._roles = value; } get ChannelId() { return this._channelId; } SetDevice(device) { this._device = device; this._channelId = `${device.Guid}_${this.Number}`; this._device.OnDeviceStateChangedEvent().Subscribe((state) => { if (state.Data.Channel == this.Number) this._onChannelStateChangedEvent.Invoke(state); }); this._device.OnDeviceStateRefreshedOrChangedEvent().Subscribe((state) => { if (state.Data.Channel == this.Number) this._onChannelStateRefreshedOrChangedEvent.Invoke(state); }); } GetDevice() { return this._device; } SetCustomDataAsync(dataId, data) { return __awaiter(this, void 0, void 0, function* () { const current = this.GetChannelConfigurationData(); current.CustomData[dataId] = data; this._customData[dataId] = data; const conf = new DeviceChannelConfiguration(); conf.DeviceGuid = this._device.Guid; conf.ChannelConfiguration = current; return this.ChangeConfigurationAsync(conf); }); } RemoveCustomDataAsync(dataId) { const current = this.GetChannelConfigurationData(); delete current.CustomData[dataId]; delete this._customData[dataId]; const conf = new DeviceChannelConfiguration(); conf.DeviceGuid = this._device.Guid; conf.ChannelConfiguration = current; return this.ChangeConfigurationAsync(conf); } GetChannelConfigurationData() { const data = new ChannelConfigurationData(); data.Channel = this._number; data.ChannelName = this._name; data.Configuration = this._mode; data.CustomData = this._customData; data.GroupsGuids = this._channelGroups; data.Hidden = this._isHidden; data.IconName = this._iconName; return data; } HideAsync() { const data = this.GetChannelConfigurationData(); this._isHidden = true; data.Hidden = true; const conf = new DeviceChannelConfiguration(); conf.DeviceGuid = this._device.Guid; conf.ChannelConfiguration = data; return this.ChangeConfigurationAsync(conf); } ShowAsync() { const data = this.GetChannelConfigurationData(); data.Hidden = false; this._isHidden = false; const conf = new DeviceChannelConfiguration(); conf.DeviceGuid = this._device.Guid; conf.ChannelConfiguration = data; return this.ChangeConfigurationAsync(conf); } ChangeNameAsync(name) { const data = this.GetChannelConfigurationData(); const conf = new DeviceChannelConfiguration(); conf.DeviceGuid = this._device.Guid; conf.ChannelConfiguration = data; data.ChannelName = name; this._name = name; return this.ChangeConfigurationAsync(conf); } ChangeIconNameAsync(name) { const data = this.GetChannelConfigurationData(); data.IconName = name; const conf = new DeviceChannelConfiguration(); conf.DeviceGuid = this._device.Guid; conf.ChannelConfiguration = data; this._iconName = name; return this.ChangeConfigurationAsync(conf); } ChangeGroupsAsync(groups) { const data = this.GetChannelConfigurationData(); data.GroupsGuids = groups; const conf = new DeviceChannelConfiguration(); conf.DeviceGuid = this._device.Guid; conf.ChannelConfiguration = data; this._channelGroups = groups; return this.ChangeConfigurationAsync(conf); } ChangeConfigurationAsync(configuration) { return __awaiter(this, void 0, void 0, function* () { var _a, _b; try { const result = yield Api.Get(ExalusConnectionService.ServiceName) .SendAndWaitForResponseAsync(new DeviceChannelConfigurationRequest(configuration), 20000, false); (_a = DependencyContainer.Log) === null || _a === void 0 ? void 0 : _a.Error("Device", `EXIT CONFIGURATION MODE WILL BE FIRED! ChangeConfigurationAsync()`); yield Api.Get(ControllerConfigurationService.ServiceName).ExitConfigurationModeAsync(); switch (result.Status) { case Status.OK: this._name = configuration.ChannelConfiguration.ChannelName; this._iconName = configuration.ChannelConfiguration.IconName; this._channelGroups = configuration.ChannelConfiguration.GroupsGuids; this._isHidden = configuration.ChannelConfiguration.Hidden; this._mode = configuration.ChannelConfiguration.Configuration; this._customData = configuration.ChannelConfiguration.CustomData; this._roles = configuration.ChannelConfiguration.Roles; this._configurations = configuration.ChannelConfiguration.Configurations; return new FieldChangeResult(FieldChangeResultType.Changed, ""); case Status.WrongData: switch (result.Data) { case "DeviceNotFound": (_b = DependencyContainer.Log) === null || _b === void 0 ? void 0 : _b.Error(`Failed to change channel configuration, referenced device does not exist!`); return new FieldChangeResult(FieldChangeResultType.WrongData, "DeviceNotFound"); } break; case Status.FatalError: return new FieldChangeResult(FieldChangeResultType.UnknownError, ""); } } catch (ex) { if (ex.message.includes("timeout")) return new FieldChangeResult(FieldChangeResultType.ControllerResponseTimeout, ""); } return new FieldChangeResult(FieldChangeResultType.Unknown, ""); }); } get Number() { return this._number; } set Number(value) { this._number = value; } get Name() { return this._name; } set Name(value) { this._name = value; } get IconName() { return this._iconName; } set IconName(value) { this._iconName = value; } get States() { if (LoggerService.DoesThrowStackTrace) return (Api.Get(DevicesService.ServiceName)).GetDeviceStates(this.GetDevice()) .where(a => a.Channel == this.Number) .selectMany(a => a.States) .toArray(); else return DeviceChannel._devicesService .GetDeviceStates(this.GetDevice()) .where(a => a.Channel == this.Number) .selectMany(a => a.States) .toArray(); } get Mode() { return this._mode; } set Mode(value) { this._mode = value; } get ChannelGroups() { return this._channelGroups; } set ChannelGroups(value) { this._channelGroups = value; } get IsHidden() { return this._isHidden; } set IsHidden(value) { this._isHidden = value; } get AvailableTaskTypes() { return this._availableTasksTypes; } set AvailableTaskTypes(value) { this._availableTasksTypes = value; } get AvailableResponseTypes() { return this._availableResponseTypes; } set AvailableResponseTypes(value) { this._availableResponseTypes = value; } OnTasksExecutionChangeEvent() { return this._onTasksExecutionChangedEvent; } ExecuteTaskAsync(task) { if (this._device != null) { task.Channel = this.Number; task.DeviceGuid = this._device.Guid; return DeviceChannel._devicesService.ExecuteDeviceTaskAsync(this._device, task); } else return Promise.resolve(DeviceTaskExecutionResult.Failed); } } DeviceChannel._devicesService = null; export class DeviceChannelConfiguration { constructor() { this.DeviceGuid = ""; this.ChannelConfiguration = new ChannelConfigurationData(); } } export class ChannelConfigurationData { constructor() { this.Channel = 0; this.ChannelName = ""; this.Configuration = ChannelConfigurations.Default; this.GroupsGuids = []; this.IconName = ""; this.Hidden = false; this.Configurations = {}; this.CustomData = {}; this.Roles = []; } } class DeviceChannelConfigurationRequest extends DataFrame { constructor(configuration) { super(); this.Resource = "/devices/device/channel/configuration"; this.Method = Method.Put; this.Data = configuration; } } //# sourceMappingURL=DeviceChannel.js.map