UNPKG

lavva.exalushome.extalife

Version:

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

434 lines 24.2 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 { ControllerConfigurationService } from "lavva.exalushome/build/js/Services/Controller/ControllerConfigurationService"; import { LoggerService } from "lavva.exalushome/build/js/Services/Logging/LoggerService"; import { GetParamsErrorCode } from "../../../ExtaLife"; import { GetChannelParamsRequest } from "../Common/DataRequest"; import { LimitSwitchConfiguration, TypeOfGateChannel } from "./Rob21ConfigParams"; import { DevicesService } from "lavva.exalushome/build/js/Services/Devices/DevicesService"; export class Rob21ConfigService { constructor() { this._connection = Api.Get(ExalusConnectionService.ServiceName); this._logger = Api.Get(LoggerService.ServiceName); this._controllerConfigurationService = Api.Get(ControllerConfigurationService.ServiceName); } GetServiceName() { return Rob21ConfigService.ServiceName; } GetGateModeAsync(device, channel) { return __awaiter(this, void 0, void 0, function* () { var _a; try { if (!device.Channels.any(ch => ch.Number === channel)) return new ResponseResult(GetParamsErrorCode.InvalidChannelNumber, `Cannot get gate working mode - invalid channel number, channel: ${channel}`); const result = yield ((_a = this._connection) === null || _a === void 0 ? void 0 : _a.SendAndWaitForResponseAsync(new GetChannelTypeRequest(new GetChannelParamsRequest(device.Guid, channel)), 8000, false)); if (result == null || result.Status == null) return new ResponseResult(GetParamsErrorCode.OtherError, `Cannot get gate working mode - unknown error.`); if (result.Status !== Status.OK) return new ResponseResult(GetParamsErrorCode.OtherError, `Cannot get gate working mode - controller responded with error code: ${result.Status}.`); if (result.Data == null) return new ResponseResult(GetParamsErrorCode.NoData, `Cannot get gate working mode - no data in response.`); return result.Data.TypeOfGateChannel; } catch (error) { return new ResponseResult(GetParamsErrorCode.OtherError, `Cannot get gate working mode - unknown error.`); } }); } SetGateModeAsync(device, channel, mode) { return __awaiter(this, void 0, void 0, function* () { var _a; try { const request = new TimeAndTypeOfChannelParameters(); request.DeviceGuid = device.Guid; request.Channel = channel; request.TypeOfGateChannel = mode; yield this._controllerConfigurationService.EnterConfigurationModeAsync(); const result = yield ((_a = this._connection) === null || _a === void 0 ? void 0 : _a.SendAndWaitForResponseAsync(new SetChannelTypeRequest(request), 8000, false)); if (result == null || result.Status == null) { this._logger.Error(Rob21ConfigService.ServiceName, `Failed to set working mode parameter - unknown error.`); return Status.FatalError; } if (result.Status !== Status.OK) this._logger.Error(Rob21ConfigService.ServiceName, `Failed to set working mode parameter, response status: ${result.Status}`); return result.Status; } catch (error) { this._logger.Error(Rob21ConfigService.ServiceName, `Failed to set working mode parameter - unknown error.`); return Status.FatalError; } finally { this._logger.Error(Rob21ConfigService.ServiceName, `EXIT CONFIGURATION MODE WILL BE FIRED! SetGateModeAsync()`); yield this._controllerConfigurationService.ExitConfigurationModeAsync(); Api.Get(DevicesService.ServiceName).GetDevicesAsync(); } }); } GetPulseTimeAsync(device, channel) { return __awaiter(this, void 0, void 0, function* () { var _a; try { if (!device.Channels.any(ch => ch.Number === channel)) return new ResponseResult(GetParamsErrorCode.InvalidChannelNumber, `Cannot get pulse time - invalid channel number, channel: ${channel}`); const result = yield ((_a = this._connection) === null || _a === void 0 ? void 0 : _a.SendAndWaitForResponseAsync(new GetPulseTimeRequest(new GetChannelParamsRequest(device.Guid, channel)), 8000, false)); if (result == null || result.Status == null) return new ResponseResult(GetParamsErrorCode.OtherError, `Cannot get pulse time - unknown error.`); if (result.Status !== Status.OK) return new ResponseResult(GetParamsErrorCode.OtherError, `Cannot get pulse time - controller responded with error code: ${result.Status}.`); if (result.Data == null) return new ResponseResult(GetParamsErrorCode.NoData, `Cannot get pulse time - no data in response.`); return result.Data.PulseTime / 10; } catch (error) { return new ResponseResult(GetParamsErrorCode.OtherError, `Cannot get pulse time - unknown error.`); } }); } SetPulseTimeAsync(device, channel, pulseTime) { return __awaiter(this, void 0, void 0, function* () { var _a; try { if (pulseTime < 0.1 || pulseTime > 300 || !(pulseTime * Math.pow(10, 1) % 1 == 0)) { this._logger.Error(Rob21ConfigService.ServiceName, `Failed to set pulse time - value is out of range!`); return Status.WrongData; } const currentMode = yield this.GetGateModeAsync(device, channel); if (currentMode.Type != null) { this._logger.Error(Rob21ConfigService.ServiceName, `Failed to set pulse time - cannot get current mode of gate controller.`); return Status.Error; } else if (currentMode == TypeOfGateChannel.Monostable) { this._logger.Error(Rob21ConfigService.ServiceName, `Failed to set pulse time - cannot set pulse time when channel mode is 'monostable'.`); return Status.OperationNotPermitted; } const request = new TimeAndTypeOfChannelParameters(); request.DeviceGuid = device.Guid; request.Channel = channel; request.PulseTime = pulseTime * 10; yield this._controllerConfigurationService.EnterConfigurationModeAsync(); const result = yield ((_a = this._connection) === null || _a === void 0 ? void 0 : _a.SendAndWaitForResponseAsync(new SetPulseTimeRequest(request), 8000, false)); if (result == null || result.Status == null) { this._logger.Error(Rob21ConfigService.ServiceName, `Failed to set pulse time - unknown error.`); return Status.FatalError; } if (result.Status !== Status.OK) this._logger.Error(Rob21ConfigService.ServiceName, `Failed to set pulse time, response status: ${result.Status}`); return result.Status; } catch (error) { this._logger.Error(Rob21ConfigService.ServiceName, `Failed to set pulse time - unknown error.`); return Status.FatalError; } finally { this._logger.Error(Rob21ConfigService.ServiceName, `EXIT CONFIGURATION MODE WILL BE FIRED! SetPulseTimeAsync()`); yield this._controllerConfigurationService.ExitConfigurationModeAsync(); } }); } GetLimitingSwitchesConfigurationsAsync(device, channel) { return __awaiter(this, void 0, void 0, function* () { var _a, _b; try { const resultSwitch1 = yield ((_a = this._connection) === null || _a === void 0 ? void 0 : _a.SendAndWaitForResponseAsync(new GetLimitSwitchRequest(new LimitSwitchRequestFrame(device.Guid, 1)), 8000, false)); const resultSwitch2 = yield ((_b = this._connection) === null || _b === void 0 ? void 0 : _b.SendAndWaitForResponseAsync(new GetLimitSwitchRequest(new LimitSwitchRequestFrame(device.Guid, 2)), 8000, false)); if ((resultSwitch1 == null || resultSwitch1.Status == null || resultSwitch1.Data == null) || (resultSwitch2 == null || resultSwitch2.Status == null || resultSwitch2.Data == null)) { return new ResponseResult(GetParamsErrorCode.OtherError, `Cannot get limiting switch configuration - unknown error.`); } if (resultSwitch1.Status !== Status.OK || resultSwitch2.Status !== Status.OK) return new ResponseResult(GetParamsErrorCode.OtherError, `Cannot get limiting switch configuration - controller responded with error code: ${resultSwitch1.Status != Status.OK ? resultSwitch1.Status : resultSwitch2.Status}.`); const resultArray = []; resultArray.push(resultSwitch1.Data); resultArray.push(resultSwitch2.Data); return resultArray.map(r => { const config = new LimitSwitchConfiguration(); config.Channel = r.Channel; config.Function = r.Function; config.Input = r.Input; config.Type = r.Type; return config; }); } catch (error) { return new ResponseResult(GetParamsErrorCode.OtherError, `Cannot get limiting switch configuration - unknown error.`); } }); } SetLimitingSwitchConfigurationAsync(device, params) { return __awaiter(this, void 0, void 0, function* () { var _a; try { const currentMode = yield this.GetGateModeAsync(device, params.Channel); if (currentMode.Type != null) { this._logger.Error(Rob21ConfigService.ServiceName, `Failed to set limiting switch parameters - cannot get current mode of gate controller.`); return Status.Error; } else if (currentMode == TypeOfGateChannel.Monostable) { this._logger.Error(Rob21ConfigService.ServiceName, `Failed to set limiting switch parameters - cannot set this params when channel mode is 'monostable'.`); return Status.OperationNotPermitted; } yield this._controllerConfigurationService.EnterConfigurationModeAsync(); const result = yield ((_a = this._connection) === null || _a === void 0 ? void 0 : _a.SendAndWaitForResponseAsync(new SetLimitSwitchRequest(Object.assign(Object.assign({}, params), { DeviceGuid: device.Guid })), 8000, false)); if (result == null || result.Status == null) { this._logger.Error(Rob21ConfigService.ServiceName, `Failed to set limiting switch parameters - unknown error.`); return Status.FatalError; } if (result.Status !== Status.OK) this._logger.Error(Rob21ConfigService.ServiceName, `Failed to set limiting switch parameters, response status: ${result.Status}`); return result.Status; } catch (error) { this._logger.Error(Rob21ConfigService.ServiceName, `Failed to set limiting switch parameters - unknown error.`); return Status.FatalError; } finally { this._logger.Error(Rob21ConfigService.ServiceName, `EXIT CONFIGURATION MODE WILL BE FIRED! SetLimitingSwitchConfigurationAsync()`); yield this._controllerConfigurationService.ExitConfigurationModeAsync(); } }); } GetTimeBetweenPulsesAsync(device, channel) { return __awaiter(this, void 0, void 0, function* () { var _a; try { if (!device.Channels.any(ch => ch.Number === channel)) return new ResponseResult(GetParamsErrorCode.InvalidChannelNumber, `Cannot get time between pulses - invalid channel number, channel: ${channel}`); const result = yield ((_a = this._connection) === null || _a === void 0 ? void 0 : _a.SendAndWaitForResponseAsync(new GetTimeBetweenPulsesRequest(new GetChannelParamsRequest(device.Guid, channel)), 8000, false)); if (result == null || result.Status == null) return new ResponseResult(GetParamsErrorCode.OtherError, `Cannot get time between pulses - unknown error.`); if (result.Status !== Status.OK) return new ResponseResult(GetParamsErrorCode.OtherError, `Cannot get time between pulses - controller responded with error code: ${result.Status}.`); if (result.Data == null) return new ResponseResult(GetParamsErrorCode.NoData, `Cannot get time between pulses - no data in response.`); return result.Data.TimeBetwenPulse / 10; } catch (error) { return new ResponseResult(GetParamsErrorCode.OtherError, `Cannot get time between pulses - unknown error.`); } }); } SetTimeBetweenPulsesAsync(device, channel, timeBetweenPulse) { return __awaiter(this, void 0, void 0, function* () { var _a; try { if (timeBetweenPulse < 1 || timeBetweenPulse > 300 || timeBetweenPulse % 1 != 0) { this._logger.Error(Rob21ConfigService.ServiceName, `Failed to set time between pulses - value is out of range!`); return Status.WrongData; } const currentMode = yield this.GetGateModeAsync(device, channel); if (currentMode.Type != null) { this._logger.Error(Rob21ConfigService.ServiceName, `Failed to set time between pulses - cannot get current mode of gate controller.`); return Status.Error; } else if (currentMode != TypeOfGateChannel.Gateway) { this._logger.Error(Rob21ConfigService.ServiceName, `Failed to set time between pulses - cannot set this parameter when channel mode is 'monostable' or 'gate'.`); return Status.OperationNotPermitted; } const request = new TimeAndTypeOfChannelParameters(); request.DeviceGuid = device.Guid; request.Channel = channel; request.TimeBetwenPulse = timeBetweenPulse * 10; yield this._controllerConfigurationService.EnterConfigurationModeAsync(); const result = yield ((_a = this._connection) === null || _a === void 0 ? void 0 : _a.SendAndWaitForResponseAsync(new SetTimeBetweenPulsesRequest(request), 8000, false)); if (result == null || result.Status == null) { this._logger.Error(Rob21ConfigService.ServiceName, `Failed to set time between pulses - unknown error.`); return Status.FatalError; } if (result.Status !== Status.OK) this._logger.Error(Rob21ConfigService.ServiceName, `Failed to set time between pulses, response status: ${result.Status}`); return result.Status; } catch (error) { this._logger.Error(Rob21ConfigService.ServiceName, `Failed to set time between pulses - unknown error.`); return Status.FatalError; } finally { this._logger.Error(Rob21ConfigService.ServiceName, `EXIT CONFIGURATION MODE WILL BE FIRED! SetTimeBetweenPulsesAsync()`); yield this._controllerConfigurationService.ExitConfigurationModeAsync(); } }); } GetOpeningTime(device, channel) { return __awaiter(this, void 0, void 0, function* () { var _a; try { if (!device.Channels.any(ch => ch.Number === channel)) return new ResponseResult(GetParamsErrorCode.InvalidChannelNumber, `Cannot get gate opening time - invalid channel number, channel: ${channel}`); const result = yield ((_a = this._connection) === null || _a === void 0 ? void 0 : _a.SendAndWaitForResponseAsync(new GetOpeningTimeRequest(new GetChannelParamsRequest(device.Guid, channel)), 8000, false)); if (result == null || result.Status == null) return new ResponseResult(GetParamsErrorCode.OtherError, `Cannot get gate opening time - unknown error.`); if (result.Status !== Status.OK) return new ResponseResult(GetParamsErrorCode.OtherError, `Cannot get gate opening time - controller responded with error code: ${result.Status}.`); if (result.Data == null) return new ResponseResult(GetParamsErrorCode.NoData, `Cannot get gate opening time - no data in response.`); return result.Data.GateOpenTime; } catch (error) { return new ResponseResult(GetParamsErrorCode.OtherError, `Cannot get gate opening time - unknown error.`); } }); } SetOpeningTimeAsync(device, channel, openingTime) { return __awaiter(this, void 0, void 0, function* () { var _a; try { if (openingTime < 1 || openingTime > 300 || openingTime % 1 != 0) { this._logger.Error(Rob21ConfigService.ServiceName, `Failed to set opening time - value is out of range!`); return Status.WrongData; } const currentMode = yield this.GetGateModeAsync(device, channel); if (currentMode.Type != null) { this._logger.Error(Rob21ConfigService.ServiceName, `Failed to set opening time - cannot get current mode of gate controller.`); return Status.Error; } else if (currentMode != TypeOfGateChannel.Gateway) { this._logger.Error(Rob21ConfigService.ServiceName, `Failed to set opening time - cannot set this parameter when channel mode is 'monostable' or 'gate'.`); return Status.OperationNotPermitted; } const request = new TimeAndTypeOfChannelParameters(); request.DeviceGuid = device.Guid; request.Channel = channel; request.GateOpenTime = openingTime; yield this._controllerConfigurationService.EnterConfigurationModeAsync(); const result = yield ((_a = this._connection) === null || _a === void 0 ? void 0 : _a.SendAndWaitForResponseAsync(new SetOpeningTimeRequest(request), 8000, false)); if (result == null || result.Status == null) { this._logger.Error(Rob21ConfigService.ServiceName, `Failed to set opening time - unknown error.`); return Status.FatalError; } if (result.Status !== Status.OK) this._logger.Error(Rob21ConfigService.ServiceName, `Failed to set opening time, response status: ${result.Status}`); return result.Status; } catch (error) { this._logger.Error(Rob21ConfigService.ServiceName, `Failed to set opening time - unknown error.`); return Status.FatalError; } finally { this._logger.Error(Rob21ConfigService.ServiceName, `EXIT CONFIGURATION MODE WILL BE FIRED! SetOpeningTimeAsync()`); yield this._controllerConfigurationService.ExitConfigurationModeAsync(); } }); } } Rob21ConfigService.ServiceName = "Rob21ConfigService"; class TimeAndTypeOfChannelParameters { constructor() { this.Channel = 0; this.DeviceGuid = ""; this.PulseTime = 0; this.TimeBetwenPulse = 0; this.GateOpenTime = 0; this.TypeOfGateChannel = TypeOfGateChannel.Gate; } } //Mode class GetChannelTypeRequest extends DataFrame { constructor(data) { super(); this.Resource = "/extalife/device/parameters/channel/type"; this.Method = Method.Get; this.Data = data; } } class SetChannelTypeRequest extends DataFrame { constructor(data) { super(); this.Resource = "/extalife/device/parameters/channel/type"; this.Method = Method.Put; this.Data = data; } } //Pulse time class GetPulseTimeRequest extends DataFrame { constructor(data) { super(); this.Resource = "/extalife/device/parameters/time/pulse"; this.Method = Method.Get; this.Data = data; } } class SetPulseTimeRequest extends DataFrame { constructor(data) { super(); this.Resource = "/extalife/device/parameters/time/pulse"; this.Method = Method.Put; this.Data = data; } } //LimitSwitch class LimitSwitchRequestFrame { constructor(guid, input) { this.DeviceGuid = ""; this.Input = 0; this.DeviceGuid = guid; this.Input = input; } } class LimitSwitchConfigurationFrame extends LimitSwitchConfiguration { constructor() { super(...arguments); this.DeviceGuid = ""; } } class GetLimitSwitchRequest extends DataFrame { constructor(data) { super(); this.Resource = "/extalife/device/parameters/inputs/configuration"; this.Method = Method.Get; this.Data = data; } } class SetLimitSwitchRequest extends DataFrame { constructor(data) { super(); this.Resource = "/extalife/device/parameters/inputs/configuration"; this.Method = Method.Put; this.Data = data; } } //Time between pulses class GetTimeBetweenPulsesRequest extends DataFrame { constructor(data) { super(); this.Resource = "/extalife/device/parameters/time/between/pulse"; this.Method = Method.Get; this.Data = data; } } class SetTimeBetweenPulsesRequest extends DataFrame { constructor(data) { super(); this.Resource = "/extalife/device/parameters/time/between/pulse"; this.Method = Method.Put; this.Data = data; } } //OpeningTime class GetOpeningTimeRequest extends DataFrame { constructor(data) { super(); this.Resource = "/extalife/device/parameters/time/gate/opening"; this.Method = Method.Get; this.Data = data; } } class SetOpeningTimeRequest extends DataFrame { constructor(data) { super(); this.Resource = "/extalife/device/parameters/time/gate/opening"; this.Method = Method.Put; this.Data = data; } } //# sourceMappingURL=Rob21ConfigService.js.map