zhangzengfei
2023-08-10 0718b07276198334437f52aa5c6c07cca2002225
collector/collector.go
@@ -11,7 +11,7 @@
   plc4go "github.com/apache/plc4x/plc4go/pkg/api"
)
var mapTask sync.Map
var tasksStore sync.Map
type collectorProc struct {
   device *msg.PLCDevice
@@ -19,22 +19,7 @@
}
func InitTask() {
   //device := msg.PLCDevice{
   //   DeviceID:   "0",
   //   DeviceName: "test",
   //   DeviceIP:   "192.168.1.188",
   //   Brand:      "sim",
   //   Method:     "modbus",
   //   PortName:   "",
   //   Frequency:  1,
   //   Details: []*msg.PLCAddress{&msg.PLCAddress{
   //      StartAddress: 17021,
   //      Length:       1,
   //      Type:         "int",
   //      FieldName:    "count",
   //   }},
   //}
   logger.Debug("init task")
   devices, err := getDeviceList()
   if err != nil {
      return
@@ -46,7 +31,16 @@
         continue
      }
      logger.Debug("add collector task,device %s", dev.DeviceName)
      addTask(&devices[idx])
   }
}
func stopTask(device *msg.PLCDevice) {
   if task, ok := tasksStore.Load(device.DeviceID); ok {
      // 存在的任务, 先停止掉, 然后重新开启一个
      task.(collectorProc).cancel()
      tasksStore.Delete(device.DeviceID)
   }
}
@@ -57,7 +51,7 @@
      cancel: cancel,
   }
   mapTask.Store(device.DeviceID, &proc)
   tasksStore.Store(device.DeviceID, &proc)
   go connectingDevice(ctx, device)
}
@@ -124,7 +118,7 @@
                  Length:       addr.Length,
                  Type:         addr.Type,
                  FieldName:    addr.FieldName,
                  Data:         result,
                  RawData:      result,
               })
            }
         }