Skip to content

Commit

Permalink
add scripts for being able to run macros externally
Browse files Browse the repository at this point in the history
means you can run the macros externally from node without chaning them
allows you to use your own editor, and git, for macro development

note you need to do 'npm install jsxapi' to be able to run it,
if you dont have jsaxpi in your global environment
  • Loading branch information
tbjolset committed Nov 24, 2021
1 parent 7ed259e commit 1dbda64
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
24 changes: 24 additions & 0 deletions run-macro
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env node

/**
* Let's you run macros easily from node (your laptop) instead of macro editor (device)
* without changing any code in the macro.
*/
const adapter = require('./universal-adapter');

async function go(host, username, password, macro) {
const videoDevice = { host, username, password };
await adapter(videoDevice);
require(macro); // macro starts with xapi available
}

if (process.argv.length < 6) {
console.log('Run a Webex Device macro remotely from a node.js environment');
console.log('Usage: ./run-macro <host> <username> <password> <macrofile>');
console.log('\neg: ./run 10.47.31.28 admin mypwd ./mymacro.js')
console.log('NOTE: macro file path is relative to the run-macro scripts folder, and must typically start with ./');
process.exit(1);
}

const [exec, script, host, username, password, macro] = process.argv;
go(host, username, password, macro);
47 changes: 47 additions & 0 deletions universal-adapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Adapter that makes it possible to run a pure macro from an external node context instead.
* Useful to develop, test and commit macros on own laptop instead of in macro editor,
* then just copy/paste/export/commit & push the macro file when done.
*
* Macros on video device can just import the xapi object directly, but when external you need
* to provide login details. Some magic here makes lets you connect to the video device with jsxapi,
* then require the macro file, which will run as if it had the same xapi object as the video device
*
* Usage:
* const adapter = require('./universal-adapter');
* const videoDevice = { host: '10.0.0.99', username: 'admin', password: 'password' };
* await adapter(videoDevice)
* require('./mymacro'); // macro starts with xapi available
*
*/

const jsxapi = require('jsxapi');
const Module = require('module');

const originalRequire = Module.prototype.require;

function connectXapi ({ host, username, password }) {
return new Promise((resolve, reject) => {
jsxapi
.connect('wss://' + host, {
username,
password,
})
.on('error', e => reject(e))
.on('ready', async (xapi) => {
console.log('Connected to', host);
resolve(xapi);
});
});
}

async function init(deviceLogin) {
const xapi = await connectXapi(deviceLogin);
Module.prototype.require = function() {
const moduleName = arguments[0];
if (moduleName === 'xapi') return xapi;
return originalRequire.apply(this, arguments);
}
}

module.exports = init;

0 comments on commit 1dbda64

Please sign in to comment.