@@ -212,21 +212,23 @@ export default class ModbusClient implements ProtocolClient {
212212 return endianness ;
213213 }
214214
215- // This generates a form with url content based on the uri scheme
215+ // This generates a form used internally with url content based on the uri scheme
216216 // Ideally, more code should be refactored to use uri only
217- private generateFormFromURLPath ( input : ModbusForm ) {
217+ private addFormElementsFromURLPath ( input : ModbusForm ) : ModbusForm {
218+ const returnForm :ModbusForm = { ...input }
218219 const { pathname, searchParams : query } = new URL ( input . href ) ;
219220 const pathComp = pathname . split ( "/" ) ;
220221 if ( pathComp . length < 3 || pathComp [ 1 ] === "" || pathComp [ 2 ] === "" ) {
221222 throw new Error ( "Malformed href: unitID and address must be defined" ) ;
222223 }
223- input [ "modv:unitID" ] = parseInt ( pathComp [ 1 ] , 10 ) ;
224- input [ "modv:address" ] = parseInt ( pathComp [ 2 ] , 10 ) ;
224+ returnForm [ "modv:unitID" ] = parseInt ( pathComp [ 1 ] , 10 ) ;
225+ returnForm [ "modv:address" ] = parseInt ( pathComp [ 2 ] , 10 ) ;
225226
226227 const queryQuantity = query . get ( "quantity" ) ;
227228 if ( queryQuantity != null ) {
228- input [ "modv:quantity" ] = parseInt ( queryQuantity , 10 ) ;
229+ returnForm [ "modv:quantity" ] = parseInt ( queryQuantity , 10 ) ;
229230 }
231+ return returnForm ;
230232 }
231233
232234 private validateBufferLength ( form : ModbusFormWithDefaults , buffer : Buffer ) {
@@ -243,33 +245,33 @@ export default class ModbusClient implements ProtocolClient {
243245 }
244246 }
245247
246- private validateAndFillDefaultForm ( form : ModbusForm , contentLength = 0 ) : ModbusFormWithDefaults {
248+ private validateAndFillDefaultForm ( inputForm : ModbusForm , contentLength = 0 ) : ModbusFormWithDefaults {
247249 const mode = contentLength > 0 ? "w" : "r" ;
248250
249251 // Use URI values to generate form keys
250- this . generateFormFromURLPath ( form ) ;
252+ const filledForm : ModbusForm = this . addFormElementsFromURLPath ( inputForm ) ;
251253
252254 // take over latest content of form into a new result set
253- const result : ModbusForm = { ...form } ;
255+ const result : ModbusForm = { ...filledForm } ;
254256
255- if ( form [ "modv:function" ] == null && form [ "modv:entity" ] == null ) {
257+ if ( filledForm [ "modv:function" ] == null && filledForm [ "modv:entity" ] == null ) {
256258 throw new Error ( "Malformed form: modv:function or modv:entity must be defined" ) ;
257259 }
258260
259- if ( form [ "modv:function" ] != null ) {
261+ if ( filledForm [ "modv:function" ] != null ) {
260262 // Convert string function to enums if defined
261- if ( typeof form [ "modv:function" ] === "string" ) {
262- result [ "modv:function" ] = ModbusFunction [ form [ "modv:function" ] ] ;
263+ if ( typeof filledForm [ "modv:function" ] === "string" ) {
264+ result [ "modv:function" ] = ModbusFunction [ filledForm [ "modv:function" ] ] ;
263265 }
264266
265267 // Check if the function is a valid modbus function code
266- if ( ! Object . keys ( ModbusFunction ) . includes ( form [ "modv:function" ] . toString ( ) ) ) {
267- throw new Error ( "Undefined function number or name: " + form [ "modv:function" ] ) ;
268+ if ( ! Object . keys ( ModbusFunction ) . includes ( filledForm [ "modv:function" ] . toString ( ) ) ) {
269+ throw new Error ( "Undefined function number or name: " + filledForm [ "modv:function" ] ) ;
268270 }
269271 }
270272
271- if ( form [ "modv:entity" ] ) {
272- switch ( form [ "modv:entity" ] ) {
273+ if ( filledForm [ "modv:entity" ] ) {
274+ switch ( filledForm [ "modv:entity" ] ) {
273275 case "Coil" :
274276 result [ "modv:function" ] =
275277 mode === "r"
@@ -294,18 +296,18 @@ export default class ModbusClient implements ProtocolClient {
294296 result [ "modv:function" ] = ModbusFunction . readDiscreteInput ;
295297 break ;
296298 default :
297- throw new Error ( "Unknown modbus entity: " + form [ "modv:entity" ] ) ;
299+ throw new Error ( "Unknown modbus entity: " + filledForm [ "modv:entity" ] ) ;
298300 }
299301 } else {
300302 // 'modv:entity' undefined but modv:function defined
301303 result [ "modv:entity" ] = modbusFunctionToEntity ( result [ "modv:function" ] as ModbusFunction ) ;
302304 }
303305
304- if ( form [ "modv:address" ] === undefined || form [ "modv:address" ] === null ) {
306+ if ( filledForm [ "modv:address" ] === undefined || filledForm [ "modv:address" ] === null ) {
305307 throw new Error ( "Malformed form: address must be defined" ) ;
306308 }
307309
308- const hasQuantity = form [ "modv:quantity" ] != null ;
310+ const hasQuantity = filledForm [ "modv:quantity" ] != null ;
309311
310312 if ( ! hasQuantity && contentLength === 0 ) {
311313 result [ "modv:quantity" ] = 1 ;
0 commit comments