数据 输入输出 处理
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.

285 lines
8.0 KiB

package adaptors
import (
"encoding/json"
"fmt"
"goInOut/models"
"log"
"math"
"strings"
"time"
)
// Adaptor_AXYES_GDKS
type Adaptor_AXYES_GDKS struct {
//传感器code转换信息
GnssMap map[string]string
RainMap map[string]string
NBWYMap map[string]string
DXSWMap map[string]string
//一些必要信息
Info map[string]string
}
func (the Adaptor_AXYES_GDKS) Transform(rawMsg string) []byte {
esAggTop := models.EsAggTop{}
err := json.Unmarshal([]byte(rawMsg), &esAggTop)
if err != nil {
return nil
}
return the.EsAggTopToGDKS(esAggTop)
}
func (the Adaptor_AXYES_GDKS) EsAggTopToGDKS(esAggTop models.EsAggTop) (result []byte) {
//var transBytes []byte
fileContent := strings.Builder{}
//写入文本内容的固定头
contentHeader := the.getContentHeader()
fileContent.WriteString(contentHeader)
for _, bucket := range esAggTop.Aggregations.SensorId.Buckets {
for _, hit := range bucket.Last.Hits.Hits {
source := hit.Source
sensorContentTemplate := the.getTemplateStr(source)
sensorContentObj := the.getTemplateObj(source)
if len(sensorContentObj) == 0 {
continue
}
txt, err := templateWork(sensorContentTemplate, sensorContentObj)
if err != nil {
log.Printf("传感器[%s]数据生成出现异常,err=%s", source.SensorName, err.Error())
continue
}
fileContent.WriteString(txt)
}
}
//单个文本文件用“]]]”表示结束
fileContent.WriteString("]]]")
return []byte(fileContent.String())
}
func (the Adaptor_AXYES_GDKS) getTemplateStr(sensorData models.Source) string {
templateStr := ""
switch sensorData.Factor {
case models.AXY_FactorType_BMWY:
templateStr = "{{.structUploadCode}}{{.sensorType}}{{.sensorCode}};{{.sensorType}};{{.sensorName}};{{.longitude}};{{.latitude}};{{.height}};" +
"{{.x}}|{{.y}}|{{.z}}||||{{.xAcc}}|{{.yAcc}}|{{.zAcc}};{{.unit}};{{.state}};{{.time}}^"
case models.AXY_FactorType_Rain_New:
templateStr = "{{.structUploadCode}}{{.sensorType}}{{.sensorCode}};{{.sensorType}};{{.sensorName}};{{.longitude}};{{.latitude}};{{.height}};" +
"{{.totrainfall}};{{.unit}};{{.state}};{{.time}}^"
case models.AXY_FactorType_SBSPWY:
templateStr = "{{.structUploadCode}}{{.sensorType}}{{.sensorCode}};{{.sensorType}};{{.sensorName}};{{.longitude}};{{.latitude}};{{.height}};" +
"{{.x}}|{{.y}};{{.unit}};{{.state}};{{.time}}^"
case models.AXY_FactorType_DXSW:
templateStr = "{{.structUploadCode}}{{.sensorType}}{{.sensorCode}};{{.sensorType}};{{.sensorName}};{{.longitude}};{{.latitude}};{{.height}};" +
"{{.waterLevel}};{{.unit}};{{.state}};{{.time}}^"
}
return templateStr
}
// 广东矿山GNSS 表面唯一缓存,用于单次变化量
var cacheGDKS = make(map[string]float64)
func (the Adaptor_AXYES_GDKS) getTemplateObj(sensorData models.Source) map[string]string {
templateMap := make(map[string]string)
sensorCode, sensorType := the.getSensorCodeAndType(sensorData)
if sensorCode == "" {
return templateMap
}
for k, v := range the.Info {
templateMap[k] = v
}
templateMap["sensorCode"] = sensorCode
templateMap["sensorType"] = sensorType
templateMap["sensorName"] = sensorData.SensorName
longitude, latitude, height := the.getJWDbySensorName(sensorData)
templateMap["longitude"] = longitude
templateMap["latitude"] = latitude
templateMap["height"] = height
templateMap["state"] = "00000000"
templateMap["time"] = sensorData.CollectTime.Add(8 * time.Hour).Format("2006-01-02 15:04:05")
switch sensorData.Factor {
case models.AXY_FactorType_BMWY:
//templateMap["x"] = fmt.Sprintf("%.3f", sensorData.Data["x"])
//templateMap["y"] = fmt.Sprintf("%.3f", sensorData.Data["y"])
//templateMap["z"] = fmt.Sprintf("%.3f", sensorData.Data["z"])
templateMap["xAcc"] = "0"
templateMap["yAcc"] = "0"
templateMap["zAcc"] = "0"
xAcc := 0.0
yAcc := 0.0
zAcc := 0.0
cacheKey_x := fmt.Sprintf("%s_x", sensorData.SensorName)
cacheKey_y := fmt.Sprintf("%s_y", sensorData.SensorName)
cacheKey_z := fmt.Sprintf("%s_z", sensorData.SensorName)
xLast, isHave := cacheGDKS[cacheKey_x]
if isHave {
log.Printf("[%s]上次值 xLast=%.3f", sensorData.SensorName, xLast)
xAcc = sensorData.Data["x"] - xLast
}
yLast, isHave := cacheGDKS[cacheKey_y]
if isHave {
log.Printf("[%s]上次值 yLast=%.3f", sensorData.SensorName, yLast)
yAcc = sensorData.Data["y"] - yLast
}
zLast, isHave := cacheGDKS[cacheKey_z]
if isHave {
log.Printf("[%s]上次值 zLast=%.3f", sensorData.SensorName, zLast)
zAcc = sensorData.Data["z"] - zLast
}
templateMap["x"] = fmt.Sprintf("%.3f", xAcc)
templateMap["y"] = fmt.Sprintf("%.3f", yAcc)
templateMap["z"] = fmt.Sprintf("%.3f", zAcc)
templateMap["unit"] = "mm"
cacheGDKS[cacheKey_x] = sensorData.Data["x"]
cacheGDKS[cacheKey_y] = sensorData.Data["y"]
cacheGDKS[cacheKey_z] = sensorData.Data["z"]
case models.AXY_FactorType_SBSPWY:
templateMap["x"] = fmt.Sprintf("%.3f", sensorData.Data["x"])
templateMap["y"] = fmt.Sprintf("%.3f", sensorData.Data["y"])
templateMap["unit"] = "mm"
case models.AXY_FactorType_Rain_New:
templateMap["totrainfall"] = fmt.Sprintf("%.3f", sensorData.Data["totrainfall"])
templateMap["unit"] = "mm"
case models.AXY_FactorType_DXSW:
templateMap["waterLevel"] = fmt.Sprintf("%.3f", sensorData.Data["waterLevel"])
templateMap["unit"] = "m"
}
return templateMap
}
func (the Adaptor_AXYES_GDKS) getStructureId() string {
structureId, ok := the.Info["structureId"]
if !ok {
log.Panic("配置文件info中 无structureId")
}
return structureId
}
func (the Adaptor_AXYES_GDKS) getJWDbySensorName(sensorData models.Source) (longitude, latitude, height string) {
switch sensorData.Factor {
case models.AXY_FactorType_BMWY:
v, isValid := the.GnssMap[sensorData.SensorName]
if !isValid {
return
}
codeArr := strings.Split(v, ";")
if len(codeArr) != 4 {
return
}
longitude = codeArr[1]
latitude = codeArr[2]
height = codeArr[3]
case models.AXY_FactorType_Rain_New:
case models.AXY_FactorType_SBSPWY:
case models.AXY_FactorType_DXSW:
}
return
}
func (the Adaptor_AXYES_GDKS) getSensorCodeAndType(sensorData models.Source) (code, typeCode string) {
switch sensorData.Factor {
case models.AXY_FactorType_BMWY:
typeCode = "2001"
v, isValid := the.GnssMap[sensorData.SensorName]
if !isValid {
return
}
codeArr := strings.Split(v, ";")
code = codeArr[0]
case models.AXY_FactorType_Rain_New:
typeCode = "0030"
v, isValid := the.RainMap[sensorData.SensorName]
if !isValid {
code = ""
}
code = v
case models.AXY_FactorType_SBSPWY:
typeCode = "2002"
v, isValid := the.NBWYMap[sensorData.SensorName]
if !isValid {
code = ""
}
code = v
case models.AXY_FactorType_DXSW:
typeCode = "2302"
v, isValid := the.DXSWMap[sensorData.SensorName]
if !isValid {
code = ""
}
code = v
}
return
}
func (the Adaptor_AXYES_GDKS) 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_AXYES_GDKS) 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_AXYES_GDKS) getContentHeader() string {
header := the.Info["fileContentHeader"]
//数据上传时间
timeNow := time.Now().Format("2006-01-02 15:04:05")
header += fmt.Sprintf("%s^", timeNow)
return header
}
// 测点信息文件
func (the Adaptor_AXYES_GDKS) getInfoContentHeader() string {
header := the.Info["fileContentHeader"]
//安标有效期
vaildDate := "2026-12-30"
//数据上传时间
timeNow := time.Now().Format("2006-01-02 15:04:05")
header += fmt.Sprintf("%s;%s^", vaildDate, timeNow)
return header
}