UNPKG

unifi-client

Version:

NodeJs client for Unifi products (https://www.ui.com/)

465 lines (464 loc) 21.1 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); 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()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseDevice = void 0; var _ObjectSubSite_1 = require("../commons/_ObjectSubSite"); var Errors_1 = require("../Errors"); var util_1 = require("../util"); var Validate_1 = require("../commons/Validate"); var BaseDevice = /** @class */ (function (_super) { __extends(BaseDevice, _super); function BaseDevice(config, props) { var _this = _super.call(this, config) || this; _this.mac = props === null || props === void 0 ? void 0 : props.mac; if (!_this.mac) { throw new Errors_1.ClientError('mac is needed', Errors_1.EErrorsCodes.UNKNOWN_ERROR); } _this.debug = BaseDevice.debug.extend(_this.mac); _this.import(props); return _this; } BaseDevice.prototype.import = function (props) { var _a; if (!Validate_1.Validate.isUndefined(props._id)) { this._id = props._id; } if (!Validate_1.Validate.isUndefined(props.ip)) { this.ip = props.ip; } if (!Validate_1.Validate.isUndefined(props.model)) { this.model = props.model; } if (!Validate_1.Validate.isUndefined(props.model_in_lts)) { this.modelInLTS = props.model_in_lts; } if (!Validate_1.Validate.isUndefined(props.model_in_eol)) { this.modelInEOL = props.model_in_eol; } if (!Validate_1.Validate.isUndefined(props.type)) { this.type = props.type; } if (!Validate_1.Validate.isUndefined(props.version)) { this.version = props.version; } if (!Validate_1.Validate.isUndefined(props.adopted)) { this.adopted = props.adopted; } if (!Validate_1.Validate.isUndefined(props.site_id)) { this.siteId = props.site_id; } if (!Validate_1.Validate.isUndefined(props.x_authkey)) { this.xAuthkey = props.x_authkey; } if (!Validate_1.Validate.isUndefined(props.cfgversion)) { this.cfgVersion = props.cfgversion; } if (!Validate_1.Validate.isUndefined(props.syslog_key)) { this.syslogKey = props.syslog_key; } if (!Validate_1.Validate.isUndefined(props.config_network)) { this.configNetwork = props.config_network; } if (!Validate_1.Validate.isUndefined(props.setup_id)) { this.setupId = props.setup_id; } if (!Validate_1.Validate.isUndefined(props.dot1x_portctrl_enabled)) { this.dot1xPortctrlEnabled = props.dot1x_portctrl_enabled; } if (!Validate_1.Validate.isUndefined(props.license_state)) { this.licenseState = props.license_state; } if (!Validate_1.Validate.isUndefined(props.x_fingerprint)) { this.xFingerprint = props.x_fingerprint; } if (!Validate_1.Validate.isUndefined(props.inform_url)) { this.informUrl = props.inform_url; } if (!Validate_1.Validate.isUndefined(props.inform_ip)) { this.informIp = props.inform_ip; } if (!Validate_1.Validate.isUndefined(props.x_aes_gcm)) { this.xAesGcm = props.x_aes_gcm; } if (!Validate_1.Validate.isUndefined(props.required_version)) { this.requiredVersion = props.required_version; } if (!Validate_1.Validate.isUndefined(props.kernel_version)) { this.kernelVersion = props.kernel_version; } if (!Validate_1.Validate.isUndefined(props.architecture)) { this.architecture = props.architecture; } if (!Validate_1.Validate.isUndefined(props.board_rev)) { this.boardRevision = props.board_rev; } if (!Validate_1.Validate.isUndefined(props.manufacturer_id)) { this.manufacturerId = props.manufacturer_id; } if (!Validate_1.Validate.isUndefined(props.model_incompatible)) { this.modelIncompatible = props.model_incompatible; } if (!Validate_1.Validate.isUndefined(props.internet)) { this.internet = props.internet; } if (!Validate_1.Validate.isUndefined(props.ethernet_table)) { this.ethernetTable = props.ethernet_table; } if (!Validate_1.Validate.isUndefined(props.port_table)) { this.portTable = props.port_table; } if (!Validate_1.Validate.isUndefined(props.has_speaker)) { this.hasSpeaker = props.has_speaker; } if (!Validate_1.Validate.isUndefined(props.has_eth1)) { this.hasEth1 = props.has_eth1; } if (!Validate_1.Validate.isUndefined(props.fw_caps)) { this.fwCaps = props.fw_caps; } if (!Validate_1.Validate.isUndefined(props.hw_caps)) { this.hwCaps = props.hw_caps; } if (!Validate_1.Validate.isUndefined(props.wifi_caps)) { this.wifiCaps = props.wifi_caps; } if (!Validate_1.Validate.isUndefined(props.switch_caps)) { this.switchCaps = props.switch_caps; } if (!Validate_1.Validate.isUndefined(props.has_fan)) { this.hasFan = props.has_fan; } if (!Validate_1.Validate.isUndefined(props.has_temperature)) { this.hasTemperature = props.has_temperature; } if (!Validate_1.Validate.isUndefined(props.connected_at)) { this.connectedAt = props.connected_at; } if (!Validate_1.Validate.isUndefined(props.provisioned_at)) { this.provisionedAt = props.provisioned_at; } if (!Validate_1.Validate.isUndefined(props.unsupported)) { this.unsupported = props.unsupported; } if (!Validate_1.Validate.isUndefined(props.unsupported_reason)) { this.unsupportedReason = props.unsupported_reason; } if (!Validate_1.Validate.isUndefined(props.serial)) { this.serial = props.serial; } if (!Validate_1.Validate.isUndefined(props.hash_id)) { this.hashId = props.hash_id; } if (!Validate_1.Validate.isUndefined(props.anon_id)) { this.anonId = props.anon_id; } if (!Validate_1.Validate.isUndefined(props.two_phase_adopt)) { this.twoPhaseAdopt = props.two_phase_adopt; } if (!Validate_1.Validate.isUndefined(props.name)) { this.name = props.name; } if (!Validate_1.Validate.isUndefined(props.device_id)) { this.deviceId = props.device_id; } if (!Validate_1.Validate.isUndefined(props.state)) { this.state = props.state; } if (!Validate_1.Validate.isUndefined(props.start_disconnected_millis)) { this.startDisconnectedMillis = props.start_disconnected_millis; } if (!Validate_1.Validate.isUndefined(props.last_seen)) { this.lastSeen = props.last_seen; } if (!Validate_1.Validate.isUndefined(props.known_cfgversion)) { this.knownCfgversion = props.known_cfgversion; } if (!Validate_1.Validate.isUndefined(props.start_connected_millis)) { this.startConnectedMillis = props.start_connected_millis; } if (!Validate_1.Validate.isUndefined(props.min_inform_interval_seconds)) { this.minInformIntervalSeconds = props.min_inform_interval_seconds; } if (!Validate_1.Validate.isUndefined(props.upgradable)) { this.upgradable = props.upgradable; } if (!Validate_1.Validate.isUndefined(props.adoptable_when_upgraded)) { this.adoptableWhenUpgraded = props.adoptable_when_upgraded; } if (!Validate_1.Validate.isUndefined(props.rollupgrade)) { this.rollupgrade = props.rollupgrade; } if (!Validate_1.Validate.isUndefined(props.next_interval)) { this.nextInterval = props.next_interval; } if (!Validate_1.Validate.isUndefined(props.uptime) || !Validate_1.Validate.isUndefined(props._uptime)) { this.uptime = (_a = props.uptime) !== null && _a !== void 0 ? _a : props._uptime; } if (!Validate_1.Validate.isUndefined(props.locating)) { this.locating = props.locating; } if (!Validate_1.Validate.isUndefined(props.sys_stats)) { this.sysStats = props.sys_stats; } if (!Validate_1.Validate.isUndefined(props['system-stats'])) { this.systemStats = props['system-stats']; } if (!Validate_1.Validate.isUndefined(props.lldp_table)) { this.lldpTable = props.lldp_table; } if (!Validate_1.Validate.isUndefined(props.displayable_version)) { this.displayableVersion = props.displayable_version; } if (!Validate_1.Validate.isUndefined(props.connection_network_name)) { this.connectionNetworkName = props.connection_network_name; } if (!Validate_1.Validate.isUndefined(props.startup_timestamp)) { this.startupTimestamp = props.startup_timestamp; } if (!Validate_1.Validate.isUndefined(props.guest_kicks)) { this.guestKicks = props.guest_kicks; } if (!Validate_1.Validate.isUndefined(props.guest_token)) { this.guestToken = props.guest_token; } if (!Validate_1.Validate.isUndefined(props.uplink)) { this.uplink = props.uplink; } if (!Validate_1.Validate.isUndefined(props.downlink_table)) { this.downlinkTable = props.downlink_table; } if (!Validate_1.Validate.isUndefined(props.connect_request_ip)) { this.connectRequestIp = props.connect_request_ip; } if (!Validate_1.Validate.isUndefined(props.connect_request_port)) { this.connectRequestPort = props.connect_request_port; } if (!Validate_1.Validate.isUndefined(props.prev_non_busy_state)) { this.prevNonBusyState = props.prev_non_busy_state; } if (!Validate_1.Validate.isUndefined(props.stat)) { this.stat = props.stat; } if (!Validate_1.Validate.isUndefined(props.tx_bytes)) { this.txBytes = props.tx_bytes; } if (!Validate_1.Validate.isUndefined(props.rx_bytes)) { this.rxBytes = props.rx_bytes; } if (!Validate_1.Validate.isUndefined(props.bytes)) { this.bytes = props.bytes; } if (!Validate_1.Validate.isUndefined(props.num_sta)) { this.numSta = props.num_sta; } if (!Validate_1.Validate.isUndefined(props['user-wlan-num_sta'])) { this.userWLANNumSta = props['user-wlan-num_sta']; } if (!Validate_1.Validate.isUndefined(props['user-num_sta'])) { this.userNumSta = props['user-num_sta']; } if (!Validate_1.Validate.isUndefined(props['guest-wlan-num_sta'])) { this.guestWLANNumSta = props['guest-wlan-num_sta']; } if (!Validate_1.Validate.isUndefined(props['guest-num_sta'])) { this.guestNumSta = props['guest-num_sta']; } if (!Validate_1.Validate.isUndefined(props.x_has_ssh_hostkey)) { this.xHasSshHostkey = props.x_has_ssh_hostkey; } if (!Validate_1.Validate.isUndefined(props.gateway_mac)) { this.gatewayMac = props.gateway_mac; } if (!Validate_1.Validate.isUndefined(props.x_ssh_hostkey_fingerprint)) { this.xSshHostkeyFingerprint = props.x_ssh_hostkey_fingerprint; } if (!Validate_1.Validate.isUndefined(props.satisfaction)) { this.satisfaction = props.satisfaction; } if (!Validate_1.Validate.isUndefined(props.sys_error_caps)) { this.sysErrorCaps = props.sys_error_caps; } if (!Validate_1.Validate.isUndefined(props.last_uplink)) { this.lastUplink = props.last_uplink; } if (!Validate_1.Validate.isUndefined(props.ssh_session_table)) { this.sshSessionTable = props.ssh_session_table; } if (!Validate_1.Validate.isUndefined(props.led_override)) { this.ledOverride = props.led_override; } if (!Validate_1.Validate.isUndefined(props.led_override_color)) { this.ledOverrideColor = props.led_override_color; } if (!Validate_1.Validate.isUndefined(props.led_override_color_brightness)) { this.ledOverrideColorBrightness = props.led_override_color_brightness; } if (!Validate_1.Validate.isUndefined(props.outdoor_mode_override)) { this.outdoorModeOverride = props.outdoor_mode_override; } if (!Validate_1.Validate.isUndefined(props.lcm_brightness_override)) { this.lcmBrightnessOverride = props.lcm_brightness_override; } if (!Validate_1.Validate.isUndefined(props.lcm_idle_timeout_override)) { this.lcmIdleTimeoutOverride = props.lcm_idle_timeout_override; } if (!Validate_1.Validate.isUndefined(props.default)) { this.default = props.default; } if (!Validate_1.Validate.isUndefined(props.discovered_via)) { this.discoveredVia = props.discovered_via; } if (!Validate_1.Validate.isUndefined(props.adopt_ip)) { this.adoptIp = props.adopt_ip; } if (!Validate_1.Validate.isUndefined(props.adopt_url)) { this.adoptUrl = props.adopt_url; } if (!Validate_1.Validate.isUndefined(props.disabled)) { this.disabled = props.disabled; } return this; }; /** * * @param type - two options: 'soft' or 'hard', defaults to soft * soft can be used for all devices, requests a plain restart of that device * hard is special for PoE switches and besides the restart also requests a * power cycle on all PoE capable ports. Keep in mind that a 'hard' reboot * does *NOT* trigger a factory-reset. */ BaseDevice.prototype.reboot = function () { return __awaiter(this, arguments, void 0, function (type) { if (type === void 0) { type = 'soft'; } return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.site.devManager({ cmd: 'restart', mac: this.mac.toLowerCase(), reboot_type: type })]; case 1: return [2 /*return*/, ((_a.sent()).meta.rc === 'ok')]; } }); }); }; // not tested BaseDevice.prototype.forceProvision = function () { return this.site.devManager({ cmd: 'force-provision', mac: this.mac.toLowerCase() }); }; /** * Override LED mode for a device (using REST) */ BaseDevice.prototype.setLedOverride = function (props) { var payload = {}; if (Validate_1.Validate.isDefinedNotNull(props.mode)) { payload.led_override = props.mode; } if (props.color) { if (!Validate_1.Validate.hexColor(props.color)) { throw new Errors_1.ClientError('color need to be in hexadecimal format', Errors_1.EErrorsCodes.BAD_PARAMETERS); } payload.led_override_color = props.color; } if (props.brightness) { if (!Validate_1.Validate.isNumber(props.brightness) || props.brightness < 0) { throw new Errors_1.ClientError('brightness need to be a positive number', Errors_1.EErrorsCodes.BAD_PARAMETERS); } payload.led_override_color_brightness = props.brightness; } return this.updateDevice(payload); }; BaseDevice.prototype.locate = function () { return __awaiter(this, arguments, void 0, function (locate) { if (locate === void 0) { locate = true; } return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.site.devManager({ mac: this.mac.toLowerCase(), cmd: locate ? 'set-locate' : 'unset-locate' })]; case 1: return [2 /*return*/, ((_a.sent()).meta.rc === 'ok')]; } }); }); }; BaseDevice.prototype.updateDevice = function (payload) { return this._updateDevice(payload); }; BaseDevice.prototype._updateDevice = function (payload) { return __awaiter(this, void 0, void 0, function () { var res; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.instance.put('/rest/device/:_id', payload, { urlParams: { _id: this._id } })]; case 1: res = (_a.sent()).data; if (res.data.length > 0) { this.import(res.data[0]); } return [2 /*return*/, this]; } }); }); }; BaseDevice.debug = (0, util_1.createDebugger)('Device'); return BaseDevice; }(_ObjectSubSite_1._ObjectSubSite)); exports.BaseDevice = BaseDevice;