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