lavva.exalushome
Version:
Library implementing communication and abstraction layers for ExalusHome system
155 lines • 7.28 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 } from "../../DataFrame";
import { Helpers, Task } from "../../Helpers";
import { ExalusConnectionService } from "../ExalusConnectionService";
import { LocalStorageService } from "../LocalStorageService";
import { LoggerService } from "../Logging/LoggerService";
import { SessionService } from "../Session/SessionService";
export class FindControllerService {
constructor() {
this._localIpAddress = null;
this._publicIpAddress = null;
this._gatewayIpAddress = null;
this._macAddress = null;
this._isInLocalNetwork = false;
this.nativeFindControllerApi = null;
if (navigator.findEfcController !== undefined)
this.nativeFindControllerApi = navigator.findEfcController;
this._connection = Api.Get(ExalusConnectionService.ServiceName);
this._logger = Api.Get(LoggerService.ServiceName);
this._storage = Api.Get(LocalStorageService.ServiceName);
Api.Get(SessionService.ServiceName).OnUserLoggedInEvent().Subscribe((user) => __awaiter(this, void 0, void 0, function* () {
var _a;
try {
yield Task.Delay(5000);
const res = yield this.GetControllerNetworkConfigurationAsync();
if (res != null) {
this._localIpAddress = res.LocalIPv4;
this._publicIpAddress = res.PublicIPv4;
this._gatewayIpAddress = res.GatewayIPv4;
this._macAddress = res.MAC;
this._storage.Save(FindControllerService.ServiceName, "LocalIpAddress", this._localIpAddress);
const controllerHostName = this.GetControllerHostName(this._connection.GetControllerSerialNumber());
this._logger.Debug(`Looking for controller in local network with host name: ${controllerHostName}`);
let r = null;
if (Helpers.IsIosNative()) {
r = yield this.getIosControllerIpAsync(controllerHostName);
}
else {
if (this.nativeFindControllerApi !== null) {
r = yield ((_a = this.nativeFindControllerApi) === null || _a === void 0 ? void 0 : _a.FindControllerAsync(controllerHostName));
}
}
if (r != null && r != "") {
this._isInLocalNetwork = true;
this._logger.Debug(FindControllerService.ServiceName, `Controller found in local network with ip address: ${r}`);
}
else {
this._isInLocalNetwork = false;
this._logger.Debug(FindControllerService.ServiceName, `Controller not found in local network.`);
}
}
else
this._logger.Error(FindControllerService.ServiceName, `Failed to get network configuration data.`);
}
catch (e) {
this._logger.Error(FindControllerService.ServiceName, `Failed to get network configuration data - ${e}`);
}
}));
}
FindControllerInLocalNetworkAsync(controllerHostName) {
return __awaiter(this, void 0, void 0, function* () {
if (Helpers.IsIosNative()) {
const r = yield this.getIosControllerIpAsync(controllerHostName);
if (r == "")
return null;
return r;
}
else {
if (this.nativeFindControllerApi !== null)
return this.nativeFindControllerApi.FindControllerAsync(controllerHostName);
else
return Promise.resolve(null);
}
});
}
GetLastKnownControllerLocalIpAddress() {
if (this._localIpAddress !== null)
return this._localIpAddress;
return this._storage.Read(FindControllerService.ServiceName, "LocalIpAddress");
}
GetControllerHostName(controllerSerial) {
return `ExalusTR7-${controllerSerial}`;
}
GetMdnsHostName() {
return `ExalusTR7-${this.GetControllerHostName(this._connection.GetControllerSerialNumber())}.local`.toLowerCase();
}
GetControllerNetworkConfigurationAsync() {
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 GetNetworkConfiguration(), 20000, false));
if (result == null || result.Status == null || result.Data == null) {
this._logger.Error(FindControllerService.ServiceName, `Failed to get network configuration data - unknow error.`);
return null;
}
return result.Data;
});
}
GetServiceName() {
return FindControllerService.ServiceName;
}
getIosControllerIpAsync(hostname, timout = 20000) {
let timer;
return new Promise((resolve, reject) => {
timer = setTimeout(() => {
this._logger.Error(`Cannot find controller in local network, timeout!`);
resolve("");
}, timout);
try {
window.nativeEvent.exalusFindController.handleFindController = (controllerAddress) => {
clearTimeout(timer);
resolve(controllerAddress);
};
window.webkit.messageHandlers.exalusFindController.postMessage(hostname);
}
catch (err) {
this._logger.Error(`Cannot find controller in local network, error: ${err}`);
resolve("");
}
});
}
}
FindControllerService.ServiceName = "FindControllerService";
class NetworkConfigurationResponse {
constructor() {
this.Id = "";
this.Name = "";
this.LocalIPv4 = "";
this.MaskIPv4 = "";
this.GatewayIPv4 = "";
this.BroadcastIp = "";
this.PublicIPv4 = "";
this.MAC = "";
this.IsDhcpEnabled = false;
this.InterfaceType = NetworkInterfaceType.Unknown;
this.OperationalStatus = OperationalStatus.Unknown;
this.Speed = 0;
}
}
class GetNetworkConfiguration extends DataFrame {
constructor() {
super();
this.Resource = "/network/lan/";
this.Method = Method.Get;
}
}
//# sourceMappingURL=FindControllerService.js.map