"use strict";module.exports=function(t){require("source-map-support").install();var n=require("./modbus-basics"),s=require("./core/modbus-core"),a=require("debug")("contribModbus:flex:write");t.nodes.registerType("modbus-flex-write",function(e){t.nodes.createNode(this,e),this.name=e.name,this.showStatusActivities=e.showStatusActivities,this.showErrors=e.showErrors,this.showWarnings=e.showWarnings,this.emptyMsgOnFail=e.emptyMsgOnFail,this.keepMsgProperties=e.keepMsgProperties,this.internalDebugLog=a,this.verboseLogging=t.settings.verbose,this.delayOnStart=e.delayOnStart,this.startDelayTime=parseInt(e.startDelayTime)||10;var i=this,r=(i.bufferMessageList=new Map,i.INPUT_TIMEOUT_MILLISECONDS=1e3,i.delayOccured=!1,i.inputDelayTimer=null,n.setNodeStatusTo("waiting",i),t.nodes.getNode(e.server));function o(e){t.settings.verbose&&i.showWarnings&&i.warn("Flex-Write -> "+e+" "+i.serverInfo)}r&&(r.registerForModbus(i),n.initModbusClientEvents(i,r),i.onModbusWriteDone=function(e,t){i.showStatusActivities&&n.setNodeStatusTo("writing done",i),i.send(s.buildMessage(i.bufferMessageList,t.payload,e,t)),i.emit("modbusFlexWriteNodeDone")},i.errorProtocolMsg=function(e,t){i.showErrors&&n.logMsgError(i,e,t)},i.onModbusWriteError=function(e,t){i.internalDebugLog(e.message);var a=s.getOriginalMessage(i.bufferMessageList,t);i.errorProtocolMsg(e,a),n.sendEmptyMsgOnFail(i,e,t),n.setModbusError(i,r,e,a),i.emit("modbusFlexWriteNodeError")},i.prepareMsg=function(e){return"string"==typeof e.payload&&(e.payload=JSON.parse(e.payload)),e.payload.fc=parseInt(e.payload.fc),e.payload.unitid=parseInt(e.payload.unitid),e.payload.address=parseInt(e.payload.address),e.payload.quantity=parseInt(e.payload.quantity),e},i.isValidModbusMsg=function(e){var t=!0;return Number.isInteger(e.payload.fc)&&(5===e.payload.fc||6===e.payload.fc||15===e.payload.fc||16===e.payload.fc)||(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.setMsgPayloadFromHTTPRequests=function(e){return Object.prototype.hasOwnProperty.call(e.payload,"value")&&"string"==typeof e.payload.value&&("true"===e.payload.value||"false"===e.payload.value?e.payload.value="true"===e.payload.value:-1<e.payload.value.indexOf(",")&&(e.payload.value=JSON.parse(e.payload.value))),e},i.buildNewMessageObject=function(e,t){var a=s.getObjectId();return{topic:t.topic||e.id,messageId:a,payload:{value:Object.prototype.hasOwnProperty.call(t.payload,"value")?t.payload.value:t.payload,unitid:t.payload.unitid,fc:t.payload.fc,address:t.payload.address,quantity:t.payload.quantity,messageId:a}}},i.isReadyForInput=function(){return r.client&&r.isActive()&&i.delayOccured},i.isNotReadyForInput=function(){return!i.isReadyForInput()},i.resetInputDelayTimer=function(){i.inputDelayTimer&&(o("reset input delay timer node "+i.id),clearTimeout(i.inputDelayTimer)),i.inputDelayTimer=null,i.delayOccured=!1},i.initializeInputDelayTimer=function(){i.resetInputDelayTimer(),i.delayOnStart?(o("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(n.invalidPayloadIn(t))o("Invalid message on input.");else if(i.isNotReadyForInput())o("Inject while node is not ready for input.");else{if(r.isInactive())return o("You sent an input to inactive client. Please use initial delay on start or send data more slowly."),!1;t=Object.assign({},t);try{var e,a,s=i.prepareMsg(t);i.isValidModbusMsg(s)&&(e=i.setMsgPayloadFromHTTPRequests(s),a=i.buildNewMessageObject(i,e),i.bufferMessageList.set(a.messageId,n.buildNewMessage(i.keepMsgProperties,e,a)),r.emit("writeModbus",a,i.onModbusWriteDone,i.onModbusWriteError))}catch(e){i.errorProtocolMsg(e,t),n.sendEmptyMsgOnFail(i,e,t)}i.showStatusActivities&&n.setNodeStatusTo(r.actualServiceState,i)}}),i.on("close",function(e){n.setNodeStatusTo("closed",i),i.bufferMessageList.clear(),r.deregisterForModbus(i.id,e)}),i.showStatusActivities||n.setNodeDefaultStatus(i))})};
//# sourceMappingURL=maps/modbus-flex-write.js.map
