UNPKG

unifi-client

Version:

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

504 lines (503 loc) 23.4 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 __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; 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.Client = void 0; var _ObjectSubSite_1 = require("../commons/_ObjectSubSite"); var Errors_1 = require("../Errors"); var Validate_1 = require("../commons/Validate"); var util_1 = require("../util"); var Client = /** @class */ (function (_super) { __extends(Client, _super); function Client(config, props) { var _this = _super.call(this, config) || this; _this.mac = props.mac; if (!_this.mac) { throw new Errors_1.ClientError('mac is needed', Errors_1.EErrorsCodes.UNKNOWN_ERROR); } _this.debug = Client.debug.extend(_this.mac); _this.old = { mac: _this.mac }; _this.import(props); return _this; } Object.defineProperty(Client.prototype, "old", { get: function () { return this.getPrivate('old'); }, set: function (data) { this.setPrivate('old', data); }, enumerable: false, configurable: true }); Client.prototype.importValue = function (k, v) { // @ts-ignore this.old[k] = v; // @ts-ignore this[k] = v; }; Client.prototype.import = function (props) { var _a, _b, _c, _d, _e, _f; this.debug('import()'); if (!Validate_1.Validate.isUndefined(props._id)) { this.importValue('_id', props._id); } if (!Validate_1.Validate.isUndefined(props.site_id)) { this.importValue('siteId', props.site_id); } if (!Validate_1.Validate.isUndefined(props.oui)) { this.importValue('oui', props.oui); } if (!Validate_1.Validate.isUndefined(props.hostname)) { this.importValue('hostname', props.hostname); } //prepare the device var device = (_c = (_b = (_a = this.device) === null || _a === void 0 ? void 0 : _a._original) !== null && _b !== void 0 ? _b : this.device) !== null && _c !== void 0 ? _c : {}; if (!Validate_1.Validate.isUndefined(props.fingerprint_source)) { device._fingerPrintSource = props.fingerprint_source; } if (!Validate_1.Validate.isUndefined(props.dev_cat)) { device.category = props.dev_cat; } if (!Validate_1.Validate.isUndefined(props.dev_family)) { device.family = props.dev_family; } if (!Validate_1.Validate.isUndefined(props.os_name)) { device.os = props.os_name; } if (!Validate_1.Validate.isUndefined(props.os_name)) { device.os = props.os_name; } if (!Validate_1.Validate.isUndefined(props.dev_vendor)) { device.vendor = props.dev_vendor; } if (!Validate_1.Validate.isUndefined(props.dev_id)) { device.id = props.dev_id; } if (!Validate_1.Validate.isUndefined(props.fingerprint_engine_version)) { device._fingerprintEngineVersion = props.fingerprint_engine_version; } if (!Validate_1.Validate.isUndefined(props.device_name)) { device.name = props.device_name; } if ((_f = (_d = props.fingerprint_override) !== null && _d !== void 0 ? _d : (_e = this.device) === null || _e === void 0 ? void 0 : _e._overridden) !== null && _f !== void 0 ? _f : false) { var _original = __assign({}, device); device = { id: props.dev_id_override, _overridden: true, _original: _original }; } this.importValue('device', device); if (!Validate_1.Validate.isUndefined(props.confidence)) { this.importValue('confidence', props.confidence); } if (!Validate_1.Validate.isUndefined(props.fw_version)) { this.importValue('firmwareVersion', props.fw_version); } if (!Validate_1.Validate.isUndefined(props.score)) { this.importValue('score', props.score); } if (!Validate_1.Validate.isUndefined(props.blocked)) { this.importValue('blocked', props.blocked); } if (!Validate_1.Validate.isUndefined(props.user_group_id) || !Validate_1.Validate.isUndefined(props.usergroup_id)) { this.importValue('groupId', props.user_group_id || props.usergroup_id); } if (!Validate_1.Validate.isUndefined(props.name)) { this.importValue('name', props.name); } if (!Validate_1.Validate.isUndefined(props.note)) { this.importValue('note', props.note); } if (!Validate_1.Validate.isUndefined(props.noted)) { this.importValue('noted', props.noted); } if (!Validate_1.Validate.isUndefined(props.is_guest)) { this.importValue('isGuest', props.is_guest); } if (!Validate_1.Validate.isUndefined(props.is_wired)) { this.importValue('isWired', props.is_wired); } if (!Validate_1.Validate.isUndefined(props.fixed_ip)) { this.importValue('fixedIp', props.fixed_ip); } if (!Validate_1.Validate.isUndefined(props.network_id)) { this.importValue('networkId', props.network_id); } if (!Validate_1.Validate.isUndefined(props.use_fixedip)) { this.importValue('useFixedIp', props.use_fixedip); } if (!Validate_1.Validate.isUndefined(props.first_seen)) { this.importValue('firstSeen', (0, util_1.convertTimestampSecondsToDate)(props.first_seen)); } if (!Validate_1.Validate.isUndefined(props.last_seen)) { this.importValue('lastSeen', (0, util_1.convertTimestampSecondsToDate)(props.last_seen)); } if (!Validate_1.Validate.isUndefined(props.assoc_time)) { this.importValue('assocTime', (0, util_1.convertTimestampSecondsToDate)(props.assoc_time)); } if (!Validate_1.Validate.isUndefined(props.latest_assoc_time)) { this.importValue('latestAssocTime', (0, util_1.convertTimestampSecondsToDate)(props.latest_assoc_time)); } if (!Validate_1.Validate.isUndefined(props.user_id)) { this.importValue('userId', props.user_id); } if (!Validate_1.Validate.isUndefined(props._uptime_by_ugw)) { this.importValue('_uptimeByUGW', props._uptime_by_ugw); } if (!Validate_1.Validate.isUndefined(props._last_seen_by_ugw)) { this.importValue('_lastSeenByUGW', (0, util_1.convertTimestampSecondsToDate)(props._last_seen_by_ugw)); } if (!Validate_1.Validate.isUndefined(props._is_guest_by_ugw)) { this.importValue('_isGuestByUGW', props._is_guest_by_ugw); } if (!Validate_1.Validate.isUndefined(props.gw_mac)) { this.importValue('gwMac', props.gw_mac); } if (!Validate_1.Validate.isUndefined(props.network)) { this.importValue('network', props.network); } if (!Validate_1.Validate.isUndefined(props.uptime)) { this.importValue('uptime', props.uptime); } if (!Validate_1.Validate.isUndefined(props.tx_bytes)) { this.importValue('txBytes', props.tx_bytes); } if (!Validate_1.Validate.isUndefined(props.rx_bytes)) { this.importValue('rxBytes', props.rx_bytes); } if (!Validate_1.Validate.isUndefined(props.tx_packets)) { this.importValue('txPackets', props.tx_packets); } if (!Validate_1.Validate.isUndefined(props.tx_retries)) { this.importValue('txRetries', props.tx_retries); } if (!Validate_1.Validate.isUndefined(props.wifi_tx_attempts)) { this.importValue('wifiTxAttempts', props.wifi_tx_attempts); } if (!Validate_1.Validate.isUndefined(props.rx_packets)) { this.importValue('rxPackets', props.rx_packets); } if (!Validate_1.Validate.isUndefined(props['tx_bytes-r'])) { this.importValue('txBytesR', props['tx_bytes-r']); } if (!Validate_1.Validate.isUndefined(props['rx_bytes-r'])) { this.importValue('rxBytesR', props['rx_bytes-r']); } if (!Validate_1.Validate.isUndefined(props.qos_policy_applied)) { this.importValue('qosPolicyApplied', props.qos_policy_applied); } if (!Validate_1.Validate.isUndefined(props._uptime_by_usw)) { this.importValue('_uptimeByUSW', props._uptime_by_usw); } if (!Validate_1.Validate.isUndefined(props._last_seen_by_usw)) { this.importValue('_lastSeenByUSW', (0, util_1.convertTimestampSecondsToDate)(props._last_seen_by_usw)); } if (!Validate_1.Validate.isUndefined(props._is_guest_by_usw)) { this.importValue('_isGuestByUSW', props._is_guest_by_usw); } if (!Validate_1.Validate.isUndefined(props.sw_mac)) { this.importValue('swMac', props.sw_mac); } if (!Validate_1.Validate.isUndefined(props.sw_depth)) { this.importValue('swDepth', props.sw_depth); } if (!Validate_1.Validate.isUndefined(props.sw_port)) { this.importValue('swPort', props.sw_port); } if (!Validate_1.Validate.isUndefined(props.wired_rate_mbps)) { this.importValue('wiredRateMbps', props.wired_rate_mbps); } if (!Validate_1.Validate.isUndefined(props.anomalies)) { this.importValue('anomalies', props.anomalies); } if (!Validate_1.Validate.isUndefined(props.ip)) { this.importValue('ip', props.ip); } if (!Validate_1.Validate.isUndefined(props.satisfaction)) { this.importValue('satisfaction', props.satisfaction); } if (!Validate_1.Validate.isUndefined(props['bytes-r'])) { this.importValue('bytesR', props['bytes-r']); } if (!Validate_1.Validate.isUndefined(props._uptime_by_uap)) { this.importValue('_uptimeByUAP', props._uptime_by_uap); } if (!Validate_1.Validate.isUndefined(props._last_seen_by_uap)) { this.importValue('_lastSeenByUAP', (0, util_1.convertTimestampSecondsToDate)(props._last_seen_by_uap)); } if (!Validate_1.Validate.isUndefined(props._is_guest_by_uap)) { this.importValue('_isGuestByUAP', props._is_guest_by_uap); } if (!Validate_1.Validate.isUndefined(props.ap_mac)) { this.importValue('apMac', props.ap_mac); } if (!Validate_1.Validate.isUndefined(props.channel)) { this.importValue('channel', props.channel); } if (!Validate_1.Validate.isUndefined(props.radio)) { this.importValue('radio', props.radio); } if (!Validate_1.Validate.isUndefined(props.radio_name)) { this.importValue('radioName', props.radio_name); } if (!Validate_1.Validate.isUndefined(props.essid)) { this.importValue('essid', props.essid); } if (!Validate_1.Validate.isUndefined(props.bssid)) { this.importValue('bssid', props.bssid); } if (!Validate_1.Validate.isUndefined(props.powersave_enabled)) { this.importValue('powersaveEnabled', props.powersave_enabled); } if (!Validate_1.Validate.isUndefined(props.is_11r)) { this.importValue('is11r', props.is_11r); } if (!Validate_1.Validate.isUndefined(props.user_group_id_computed)) { this.importValue('userGroupIdComputed', props.user_group_id_computed); } if (!Validate_1.Validate.isUndefined(props.ccq)) { this.importValue('ccq', props.ccq); } if (!Validate_1.Validate.isUndefined(props.rssi)) { this.importValue('rssi', props.rssi); } if (!Validate_1.Validate.isUndefined(props['wired-tx_packets'])) { this.importValue('wiredTxPackets', props['wired-tx_packets']); } if (!Validate_1.Validate.isUndefined(props['wired-rx_packets'])) { this.importValue('wiredRxPackets', props['wired-rx_packets']); } if (!Validate_1.Validate.isUndefined(props['wired-tx_bytes-r'])) { this.importValue('wiredTxBytesR', props['wired-tx_bytes-r']); } if (!Validate_1.Validate.isUndefined(props['wired-rx_bytes-r'])) { this.importValue('wiredRxBytesR', props['wired-rx_bytes-r']); } if (!Validate_1.Validate.isUndefined(props.noise)) { this.importValue('noise', props.noise); } if (!Validate_1.Validate.isUndefined(props.signal)) { this.importValue('signal', props.signal); } if (!Validate_1.Validate.isUndefined(props.tx_rate)) { this.importValue('txRate', props.tx_rate); } if (!Validate_1.Validate.isUndefined(props.rx_rate)) { this.importValue('rxRate', props.rx_rate); } if (!Validate_1.Validate.isUndefined(props.tx_power)) { this.importValue('txPower', props.tx_power); } if (!Validate_1.Validate.isUndefined(props.idletime)) { this.importValue('idletime', props.idletime); } if (!Validate_1.Validate.isUndefined(props.dhcpend_time)) { this.importValue('dhcpendTime', props.dhcpend_time); } if (!Validate_1.Validate.isUndefined(props.anon_client_id)) { this.importValue('anonClientId', props.anon_client_id); } if (!Validate_1.Validate.isUndefined(props.tx_mcs)) { this.importValue('txMcs', props.tx_mcs); } if (!Validate_1.Validate.isUndefined(props.vlan)) { this.importValue('vlan', props.vlan); } if (!Validate_1.Validate.isUndefined(props.radio_proto)) { this.importValue('radioProto', props.radio_proto); } if (!Validate_1.Validate.isUndefined(props['wired-tx_bytes'])) { this.importValue('wiredTxBytes', props['wired-tx_bytes']); } if (!Validate_1.Validate.isUndefined(props['wired-rx_bytes'])) { this.importValue('wiredRxBytes', props['wired-rx_bytes']); } if (!Validate_1.Validate.isUndefined(props['disconnect_timestamp'])) { this.importValue('lastDisconnect', (0, util_1.convertTimestampSecondsToDate)(props.disconnect_timestamp)); } return this; }; /** * Forget one or more client devices * * NOTE: * only supported with controller versions 5.9.X and higher, can be * slow (up to 5 minutes) on larger controllers */ Client.prototype.forget = function () { return __awaiter(this, void 0, void 0, function () { var json, res; var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: this.checkNeedVersion('5.9.0'); this.debug('forget()'); json = { cmd: 'forget-sta', macs: [this.mac] }; return [4 /*yield*/, this.instance.post('/cmd/stamgr', json, { timeout: 10 * 60 * 1000 })]; case 1: res = (_b.sent()).data; return [2 /*return*/, ((_a = res === null || res === void 0 ? void 0 : res.meta) === null || _a === void 0 ? void 0 : _a.rc) === 'ok']; } }); }); }; Client.prototype.save = function () { return __awaiter(this, void 0, void 0, function () { var device, res; return __generator(this, function (_a) { switch (_a.label) { case 0: this.debug('save()'); device = {}; device.name = this.name || ''; // check if note was updated if (this.note != this.old.note) { // add note / noted device.note = this.note; device.noted = !!this.note; } //set user group id device.user_group_id = this.groupId; device.usergroup_id = this.groupId; //check fixed ip if (this.useFixedIp && !this.fixedIp) { throw new Errors_1.ClientError('fixed_ip is needed to use fixed_ip', Errors_1.EErrorsCodes.FIXED_IP_NEEDED); } else if (this.fixedIp && !this.useFixedIp) { this.debug('warn : please set useFixedIp to true'); this.useFixedIp = true; } device.use_fixedip = this.useFixedIp; device.fixed_ip = this.fixedIp; return [4 /*yield*/, this.instance.post('/upd/user/:id', device, { urlParams: { site: this.site.name, id: this._id } })]; case 1: res = ((_a.sent()).data.data || []).pop(); //import only if res is updated if (res) { this.import(res); } return [2 /*return*/, this]; } }); }); }; Client.prototype.block = function () { return __awaiter(this, void 0, void 0, function () { var res; var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: this.debug('block()'); return [4 /*yield*/, this.instance.post('/cmd/stamgr', { cmd: 'block-sta', mac: this.mac.toLowerCase() })]; case 1: res = (_a = (_b.sent()).data) === null || _a === void 0 ? void 0 : _a.data; if (res) { this.import(res); } return [2 /*return*/, this]; } }); }); }; Client.prototype.unblock = function () { return __awaiter(this, void 0, void 0, function () { var res; var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: this.debug('unblock()'); return [4 /*yield*/, this.instance.post('/cmd/stamgr', { cmd: 'unblock-sta', mac: this.mac.toLowerCase() })]; case 1: res = (_a = (_b.sent()).data) === null || _a === void 0 ? void 0 : _a.data; if (res) { this.import(res); } return [2 /*return*/, this]; } }); }); }; Client.debug = (0, util_1.createDebugger)('client'); return Client; }(_ObjectSubSite_1._ObjectSubSite)); exports.Client = Client;