UNPKG

@autocodingsystems/gateway-client

Version:

Library and commandline utility to control the acs gateway - device drivers for the most common industrial production line devices

60 lines (48 loc) 2.07 kB
var api = require('../api'); const {promisify} = require('util'); const fs = require('fs'); const readfile = promisify(fs.readFile); const stripJsonComments = require('strip-json-comments'); async function add_command(argv, conf, options) { var target = require('../parameters/target')(argv); var file = require('../parameters/devicefile')(argv); var mode = require('../parameters/namecollisionmode')(argv); var name = require('../parameters/newname')(argv); var raw = require('../parameters/raw')(argv); if (!target.value || !mode.value || !api(null).collisionmodes[mode.value] || !file.value) { usage(); return; } try { var json = await readfile(file.value, 'utf8'); json = JSON.parse(stripJsonComments(json)); json.DeviceName = name.value || json.DeviceName; if (!json.DeviceName) { console.log('DeviceName must be specified in the JSON document'); usage(); return; } var originalname = json.DeviceName; var existingdevices = await api(target.value).raw.getDevices(); json.DeviceName = api(target.value).collisionmodes[mode.value](json.DeviceName, existingdevices); var addresult = (json.DeviceName ? await api(target.value).raw.addDevice(json) : {skipped: true, originalname}); if (raw.value) { console.log(JSON.stringify(addresult)); } else if (addresult.skipped) { console.log(`Skipped as device already exists with name: ${addresult.originalname}`) } else { console.log(`Added ${addresult.DeviceId} ${json.DeviceName}`) } } catch (e) { throw e; } function usage() { console.log('add: add a device to a target gateway: '); console.log(' ' + target.description); console.log(' ' + file.description); console.log(' ' + mode.description); console.log(' ' + name.description); console.log(' ' + raw.description); } } module.exports = add_command;