Skip to content

Commit

Permalink
feat: Accept DISCRETES_INPUT and DISCRETE_INPUTS as primary table
Browse files Browse the repository at this point in the history
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 <weichou1229@gmail.com>
  • Loading branch information
weichou1229 committed Oct 24, 2023
1 parent 4cb387c commit e3f0025
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
2 changes: 2 additions & 0 deletions internal/driver/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -43,6 +44,7 @@ const (

var PrimaryTableBitCountMap = map[string]uint16{
DISCRETES_INPUT: 1,
DISCRETE_INPUTS: 1,
COILS: 1,
INPUT_REGISTERS: 16,
HOLDING_REGISTERS: 16,
Expand Down
36 changes: 26 additions & 10 deletions internal/driver/deviceclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions internal/driver/modbusclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit e3f0025

Please sign in to comment.