Browse Source

update 添加 光电扰度转路安to江苏省农村公路桥梁监测 的协议处理部分

pull/2/head
lucas 2 months ago
parent
commit
becd732f62
  1. 84
      adaptors/光电扰度转路安to江苏省农村公路桥梁监测.go
  2. 7
      models/GDND2LA.go
  3. 16
      testUnit/江苏智感测试_test.go

84
adaptors/光电扰度转路安to江苏省农村公路桥梁监测.go

@ -0,0 +1,84 @@
package adaptors
import (
"bytes"
"encoding/binary"
"encoding/json"
"fmt"
"goInOut/models"
"goInOut/utils"
"log"
"time"
)
// Adaptor_GDND2LA_JSNCGLQL 光电挠度2路安数据 转换 江苏农村公路桥梁监测系统
type Adaptor_GDND2LA_JSNCGLQL struct {
IdMap map[string]string
BridgeCode string
}
func (the Adaptor_GDND2LA_JSNCGLQL) Transform(inTopic, rawMsg string) []NeedPush {
gdnd := models.GDND2luAn{}
json.Unmarshal([]byte(rawMsg), &gdnd)
return the.RawToJSNCGLQL(gdnd)
}
func (the Adaptor_GDND2LA_JSNCGLQL) RawToJSNCGLQL(raw models.GDND2luAn) (result []NeedPush) {
Atime, err := time.Parse("2006-01-02 15:04:05", raw.Time)
if err != nil {
log.Printf("光电扰度 设备[%s] 数据时间 %s 解析错误", raw.Id, raw.Time)
return
}
sensorDataList := raw.Value[raw.Id]
//获取对方系统 id
sensorCode := the.getSensorCode(raw.Id)
if sensorCode == "" {
log.Printf("光电扰度 设备[%s] 无匹配的 江苏农村公路桥梁监测系统 测点id,请检查配置文件", raw.Id)
return
}
topic := fmt.Sprintf("data/%s/%s", the.BridgeCode, sensorCode)
var transBytes []byte
//添加时间段
transBytes = append(transBytes, the.getTimeBytes(Atime)...)
for _, sensorData := range sensorDataList {
//添加数据值段
bs := utils.Float32ToBytes(sensorData)
transBytes = append(transBytes, bs...)
}
result = append(result, NeedPush{
Topic: topic,
Payload: transBytes,
})
return result
}
func (the Adaptor_GDND2LA_JSNCGLQL) getSensorCode(rawSensorName string) string {
v, isValid := the.IdMap[rawSensorName]
if !isValid {
v = ""
}
return v
}
func (the Adaptor_GDND2LA_JSNCGLQL) getTimeBytes(sensorTime time.Time) []byte {
year := uint16(sensorTime.Year())
month := int8(sensorTime.Month())
day := int8(sensorTime.Day())
hour := int8(sensorTime.Hour())
minute := int8(sensorTime.Minute())
second := int8(sensorTime.Second())
bytesBuffer := bytes.NewBuffer([]byte{})
binary.Write(bytesBuffer, binary.BigEndian, year)
binary.Write(bytesBuffer, binary.BigEndian, month)
binary.Write(bytesBuffer, binary.BigEndian, day)
binary.Write(bytesBuffer, binary.BigEndian, hour)
binary.Write(bytesBuffer, binary.BigEndian, minute)
binary.Write(bytesBuffer, binary.BigEndian, second)
return bytesBuffer.Bytes()
}

7
models/GDND2LA.go

@ -0,0 +1,7 @@
package models
type GDND2luAn struct {
Id string `json:"id"`
Time string `json:"time"`
Value map[string][]float32 `json:"value"`
}

16
testUnit/江苏智感测试_test.go

@ -64,3 +64,19 @@ func Test_江苏智感_加速度(t *testing.T) {
bytes := adp.Transform(matchTopic, Msg)
log.Println(bytes)
}
func Test_江苏智感_光电挠度(t *testing.T) {
Msg := `
{"id":"DY-DIS-ZG03-01","time":"2024-11-14 08:55:10","value":{"DY-DIS-ZG03-01":[-0.1338,-0.1458,-0.1549,-0.1566,-0.1473,-0.1338,-0.1338,-0.1338,-0.1338,-0.1338,-0.1338,-0.1338,-0.1338,-0.1338,-0.1338,-0.1338,-0.1338,-0.1338,-0.1338,-0.1338]}}
`
adp := adaptors.Adaptor_GDND2LA_JSNCGLQL{
IdMap: map[string]string{
"DY-DIS-ZG03-01": "LHTDQ-DIS-C08-001-01",
},
BridgeCode: "G2320281L0012",
}
matchTopic := "upload/GDND"
bytes := adp.Transform(matchTopic, Msg)
log.Println(bytes)
}

Loading…
Cancel
Save