"use strict";module.exports=function(t){require("source-map-support").install();var o=require("./modbus-basics"),n=require("./core/modbus-core"),i=require("debug")("contribModbus:write");t.nodes.registerType("modbus-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.unitid=e.unitid,this.dataType=e.dataType,this.adr=Number(e.adr),this.quantity=e.quantity,this.emptyMsgOnFail=e.emptyMsgOnFail,this.keepMsgProperties=e.keepMsgProperties,this.internalDebugLog=i,this.verboseLogging=t.settings.verbose,this.delayOnStart=e.delayOnStart,this.startDelayTime=parseInt(e.startDelayTime)||10;var s=this,a=(s.bufferMessageList=new Map,s.INPUT_TIMEOUT_MILLISECONDS=1e3,s.delayOccured=!1,s.inputDelayTimer=null,o.setNodeStatusTo("waiting",s),t.nodes.getNode(e.server));function r(e){t.settings.verbose&&s.showWarnings&&s.warn("Writer -> "+e)}a&&(a.registerForModbus(s),o.initModbusClientEvents(s,a),s.onModbusWriteDone=function(e,t){s.showStatusActivities&&o.setNodeStatusTo("write done",s),s.send(n.buildMessage(s.bufferMessageList,t.payload,e,t)),s.emit("modbusWriteNodeDone")},s.errorProtocolMsg=function(e,t){s.showErrors&&o.logMsgError(s,e,t)},s.onModbusWriteError=function(e,t){s.internalDebugLog(e.message);var i=n.getOriginalMessage(s.bufferMessageList,t);s.errorProtocolMsg(e,i),o.sendEmptyMsgOnFail(s,e,t),o.setModbusError(s,a,e,i),s.emit("modbusWriteNodeError")},s.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},s.buildNewMessageObject=function(e,t){var i=n.getObjectId();return{topic:t.topic||e.id,messageId:i,payload:{value:Object.prototype.hasOwnProperty.call(t.payload,"value")?t.payload.value:t.payload,unitid:e.unitid,fc:n.functionCodeModbusWrite(e.dataType),address:e.adr,quantity:e.quantity,messageId:i}}},s.isReadyForInput=function(){return a.client&&a.isActive()&&s.delayOccured},s.isNotReadyForInput=function(){return!s.isReadyForInput()},s.resetInputDelayTimer=function(){s.inputDelayTimer&&(r("reset input delay timer node "+s.id),clearTimeout(s.inputDelayTimer)),s.inputDelayTimer=null,s.delayOccured=!1},s.initializeInputDelayTimer=function(){s.resetInputDelayTimer(),s.delayOnStart?(r("initialize input delay timer node "+s.id),s.inputDelayTimer=setTimeout(function(){s.delayOccured=!0},s.INPUT_TIMEOUT_MILLISECONDS*s.startDelayTime)):s.delayOccured=!0},s.initializeInputDelayTimer(),s.on("input",function(t){if(o.invalidPayloadIn(t))r("Invalid message on input.");else if(s.isNotReadyForInput())r("Inject while node is not ready for input.");else{if(a.isInactive())return r("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=s.setMsgPayloadFromHTTPRequests(t),i=s.buildNewMessageObject(s,e);s.bufferMessageList.set(i.messageId,o.buildNewMessage(s.keepMsgProperties,e,i)),a.emit("writeModbus",i,s.onModbusWriteDone,s.onModbusWriteError),s.showStatusActivities&&o.setNodeStatusTo(a.actualServiceState,s)}catch(e){s.errorProtocolMsg(e,t),o.sendEmptyMsgOnFail(s,e,t)}}}),s.on("close",function(e){o.setNodeStatusTo("closed",s),s.bufferMessageList.clear(),a.deregisterForModbus(s.id,e)}),s.showStatusActivities||o.setNodeDefaultStatus(s))})};
//# sourceMappingURL=maps/modbus-write.js.map
