From 8013d05145761a6228c22dae13d3306614dc7d47 Mon Sep 17 00:00:00 2001 From: Ryan Date: Mon, 13 Mar 2023 20:04:10 +0800 Subject: [PATCH] do not report if the twin data is not changed to prevent triggering traffic limiting Signed-off-by: Ryan --- mappers/modbus-dmi/device/device.go | 3 ++- mappers/modbus-dmi/device/twindata.go | 22 +++++++++++++++------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/mappers/modbus-dmi/device/device.go b/mappers/modbus-dmi/device/device.go index 4ae6cfdd..7b93e06b 100644 --- a/mappers/modbus-dmi/device/device.go +++ b/mappers/modbus-dmi/device/device.go @@ -308,7 +308,8 @@ func getTwinData(deviceID string, twin common.Twin, client *modbus.ModbusClient) VisitorConfig: &visitorConfig, Topic: fmt.Sprintf(common.TopicTwinUpdate, deviceID), } - return td.GetPayload() + payload, _, err := td.GetPayload() + return payload, err } func (d *DevPanel) GetDevice(deviceID string) (interface{}, error) { diff --git a/mappers/modbus-dmi/device/twindata.go b/mappers/modbus-dmi/device/twindata.go index 02c5d616..fa489429 100644 --- a/mappers/modbus-dmi/device/twindata.go +++ b/mappers/modbus-dmi/device/twindata.go @@ -44,6 +44,7 @@ type TwinData struct { Type string VisitorConfig *modbus.ModbusVisitorConfig Results []byte + LastValue string Topic string } @@ -125,41 +126,48 @@ func TransferData(isRegisterSwap bool, isSwap bool, } } -func (td *TwinData) GetPayload() ([]byte, error) { +func (td *TwinData) GetPayload() ([]byte, bool, error) { var err error td.Results, err = td.Client.Get(td.VisitorConfig.Register, td.VisitorConfig.Offset, uint16(td.VisitorConfig.Limit)) if err != nil { - return nil, fmt.Errorf("get register failed: %v", err) + return nil, false, fmt.Errorf("get register failed: %v", err) } // transfer data according to the dpl configuration sData, err := TransferData(td.VisitorConfig.IsRegisterSwap, td.VisitorConfig.IsSwap, td.Type, td.VisitorConfig.Scale, td.Results) if err != nil { - return nil, fmt.Errorf("transfer Data failed: %v", err) + return nil, false, fmt.Errorf("transfer Data failed: %v", err) } + + // do not report if the twin data is not changed to prevent triggering traffic limiting + changed := sData != td.LastValue + td.LastValue = sData // construct payload var payload []byte if strings.Contains(td.Topic, "$hw") { if payload, err = common.CreateMessageTwinUpdate(td.Name, td.Type, sData); err != nil { - return nil, fmt.Errorf("create message twin update failed: %v", err) + return nil, false, fmt.Errorf("create message twin update failed: %v", err) } } else { if payload, err = common.CreateMessageData(td.Name, td.Type, sData); err != nil { - return nil, fmt.Errorf("create message data failed: %v", err) + return nil, false, fmt.Errorf("create message data failed: %v", err) } } klog.V(2).Infof("Get the %s value as %s", td.Name, sData) - return payload, nil + return payload, changed, nil } // Run timer function. func (td *TwinData) Run() { - payload, err := td.GetPayload() + payload, changed, err := td.GetPayload() if err != nil { klog.Errorf("twindata %s get payload failed, err: %s", td.Name, err) return } + if !changed { + return + } var msg common.DeviceTwinUpdate if err = json.Unmarshal(payload, &msg); err != nil {