unifi-client
Version:
NodeJs client for Unifi products (https://www.ui.com/)
234 lines (233 loc) • 10.8 kB
JavaScript
"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.Site = void 0;
var Hotspot_1 = require("../Hotspot");
var Clients_1 = require("../Clients");
var Firewall_1 = require("../Firewall");
var _ObjectSubController_1 = require("../commons/_ObjectSubController");
var Validate_1 = require("../commons/Validate");
var WebSockets_1 = require("../WebSockets");
var Stats_1 = require("../Stats");
var Errors_1 = require("../Errors");
var interfaces_1 = require("../interfaces");
var util_1 = require("../util");
var ClientsGroups_1 = require("../Clients/ClientsGroups");
var Devices_1 = require("../Devices");
var Networks_1 = require("../Networks");
var Site = /** @class */ (function (_super) {
__extends(Site, _super);
function Site(controller, props) {
var _this = _super.call(this, {
controller: controller,
instance: controller.controllerInstance
}) || this;
if (!props.name) {
throw new Errors_1.ClientError('name is mandatory for a site . The default unifi site name is "default"');
}
_this.name = props.name;
_this.debug = Site.debug.extend(_this.name);
if (!Validate_1.Validate.isUndefined(props._id)) {
_this._id = props._id;
}
if (!Validate_1.Validate.isUndefined(props.anonymous_id)) {
_this.anonymous_id = props.anonymous_id;
}
if (!Validate_1.Validate.isUndefined(props.desc)) {
_this.desc = props.desc;
}
if (!Validate_1.Validate.isUndefined(props.attr_hidden_id)) {
_this.attr_hidden_id = props.attr_hidden_id;
}
if (!Validate_1.Validate.isUndefined(props.attr_no_delete)) {
_this.attr_no_delete = props.attr_no_delete;
}
if (!Validate_1.Validate.isUndefined(props.role)) {
_this.role = props.role;
}
_this.needVersion('role_hotspot', props.role_hotspot, undefined, true);
var config = {
controller: _this.controller,
site: _this
};
//init objects
_this.firewall = new Firewall_1.Firewall(config);
_this.hotspots = new Hotspot_1.Hotspots(config);
_this.clients = new Clients_1.Clients(config);
_this.clientsGroups = new ClientsGroups_1.ClientsGroups(config);
_this.devices = new Devices_1.Devices(config);
_this.stats = new Stats_1.Stats(config);
_this.networks = new Networks_1.Networks(config);
_this.instance = _this.controller.createInstance(_this.name, {
proxyNamespace: interfaces_1.EProxyNamespaces.NETWORK,
apiPart: 'api'
});
return _this;
}
// TODO test return
Site.prototype.adoptDevice = function (device) {
return __awaiter(this, void 0, void 0, function () {
var mac, payload;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (Validate_1.Validate.implementsTKeys(device, ['mac'])) {
mac = device.mac;
}
else if (!Validate_1.Validate.mac(device)) {
throw new Errors_1.ClientError('parameter need to be a mac address', Errors_1.EErrorsCodes.BAD_PARAMETERS);
}
else {
mac = device;
}
payload = { cmd: 'adopt', mac: mac.toLowerCase() };
return [4 /*yield*/, this.devManager(payload)];
case 1: return [2 /*return*/, (_a.sent()).data];
}
});
});
};
/**
*
* @param enable - true switches LEDs of all the access points ON, false switches them OFF
*/
Site.prototype.enableLEDs = function () {
return __awaiter(this, arguments, void 0, function (enable) {
if (enable === void 0) { enable = true; }
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.settingsManager({ led_enabled: enable })];
case 1: return [2 /*return*/, (_a.sent()).data];
}
});
});
};
Site.prototype.getSettings = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.instance.get('/get/setting')];
case 1: return [2 /*return*/, (_a.sent()).data.data];
}
});
});
};
Site.prototype.settingsManager = function (settings) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.instance.post('/set/setting/mgmt', settings)];
case 1: return [2 /*return*/, (_a.sent()).data];
}
});
});
};
Site.prototype.devManager = function (payload) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.instance.post('/cmd/devmgr', payload)];
case 1: return [2 /*return*/, (_a.sent()).data];
}
});
});
};
// this function need to never be async !!! but return a promise ( so this.ws is init before the real init )
Site.prototype.initWebSockets = function () {
this.debug('initWebSockets()');
//build WS url
var wsConfig = this.controller.buildUrl({
baseURL: this.controllerInstance.defaults.baseURL,
url: '/events',
site: this.name
}, true);
if (!wsConfig.url) {
throw new Errors_1.ClientError('fail to generate site WS url', Errors_1.EErrorsCodes.UNKNOWN_ERROR);
}
var wsURL = "".concat(wsConfig.baseURL).concat(wsConfig.url);
this.ws = new WebSockets_1.UnifiWebsockets({
controller: this.controller,
strictSSL: this.controller.strictSSL,
url: wsURL.toString(),
isController: false
});
return this.ws.initWebSockets();
};
Site.prototype.on = function (eventName, cb) {
this.debug('on(%s)', eventName);
if (!this.ws) {
//ws initialization will be done asynchronously
// noinspection JSIgnoredPromiseFromCall
this.initWebSockets();
}
this.ws.on(eventName, cb);
return this;
};
Site.prototype.getNetworkStatus = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
this.debug('getNetworkStatus()');
return [4 /*yield*/, this.instance.get("/network_status", {
apiVersion: 2
})];
case 1: return [2 /*return*/, (_a.sent()).data];
}
});
});
};
Site.debug = (0, util_1.createDebugger)('site');
return Site;
}(_ObjectSubController_1._ObjectSubController));
exports.Site = Site;