lavva.exalushome
Version:
Library implementing communication and abstraction layers for ExalusHome system
236 lines • 12.9 kB
JavaScript
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 { ParseObjToMap } from "../../Helpers";
import { DevicesService } from "../Devices/DevicesService";
import { DeviceResponseType } from "../Devices/IDevice";
import { ExalusConnectionService } from "../ExalusConnectionService";
import { ResponseResult } from "../FieldChangeResult";
import { UpdatesProvider } from "../Updates/UpdatesProvider";
import { StateHistoryErrorCode } from "./IStatesHistoryService";
import { AvailableState, StateInterval } from "./StatesHistory";
export class StatesHistoryService {
constructor() {
this._connection = null;
this._connection = Api.Get(ExalusConnectionService.ServiceName);
}
GetServiceName() {
return StatesHistoryService.ServiceName;
}
GetStatesByIntervalAsync(dev_1, channel_1, responseType_1, stateFrom_1, limit_1, offset_1) {
return __awaiter(this, arguments, void 0, function* (dev, channel, responseType, stateFrom, limit, offset, orderByDesc = false) {
var _a;
try {
if (!(yield this.IsFunctionalitySupportedAsync()))
return new ResponseResult(StateHistoryErrorCode.FunctionalityNotSupported, `State history data is not supported with this version of controller software, update software to get this functionality.`);
let device;
if (typeof dev == 'string') {
const result = yield Api.Get(DevicesService.ServiceName).GetDevice(dev);
if (result != null)
device = result;
else
return new ResponseResult(StateHistoryErrorCode.CannotFindDevice, `Device with specified GUID ${dev} does not exists.`);
}
else {
device = dev;
}
if (!device.Channels.any(ch => ch.Number == channel))
return new ResponseResult(StateHistoryErrorCode.InvalidChannelNumber, `Given channel not found in device with guid: ${device.Guid}.`);
if (!device.Channels.any(ch => ch.Number == channel && ch.AvailableResponseTypes.any(r => r.Type == responseType)))
return new ResponseResult(StateHistoryErrorCode.ResponseTypeNotSupported, `Device or channel not supporting requested DeviceResponseType.`);
const request = new StateDataRequest();
request.DeviceGuid = device.Guid;
request.DeviceChannel = channel;
request.Range = stateFrom;
request.StateInterfaceType = responseType;
request.ReverseOrder = orderByDesc;
request.Limit = limit;
request.Offset = offset;
const result = yield ((_a = this._connection) === null || _a === void 0 ? void 0 : _a.SendAndWaitForResponseAsync(new GetLastStatesRequest(request), 20000, false));
if (result == null)
return new ResponseResult(StateHistoryErrorCode.OtherError, `Cannot get state history data - response is null.`);
switch (result.Status) {
case Status.WrongData:
switch (result.Data) {
case "IncorrectLimitValue":
return new ResponseResult(StateHistoryErrorCode.IncorrectLimitValue, `Cannot get state history data - limit value is incorrect!`);
case "IncorrectOffsetValue":
return new ResponseResult(StateHistoryErrorCode.IncorrectOffsetValue, `Cannot get state history data - offset value is incorrect!`);
case "IncorrectArguments":
return new ResponseResult(StateHistoryErrorCode.OtherError, `Cannot get state history data - some parameters are incorrect!`);
default:
return new ResponseResult(StateHistoryErrorCode.OtherError, `Cannot get state history data - unknown error!`);
}
case Status.FatalError:
return new ResponseResult(StateHistoryErrorCode.FatalError, `Cannot get state history data - an exception occurred in the controller while reading data!`);
case Status.ResourceDoesNotExists:
return new ResponseResult(StateHistoryErrorCode.ResponseTypeNotSupported, `Device or channel not supporting requested DeviceResponseType.`);
case Status.OK:
if (result.Data == null)
return new ResponseResult(StateHistoryErrorCode.NoData, `Controller responede with status OK, but response does not contain data!`);
result.Data.AggregateDataList = result.Data.AggregateDataList.map(d => {
d.AggregateData = ParseObjToMap(d.AggregateData);
return d;
});
switch (responseType) {
case DeviceResponseType.MeasuredEnergy:
const parseData = result.Data.Data.map(d => {
const result = d;
result.Values.MeasurementAveragingParameters = ParseObjToMap(d.Values.MeasurementAveragingParameters);
result.Values.MeasurementNonAveragingParmeters = ParseObjToMap(d.Values.MeasurementNonAveragingParmeters);
return result;
});
result.Data.Data = parseData;
return result.Data;
default:
return result.Data;
}
default:
return new ResponseResult(StateHistoryErrorCode.OtherError, `Cannot get state history data - controller responded with response code ${result.Status}`);
}
}
catch (error) {
return new ResponseResult(StateHistoryErrorCode.FatalError, `Cannot get state history data - exeption occurs! ${error}`);
}
});
}
GetAvailableStatesAsync() {
return __awaiter(this, void 0, void 0, function* () {
var _a;
try {
const result = yield ((_a = this._connection) === null || _a === void 0 ? void 0 : _a.SendAndWaitForResponseAsync(new GetAvailableStatesRequest(), 12000, false));
if (result == null)
return new ResponseResult(StateHistoryErrorCode.OtherError, `Cannot get available states - response is null.`);
if (result.Status != Status.OK)
return new ResponseResult(StateHistoryErrorCode.OtherError, `Cannot get available states - controller responded with response code ${result.Status}.`);
if (result.Data == null)
return new ResponseResult(StateHistoryErrorCode.NoData, `Cannot get available states - controller responede with status OK, but response does not contain data!`);
return result.Data.map(r => {
const x = new AvailableState();
x.StateInterfaceType = r.StateInterfaceType;
x.StateObjectType = r.StateObjectType;
return x;
});
}
catch (error) {
return new ResponseResult(StateHistoryErrorCode.FatalError, `Cannot get available states - exeption occurs! ${error}`);
}
});
}
IsFunctionalitySupportedAsync() {
return __awaiter(this, void 0, void 0, function* () {
if (parseFloat((yield Api.Get(UpdatesProvider.ServiceName).GetSoftwareRuntimeInfoAsync()).SoftwareVersion) <= 3.42)
return false;
else
return true;
});
}
GetAvailableStatesPerChannelAsync(dev, channel) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
let device;
if (typeof dev == 'string') {
const result = yield Api.Get(DevicesService.ServiceName).GetDevice(dev);
if (result != null)
device = result;
else
return new ResponseResult(StateHistoryErrorCode.CannotFindDevice, `Device with specified GUID ${dev} does not exists.`);
}
else {
device = dev;
}
const result = yield ((_a = this._connection) === null || _a === void 0 ? void 0 : _a.SendAndWaitForResponseAsync(new GetAvailableStatesPerChannelRequest(new ChannelParamRequest(device.Guid, channel)), 8000, false));
if (result == null)
return new ResponseResult(StateHistoryErrorCode.OtherError, `Cannot get available states - response is null.`);
switch (result.Status) {
case Status.ResourceDoesNotExists: {
switch (result.Data) {
case "DeviceNotFound":
return new ResponseResult(StateHistoryErrorCode.CannotFindDevice, `Cannot get available states - device does not exists!`);
case "ChannelNotFound":
return new ResponseResult(StateHistoryErrorCode.InvalidChannelNumber, `Cannot get available states - wrong channel!`);
default:
return new ResponseResult(StateHistoryErrorCode.OtherError, `Cannot get available states - unknown error!`);
}
}
case Status.OK: {
if (result.Data == null)
return new ResponseResult(StateHistoryErrorCode.NoData, `Cannot get available states - controller responede with status OK, but response does not contain data!`);
const remappedStates = result.Data.AvailableStates.map(r => {
const x = new AvailableState();
x.StateInterfaceType = r.StateInterfaceType;
x.StateObjectType = r.StateObjectType;
return x;
});
result.Data.AvailableStates = remappedStates;
return result.Data;
}
default:
return new ResponseResult(StateHistoryErrorCode.OtherError, `Cannot get available states - controller responded with response code ${result.Status}`);
}
});
}
}
StatesHistoryService.ServiceName = "StatesHistoryService";
class GetLastStatesRequest extends DataFrame {
constructor(data) {
super();
this.Resource = "/statehistory/states/get/last";
this.Method = Method.Get;
this.Data = data;
}
}
class StateDataRequest {
constructor() {
this.DeviceGuid = "";
this.DeviceChannel = 0;
this.StateInterfaceType = "";
this.Range = StateInterval.Day;
this.ReverseOrder = false;
this.Limit = 0;
this.Offset = 0;
}
}
class GetAvailableStatesRequest extends DataFrame {
constructor() {
super();
this.Resource = "/statehistory/states/available";
this.Method = Method.Get;
}
}
class GetAvailableStatesPerChannelRequest extends DataFrame {
constructor(request) {
super();
this.Resource = "/statehistory/states/available/per/channel";
this.Method = Method.Get;
this.Data = request;
}
}
class ChannelParamRequest {
constructor(guid, channel) {
this.DeviceGuid = guid;
this.Channel = channel;
}
}
class AvailableStateResponse {
constructor() {
this.StateInterfaceType = "";
this.StateObjectType = "";
}
}
class AvailableStatePerChannelResponse {
constructor() {
this.DeviceGuid = "";
this.Channel = 0;
this.AvailableStates = [];
}
}
//# sourceMappingURL=StatesHistoryService.js.map