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

122 lines
3.0 KiB

package adaptors
import (
"crypto/rc4"
"encoding/hex"
"encoding/json"
"goUpload/consumers/CQZG/protoFiles"
"goUpload/models"
"goUpload/utils"
"google.golang.org/protobuf/proto"
"log"
"strconv"
"time"
)
// Adaptor_OLCZ_CQZG 称重数据转换器
type Adaptor_OLCZ_CQZG struct {
IdMap map[string]int64
RC4Key string
}
func (the Adaptor_OLCZ_CQZG) Transform(rawMsg string) []byte {
cz := models.OLCZ{}
json.Unmarshal([]byte(rawMsg), &cz)
return the.OLCZtoCQZG(cz)
}
func (the Adaptor_OLCZ_CQZG) OLCZtoCQZG(cz models.OLCZ) (result []byte) {
var transBytes []byte
ComplexData_HSD := &protoFiles.ComplexData{
SensorData: make([]*protoFiles.SensorData, 0),
}
Atime, err := time.Parse("2006-01-02 15:04:05", cz.Atime)
if err != nil {
log.Printf("称重数据时间 %s 解析错误 ", cz.Atime)
Atime = time.Now()
}
sensorData := &protoFiles.SensorData{
MonitorType: protoFiles.MonitoryType_HSD, //监测类型 转换
SensorID: the.getSensorId(strconv.Itoa(int(cz.Roadno))), //id转换
EventTime: Atime.Add(-8 * time.Hour).UnixMilli(),
ChannelCode: "",
DataBody: &protoFiles.SensorData_Hsd{
Hsd: &protoFiles.HSDRealTime{
LaneId: cz.Roadno,
OperDirec: cz.OperDirec, //0上行,1下行 在称重软件上设置
AxleNum: cz.AxisCount,
AxleGrpNum: cz.AxleGrpNum, //轴组数
GrossLoad: cz.GrossWeight,
VehType: the.getVehType(cz.Carmodel),
Speed: cz.Speed,
},
},
}
ComplexData_HSD.SensorData = append(ComplexData_HSD.SensorData, sensorData)
CZStr, _ := json.Marshal(ComplexData_HSD)
log.Printf("cz [r=%d] -[%d]原始报文 ComplexData_HSD=%s",
cz.Roadno,
sensorData.SensorID,
CZStr)
transBytes, _ = proto.Marshal(ComplexData_HSD)
log.Printf("cz [r=%d] -[%d]报文 proto.Marshal=%s",
cz.Roadno,
sensorData.SensorID,
hex.EncodeToString(transBytes))
resultByCrc16 := utils.NewCRC16CCITT().GetWCRCin(transBytes)
needRC4 := append(transBytes, resultByCrc16...)
log.Printf("cz [r=%d] -[%d]报文 needRC4=%s", cz.Roadno, sensorData.SensorID, 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("cz [r=%d] -[%d]最终报文 dest1=%s", cz.Roadno, sensorData.SensorID, hex.EncodeToString(dest1))
return dest1
}
func (the Adaptor_OLCZ_CQZG) getVehType(Carmodel string) (VehType int32) {
switch Carmodel {
case "A":
VehType = 1
case "B":
VehType = 2
case "C":
VehType = 3
case "D":
VehType = 4
case "E":
VehType = 5
case "F":
VehType = 6
case "G":
VehType = 7
case "H":
VehType = 8
case "I":
VehType = 9
}
return
}
func (the Adaptor_OLCZ_CQZG) getSensorId(rawSensorName string) int64 {
v, isValid := the.IdMap[rawSensorName]
if !isValid {
v = 0
}
return v
}
// 称重软件 传感器车道和对方平台id对于关系
//var idMapCZ = map[string]int64{
// "1": 5000200024,
// "2": 5000200025,
// "3": 5000200026,
//}