Skip to content

Commit

Permalink
bugfix: fix bug for nil pointer of twin and context thread block
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan <[email protected]>
  • Loading branch information
RyanZhaoXB committed Mar 13, 2023
1 parent d1d2e88 commit bc33a9c
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 16 deletions.
15 changes: 11 additions & 4 deletions mappers/modbus-dmi/device/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,21 @@ func initTwin(ctx context.Context, dev *modbus.ModbusDev) {
}
setVisitor(&visitorConfig, &dev.Instance.Twins[i], dev.ModbusClient)

twinData := TwinData{Client: dev.ModbusClient,
twinData := TwinData{
Client: dev.ModbusClient,
Name: dev.Instance.Twins[i].PropertyName,
Type: dev.Instance.Twins[i].Desired.Metadatas.Type,
VisitorConfig: &visitorConfig,
Topic: fmt.Sprintf(common.TopicTwinUpdate, dev.Instance.ID),
DeviceName: dev.Instance.Name}
DeviceID: dev.Instance.ID,
DeviceName: dev.Instance.Name,
}
collectCycle := time.Duration(dev.Instance.Twins[i].PVisitor.CollectCycle)
// If the collect cycle is not set, set it to 1 second.
if collectCycle == 0 {
collectCycle = 1 * time.Second
}
klog.V(2).InfoS("Start to collect", "twin", twinData.Name, "cycle", collectCycle)
ticker := time.NewTicker(collectCycle)
go func() {
for {
Expand Down Expand Up @@ -171,9 +175,10 @@ func (d *DevPanel) start(ctx context.Context, dev *modbus.ModbusDev) {
dev.ModbusClient = client

go initTwin(ctx, dev)
klog.Infof("All twins has been set, %+v", dev.Instance)

<-ctx.Done()
d.wg.Done()
klog.InfoS("sync wait group donw", "deviceID", dev.Instance.ID, "device name", dev.Instance.Name)
}

// DevInit initialize the device data.
Expand Down Expand Up @@ -215,13 +220,15 @@ func NewDevPanel() *DevPanel {
// DevStart start all devices.
func (d *DevPanel) DevStart() {
for id, dev := range d.devices {
klog.V(4).Info("Dev: ", id, dev)
klog.Info("Dev: ", id, dev)
ctx, cancel := context.WithCancel(context.Background())
d.deviceMuxs[id] = cancel
d.wg.Add(1)
go d.start(ctx, dev)
}
klog.Infoln("Wait all sync wait group")
d.wg.Wait()
klog.Infoln("All sync wait group done")
}

func (d *DevPanel) UpdateDevTwins(deviceID string, twins []common.Twin) error {
Expand Down
6 changes: 4 additions & 2 deletions mappers/modbus-dmi/device/twindata.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"k8s.io/klog/v2"

dmiapi "github.com/kubeedge/kubeedge/pkg/apis/dmi/v1alpha1"

"github.com/kubeedge/mappers-go/pkg/common"
"github.com/kubeedge/mappers-go/pkg/driver/modbus"
"github.com/kubeedge/mappers-go/pkg/util/grpcclient"
Expand All @@ -36,6 +37,7 @@ import (

// TwinData is the timer structure for getting twin/data.
type TwinData struct {
DeviceID string
DeviceName string
Client *modbus.ModbusClient
Name string
Expand Down Expand Up @@ -119,7 +121,7 @@ func TransferData(isRegisterSwap bool, isSwap bool,
data := string(value)
return data, nil
default:
return "", errors.New("Data type is not support")
return "", errors.New("data type is not support")
}
}

Expand Down Expand Up @@ -168,7 +170,7 @@ func (td *TwinData) Run() {
twins := parse.ConvMsgTwinToGrpc(msg.Twin)

var rdsr = &dmiapi.ReportDeviceStatusRequest{
DeviceName: td.DeviceName,
DeviceName: td.DeviceID,
ReportedDevice: &dmiapi.DeviceStatus{
Twins: twins,
State: "OK",
Expand Down
3 changes: 2 additions & 1 deletion pkg/driver/modbus/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ import (

"k8s.io/klog/v2"

"github.com/kubeedge/mappers-go/pkg/common"
"github.com/sailorvii/modbus"

"github.com/kubeedge/mappers-go/pkg/common"
)

// ModbusTCP is the configurations of modbus TCP.
Expand Down
8 changes: 6 additions & 2 deletions pkg/grpcserver/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

dmiapi "github.com/kubeedge/kubeedge/pkg/apis/dmi/v1alpha1"

"github.com/kubeedge/mappers-go/pkg/common"
"github.com/kubeedge/mappers-go/pkg/driver/modbus"
"github.com/kubeedge/mappers-go/pkg/util/parse"
Expand Down Expand Up @@ -63,7 +64,7 @@ func (s *Server) RemoveDevice(ctx context.Context, request *dmiapi.RemoveDeviceR
}

func (s *Server) UpdateDevice(ctx context.Context, request *dmiapi.UpdateDeviceRequest) (*dmiapi.UpdateDeviceResponse, error) {
klog.V(2).Info("UpdateDevice")
klog.Info("UpdateDevice")
device := request.GetDevice()
if device == nil {
return nil, errors.New("device is nil")
Expand All @@ -78,14 +79,15 @@ func (s *Server) UpdateDevice(ctx context.Context, request *dmiapi.UpdateDeviceR
return nil, fmt.Errorf("parse device %s protocol failed, err: %s", device.Name, err)
}

klog.Infof("model: %+v", model)
klog.Infof("UpdateDevice model: %+v", model)
deviceInstance, err := parse.ParseDeviceFromGrpc(device, &model)
if err != nil {
return nil, fmt.Errorf("parse device %s instance failed, err: %s", device.Name, err)
}
deviceInstance.PProtocol = protocol

s.devPanel.UpdateDev(&model, deviceInstance, &protocol)
klog.Infof("UpdateDevice success, device: %+v", deviceInstance)

return &dmiapi.UpdateDeviceResponse{}, nil
}
Expand All @@ -107,6 +109,7 @@ func (s *Server) CreateDeviceModel(ctx context.Context, request *dmiapi.CreateDe
}

func (s *Server) UpdateDeviceModel(ctx context.Context, request *dmiapi.UpdateDeviceModelRequest) (*dmiapi.UpdateDeviceModelResponse, error) {
klog.Info("UpdateDeviceModel")
deviceModel := request.GetModel()
if deviceModel == nil {
return nil, errors.New("deviceModel is nil")
Expand All @@ -118,6 +121,7 @@ func (s *Server) UpdateDeviceModel(ctx context.Context, request *dmiapi.UpdateDe
model := parse.ParseDeviceModelFromGrpc(deviceModel)

s.devPanel.UpdateModel(&model)
klog.Infof("UpdateDeviceModel model: %+v", model)

return &dmiapi.UpdateDeviceModelResponse{}, nil
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/util/parse/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/kubeedge/kubeedge/cloud/pkg/devicecontroller/constants"
dmiapi "github.com/kubeedge/kubeedge/pkg/apis/dmi/v1alpha1"

"github.com/kubeedge/mappers-go/pkg/common"
)

Expand Down Expand Up @@ -245,6 +246,7 @@ func ParseDeviceModelFromGrpc(model *dmiapi.DeviceModel) common.DeviceModel {
Name: property.GetName(),
Description: property.GetDescription(),
}
klog.V(2).Infof("Parse device model property from grpc, name %s, type %+v", property.GetName(), property.Type)
if property.Type.GetString_() != nil {
p.DataType = "string"
p.AccessMode = property.Type.String_.GetAccessMode()
Expand Down
3 changes: 2 additions & 1 deletion pkg/util/parse/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ func ParseByUsingRegister(cfg *config.Config,
if err != nil {
return err
}
klog.Infof("RegisterMapper success, deviceList %+v, deviceModelList %+v", deviceList, deviceModelList)

if len(deviceList) == 0 || len(deviceModelList) == 0 {
return ErrEmptyData
Expand All @@ -162,7 +163,7 @@ func ParseByUsingRegister(cfg *config.Config,
instance.PProtocol = protocol
devices[instance.ID] = new(common.DeviceInstance)
devices[instance.ID] = instance
klog.V(4).Info("Instance: ", instance.ID)
klog.Info("Instance: ", instance.ID)
dms[instance.Model] = modelMap[instance.Model]
protocols[instance.ProtocolName] = protocol
}
Expand Down
7 changes: 1 addition & 6 deletions pkg/util/parse/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/kubeedge/kubeedge/cloud/pkg/devicecontroller/constants"
"github.com/kubeedge/kubeedge/pkg/apis/devices/v1alpha2"
dmiapi "github.com/kubeedge/kubeedge/pkg/apis/dmi/v1alpha1"

"github.com/kubeedge/mappers-go/pkg/common"
)

Expand Down Expand Up @@ -336,12 +337,6 @@ func ConvMsgTwinToGrpc(msgTwin map[string]*common.MsgTwin) []*dmiapi.Twin {
for name, twin := range msgTwin {
twinData := &dmiapi.Twin{
PropertyName: name,
Desired: &dmiapi.TwinProperty{
Value: *twin.Expected.Value,
Metadata: map[string]string{
"type": twin.Metadata.Type,
"timestamp": twin.Expected.Metadata.Timestamp,
}},
Reported: &dmiapi.TwinProperty{
Value: *twin.Actual.Value,
Metadata: map[string]string{
Expand Down

0 comments on commit bc33a9c

Please sign in to comment.