UNPKG

@autocodingsystems/gateway-client

Version:

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

27 lines (21 loc) 1.28 kB
module.exports = function applyDataToJob(job, data) { if (!Array.isArray(data)) { throw new Error('data argument must be an array')} if (typeof job !== 'object' || Array.isArray(job)) { throw new Error('job argument must be an object')} if (typeof job.VariableSet !== 'object') { throw new Error('job object must have a variableset')} if (!Array.isArray(job.VariableSet.Values)) { throw new Error('job object must have a variableset with a values array')} data.forEach( (d,i) => { try { if (typeof d != 'object') { throw new Error('Data must consist of an array of objects')} if (typeof d.Key != 'string') { throw new Error('Data objects must have a "Key" property that is a string')} if (typeof d.Value != 'string') { throw new Error('Data objects must have a "Key" property that is a string')} } catch (e) { throw new Error(`Data error at index ${i}: ${e.Message || (typeof e == 'string' ? e : JSON.stringify(e)) }`); } var existing = job.VariableSet.Values.filter(v => v.Key === d.Key)[0]; if (!existing) { job.VariableSet.Values.push(d); existing = d; } existing.Value = d.Value; }); }