UNPKG

lavva.exalushome.extalife

Version:

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

182 lines 10.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 { DependencyContainer } from "lavva.exalushome/build/js/DependencyContainer"; import { ExalusConnectionService } from "lavva.exalushome/build/js/Services/ExalusConnectionService"; import { ResponseResult } from "lavva.exalushome/build/js/Services/FieldChangeResult"; import { GetParamsErrorCode } from "../../../../../ExtaLife"; import { GetChannelParamsRequest, GetParamsRequest } from "../../../Common/DataRequest"; import { MultisensorDetectionThresholdParams, MultisensorIntervalParams, Rcm21Channels } from "../../Rcm21ConfigParams"; import { Rcm21ConfigService } from "../../Rcm21ConfigService"; export class Rcm21VersionedService { constructor() { this._connection = null; this._connection = Api.Get(ExalusConnectionService.ServiceName); } GetDetectionThresholdDataAsync(device, channel) { return __awaiter(this, void 0, void 0, function* () { var _a; const result = yield ((_a = this._connection) === null || _a === void 0 ? void 0 : _a.SendAndWaitForResponseAsync(new GetDetectionThresholdParams(device.Guid, channel.valueOf()), 35000, false)); if (result === null || result === undefined) return new ResponseResult(GetParamsErrorCode.UnknownError, `Failed to retrieve parameters - unknow error.`); switch (result.Status) { case Status.OK: if (result.Data != null) { result.Data.Threshold = result.Data.Threshold / 1000; return result.Data; } break; case Status.ResourceIsNotAvailable: return new ResponseResult(GetParamsErrorCode.ResourceIsNotAvailable, `Failed to retrieve parameters - cannot communicate with the device.`); } return new ResponseResult(GetParamsErrorCode.OtherError, `Failed to retrieve parameters, response error: ${result.Status} data: ${result.Data}`); }); } SetDetectionThresholdAsync(device, channel, threshold) { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d, _e; try { switch (channel) { case Rcm21Channels.PressureChannel: if (threshold < .3 || threshold > 30 || !(threshold * Math.pow(10, 1) % 1 == 0)) return Status.WrongData; break; case Rcm21Channels.HuminidityChannel: if (threshold < 1 || threshold > 99 || !Number.isInteger(threshold)) return Status.WrongData; break; case Rcm21Channels.PressureChannel: case Rcm21Channels.BrightnessChannel: if (threshold < 1 || threshold > 100 || !Number.isInteger(threshold)) return Status.WrongData; break; default: return Status.WrongData; } const currentConfig = yield this.GetCurrentDetectionThresholdConfigAsync(device.Guid, channel.valueOf()); const params = new MultisensorDetectionThresholdParams(); if (currentConfig === null) { (_a = DependencyContainer.Log) === null || _a === void 0 ? void 0 : _a.Error(Rcm21ConfigService.ServiceName, `Failed to change device parameters - cannot read current configuration of device`); return Status.ResourceIsNotAvailable; } params.DeviceGuid = device.Guid; params.Channel = channel; params.Hysteresis = currentConfig.Hysteresis; params.Threshold = threshold * 1000; const result = yield ((_b = this._connection) === null || _b === void 0 ? void 0 : _b.SendAndWaitForResponseAsync(new SetDetectionThresholdParamsRequest(params), 35000, false)); if (result == null || result.Status == null) { (_c = DependencyContainer.Log) === null || _c === void 0 ? void 0 : _c.Error(Rcm21ConfigService.ServiceName, `Failed to change device parameters - unknow error.`); return Status.FatalError; } if (result.Status !== Status.OK) (_d = DependencyContainer.Log) === null || _d === void 0 ? void 0 : _d.Error(Rcm21ConfigService.ServiceName, `Failed to change device parameters, response status: ${result.Status}`); return result.Status; } catch (error) { (_e = DependencyContainer.Log) === null || _e === void 0 ? void 0 : _e.Error(Rcm21ConfigService.ServiceName, `Cannot change device parameters. ${error}`); return Status.FatalError; } }); } GetFramesIntervalAsync(device, channel) { return __awaiter(this, void 0, void 0, function* () { var _a; const result = yield ((_a = this._connection) === null || _a === void 0 ? void 0 : _a.SendAndWaitForResponseAsync(new GetIntervalParams(device.Guid), 35000, false)); if (result === null || result === undefined) return new ResponseResult(GetParamsErrorCode.UnknownError, `Failed to retrieve parameters - unknow error.`); switch (result.Status) { case Status.OK: if (result.Data != null) { return result.Data; } break; case Status.ResourceIsNotAvailable: return new ResponseResult(GetParamsErrorCode.ResourceIsNotAvailable, `Failed to retrieve parameters - cannot communicate with the device.`); } return new ResponseResult(GetParamsErrorCode.OtherError, `Failed to retrieve parameters, response error: ${result.Status} data: ${result.Data}`); }); } SetFramesIntervalAsync(device, channel, interval) { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d; try { if (interval < 1 || interval > 6480) return Status.WrongData; const params = new MultisensorIntervalParams(); params.DeviceGuid = device.Guid; params.Interval = interval; const result = yield ((_a = this._connection) === null || _a === void 0 ? void 0 : _a.SendAndWaitForResponseAsync(new SetIntervalParamsRequest(params), 35000, false)); if (result == null || result.Status == null) { (_b = DependencyContainer.Log) === null || _b === void 0 ? void 0 : _b.Error(Rcm21ConfigService.ServiceName, `Failed to change device parameters - unknow error.`); return Status.FatalError; } if (result.Status !== Status.OK) (_c = DependencyContainer.Log) === null || _c === void 0 ? void 0 : _c.Error(Rcm21ConfigService.ServiceName, `Failed to change device parameters, response status: ${result.Status}`); return result.Status; } catch (error) { (_d = DependencyContainer.Log) === null || _d === void 0 ? void 0 : _d.Error(Rcm21ConfigService.ServiceName, `Cannot change device parameters. ${error}`); return Status.FatalError; } }); } GetCurrentDetectionThresholdConfigAsync(deviceGuid, channel) { return __awaiter(this, void 0, void 0, function* () { var _a; const result = yield ((_a = this._connection) === null || _a === void 0 ? void 0 : _a.SendAndWaitForResponseAsync(new GetDetectionThresholdParams(deviceGuid, channel), 35000, false)); if (result === null || result === undefined) return null; switch (result.Status) { case Status.OK: if (result.Data != null) { return result.Data; } break; case Status.ResourceIsNotAvailable: return null; } throw new Error(`Failed to read current configuration of device!`); }); } } class GetIntervalParams extends DataFrame { constructor(guid) { super(); this.Resource = "/extalife/device/parameters/multisensor/interval"; this.Method = Method.Get; this.Data = new GetParamsRequest(guid); } } export class GetDetectionThresholdParams extends DataFrame { constructor(guid, channel) { super(); this.Resource = "/extalife/device/parameters/multisensor/detectionthreshold"; this.Method = Method.Get; this.Data = new GetChannelParamsRequest(guid, channel); } } export class SetDetectionThresholdParamsRequest extends DataFrame { constructor(data) { super(); this.Resource = "/extalife/device/parameters/multisensor/detectionthreshold"; this.Method = Method.Put; this.Data = data; } } export class SetIntervalParamsRequest extends DataFrame { constructor(data) { super(); this.Resource = "/extalife/device/parameters/multisensor/interval"; this.Method = Method.Put; this.Data = data; } } //# sourceMappingURL=Rcm21VersionedService.js.map