module.exports=function(d){require("source-map-support").install();var l=require("./modbus-basics"),u=require("./core/modbus-core"),p=require("./core/modbus-io-core"),g=require("debug")("contribModbus:flex:getter");d.nodes.registerType("modbus-flex-getter",function(e){d.nodes.createNode(this,e),this.name=e.name,this.showStatusActivities=e.showStatusActivities,this.showErrors=e.showErrors,this.showWarnings=e.showWarnings,this.connection=null,this.useIOFile=e.useIOFile,this.ioFile=d.nodes.getNode(e.ioFile),this.useIOForPayload=e.useIOForPayload,this.logIOActivities=e.logIOActivities,this.emptyMsgOnFail=e.emptyMsgOnFail,this.keepMsgProperties=e.keepMsgProperties,this.internalDebugLog=g,this.verboseLogging=d.settings.verbose,this.delayOnStart=e.delayOnStart,this.enableDeformedMessages=e.enableDeformedMessages,this.startDelayTime=parseInt(e.startDelayTime)||10;var a,i,r=this,o=(r.bufferMessageList=new Map,r.INPUT_TIMEOUT_MILLISECONDS=1e3,r.delayOccured=!1,r.inputDelayTimer=null,l.setNodeStatusTo("waiting",r),d.nodes.getNode(e.server));function t(e){d.settings.verbose&&r.showWarnings&&r.warn("Flex-Getter -> "+e)}function n(){setImmediate(function(){0<a.length?s():i=!1})}function s(){if(0===a.length)i=!1;else{var t=a.shift(),t=Object.assign({},t);try{var e,s=r.prepareMsg(t);r.isValidModbusMsg(s)?(e=r.buildNewMessageObject(r,s),r.bufferMessageList.set(e.messageId,l.buildNewMessage(r.keepMsgProperties,s,e)),o.emit("readModbus",e,function(e,t){try{r.onModbusReadDone(e,t)}finally{n()}},function(e,t){try{r.onModbusReadError(e,t)}finally{n()}})):(r.errorProtocolMsg(new Error("Invalid Modbus message"),t),l.sendEmptyMsgOnFail(r,new Error("Invalid Modbus message"),t),n())}catch(e){try{r.onModbusReadError(e,t)}finally{n()}}}}o&&(o.registerForModbus(r),l.initModbusClientEvents(r,o),r.onModbusReadDone=function(e,t){u.getOriginalMessage(r.bufferMessageList,t),r.showStatusActivities&&l.setNodeStatusTo("reading done",r),r.send(p.buildMessageWithIO(r,e.data,e,t)),r.emit("modbusFlexGetterNodeDone")},r.errorProtocolMsg=function(e,t){r.showErrors&&l.logMsgError(r,e,t)},r.onModbusReadError=function(e,t){r.internalDebugLog(e.message);t=u.getOriginalMessage(r.bufferMessageList,t);r.errorProtocolMsg(e,t),l.sendEmptyMsgOnFail(r,e,t),l.setModbusError(r,o,e,t),r.emit("modbusFlexGetterNodeError")},r.prepareMsg=function(e){return"string"==typeof e.payload&&(e.payload=JSON.parse(e.payload)),e.payload.fc=parseInt(e.payload.fc)||3,e.payload.unitid=parseInt(e.payload.unitid),e.payload.address=parseInt(e.payload.address)||0,e.payload.quantity=parseInt(e.payload.quantity)||1,e},r.isValidModbusMsg=function(e){var t=!0;return Number.isInteger(e.payload.fc)&&1<=e.payload.fc&&e.payload.fc<=4||(r.error("FC Not Valid",e),t&=!1),!t||Number.isInteger(e.payload.address)&&0<=e.payload.address&&e.payload.address<=65535||(r.error("Address Not Valid",e),t&=!1),!t||Number.isInteger(e.payload.quantity)&&1<=e.payload.quantity&&e.payload.quantity<=65535||(r.error("Quantity Not Valid",e),t&=!1),t},r.showWarnings&&r.enableDeformedMessages&&r.warn("Deformed Message support is enabled"),r.buildNewMessageObject=function(e,t){var s=u.getObjectId();return{topic:t.topic||e.id,messageId:s,payload:{value:t.payload.value||t.value,unitid:t.payload.unitid,fc:t.payload.fc,address:t.payload.address,quantity:t.payload.quantity,emptyMsgOnFail:e.emptyMsgOnFail,keepMsgProperties:e.keepMsgProperties,enableDeformedMessages:e.enableDeformedMessages,messageId:s}}},r.isReadyForInput=function(){return o.client&&o.isActive()&&r.delayOccured},r.isNotReadyForInput=function(){return!r.isReadyForInput()},r.resetInputDelayTimer=function(){r.inputDelayTimer&&(t("reset input delay timer node "+r.id),clearTimeout(r.inputDelayTimer)),r.inputDelayTimer=null,r.delayOccured=!1},r.initializeInputDelayTimer=function(){r.resetInputDelayTimer(),r.delayOnStart?(t("initialize input delay timer node "+r.id),r.inputDelayTimer=setTimeout(function(){r.delayOccured=!0},r.INPUT_TIMEOUT_MILLISECONDS*r.startDelayTime)):r.delayOccured=!0},r.initializeInputDelayTimer(),i=!(a=[]),r.on("input",function(e){l.invalidPayloadIn(e)?t("Invalid message on input."):r.isNotReadyForInput()?t("Inject while node is not ready for input."):o.isInactive()?t("You sent an input to inactive client. Please use initial delay on start or send data more slowly."):(a.push(e),i||(i=!0,s()))}),r.on("close",function(e){r.resetInputDelayTimer(),l.setNodeStatusTo("closed",r),r.bufferMessageList.clear(),a.length=0,i=!1,o.deregisterForModbus(r.id,e),clearInterval(r.cleanTimer)}),r.showStatusActivities||l.setNodeDefaultStatus(r))})};
//# sourceMappingURL=maps/modbus-flex-getter.js.map
