From e3f00256943a58c81974b75910b32e26900dac71 Mon Sep 17 00:00:00 2001 From: bruce Date: Tue, 24 Oct 2023 11:14:33 +0800 Subject: [PATCH] feat: Accept DISCRETES_INPUT and DISCRETE_INPUTS as primary table Since the DISCRETES_INPUT looks like a fairly obvious typo, the best way is to accept both DISCRETE_INPUTS and DISCRETES_INPUT Signed-off-by: bruce --- internal/driver/constant.go | 2 ++ internal/driver/deviceclient_test.go | 36 ++++++++++++++++++++-------- internal/driver/modbusclient.go | 4 ++-- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/internal/driver/constant.go b/internal/driver/constant.go index 295ce7c3..7f1a957b 100644 --- a/internal/driver/constant.go +++ b/internal/driver/constant.go @@ -25,6 +25,7 @@ const ( FLOAT64 = "FLOAT64" DISCRETES_INPUT = "DISCRETES_INPUT" + DISCRETE_INPUTS = "DISCRETE_INPUTS" // since DISCRETES_INPUT looks like a typo and it come from Modbus spec, so we also support DISCRETE_INPUTS as primary table COILS = "COILS" INPUT_REGISTERS = "INPUT_REGISTERS" HOLDING_REGISTERS = "HOLDING_REGISTERS" @@ -43,6 +44,7 @@ const ( var PrimaryTableBitCountMap = map[string]uint16{ DISCRETES_INPUT: 1, + DISCRETE_INPUTS: 1, COILS: 1, INPUT_REGISTERS: 16, HOLDING_REGISTERS: 16, diff --git a/internal/driver/deviceclient_test.go b/internal/driver/deviceclient_test.go index 00bef75f..8a2c09ff 100644 --- a/internal/driver/deviceclient_test.go +++ b/internal/driver/deviceclient_test.go @@ -209,17 +209,33 @@ func TestTransformDataBytesToResult_BOOL(t *testing.T) { STARTING_ADDRESS: 10, }, } - commandInfo, err := createCommandInfo(&req) - require.NoError(t, err) - dataBytes := []byte{1} // => 00000001 - expected := true - commandValue, err := TransformDataBytesToResult(&req, dataBytes, commandInfo) - require.NoError(t, err) - result, err := commandValue.BoolValue() - require.NoError(t, err) - - assert.Equal(t, expected, result) + tests := []struct { + name string + primaryTable string + data []byte + expected bool + }{ + {"transform true value from DISCRETES_INPUT", DISCRETES_INPUT, []byte{1}, true}, + {"transform false value from DISCRETES_INPUT", DISCRETES_INPUT, []byte{0}, false}, + {"transform true value from DISCRETE_INPUTS", DISCRETE_INPUTS, []byte{1}, true}, + {"transform false value from DISCRETE_INPUTS", DISCRETE_INPUTS, []byte{0}, false}, + {"transform true value from COILS", COILS, []byte{1}, true}, + {"transform false value from COILS", COILS, []byte{0}, false}, + } + for _, testCase := range tests { + t.Run(testCase.name, func(t *testing.T) { + req.Attributes[PRIMARY_TABLE] = testCase.primaryTable + commandInfo, err := createCommandInfo(&req) + require.NoError(t, err) + commandValue, err := TransformDataBytesToResult(&req, testCase.data, commandInfo) + require.NoError(t, err) + result, err := commandValue.BoolValue() + require.NoError(t, err) + + assert.Equal(t, testCase.expected, result) + }) + } } func TestTransformDataBytesToResult_RawType_INT16_ValueType_FLOAT32(t *testing.T) { diff --git a/internal/driver/modbusclient.go b/internal/driver/modbusclient.go index 837429b5..a76853af 100644 --- a/internal/driver/modbusclient.go +++ b/internal/driver/modbusclient.go @@ -64,7 +64,7 @@ func (c *ModbusClient) GetValue(commandInfo interface{}) ([]byte, error) { var err error switch modbusCommandInfo.PrimaryTable { - case DISCRETES_INPUT: + case DISCRETES_INPUT, DISCRETE_INPUTS: response, err = c.client.ReadDiscreteInputs(modbusCommandInfo.StartingAddress, modbusCommandInfo.Length) case COILS: response, err = c.client.ReadCoils(modbusCommandInfo.StartingAddress, modbusCommandInfo.Length) @@ -94,7 +94,7 @@ func (c *ModbusClient) SetValue(commandInfo interface{}, value []byte) error { var err error switch modbusCommandInfo.PrimaryTable { - case DISCRETES_INPUT: + case DISCRETES_INPUT, DISCRETE_INPUTS: err = fmt.Errorf("Error: DISCRETES_INPUT is Read-Only..!!") case COILS: