lavva.exalushome.extalife
Version:
Library implementing communication and abstraction layers for ExtaLife API in ExalusHome system
108 lines • 6.01 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 "lavva.exalushome";
import { DevicesService } from "lavva.exalushome/build/js/Services/Devices/DevicesService";
import { ResponseResult } from "lavva.exalushome/build/js/Services/FieldChangeResult";
import { DiagnosticResult, DiagnosticErrorCode, DeviceDiagnosticsStatus } from "./DiagnosticParams";
import { DataFrame, Method, Status } from "lavva.exalushome/build/js/DataFrame";
import { ExalusConnectionService } from "lavva.exalushome/build/js/Services/ExalusConnectionService";
import { LoggerService } from "lavva.exalushome/build/js/Services/Logging/LoggerService";
import { ControllerConfigurationService } from "lavva.exalushome/build/js/Services/Controller/ControllerConfigurationService";
export class DiagnosticService {
constructor() {
this._connection = Api.Get(ExalusConnectionService.ServiceName);
this._controllerConfigurationService = Api.Get(ControllerConfigurationService.ServiceName);
}
RunDiagnosticAsync(dev) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
try {
let device;
if (typeof dev == 'string') {
const result = yield Api.Get(DevicesService.ServiceName).GetDevice(dev);
if (result != null)
device = result;
else
return new ResponseResult(DiagnosticErrorCode.CannotFindDevice, `Device with specified GUID ${dev} does not exists.`);
}
else {
device = dev;
}
yield this._controllerConfigurationService.EnterConfigurationModeAsync();
const result = yield ((_a = this._connection) === null || _a === void 0 ? void 0 : _a.SendAndWaitForResponseAsync(new RunDiagnosticRequest(device.Guid), 60000, false));
if (result == null)
return new ResponseResult(DiagnosticErrorCode.OtherError, `Cannot run diagnostic - unknown error.`);
if (result.Status !== Status.OK || result.Data == null)
return new ResponseResult(DiagnosticErrorCode.OtherError, `Cannot run diagnostic - device responded with code ${result.Status}, data: ${result.Data}`);
const diagResutl = new DiagnosticResult();
diagResutl.DeviceDiagnosticsStatus = result.Data;
diagResutl.DeviceGuid = device.Guid;
diagResutl.CanBeFixed = result.Data == DeviceDiagnosticsStatus.Unpaired ? true : false;
return diagResutl;
}
catch (error) {
return new ResponseResult(DiagnosticErrorCode.FatalError, `Cannot run diagnostic - exeption occurs! ${error}`);
}
finally {
Api.Get(LoggerService.ServiceName).Error(DiagnosticService.ServiceName, `EXIT CONFIGURATION MODE WILL BE FIRED! RunDiagnosticAsync()`);
yield this._controllerConfigurationService.ExitConfigurationModeAsync();
}
});
}
RepairDeviceAsync(diagnosticResult) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
try {
yield this._controllerConfigurationService.EnterConfigurationModeAsync();
switch (diagnosticResult.DeviceDiagnosticsStatus) {
case DeviceDiagnosticsStatus.Unpaired:
const req = new DevicePairRequest();
req.Data = diagnosticResult.DeviceGuid;
const res = yield ((_a = this._connection) === null || _a === void 0 ? void 0 : _a.SendAndWaitForResponseAsync(req, 20000, false));
if ((res === null || res === void 0 ? void 0 : res.Status) == Status.OK) {
return Status.OK;
}
else {
return new ResponseResult(DiagnosticErrorCode.DeviceRepairFailed, `Cannot repair device - pairing error.`);
}
default:
return new ResponseResult(DiagnosticErrorCode.RepairingIsUnsupported, `Cannot repair device - repair for given diagnostic result is unsupported.`);
}
}
catch (error) {
return new ResponseResult(DiagnosticErrorCode.FatalError, `Cannot repair device - exeption occurs! ${error}`);
}
finally {
Api.Get(LoggerService.ServiceName).Error(DiagnosticService.ServiceName, `EXIT CONFIGURATION MODE WILL BE FIRED! RepairDeviceAsync()`);
yield this._controllerConfigurationService.ExitConfigurationModeAsync();
}
});
}
GetServiceName() {
return DiagnosticService.ServiceName;
}
}
DiagnosticService.ServiceName = "DiagnosticService";
class RunDiagnosticRequest extends DataFrame {
constructor(deviceGuid) {
super();
this.Resource = "/extalife/device/diagnose";
this.Method = Method.Get;
this.Data = deviceGuid;
}
}
class DevicePairRequest extends DataFrame {
constructor() {
super();
this.Resource = "/extalife/device/pair";
this.Method = Method.Get;
}
}
//# sourceMappingURL=DiagnosticService.js.map