数据上报
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.

126 lines
2.6 KiB

package adaptors
import (
"encoding/json"
"goUpload/models"
"log"
"math"
"time"
)
// Adaptor_ZD_XTJC 振动DAAS数据 转换 湘潭健康监测平台
type Adaptor_ZD_XTJC struct {
IdMap map[string]int16
}
func (the Adaptor_ZD_XTJC) Transform(rawMsg string) [][]byte {
zd := models.ZD{}
var result [][]byte
json.Unmarshal([]byte(rawMsg), &zd)
result = append(result, the.ZDtoXTJC(zd))
return result
}
func (the Adaptor_ZD_XTJC) ZDtoXTJC(zd models.ZD) (result []byte) {
Atime := time.UnixMilli(zd.Ticks)
var transBytes []byte
var sensorDataList []float32 = zd.AccValues
//1.添加通用报文头段
commPayloadHeader := the.getPayloadHeader(int16(len(sensorDataList)))
transBytes = append(transBytes, commPayloadHeader...)
//2.添加时间段
transBytes = append(transBytes, the.getTimeBytes(Atime)...)
//3.添加对象码段
sensorCode := the.getSensorId(zd.Module)
if sensorCode != 0 {
transBytes = append(transBytes, the.getCodeBytes(sensorCode)...)
} else {
log.Printf("振动DAAS 设备[%s] 无匹配的湘潭监测id,请检查配置文件", zd.Module)
return
}
//4.添加数据值段
transBytes = append(transBytes, the.getDatasBytes(sensorDataList)...)
return transBytes
}
func (the Adaptor_ZD_XTJC) getSensorId(rawSensorName string) int16 {
v, isValid := the.IdMap[rawSensorName]
if !isValid {
v = 0
}
return v
}
func (the Adaptor_ZD_XTJC) getCodeBytes(sensorCode int16) []byte {
bytes := make([]byte, 0)
bytes = append(bytes,
byte(sensorCode&0xFF),
byte(sensorCode>>8),
)
return bytes
}
func (the Adaptor_ZD_XTJC) getTimeBytes(sensorTime time.Time) []byte {
year := int8(sensorTime.Year() - 1900)
month := int8(sensorTime.Month())
day := int8(sensorTime.Day())
hour := int8(sensorTime.Hour())
minute := int8(sensorTime.Minute())
millisecond := uint16(sensorTime.Second()*1000 + sensorTime.Nanosecond()/1e6)
bytes := make([]byte, 0)
bytes = append(bytes,
byte(year),
byte(month),
byte(day),
byte(hour),
byte(minute),
byte(millisecond&0xFF),
byte(millisecond>>8),
)
return bytes
}
func (the Adaptor_ZD_XTJC) getDatasBytes(datas []float32) []byte {
bytes := make([]byte, 0)
for _, data := range datas {
bits := math.Float32bits(data)
bytes = append(bytes,
byte(bits&0xFF),
byte(bits>>8&0xFF),
byte(bits>>16&0xFF),
byte(bits>>24&0xFF),
)
}
return bytes
}
func (the Adaptor_ZD_XTJC) getPayloadHeader(floatCount int16) []byte {
bytes := make([]byte, 0)
bytes = append(bytes,
//报文类型
0x02,
0x00,
//1:上行信息
0x01,
//默认,通讯计算机编号
0x00,
//命令码
0x01,
//报文长度
byte((floatCount*4+9)&0xFF),
byte((floatCount*4+9)>>8),
)
return bytes
}