lavva.exalushome.network
Version:
Library implementing communication and abstraction layers for network configuration API in ExalusHome system
313 lines • 12.4 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 { DependencyContainer } from 'lavva.exalushome/build/js/DependencyContainer';
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 { NetworkInterfaceType, OperationalStatus } from "lavva.exalushome/build/js/INetworkService";
export class NetworkConfigurationService {
constructor() {
this._connection = Api.Get(ExalusConnectionService.ServiceName);
this._logger = Api.Get(LoggerService.ServiceName);
}
static Init() {
DependencyContainer.Instance.RegisterService(new NetworkConfigurationService());
Api.Get(LoggerService.ServiceName).Debug("Initialized NetworkConfigurationService");
}
GetServiceName() {
return NetworkConfigurationService.ServiceName;
}
EnableDhcpAsync() {
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 EnableDhcpRequest(), 20000, false));
if (result == null || result.Status == null) {
this._logger.Error(NetworkConfigurationService.ServiceName, `Failed to enable DHCP - unknow error.`);
return Status.FatalError;
}
if (result.Status !== Status.OK)
this._logger.Error(NetworkConfigurationService.ServiceName, `Failed to enable DHCP, response status: ${result.Status}`);
return result.Status;
});
}
SetStaticIpConfigurationAsync(configuration) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
const reg = new RegExp("^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$");
const valid = reg.test(configuration.IpAddress) && reg.test(configuration.SubnetMask) && reg.test(configuration.DefaultGateway) && reg.test(configuration.Broadcast);
if (!valid)
return Status.WrongData;
const ipConfig = new StaticIpConfigurationFrame();
ipConfig.ControllerStaticIp = configuration.IpAddress;
ipConfig.SubnetMaskIp = configuration.SubnetMask;
ipConfig.DefaultGatewayIp = configuration.DefaultGateway;
ipConfig.BroadcastIp = configuration.Broadcast;
const result = yield ((_a = this._connection) === null || _a === void 0 ? void 0 : _a.SendAndWaitForResponseAsync(new SetStaticIpRequest(ipConfig), 20000, false));
if (result == null || result.Status == null) {
this._logger.Error(NetworkConfigurationService.ServiceName, `Failed to set static network configuration - unknow error.`);
return Status.FatalError;
}
if (result.Status !== Status.OK)
this._logger.Error(NetworkConfigurationService.ServiceName, `Failed to set static network configuration response status: ${result.Status}`);
return result.Status;
});
}
GetIpConfigurationAsync() {
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(NetworkConfigurationService.ServiceName, `Failed to get network configuration data - unknow error.`);
return null;
}
const statics = new NetworkStatisticsData();
statics.BytesReceived = result.Data.Statistics.BytesReceived;
statics.BytesSent = result.Data.Statistics.BytesSent;
const config = new NetworkConfigurationData();
config.Id = result.Data.Id;
config.InterfaceSpeed = result.Data.Speed;
config.InterfaceType = result.Data.InterfaceType;
config.Ipv4Address = result.Data.LocalIPv4;
config.Ipv4DefaultGateway = result.Data.GatewayIPv4;
config.Ipv4PublicAddress = result.Data.PublicIPv4;
config.Ipv4SubnetMask = result.Data.MaskIPv4;
config.MacAddress = result.Data.MAC;
config.IsDhcpEnabled = result.Data.IsDhcpEnabled;
config.Name = result.Data.Name;
config.OperationalStatus = result.Data.OperationalStatus;
config.Statistics = statics;
return config;
});
}
RestoreDefaultConfigurationAsync() {
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 RestoreDefaultConfigRequest(), 20000, false));
if (result == null || result.Status == null) {
this._logger.Error(NetworkConfigurationService.ServiceName, `Failed to restore default configuration - unknow error.`);
return Status.FatalError;
}
if (result.Status !== Status.OK)
this._logger.Error(NetworkConfigurationService.ServiceName, `Failed to restore default configuration, response status: ${result.Status}`);
return result.Status;
});
}
GetDnsServersAsync() {
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 GetDnsRequest(), 20000, false));
const dns = [];
if (result == null || result.Status == null || result.Data == null) {
this._logger.Error(NetworkConfigurationService.ServiceName, `Failed to get network dns servers - unknow error.`);
return dns;
}
dns.push(result.Data);
return dns;
});
}
AddDnsServerAsync(address) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
const reg = new RegExp("^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$");
if (!reg.test(address))
return Status.WrongData;
const result = yield ((_a = this._connection) === null || _a === void 0 ? void 0 : _a.SendAndWaitForResponseAsync(new AddDnsRequest(address), 20000, false));
if (result == null || result.Status == null) {
this._logger.Error(NetworkConfigurationService.ServiceName, `Failed to add dns server - unknow error.`);
return Status.FatalError;
}
return result.Status;
});
}
}
NetworkConfigurationService.ServiceName = "NetworkConfigurationService";
class EnableDhcpRequest extends DataFrame {
constructor() {
super();
this.Resource = "/network/lan/dhcp/enable";
this.Method = Method.Put;
}
}
class StaticIpConfigurationFrame {
constructor() {
this.ControllerStaticIp = "";
this.DefaultGatewayIp = "";
this.SubnetMaskIp = "";
this.BroadcastIp = "";
}
}
class GetNetworkConfiguration extends DataFrame {
constructor() {
super();
this.Resource = "/network/lan/";
this.Method = Method.Get;
}
}
class NetworkConfigurationData {
constructor() {
this._id = "";
this._name = "";
this._ipv4Address = "";
this._ipv4SubnetMask = "";
this._ipv4DefaultGateway = "";
this._ipv4PublicAddress = "";
this._macAddress = "";
this._isDhcpEnabled = true;
this._interfaceType = NetworkInterfaceType.Unknown;
this._operationalStatus = OperationalStatus.Unknown;
this._interfaceSpeed = 0;
this._statistics = new NetworkStatisticsData();
}
get Id() {
return this._id;
}
get Name() {
return this._name;
}
get Ipv4Address() {
return this._ipv4Address;
}
get Ipv4SubnetMask() {
return this._ipv4SubnetMask;
}
get Ipv4DefaultGateway() {
return this._ipv4DefaultGateway;
}
get Ipv4PublicAddress() {
return this._ipv4PublicAddress;
}
get MacAddress() {
return this._macAddress;
}
get IsDhcpEnabled() {
return this._isDhcpEnabled;
}
get InterfaceType() {
return this._interfaceType;
}
get OperationalStatus() {
return this._operationalStatus;
}
get InterfaceSpeed() {
return this._interfaceSpeed;
}
get Statistics() {
return this._statistics;
}
set Id(value) {
this._id = value;
}
set Name(value) {
this._name = value;
}
set Ipv4Address(value) {
this._ipv4Address = value;
}
set Ipv4SubnetMask(value) {
this._ipv4SubnetMask = value;
}
set Ipv4DefaultGateway(value) {
this._ipv4DefaultGateway = value;
}
set Ipv4PublicAddress(value) {
this._ipv4PublicAddress = value;
}
set MacAddress(value) {
this._macAddress = value;
}
set IsDhcpEnabled(value) {
this._isDhcpEnabled = value;
}
set InterfaceType(value) {
this._interfaceType = value;
}
set OperationalStatus(value) {
this._operationalStatus = value;
}
set InterfaceSpeed(value) {
this._interfaceSpeed = value;
}
set Statistics(value) {
this._statistics = value;
}
}
class NetworkStatisticsData {
constructor() {
this._bytesReceived = 0;
this._bytesSent = 0;
}
get BytesReceived() {
return this._bytesReceived;
}
get BytesSent() {
return this._bytesSent;
}
set BytesReceived(value) {
this._bytesReceived = value;
}
set BytesSent(value) {
this._bytesSent = value;
}
}
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;
this.Statistics = new StatisticsResponse();
}
}
class StatisticsResponse {
constructor() {
this.BytesReceived = 0;
this.BytesSent = 0;
}
}
class SetStaticIpRequest extends DataFrame {
constructor(data) {
super();
this.Resource = "/network/lan/configuration/";
this.Method = Method.Put;
this.Data = data;
}
}
class RestoreDefaultConfigRequest extends DataFrame {
constructor() {
super();
this.Resource = "/networks/configuration/default";
this.Method = Method.Put;
}
}
class GetDnsRequest extends DataFrame {
constructor() {
super();
this.Resource = "/network/lan/dns";
this.Method = Method.Get;
}
}
class AddDnsRequest extends DataFrame {
constructor(data) {
super();
this.Resource = "/network/lan/dns";
this.Method = Method.Put;
this.Data = data;
}
}
//# sourceMappingURL=NetworkConfiguration.js.map