@autocodingsystems/gateway-client
Version:
Library and commandline utility to control the acs gateway - device drivers for the most common industrial production line devices
64 lines (53 loc) • 2.57 kB
JavaScript
var api = require('../api');
var globToRegExp = require('glob-to-regexp');
async function copy_command(argv, conf, options) {
var source = require('../parameters/source')(argv);
var name = require('../parameters/name')(argv);
var target = require('../parameters/target')(argv);
var mode = require('../parameters/namecollisionmode')(argv);
var raw = require('../parameters/raw')(argv);
if (!source.value || !target.value || !mode.value || !api(null).collisionmodes[mode.value]) {
usage();
return;
}
var filter = globToRegExp(name.value, {extended: true});
try {
var [devices, existingdevices] = await Promise.all([api(source.value).raw.getDevices(), api(target.value).raw.getDevices()]);
devices = devices.filter(d => filter.test(d.Name));
devices = devices.map(d => Object.assign({}, d, {NewName: api(null).collisionmodes[mode.value](d.Name, existingdevices)}));
var copies = await Promise.all(devices.map(async d => {
var details = await api(source.value).raw.getDevice(d.Id);
var copydetails = {
DeviceName: d.NewName,
SupportedDeviceId: details.SupportedDeviceId,
ApplicationNodeId: details.ApplicationNodeId,
SocketClass: details.SocketClass,
SocketConfigurationSettings: details.SocketConfiguration.map(conf => conf),
DriverConfigurationSettings: details.DeviceConfiguration.map(conf => conf)
}
var addresult = copydetails.DeviceName ? await api(target.value).raw.addDevice(copydetails) : {DeviceId: details.Id, skipped: true};
addresult = Object.assign({}, addresult, details, {NewName: d.NewName});
return addresult;
}));
copies.forEach(cr => {
if (raw.value) {
console.log(JSON.stringify(cr));
} else if (cr.skipped) {
console.log(`Skipped ${cr.Id} ${cr.Name}`)
} else {
console.log(`Copied ${cr.Id} ${cr.Name} to ${cr.DeviceId} ${cr.NewName}`)
}
})
} catch (e) {
throw e;
}
function usage() {
console.log('copy: copy devices from a source gateway to a target gateway: ');
console.log(' ' + source.description);
console.log(' ' + name.description);
console.log(' ' + target.description);
console.log(' ' + mode.description);
console.log(' ' + raw.description);
}
}
module.exports = copy_command;