UNPKG

@autocodingsystems/gateway-client

Version:

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

49 lines (39 loc) 1.46 kB
const timeout = (ms) => new Promise( (res) => setTimeout(res, ms) ); function wrap(api) { // polls the given device on the gateway until the job status is different from the currentstatus // returns the results of the last poll when done return async function waitforjobstatuschange(id, currentstatus, timeoutms) { var waiting = true; var timedout = false; var overalltimeout = null; if (timeoutms) { overalltimeout = setTimeout(_ => { waiting = false; timedout = true; overalltimeout = null; }, timeoutms); } var jobstatus = null; while (waiting) { try { jobstatus = await api.getDeviceJobStatus(id); } catch (e) { jobstatus = jobstatus || {Status: 'Unknown'}; jobstatus.Error = e.Message || (typeof e == 'string' ? e : JSON.stringify(e)) } waiting = jobstatus.Status === currentstatus; if (waiting) { await timeout(200); } } if (overalltimeout) { clearTimeout(overalltimeout); } if (timedout) { jobstatus = jobstatus || {Status: 'Unknown', Message :''}; jobstatus.Message = 'Timeout'; } return jobstatus; } } module.exports = wrap;