From 0718b07276198334437f52aa5c6c07cca2002225 Mon Sep 17 00:00:00 2001
From: zhangzengfei <zhangzengfei@smartai.com>
Date: 星期四, 10 八月 2023 18:17:32 +0800
Subject: [PATCH] 完善数据上报程序, 添加测试

---
 collector/collector.go |   32 +++++++++++++-------------------
 1 files changed, 13 insertions(+), 19 deletions(-)

diff --git a/collector/collector.go b/collector/collector.go
index 6a3f3f9..12e8f2e 100644
--- a/collector/collector.go
+++ b/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,
 					})
 				}
 			}

--
Gitblit v1.8.0