UNPKG

lavva.exalushome

Version:

Library implementing communication and abstraction layers for ExalusHome system

250 lines 9.57 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 { DataFrame, Method, Status } from '../../DataFrame'; import { DependencyContainer } from '../../DependencyContainer'; import { TypedEvent } from '../../TypedEvent'; import { ControllerConfigurationService } from '../Controller/ControllerConfigurationService'; import { ExalusConnectionService } from '../ExalusConnectionService'; import { FieldChangeResult, FieldChangeResultType } from '../FieldChangeResult'; import { LoggerService } from '../Logging/LoggerService'; import { DevicesService } from './DevicesService'; import { CommunicationWay, DeviceState, DeviceTaskType, DeviceType, IconType } from './IDevice'; export class Device { constructor() { this._onDeviceStateChangedEvent = new TypedEvent(); this._onDeviceStateRefreshedOrChangedEvent = new TypedEvent(); this._onDeviceTasksExecutionChangedOnChannelsEvent = new TypedEvent(); this._onDeviceFirmwareVersionChangedEvent = new TypedEvent(); this._iconType = IconType.Unknown; this._serialNumber = null; this._model = null; this._modelGuid = null; this._manufacturerGuid = null; this._isVirtual = false; this._isEnabled = false; this._deviceState = DeviceState.Working; this._deviceType = DeviceType.Unknown; this._communicationWay = CommunicationWay.OneWay; this._availableTasksTypes = []; this._availableResponseTypes = []; this._channels = []; this._protocolGuid = null; this._softwareVersion = null; if (Device._devicesService == null) Device._devicesService = Api.Get(DevicesService.ServiceName); const stateChangedEvent = this._onDeviceStateChangedEvent; const tasksExecutionChangeEvent = this._onDeviceTasksExecutionChangedOnChannelsEvent; Device._devicesService.OnDeviceStateChangedEvent().Subscribe(stateChange => { if (stateChange.Device.Guid != this._guid) return; stateChangedEvent.Invoke(stateChange.State); }); Device._devicesService.OnDeviceStateRefreshedOrChangedEvent().Subscribe(stateChange => { if (stateChange.Device.Guid != this._guid) return; this._onDeviceStateRefreshedOrChangedEvent.Invoke(stateChange.State); }); Device._devicesService.OnDevicesTasksExecutionChangeEvent().Subscribe(tasks => { tasksExecutionChangeEvent.Invoke(tasks.filter(task => task.DeviceGuid == this._guid).map(task => task.Channel)); }); } OnDeviceFirmwareVersionChangedEvent() { return this._onDeviceFirmwareVersionChangedEvent; } OnDeviceStateRefreshedOrChangedEvent() { return this._onDeviceStateRefreshedOrChangedEvent; } get ShouldChannelsBeGrouped() { if (this._deviceType === DeviceType.Remote) { switch (this._modelGuid) { case "afea26ac-d5de-4aa9-8e74-ff69679d097b": case "908a71ec-ca46-4431-9504-05b7801e77f8": case "d8fea502-541e-42d9-8ece-a1bcfaaa93f9": return false; default: return this._availableTasksTypes.all(a => a.Type !== DeviceTaskType.SetBlindPositionSimple); } } return false; } get States() { if (LoggerService.DoesThrowStackTrace) return (Api.Get(DevicesService.ServiceName)).GetDeviceStates(this).selectMany(a => a.States).toArray(); else return Device._devicesService.GetDeviceStates(this).selectMany(a => a.States).toArray(); } RemoveDeviceAsync() { return Device._devicesService.RemoveDeviceAsync(this); } ExecuteDeviceTaskAsync(task) { return Device._devicesService.ExecuteDeviceTaskAsync(this, task); } OnDeviceTasksExecutionChangedOnChannelsEvent() { return this._onDeviceTasksExecutionChangedOnChannelsEvent; } OnDeviceStateChangedEvent() { return this._onDeviceStateChangedEvent; } get Guid() { return this._guid == null ? "" : this._guid; } set Guid(guid) { this._guid = guid; } get IconType() { return this._iconType; } set IconType(iconType) { this._iconType = iconType; } get Name() { return this._name == null ? "" : this._name; } set Name(name) { this._name = name; } set SoftwareVersion(version) { this._softwareVersion = version; } get SoftwareVersion() { return this._softwareVersion; } get ChannelsAmount() { return this._channelsAmount == null ? 0 : this._channelsAmount; } set ChannelsAmount(amount) { this._channelsAmount = amount; } get SerialNumber() { return this._serialNumber; } set SerialNumber(serialNumber) { this._serialNumber = serialNumber; } get Model() { return this._model; } set Model(model) { this._model = model; } get ModelGuid() { return this._modelGuid; } set ModelGuid(model) { this._modelGuid = model; } get ManufacturerGuid() { return this._manufacturerGuid; } set ManufacturerGuid(manufacturerGuid) { this._manufacturerGuid = manufacturerGuid; } get IsVirtual() { return this._isVirtual; } set IsVirtual(isVirtual) { this._isVirtual = isVirtual; } get IsEnabled() { return this._isEnabled; } set IsEnabled(isEnabled) { this._isEnabled = isEnabled; } get DeviceState() { return this._deviceState; } set DeviceState(deviceState) { this._deviceState = deviceState; } get DeviceType() { return this._deviceType; } set DeviceType(deviceType) { this._deviceType = deviceType; } get CommunicationWay() { return this._communicationWay; } set CommunicationWay(communicationWay) { this._communicationWay = communicationWay; } get AvailableTaskTypes() { return this._availableTasksTypes; } set AvailableTaskTypes(availableTasksTypes) { this._availableTasksTypes = availableTasksTypes; } get AvailableResponseTypes() { return this._availableResponseTypes; } set AvailableResponseTypes(availableResponseTypes) { this._availableResponseTypes = availableResponseTypes; } get Channels() { return this._channels; } get ProtocolGuid() { return this._protocolGuid; } set ProtocolGuid(value) { this._protocolGuid = value; } ChangeDeviceNameAsync(name) { return __awaiter(this, void 0, void 0, function* () { var _a; try { const result = yield Api.Get(ExalusConnectionService.ServiceName) .SendAndWaitForResponseAsync(new DeviceChannelConfigurationRequest(this, name), 20000, false); switch (result.Status) { case Status.OK: this._name = name; (_a = DependencyContainer.Log) === null || _a === void 0 ? void 0 : _a.Error("Device", `EXIT CONFIGURATION MODE WILL BE FIRED! ChangeDeviceNameAsync()`); Api.Get(ControllerConfigurationService.ServiceName).ExitConfigurationModeAsync(); return new FieldChangeResult(FieldChangeResultType.Changed, ""); case Status.WrongData: if (result.Data == "DeviceNotFound") return new FieldChangeResult(FieldChangeResultType.Failed, "DeviceNotFound"); break; case Status.NoPermissionsToCallGivenResource: return new FieldChangeResult(FieldChangeResultType.NoPermissions, ""); } } catch (ex) { if (ex.message.includes("timeout")) return new FieldChangeResult(FieldChangeResultType.ControllerResponseTimeout, ""); return new FieldChangeResult(FieldChangeResultType.UnknownError, ""); } return new FieldChangeResult(FieldChangeResultType.Unknown, ""); }); } ExecuteTaskAsync(task) { return Device._devicesService.ExecuteDeviceTaskAsync(this, task); } } class DeviceChannelConfigurationRequest extends DataFrame { constructor(device, name) { super(); this.Resource = "/devices/device/name"; this.Method = Method.Put; const data = new DeviceNameData(); data.DeviceGuid = device.Guid; data.Name = name; this.Data = data; } } class DeviceNameData { constructor() { this.DeviceGuid = ""; this.Name = ""; } } //# sourceMappingURL=Device.js.map