workspace-integrations
Version:
Webex Workspace Integrations NodeJS SDK
136 lines (135 loc) • 6.07 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const util_1 = require("../util");
const logger_1 = require("../logger");
class XapiImpl {
constructor(http) {
this.status = {
get: async (deviceId, path, allowEmpty = false) => {
if (!(0, util_1.isStr)(deviceId) || !(0, util_1.isStr)(path)) {
throw new Error('xStatus: missing deviceId or path');
}
const name = path.replace(/ /g, '.');
const res = await this.http.xStatus(deviceId, name);
const answer = res === null || res === void 0 ? void 0 : res.result;
if ((0, util_1.emptyObj)(answer) && !allowEmpty) {
throw new Error('xStatus not found. Did you include the API in the manifest?');
}
if ((0, util_1.emptyObj)(answer) && allowEmpty) {
return false;
}
if (answer) {
return (0, util_1.removePath)(name, answer);
}
},
on: (path, callback) => {
logger_1.default.info('Subscribe to xStatus ' + path);
this.statusListeners.push({ path, callback });
},
};
this.config = {
get: async (deviceId, path) => {
var _a;
if (!(0, util_1.isStr)(deviceId) || !(0, util_1.isStr)(path)) {
throw new Error('xConfig: missing deviceId or path');
}
const name = path.replace(/ /g, '.');
const items = (_a = (await this.http.xConfig(deviceId, name))) === null || _a === void 0 ? void 0 : _a.items;
// console.log('config items', items);
if (!Object.keys(items).length) {
throw new Error('xConfig not found on device. Did you include the API in the manifest?');
}
const tree = (0, util_1.toTree)(items);
// console.log('tree:', JSON.stringify(tree, null, 2));
return (0, util_1.removePath)(name, tree);
},
set: async (deviceId, path, value) => {
if (!(0, util_1.isStr)(deviceId) || !path) {
throw new Error('xConfig: missing deviceId or path');
}
const name = (0, util_1.isStr)(path) ? path.replace(/ /g, '.') : path;
return await this.http.xConfigSet(deviceId, [{ path: name, value }]);
},
/**
* Set multiple configs in one go. Provide each config as key/value pair. Eg:
*
* ```js
* const configs = {
* 'Audio.Ultrasound.MaxVolume': 0,
* 'Audio.DefaultVolume': 33,
* 'Audio.SoundsAndAlerts.RingVolume': 66,
* };
* await integration.xapi.config.setMany(device, configs);
* ```
*/
setMany: async (deviceId, values) => {
const list = Object.entries(values).map(([p, value]) => ({ path: p, value }));
return await this.http.xConfigSet(deviceId, list);
},
};
this.event = {
on: (path, callback) => {
logger_1.default.info('Subscribing to xEvent ' + path);
this.eventListeners.push({ path, callback });
},
};
this.http = http;
this.eventListeners = [];
this.statusListeners = [];
}
async command(deviceId, path, params, multiline) {
if (!(0, util_1.isStr)(deviceId) || !(0, util_1.isStr)(path)) {
throw new Error('xCommand: missing deviceId or path');
}
if (params && !(0, util_1.isObj)(params)) {
throw new Error('xCommand: params must be object');
}
const cmd = path.replace(/ /g, '.');
const res = await this.http.xCommand(deviceId, cmd, params, multiline);
return res === null || res === void 0 ? void 0 : res.result;
}
processNotification(data) {
var _a;
const { deviceId, type } = data;
const props = (_a = data === null || data === void 0 ? void 0 : data.changes) === null || _a === void 0 ? void 0 : _a.updated;
if (type === 'status' && props) {
const notification = data;
for (const [key, value] of Object.entries(props)) {
this.statusListeners.forEach((listener) => {
if ((0, util_1.pathMatch)(key, listener.path)) {
listener.callback(deviceId, key, value, notification);
}
});
// console.log('status', `${shortName(deviceId)} => ${key}: ${value} (${timestamp})`);
}
for (const path of data.changes.removed) {
this.statusListeners.forEach((listener) => {
if ((0, util_1.pathMatch)(path, listener.path)) {
listener.callback(deviceId, path, { ghost: true }, notification);
}
});
}
}
else if (type === 'events') {
data.events.forEach((e) => {
const path = e.key;
const event = e.value;
this.eventListeners.forEach((listener) => {
const notification = data;
if ((0, util_1.pathMatch)(path, listener.path)) {
listener.callback(deviceId, path, event, notification);
}
});
// console.log('event', shortName(deviceId), path, JSON.toString(event), timestamp);
});
}
else if (type === 'healthCheck') {
console.log('xapi: got health check message');
}
else {
// console.log('Received unknown notifications', type, data);
// handle: type: 'action', add listener for it
}
}
}
exports.default = XapiImpl;