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

94 lines
2.6 KiB

package adaptors
import (
"encoding/json"
"fmt"
"goUpload/models"
"goUpload/utils"
"log"
"time"
)
// Adaptor_TYCJ_JSNCGLQL 统一采集软件数据 转换 江苏农村公路桥梁监测系统
type Adaptor_TYCJ_JSNCGLQL struct {
IdMap map[string]string
}
func (the Adaptor_TYCJ_JSNCGLQL) Transform(rawMsg string) []NeedPush {
tycj := models.TYCJ{}
json.Unmarshal([]byte(rawMsg), &tycj)
return the.TYCJtoJSNCGLQL(tycj)
}
func (the Adaptor_TYCJ_JSNCGLQL) TYCJtoJSNCGLQL(tycj models.TYCJ) (result []NeedPush) {
Atime, err := time.Parse("2006-01-02T15:04:05.000", tycj.SensorData.Time)
if err != nil {
log.Printf("统一采集 设备[%s] 数据时间 %s 解析错误", tycj.SensorData.Name, tycj.SensorData.Time)
return
}
var sensorDataList []float32
switch tycj.SensorData.FactorType {
case models.TYCJ_FactorType_YLYB, models.TYCJ_FactorType_DTQYB: //应变23,挡土墙应变13 FS-BM50
sensorDataList = append(sensorDataList, tycj.SensorData.Data.ThemeValues[0])
case models.TYCJ_FactorType_QDQX: //桥墩倾斜20 X (mm) Y (mm)
sensorDataList = append(sensorDataList, tycj.SensorData.Data.ThemeValues[0])
sensorDataList = append(sensorDataList, tycj.SensorData.Data.ThemeValues[1])
default:
log.Printf("监测因素[%d] 无匹配", tycj.SensorData.FactorType)
return
}
var transBytes []byte
//1.添加时间段
transBytes = append(transBytes, the.getTimeBytes(Atime)...)
//2.获取对方系统 id
sensorCode := the.getSensorCode(tycj.SensorData.Name)
if sensorCode == "" {
log.Printf("统一采集 设备[%s] 无匹配的 江苏农村公路桥梁监测系统 测点id,请检查配置文件", tycj.SensorData.Name)
return
}
//todo 桥梁编码
bridgeCode := sensorCode
topic := fmt.Sprintf("data/%s/%s", bridgeCode, sensorCode)
for _, sensorData := range sensorDataList {
//4.添加数据值段
bs := utils.Float32ToBytes(sensorData)
transBytes = append(transBytes, bs...)
}
result = append(result, NeedPush{
Topic: topic,
Payload: transBytes,
})
return result
}
func (the Adaptor_TYCJ_JSNCGLQL) getSensorCode(rawSensorName string) string {
v, isValid := the.IdMap[rawSensorName]
if !isValid {
v = ""
}
return v
}
func (the Adaptor_TYCJ_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())
bytes := make([]byte, 0)
bytes = append(bytes,
byte(year),
byte(month),
byte(day),
byte(hour),
byte(minute),
byte(second),
)
return bytes
}