"use strict";require("source-map-support").install();var _=require("underscore"),de=de||{biancoroyal:{modbus:{core:{server:{}}}}};de.biancoroyal.modbus.core.server.internalDebug=de.biancoroyal.modbus.core.server.internalDebug||require("debug")("contribModbus:core:server"),de.biancoroyal.modbus.core.server.bufferFactor=8,de.biancoroyal.modbus.core.server.memoryTypes=["holding","coils","input","discrete"],de.biancoroyal.modbus.core.server.memoryUint16Types=["holding","input"],de.biancoroyal.modbus.core.server.memoryUint8Types=["coils","discrete"],de.biancoroyal.modbus.core.server.getLogFunction=function(e){return e.internalDebugLog||de.biancoroyal.modbus.core.server.internalDebug},de.biancoroyal.modbus.core.server.isValidMemoryMessage=function(e){return!1===_.isUndefined(e.payload)&&e.payload.register&&Number.isInteger(e.payload.address)&&0<=e.payload.address&&e.payload.address<=65535},de.biancoroyal.modbus.core.server.isValidMessage=function(e){return!1===_.isUndefined(e)&&!1===_.isUndefined(e.payload)},de.biancoroyal.modbus.core.server.copyToModbusFlexBuffer=function(e,r){switch(r.payload.register){case"holding":r.bufferData.copy(e.registers,r.bufferSplitAddress);break;case"coils":r.bufferData.copy(e.coils,r.bufferAddress);break;case"input":r.bufferData.copy(e.registers,r.bufferAddress);break;case"discrete":r.bufferData.copy(e.coils,r.bufferSplitAddress);break;default:return!1}return!0},de.biancoroyal.modbus.core.server.writeToModbusFlexBuffer=function(e,r){switch(r.payload.register){case"holding":e.registers.writeUInt16BE(r.bufferPayload,r.bufferSplitAddress);break;case"coils":e.coils.writeUInt8(r.bufferPayload,r.bufferAddress);break;case"input":e.registers.writeUInt16BE(r.bufferPayload,r.bufferAddress);break;case"discrete":e.coils.writeUInt8(r.bufferPayload,r.bufferSplitAddress);break;default:return!1}return!0},de.biancoroyal.modbus.core.server.writeModbusFlexServerMemory=function(e,r){var o=de.biancoroyal.modbus.core.server;return r.bufferSplitAddress=(parseInt(r.payload.address)+parseInt(e.splitAddress))*o.bufferFactor,r.bufferAddress=parseInt(r.payload.address)*o.bufferFactor,o.convertInputForBufferWrite(r)?o.copyToModbusFlexBuffer(e,r):o.writeToModbusFlexBuffer(e,r)},de.biancoroyal.modbus.core.server.convertInputForBufferWrite=function(e){var r=!1;return e.payload.value.length?(e.bufferPayload=new Uint8Array(e.payload.value),e.bufferData=Buffer.alloc(e.bufferPayload.buffer.byteLength,e.bufferPayload),e.wasMultipleWrite=r=!0):(e.bufferPayload=parseInt(e.payload.value),e.wasMultipleWrite=!1),r},de.biancoroyal.modbus.core.server.copyToModbusBuffer=function(e,r){switch(r.payload.register){case"holding":r.bufferData.copy(e.modbusServer.holding,r.bufferAddress);break;case"coils":r.bufferData.copy(e.modbusServer.coils,r.bufferAddress);break;case"input":r.bufferData.copy(e.modbusServer.input,r.bufferAddress);break;case"discrete":r.bufferData.copy(e.modbusServer.discrete,r.bufferAddress);break;default:return!1}return!0},de.biancoroyal.modbus.core.server.writeToModbusBuffer=function(e,r){switch(r.payload.register){case"holding":e.modbusServer.holding.writeUInt16BE(r.bufferPayload,r.bufferAddress);break;case"coils":e.modbusServer.coils.writeUInt8(r.bufferPayload,r.bufferAddress);break;case"input":e.modbusServer.input.writeUInt16BE(r.bufferPayload,r.bufferAddress);break;case"discrete":e.modbusServer.discrete.writeUInt8(r.bufferPayload,r.bufferAddress);break;default:return!1}return!0},de.biancoroyal.modbus.core.server.writeModbusServerMemory=function(e,r){var o=de.biancoroyal.modbus.core.server;return r.bufferAddress=parseInt(r.payload.address)*o.bufferFactor,o.convertInputForBufferWrite(r)?o.copyToModbusBuffer(e,r):o.writeToModbusBuffer(e,r)},de.biancoroyal.modbus.core.server.writeToServerMemory=function(r,o){var e=de.biancoroyal.modbus.core.server;o.payload.register=o.payload.register.toLowerCase();try{e.memoryTypes.includes(o.payload.register)&&e.writeModbusServerMemory(r,o)}catch(e){o.error=e,r.error(e)}},de.biancoroyal.modbus.core.server.writeToFlexServerMemory=function(r,o){var e=de.biancoroyal.modbus.core.server;o.payload.register=o.payload.register.toLowerCase();try{e.memoryTypes.includes(o.payload.register)&&e.writeModbusFlexServerMemory(r,o)}catch(e){o.error=e,r.error(e)}},module.exports=de.biancoroyal.modbus.core.server;
//# sourceMappingURL=../maps/core/modbus-server-core.js.map
