require("source-map-support").install();var de=de||{biancoroyal:{modbus:{queue:{core:{}}}}};de.biancoroyal.modbus.queue.core.internalDebug=de.biancoroyal.modbus.queue.core.internalDebug||require("debug")("contribModbus:queue:core"),de.biancoroyal.modbus.queue.core.core=de.biancoroyal.modbus.queue.core.core||require("./modbus-core"),de.biancoroyal.modbus.queue.core.initQueue=function(e){e.bufferCommandList.clear(),e.sendingAllowed.clear(),e.unitSendingAllowed=[],e.inflightByUnitId=new Map;for(var n=0;n<=255;n++)e.bufferCommandList.set(n,[]),e.sendingAllowed.set(n,!0)},de.biancoroyal.modbus.queue.core.checkQueuesAreEmpty=function(e){for(var n=!0,o=0;o<=255;o++)n&=0===e.bufferCommandList.get(o).length;return n},de.biancoroyal.modbus.queue.core.queueSerialUnlockCommand=function(e){this.internalDebug("queue serial unlock command node name: "+e.name+" id: "+e.id),e.serialSendingAllowed=!0},de.biancoroyal.modbus.queue.core.queueSerialLockCommand=function(e){this.internalDebug("queue serial lock command node name: "+e.name+" id: "+e.id),e.serialSendingAllowed=!1},de.biancoroyal.modbus.queue.core.sequentialDequeueCommand=function(d){return this.internalDebug("sequential de-queue command"),new Promise(function(e,n){var o=de.biancoroyal.modbus.queue.core;if(d.parallelUnitIdsAllowed)for(var u=0;u<256;u+=1)o.sendQueueDataToModbus(d,u);else{var i=d.unitSendingAllowed.shift();if(!o.isValidUnitId(i))return void n(new Error("UnitId "+i+" is not valid from dequeue of sending list"));d.queueLog(JSON.stringify({type:"sequential dequeue command",unitId:i,isValidUnitId:o.isValidUnitId(i),sendingAllowed:d.sendingAllowed.get(i),serialSendingAllowed:d.serialSendingAllowed})),o.isValidUnitId(i)&&d.sendingAllowed.get(i)?o.sendQueueDataToModbus(d,i):(d.warn("sequential dequeue command not possible for Unit "+i),n="sending is allowed for Unit ",d.sendingAllowed.get(i)?d.warn(n+i):d.warn("no "+n+i),n="valid Unit ",o.isValidUnitId(i)?d.warn(n+i):d.warn("no "+n+i),n=" serial sending allowed for Unit ",d.serialSendingAllowed?d.warn(d.name+n+i):d.warn(d.name+" no"+n+i))}e()})},de.biancoroyal.modbus.queue.core.sendQueueDataToModbus=function(o,u){var e=o.bufferCommandList.get(u).length;if(o.queueLog(JSON.stringify({type:"send queue data to Modbus",unitId:u,queueLength:e,sendingAllowed:o.sendingAllowed.get(u),serialSendingAllowed:o.serialSendingAllowed})),e){var i=o.bufferCommandList.get(u).shift();if(!i)throw new Error("Command On Send Not Valid");o.sendingAllowed.set(u,!1),o.inflightByUnitId||(o.inflightByUnitId=new Map),o.inflightByUnitId.set(u,i);i.callModbus(o,i.msg,function(e,n){try{i.cb(e,n)}finally{o.inflightByUnitId.delete(u)}},function(e,n){try{i.cberr(e,n)}finally{o.inflightByUnitId.delete(u)}})}},de.biancoroyal.modbus.queue.core.dequeueLogEntry=function(e,n,o){e.queueLog(JSON.stringify({state:n.value,message:"".concat(o," ").concat(e.clienttype),delay:e.commandDelay}))},de.biancoroyal.modbus.queue.core.dequeueCommand=function(n){var o=de.biancoroyal.modbus.queue.core,u=n.actualServiceState;-1===n.messageAllowedStates.indexOf(u.value)?o.dequeueLogEntry(n,u,"dequeue command disallowed state"):o.sequentialDequeueCommand(n).then(function(){o.dequeueLogEntry(n,u,"dequeue command done")}).catch(function(e){o.dequeueLogEntry(n,u,"dequeue command error "+e.message)})},de.biancoroyal.modbus.queue.core.getUnitIdToQueue=function(e,n){return parseInt(n.payload.unitid)||parseInt(e.unit_id)||0},de.biancoroyal.modbus.queue.core.isValidUnitId=function(e){return 0<=e&&e<=255},de.biancoroyal.modbus.queue.core.getQueueLengthByUnitId=function(e,n){if(this.isValidUnitId(n))return e.bufferCommandList.get(n).length;throw new Error("(0-255) Got A Wrong Unit-Id: "+n)},de.biancoroyal.modbus.queue.core.pushToQueueByUnitId=function(i,d,t,a,r){var l=de.biancoroyal.modbus.queue.core;return new Promise(function(e,n){try{var o,u=l.getUnitIdToQueue(i,t);l.isValidUnitId(u)?(i.queueLog(JSON.stringify({info:"will push to Queue by Unit-Id",message:t.payload,unitId:u})),o=l.getQueueLengthByUnitId(i,u),t.queueLengthByUnitId={unitId:u,queueLength:o},t.queueUnitId=u,i.parallelUnitIdsAllowed&&"serial"!==i.clienttype||i.unitSendingAllowed.push(u),i.bufferCommandList.get(u).push({callModbus:d,msg:t,cb:a,cberr:r}),i.queueLog(JSON.stringify({info:"pushed to Queue by Unit-Id",message:t.payload,unitId:u})),e()):n(new Error("UnitId "+u+" is not valid from msg or node"))}catch(e){n(e)}})},module.exports=de.biancoroyal.modbus.queue.core;
//# sourceMappingURL=../maps/core/modbus-queue-core.js.map
