package adaptors import ( "encoding/hex" "encoding/json" "goUpload/models" "log" "math" "time" ) // Adaptor_TYCJ_XTJC 统一采集软件数据 转换 湘潭健康监测平台 type Adaptor_TYCJ_XTJC struct { IdMap map[string]int16 } func (the Adaptor_TYCJ_XTJC) Transform(rawMsg string) [][]byte { tycj := models.TYCJ{} json.Unmarshal([]byte(rawMsg), &tycj) return the.TYCJtoXTJC(tycj) } func (the Adaptor_TYCJ_XTJC) TYCJtoXTJC(tycj models.TYCJ) (result [][]byte) { 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_QLSSF: //桥梁伸缩缝监测22 sensorDataList = append(sensorDataList, tycj.SensorData.Data.ThemeValues[0]) case models.TYCJ_FactorType_YLYB, models.TYCJ_FactorType_DTQYB: //应变23,挡土墙应变13 sensorDataList = append(sensorDataList, tycj.SensorData.Data.ThemeValues[0]) case models.TYCJ_FactorType_RD: //扰度31 sensorDataList = append(sensorDataList, tycj.SensorData.Data.ThemeValues[0]) case models.TYCJ_FactorType_QDQX: //桥墩倾斜20 X (mm) Y (mm) Z (mm) 温度(℃) 20240315对方要求每个监测项 分开上报 sensorDataList = append(sensorDataList, tycj.SensorData.Data.ThemeValues[0]) sensorDataList = append(sensorDataList, tycj.SensorData.Data.ThemeValues[1]) case models.TYCJ_FactorType_LF: //裂缝28 sensorDataList = append(sensorDataList, tycj.SensorData.Data.ThemeValues[0]) default: log.Printf("监测因素[%d] 无匹配", tycj.SensorData.FactorType) return } for i, sensorData := range sensorDataList { var transBytes []byte //1.添加通用报文头段 commPayloadHeader := the.getPayloadHeader(int16(1)) //20240315 客户确认 如双轴倾角 改成2条上报 transBytes = append(transBytes, commPayloadHeader...) //2.添加时间段 transBytes = append(transBytes, the.getTimeBytes(Atime)...) //3.添加对象码段 sensorCode := the.getSensorId(tycj.SensorData.Name) if i == 1 { //2项数据的目前只有倾斜 2桥 =》 Y轴id=X轴id+39 1桥=》Y轴id=X轴id+10000 sensorCode = sensorCode + 39 } // if sensorCode != 0 { transBytes = append(transBytes, the.getCodeBytes(sensorCode)...) } else { log.Printf("统一采集 设备[%s] 无匹配的湘潭监测id,请检查配置文件", tycj.SensorData.Name) return } d := []float32{sensorData} //4.添加数据值段 transBytes = append(transBytes, the.getDatasBytes(d)...) log.Printf("TYCJ [f=%d]-[%s]-[%d]-[%d]报文=%s", tycj.SensorData.FactorType, tycj.SensorData.Name, i, the.getSensorId(tycj.SensorData.Name), hex.EncodeToString(transBytes)) result = append(result, transBytes) } return result } func (the Adaptor_TYCJ_XTJC) getSensorId(rawSensorName string) int16 { v, isValid := the.IdMap[rawSensorName] if !isValid { v = 0 } return v } func (the Adaptor_TYCJ_XTJC) getCodeBytes(sensorCode int16) []byte { bytes := make([]byte, 0) bytes = append(bytes, byte(sensorCode&0xFF), byte(sensorCode>>8), ) return bytes } func (the Adaptor_TYCJ_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_TYCJ_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_TYCJ_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 }