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
3.7 KiB
126 lines
3.7 KiB
2 weeks ago
|
package adaptors
|
||
|
|
||
|
import (
|
||
|
"crypto/rc4"
|
||
|
"encoding/hex"
|
||
|
"encoding/json"
|
||
|
"goUpload/consumers/CQZG/protoFiles"
|
||
|
"goUpload/models"
|
||
|
"goUpload/utils"
|
||
|
"google.golang.org/protobuf/proto"
|
||
|
"log"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
// Adaptor_TYCJ_CQZG 统一采集软件数据转换器
|
||
|
type Adaptor_TYCJ_CQZG struct {
|
||
|
IdMap map[string]int64
|
||
|
RC4Key string
|
||
|
}
|
||
|
|
||
|
func (the Adaptor_TYCJ_CQZG) Transform(rawMsg string) []byte {
|
||
|
tycj := models.TYCJ{}
|
||
|
json.Unmarshal([]byte(rawMsg), &tycj)
|
||
|
return the.TYCJtoCQZG(tycj)
|
||
|
}
|
||
|
|
||
|
func (the Adaptor_TYCJ_CQZG) TYCJtoCQZG(tycj models.TYCJ) (result []byte) {
|
||
|
Atime, err := time.Parse("2006-01-02T15:04:05.000", tycj.SensorData.Time)
|
||
|
if err != nil {
|
||
|
log.Printf("统一采集数据时间 %s 解析错误 ", tycj.SensorData.Time)
|
||
|
Atime = time.Now()
|
||
|
}
|
||
|
|
||
|
var transBytes []byte
|
||
|
switch tycj.SensorData.FactorType {
|
||
|
|
||
|
case models.TYCJ_FactorType_WSD:
|
||
|
ComplexData_WSD := &protoFiles.ComplexData{
|
||
|
SensorData: make([]*protoFiles.SensorData, 0),
|
||
|
}
|
||
|
sensorData := &protoFiles.SensorData{
|
||
|
MonitorType: the.getMonitorType(tycj.SensorData.FactorType), //监测类型 转换
|
||
|
SensorID: the.getSensorId(tycj.SensorData.Name), //id转换
|
||
|
EventTime: Atime.Add(-8 * time.Hour).UnixMilli(),
|
||
|
ChannelCode: "",
|
||
|
DataBody: &protoFiles.SensorData_Rhs{
|
||
|
Rhs: &protoFiles.RHSRealTime{
|
||
|
Temperature: []float32{tycj.SensorData.Data.ThemeValues[0]},
|
||
|
Humidity: []float32{tycj.SensorData.Data.ThemeValues[1]},
|
||
|
},
|
||
|
},
|
||
|
}
|
||
|
ComplexData_WSD.SensorData = append(ComplexData_WSD.SensorData, sensorData)
|
||
|
transBytes, _ = proto.Marshal(ComplexData_WSD)
|
||
|
|
||
|
case models.TYCJ_FactorType_YLYB: //应变 特殊 要把温度拆开为独立数据
|
||
|
ComplexData_YLYB := &protoFiles.ComplexData{
|
||
|
SensorData: make([]*protoFiles.SensorData, 0),
|
||
|
}
|
||
|
sensorData := &protoFiles.SensorData{
|
||
|
MonitorType: the.getMonitorType(tycj.SensorData.FactorType), //监测类型 转换
|
||
|
SensorID: the.getSensorId(tycj.SensorData.Name), //id转换
|
||
|
EventTime: Atime.Add(-8 * time.Hour).UnixMilli(),
|
||
|
ChannelCode: "",
|
||
|
DataBody: &protoFiles.SensorData_Rsg{
|
||
|
Rsg: &protoFiles.RSGRealTime{
|
||
|
Strain: []float32{tycj.SensorData.Data.ThemeValues[0]},
|
||
|
Temperature: []float32{tycj.SensorData.Data.RawValues[1]},
|
||
|
},
|
||
|
},
|
||
|
}
|
||
|
ComplexData_YLYB.SensorData = append(ComplexData_YLYB.SensorData, sensorData)
|
||
|
|
||
|
TYCJStr, _ := json.Marshal(ComplexData_YLYB)
|
||
|
log.Printf("TYCJ [f=%d] -[%s]-[%d]原始报文=%s",
|
||
|
tycj.SensorData.FactorType,
|
||
|
tycj.SensorData.Name,
|
||
|
the.getSensorId(tycj.SensorData.Name),
|
||
|
TYCJStr)
|
||
|
transBytes, _ = proto.Marshal(ComplexData_YLYB)
|
||
|
}
|
||
|
log.Printf("TYCJ [f=%d] -[%s]-[%d]报文proto.Marshal=%s",
|
||
|
tycj.SensorData.FactorType,
|
||
|
tycj.SensorData.Name,
|
||
|
the.getSensorId(tycj.SensorData.Name),
|
||
|
hex.EncodeToString(transBytes))
|
||
|
resultByCrc16 := utils.NewCRC16CCITT().GetWCRCin(transBytes)
|
||
|
needRC4 := append(transBytes, resultByCrc16...)
|
||
|
log.Printf("needRC4=%s", hex.EncodeToString(needRC4))
|
||
|
|
||
|
rc4Key := []byte(the.RC4Key)
|
||
|
// 加密操作
|
||
|
dest1 := make([]byte, len(needRC4))
|
||
|
rc4.NewCipher(rc4Key)
|
||
|
cipher1, _ := rc4.NewCipher(rc4Key)
|
||
|
cipher1.XORKeyStream(dest1, needRC4)
|
||
|
log.Printf("rc4加密结果=> %s ", hex.EncodeToString(dest1))
|
||
|
return dest1
|
||
|
}
|
||
|
|
||
|
func (the Adaptor_TYCJ_CQZG) getMonitorType(rawId int) (MonitorType protoFiles.MonitoryType) {
|
||
|
switch rawId {
|
||
|
case models.TYCJ_FactorType_YLYB:
|
||
|
MonitorType = protoFiles.MonitoryType_RSG
|
||
|
default:
|
||
|
MonitorType = protoFiles.MonitoryType_CMM
|
||
|
}
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func (the Adaptor_TYCJ_CQZG) getSensorId(rawSensorName string) int64 {
|
||
|
v, isValid := the.IdMap[rawSensorName]
|
||
|
if !isValid {
|
||
|
v = 0
|
||
|
}
|
||
|
|
||
|
return v
|
||
|
}
|
||
|
|
||
|
// 统一采集软件 传感器名称和对方平台id对于关系
|
||
|
//var idMapTYCJ = map[string]int64{
|
||
|
// "GXGL-G-03": 5000200001,
|
||
|
// "GXGL-P-01": 5000200002,
|
||
|
// "GXGL-G-01": 5000200003,
|
||
|
//}
|