et-go 20240919重建
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

205 lines
4.4 KiB

1 month ago
package main
import (
"bytes"
"dataSource"
"encoding/gob"
"encoding/json"
"fmt"
"gitea.anxinyun.cn/container/common_models"
"log"
master2 "master/app"
"testing"
)
func TestMaster(t *testing.T) {
log.Println("测试开始")
rawDataMsg := `{
"userId": "ce2d7eb2-e56e-422e-8bbe-95dfa18e32f8",
"thingId": "f5c6998b-91fa-42ae-8330-c2df41dcb88e",
"dimensionId": "ac06cda4-5975-4c58-9df3-e530ad20ad5d",
"dimCapId": "a8d3cb51-93d9-4ece-a414-59eec73f3be3",
"capId": "1ab8db7c-e725-4119-9c09-6555bb04ce5d",
"deviceId": "19f5e41a-6ec6-4101-b620-be0b415eaef9",
"scheduleId": "4a2a3d8b-ba33-40cc-89d8-47f6675a252c",
"taskId": "96417a21-93f1-41ce-83ec-48ba3c103bc6",
"jobId": 1,
"jobRepeatId": 1,
"triggerTime": "2024-02-28T15:09:22.000351473+08:00",
"realTime": "2024-02-28T15:09:22.001142356+08:00",
"finishTime": "2024-02-28T15:09:29.582668593+08:00",
"seq": 0,
"released": false,
"data": {
"type": 1,
"data": {
"windspeed": 9,
"humidity": 13.5,
"temp": 23.9,
"noise": 9,
"pm10": 9,
"pm25": 9,
"winddir": [1.1,2.2]
},
"result": {
"code": 5001,
"msg": "值超量程[-900,900]",
"detail": null,
"errTimes": 1,
"dropped": false
}
}
}`
// 初始化各数据源的数据通道
dataChannels := dataSource.InitChannels()
rawData := new(common_models.IotaData)
err := json.Unmarshal([]byte(rawDataMsg), rawData)
if err != nil {
log.Panicf("测试异常%s", err.Error())
}
gob.Register([]interface{}{})
// 向 RawDataChan 写数据
dataChannels.RawDataChan <- *rawData
master := new(master2.EtMaster)
master.ConnectNode()
master.DistributeData(dataChannels)
}
func TestJobEn(t *testing.T) {
// 注册空接口类型
gob.Register([]interface{}{})
// 创建一个包含空接口的切片
data := []interface{}{1, "hello", true}
// 编码数据
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err := enc.Encode(data)
if err != nil {
fmt.Println("编码错误:", err)
return
}
// 解码数据
dec := gob.NewDecoder(&buf)
var decodedData []interface{}
err = dec.Decode(&decodedData)
if err != nil {
fmt.Println("解码错误:", err)
return
}
fmt.Println(decodedData)
}
func TestJobEnMap(t *testing.T) {
// 注册空接口类型
//gob.Register([]interface{}{})
// 创建一个包含空接口的切片
data := map[string]interface{}{"a": "aa", "b": []float64{1.2, 2.2}}
// 编码数据
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err := enc.Encode(data)
if err != nil {
fmt.Println("编码错误:", err)
return
}
// 解码数据
dec := gob.NewDecoder(&buf)
var decodedData map[string]interface{}
err = dec.Decode(&decodedData)
if err != nil {
fmt.Println("解码错误:", err)
return
}
fmt.Println(decodedData)
}
func TestJobEnRawData(t *testing.T) {
// 创建一个包含空接口的切片
//data := common_models.ThemeData{
// Type: 0,
// ThemeData: map[string]interface{}{"a": 1, "b": []float64{1.1, 2.2}},
// Result: struct {
// Code int `json:"code"`
// Msg string `json:"msg"`
// Detail string `json:"detail"`
// ErrTimes int `json:"errTimes"`
// Dropped bool `json:"dropped"`
// }{},
//}
jsonStr := `{
"type": 1,
"data": {
"windspeed": 9,
"physicalvalue": [1.1,2.2]
},
"result": {
"code": 5001,
"msg": "值超量程[-900,900]",
"detail": null,
"errTimes": 1,
"dropped": false
}
}
`
data2 := &common_models.Data{}
err := json.Unmarshal([]byte(jsonStr), &data2)
s, e := json.Marshal(data2.Data)
if e != nil {
return
}
println(string(s))
if err != nil {
return
}
// 编码数据
gob.Register([]interface{}{}) // 注册空接口类型
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err = enc.Encode(data2)
if err != nil {
fmt.Println("编码错误:", err)
return
}
// 解码数据
gob.Register([]interface{}{})
dec := gob.NewDecoder(&buf)
var decodedData common_models.Data
err = dec.Decode(&decodedData)
if err != nil {
fmt.Println("解码错误:", err)
return
}
fmt.Println(decodedData)
}
func TestCH(t *testing.T) {
resultCH := make(chan bool, 1)
1 month ago
log.Printf("写入数据1")
resultCH <- true
log.Printf("写入数据2")
resultCH <- false
log.Printf("写入数据3")
resultCH <- true
}