UNPKG

lavva.exalushome.extalife

Version:

Library implementing communication and abstraction layers for ExtaLife API in ExalusHome system

443 lines 22.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 { Api } from "lavva.exalushome"; import { DataFrame, Method, Status } from "lavva.exalushome/build/js/DataFrame"; import { ExalusConnectionService } from "lavva.exalushome/build/js/Services/ExalusConnectionService"; import { ResponseResult } from "lavva.exalushome/build/js/Services/FieldChangeResult"; import { LoggerService } from "lavva.exalushome/build/js/Services/Logging/LoggerService"; import { GetChannelParamsRequest } from "../DataRequest"; import { GlobalTimeParameters, InputModeSwitch, ReactionToInput, StateOfTurnOnSwitchDeviceParameters, SwitchTurnOnState, Time as TimeParam } from "./OnOffSwitchParameters"; //import { InputOutputSwitchParameters } export class OnOffSwitchService { ; constructor() { this._connection = null; this._connection = Api.Get(ExalusConnectionService.ServiceName); this._logger = Api.Get(LoggerService.ServiceName); } GetTurnOnBehaviourAsync(device, channel) { return __awaiter(this, void 0, void 0, function* () { var _a; try { if (!device.Channels.any(ch => ch.Number === channel)) return new ResponseResult(SwitchConfigurationErrorCode.InvalidChannelNumber, `Cannot get turn on behaviour - invalid channel number, channel: ${channel}`); const result = yield ((_a = this._connection) === null || _a === void 0 ? void 0 : _a.SendAndWaitForResponseAsync(new GetTurnOnBehaviourRequest(new GetChannelParamsRequest(device.Guid, channel)), 8000, false)); if (result == null || result.Status == null) return new ResponseResult(SwitchConfigurationErrorCode.OtherError, `Cannot get turn on behaviour - unknown error.`); if (result.Status !== Status.OK) return new ResponseResult(SwitchConfigurationErrorCode.OtherError, `Cannot get turn on behaviour - controller responded with error code: ${result.Status}.`); if (result.Data == null) return new ResponseResult(SwitchConfigurationErrorCode.NoData, `Cannot get turn on behaviour - no data in response.`); const data = new StateOfTurnOnSwitchDeviceParameters(); data.Channel = result.Data.Channel; data.State = result.Data.State; //for some reasons, as default ROP sends 16 when state was not set after reset (?) if (data.State === 16) data.State = SwitchTurnOnState.Disabled; return data; } catch (error) { return new ResponseResult(SwitchConfigurationErrorCode.OtherError, `Failed to retrieve turn on behaviour - unknown error.`); } }); } SetTurnOnBehaviourAsync(device, params) { return __awaiter(this, void 0, void 0, function* () { var _a; try { const request = new StateOfTurnOnDeviceParametersFrame(); request.DeviceGuid = device.Guid; request.Channel = params.Channel; request.State = params.State; const result = yield ((_a = this._connection) === null || _a === void 0 ? void 0 : _a.SendAndWaitForResponseAsync(new SetTurnOffBehaviourRequest(request), 8000, false)); if (result == null || result.Status == null) { this._logger.Error(OnOffSwitchService.ServiceName, `Failed to set turn on behaviour - unknow error.`); return Status.FatalError; } if (result.Status !== Status.OK) this._logger.Error(OnOffSwitchService.ServiceName, `Failed to set turn on behaviour, response status: ${result.Status}`); return result.Status; } catch (error) { return Status.FatalError; } }); } GetGlobalTimeSettingsAsync(device, channel) { return __awaiter(this, void 0, void 0, function* () { var _a; try { if (!device.Channels.any(ch => ch.Number === channel)) return new ResponseResult(SwitchConfigurationErrorCode.InvalidChannelNumber, `Cannot get global time settings - invalid channel number, channel: ${channel}`); const result = yield ((_a = this._connection) === null || _a === void 0 ? void 0 : _a.SendAndWaitForResponseAsync(new GetGlobalTimeSettingsRequest(new GetChannelParamsRequest(device.Guid, channel)), 8000, false)); if (result == null || result.Status == null) return new ResponseResult(SwitchConfigurationErrorCode.OtherError, `Cannot get global time settings - unknown error.`); if (result.Status !== Status.OK) return new ResponseResult(SwitchConfigurationErrorCode.OtherError, `Cannot get global time settings - controller responded with error code: ${result.Status}.`); if (result.Data == null) return new ResponseResult(SwitchConfigurationErrorCode.NoData, `Cannot get global time settings - no data in response.`); const data = new GlobalTimeParameters(); data.Channel = result.Data.Channel; data.Time = this.ParseNumberToTime(result.Data.Time); return data; } catch (error) { return new ResponseResult(SwitchConfigurationErrorCode.OtherError, `Cannot get global time settings - unknown error.`); } }); } SetGlobalTimeSettingsAsync(device, params) { return __awaiter(this, void 0, void 0, function* () { var _a; try { const time = params.Time.Hours * 3600 + params.Time.Minutes * 60 + params.Time.Seconds; const request = new GlobalTimeParametersFrame(); request.Channel = params.Channel; request.DeviceGuid = device.Guid; request.Time = time; const result = yield ((_a = this._connection) === null || _a === void 0 ? void 0 : _a.SendAndWaitForResponseAsync(new SetGlobalTimeSettingsRequest(request), 8000, false)); if (result == null || result.Status == null) { this._logger.Error(OnOffSwitchService.ServiceName, `Failed to set global time settings - unknow error.`); return Status.FatalError; } if (result.Status !== Status.OK) this._logger.Error(OnOffSwitchService.ServiceName, `Failed to set global time settings, response status: ${result.Status}`); return result.Status; } catch (error) { return Status.FatalError; } }); } GetTurnOnTimeAsync(device, channel) { return __awaiter(this, void 0, void 0, function* () { var _a; try { if (!device.Channels.any(ch => ch.Number === channel)) return new ResponseResult(SwitchConfigurationErrorCode.InvalidChannelNumber, `Cannot get turn on time - invalid channel number, channel: ${channel}`); const result = yield ((_a = this._connection) === null || _a === void 0 ? void 0 : _a.SendAndWaitForResponseAsync(new GetTurnOnTimeSettingsRequest(new GetChannelParamsRequest(device.Guid, channel)), 8000, false)); if (result == null || result.Status == null) return new ResponseResult(SwitchConfigurationErrorCode.OtherError, `Cannot get turn on time - unknown error.`); if (result.Status !== Status.OK) return new ResponseResult(SwitchConfigurationErrorCode.OtherError, `Cannot get turn on time - controller responded with error code: ${result.Status}.`); if (result.Data == null) return new ResponseResult(SwitchConfigurationErrorCode.NoData, `Cannot get turn on time - no data in response.`); const data = new GlobalTimeParameters(); data.Channel = result.Data.Channel; data.Time = this.ParseNumberToTime(result.Data.Time); return data; } catch (error) { return new ResponseResult(SwitchConfigurationErrorCode.OtherError, `Cannot get turn on time - unknown error.`); } }); } SetTurnOnTimeAsync(device, params) { return __awaiter(this, void 0, void 0, function* () { var _a; try { const time = params.Time.Hours * 3600 + params.Time.Minutes * 60 + params.Time.Seconds; const request = new GlobalTimeParametersFrame(); request.Channel = params.Channel; request.DeviceGuid = device.Guid; request.Time = time; const result = yield ((_a = this._connection) === null || _a === void 0 ? void 0 : _a.SendAndWaitForResponseAsync(new SetTurnOnTimeSettingsRequest(request), 8000, false)); if (result == null || result.Status == null) { this._logger.Error(OnOffSwitchService.ServiceName, `Failed to set turn on time settings - unknow error.`); return Status.FatalError; } if (result.Status !== Status.OK) this._logger.Error(OnOffSwitchService.ServiceName, `Failed to set turn on time settings, response status: ${result.Status}`); return result.Status; } catch (error) { return Status.FatalError; } }); } GetSwitchInputOutputParamAsync(device, channel) { return __awaiter(this, void 0, void 0, function* () { var _a; try { if (!device.Channels.any(ch => ch.Number === channel)) return new ResponseResult(SwitchConfigurationErrorCode.InvalidChannelNumber, `Cannot get input/output parameters - invalid channel number, channel: ${channel}`); const result = yield ((_a = this._connection) === null || _a === void 0 ? void 0 : _a.SendAndWaitForResponseAsync(new GetSwitchInputOutputParamRequest(new GetChannelParamsRequest(device.Guid, channel)), 8000, false)); if (result == null || result.Status == null) return new ResponseResult(SwitchConfigurationErrorCode.OtherError, `Cannot get input/output parameters - unknown error.`); if (result.Status !== Status.OK) return new ResponseResult(SwitchConfigurationErrorCode.OtherError, `Cannot get input/output parameters - controller responded with error code: ${result.Status}.`); if (result.Data == null) return new ResponseResult(SwitchConfigurationErrorCode.NoData, `Cannot get input/output parameter - no data in response.`); return result.Data.map(param => { const res = new InputOutputSwitchParametersLocal(); res.Channel = param.Channel; res.InputMode = param.InputMode; res.InputsNumber = param.InputsNumber; res.ReactionToInput = param.ReactionToInput; res.InputChannel = param.InputChannel; return res; }); } catch (error) { return new ResponseResult(SwitchConfigurationErrorCode.OtherError, `Cannot get input/output parameters - unknown error.`); } }); } SetSwitchInputOutputParamAsync(device, params) { return __awaiter(this, void 0, void 0, function* () { var _a; try { if (params.InputMode == InputModeSwitch.Bistable && params.ReactionToInput == ReactionToInput.Bi) { this._logger.Error(OnOffSwitchService.ServiceName, `Failed to set input/output parameters - reaction to input cannot be 'bistable' when mode is set to 'bistable', switch mode to 'monostable' and call fucntion again.`); return Status.WrongData; } const request = new InputOutputSwitchParametersFrame(); request.DeviceGuid = device.Guid; request.Channel = params.Channel; request.InputChannel = params.InputChannel; request.ReactionToInput = params.ReactionToInput; request.InputMode = params.InputMode; const result = yield ((_a = this._connection) === null || _a === void 0 ? void 0 : _a.SendAndWaitForResponseAsync(new SetSwitchInputOutputParamRequest(request), 8000, false)); if (result == null || result.Status == null) { this._logger.Error(OnOffSwitchService.ServiceName, `Failed to set input/output parameters - unknow error.`); return Status.FatalError; } if (result.Status !== Status.OK) this._logger.Error(OnOffSwitchService.ServiceName, `Failed to set input/output parameters, response status: ${result.Status}`); return result.Status; } catch (error) { return Status.FatalError; } }); } SetSwitchInputOutputInputModeParamAsync(device, params) { return __awaiter(this, void 0, void 0, function* () { var _a; try { const current = yield this.GetSwitchInputOutputParamAsync(device, params.Channel); if (current.Type != null) { this._logger.Error(OnOffSwitchService.ServiceName, `Failed to set input/output parameters - cannot get current configuration.`); return Status.Error; } const currentConfig = current.firstOrDefault(e => e.Channel == params.Channel && e.InputChannel == params.InputChannel); if (currentConfig == null) { this._logger.Error(OnOffSwitchService.ServiceName, `Failed to set input/output parameters - channel or inputChannel params may be incorrect.`); return Status.WrongData; } const request = new InputOutputSwitchParametersFrame(); request.DeviceGuid = device.Guid; request.Channel = params.Channel; request.InputChannel = params.InputChannel; request.InputMode = params.InputMode; request.ReactionToInput = currentConfig.ReactionToInput; const result = yield ((_a = this._connection) === null || _a === void 0 ? void 0 : _a.SendAndWaitForResponseAsync(new SetSwitchInputOutputParamRequest(request), 8000, false)); if (result == null || result.Status == null) { this._logger.Error(OnOffSwitchService.ServiceName, `Failed to set input/output parameters - unknow error.`); return Status.FatalError; } if (result.Status !== Status.OK) this._logger.Error(OnOffSwitchService.ServiceName, `Failed to set input/output parameters, response status: ${result.Status}`); return result.Status; } catch (error) { return Status.FatalError; } }); } SetSwitchInputOutputReactionToInputParamAsync(device, params) { return __awaiter(this, void 0, void 0, function* () { var _a; try { const current = yield this.GetSwitchInputOutputParamAsync(device, params.Channel); if (current.Type != null) { this._logger.Error(OnOffSwitchService.ServiceName, `Failed to set input/output parameters - cannot get current configuration.`); return Status.Error; } const currentConfig = current.firstOrDefault(e => e.Channel == params.Channel && e.InputChannel == params.InputChannel); if (currentConfig == null) { this._logger.Error(OnOffSwitchService.ServiceName, `Failed to set input/output parameters - channel or inputChannel params may be incorrect.`); return Status.WrongData; } if (currentConfig.InputMode == InputModeSwitch.Bistable && params.ReactionToInput == ReactionToInput.Bi) { this._logger.Error(OnOffSwitchService.ServiceName, `Failed to set input/output parameters - reaction to input cannot be 'bistable' when mode is set to 'bistable', switch mode to 'monostable' and call fucntion again.`); return Status.WrongData; } const request = new InputOutputSwitchParametersFrame(); request.DeviceGuid = device.Guid; request.Channel = params.Channel; request.InputChannel = params.InputChannel; request.ReactionToInput = params.ReactionToInput; request.InputMode = currentConfig.InputMode; const result = yield ((_a = this._connection) === null || _a === void 0 ? void 0 : _a.SendAndWaitForResponseAsync(new SetSwitchInputOutputParamRequest(request), 8000, false)); if (result == null || result.Status == null) { this._logger.Error(OnOffSwitchService.ServiceName, `Failed to set input/output parameters - unknow error.`); return Status.FatalError; } if (result.Status !== Status.OK) this._logger.Error(OnOffSwitchService.ServiceName, `Failed to set input/output parameters, response status: ${result.Status}`); return result.Status; } catch (error) { return Status.FatalError; } }); } ParseNumberToTime(data) { const result = new Time(); result.Hours = Math.floor(data / 3600); result.Minutes = Math.floor((data % 3600) / 60); result.Seconds = Math.floor((data % 3600) % 60); result.Raw = data; return result; } } OnOffSwitchService.ServiceName = "OnOffSwitchService"; export var SwitchConfigurationErrorCode; (function (SwitchConfigurationErrorCode) { SwitchConfigurationErrorCode["InvalidChannelNumber"] = "InvalidChannelNumber"; SwitchConfigurationErrorCode["OtherError"] = "OtherError"; SwitchConfigurationErrorCode["NoData"] = "NoData"; })(SwitchConfigurationErrorCode || (SwitchConfigurationErrorCode = {})); //TurnOn behaviour class GetTurnOnBehaviourRequest extends DataFrame { constructor(data) { super(); this.Resource = "/extalife/device/parameters/state/turnon"; this.Method = Method.Get; this.Data = data; } } class StateOfTurnOnDeviceParametersFrame extends StateOfTurnOnSwitchDeviceParameters { constructor() { super(...arguments); this.DeviceGuid = ""; } } class SetTurnOffBehaviourRequest extends DataFrame { constructor(data) { super(); this.Resource = "/extalife/device/parameters/state/turnon"; this.Method = Method.Put; this.Data = data; } } //Global time class Time extends TimeParam { set Raw(value) { this._raw = value; } } class GetGlobalTimeSettingsRequest extends DataFrame { constructor(data) { super(); this.Resource = "/extalife/device/parameters/global/time"; this.Method = Method.Get; this.Data = data; } } class SetGlobalTimeSettingsRequest extends DataFrame { constructor(data) { super(); this.Resource = "/extalife/device/parameters/global/time"; this.Method = Method.Put; this.Data = data; } } class GlobalTimeParametersFrame { constructor() { this.Channel = 0; this.DeviceGuid = ""; this.Time = 0; } } //TurnOn time class GetTurnOnTimeSettingsRequest extends DataFrame { constructor(data) { super(); this.Resource = "/extalife/device/parameters/device/on/time"; this.Method = Method.Get; this.Data = data; } } class SetTurnOnTimeSettingsRequest extends DataFrame { constructor(data) { super(); this.Resource = "/extalife/device/parameters/device/on/time"; this.Method = Method.Put; this.Data = data; } } //Input/output class InputOutputSwitchParametersLocal { constructor() { this._channel = 0; this._inputsNumber = 0; this._inputMode = InputModeSwitch.Monostable; this._reactionToInput = ReactionToInput.None; this._inputChannel = 0; } get Channel() { return this._channel; } set Channel(value) { this._channel = value; } get InputsNumber() { return this._inputsNumber; } set InputsNumber(value) { this._inputsNumber = value; } get InputMode() { return this._inputMode; } set InputMode(value) { this._inputMode = value; } get ReactionToInput() { return this._reactionToInput; } set ReactionToInput(value) { this._reactionToInput = value; } get InputChannel() { return this._inputChannel; } set InputChannel(value) { this._inputChannel = value; } } class InputOutputSwitchParametersFrame { constructor() { this.DeviceGuid = ""; this.Channel = 0; this.InputChannel = 0; this.InputMode = InputModeSwitch.Bistable; this.ReactionToInput = ReactionToInput.None; } } class GetSwitchInputOutputParamRequest extends DataFrame { constructor(data) { super(); this.Resource = "/extalife/device/parameters/input/output"; this.Method = Method.Get; this.Data = data; } } class SetSwitchInputOutputParamRequest extends DataFrame { constructor(data) { super(); this.Resource = "/extalife/device/parameters/input/output"; this.Method = Method.Put; this.Data = data; } } //# sourceMappingURL=OnOffSwitchService.js.map