package adaptors import ( "bytes" "encoding/json" "fmt" "goUpload/models" "log" "math" "strings" "text/template" "time" ) // Adaptor_AXYES_HBKS 统一采集软件数据 转换 湘潭健康监测平台 type Adaptor_AXYES_HBKS 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_HBKS) Transform(rawMsg string) []byte { esAggTop := models.EsAggTop{} err := json.Unmarshal([]byte(rawMsg), &esAggTop) if err != nil { return nil } return the.EsAggTopToHBKS(esAggTop) } func (the Adaptor_AXYES_HBKS) EsAggTopToHBKS(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 templateWork(formulaTemplateStr string, templateParams map[string]string) (string, error) { CreateFormulaTemplate := func(name, t string) *template.Template { return template.Must(template.New(name).Parse(t)) } formulaTemplate := CreateFormulaTemplate("formulaTemplate", formulaTemplateStr) formulaBuff := &bytes.Buffer{} err := formulaTemplate.Execute(formulaBuff, templateParams) return formulaBuff.String(), err } func (the Adaptor_AXYES_HBKS) 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 cacheHBKS = make(map[string]float64) func (the Adaptor_AXYES_HBKS) 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"]) 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 := cacheHBKS[cacheKey_x] if isHave { log.Printf("[%s]上次值 xLast=%.3f", sensorData.SensorName, xLast) xAcc = sensorData.Data["x"] - xLast } yLast, isHave := cacheHBKS[cacheKey_y] if isHave { log.Printf("[%s]上次值 yLast=%.3f", sensorData.SensorName, yLast) yAcc = sensorData.Data["y"] - yLast } zLast, isHave := cacheHBKS[cacheKey_z] if isHave { log.Printf("[%s]上次值 zLast=%.3f", sensorData.SensorName, zLast) zAcc = sensorData.Data["z"] - zLast } templateMap["xAcc"] = fmt.Sprintf("%.3f", xAcc) templateMap["yAcc"] = fmt.Sprintf("%.3f", yAcc) templateMap["zAcc"] = fmt.Sprintf("%.3f", zAcc) templateMap["unit"] = "mm" cacheHBKS[cacheKey_x] = sensorData.Data["x"] cacheHBKS[cacheKey_y] = sensorData.Data["y"] cacheHBKS[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_HBKS) getStructureId() string { structureId, ok := the.Info["structureId"] //涿鹿金隅水泥有限公司大斜阳矿 if !ok { log.Panic("配置文件info中 无structureId") } return structureId } func (the Adaptor_AXYES_HBKS) getJWDbySensorName(sensorData models.Source) (longitude, latitude, height string) { structureId := the.getStructureId() switch structureId { case models.AXY_StructId_DXY: //大斜阳 longitude, latitude, height = jwdDXY(sensorData.SensorName) case models.AXY_StructId_TPB: //太平堡 longitude, latitude, height = jwdTPB(sensorData.SensorName) case models.AXY_StructId_TSJD: longitude, latitude, height = jwdTSJD(sensorData.SensorName) } return } func (the Adaptor_AXYES_HBKS) getSensorCodeAndType(sensorData models.Source) (code, typeCode string) { switch sensorData.Factor { case models.AXY_FactorType_BMWY: typeCode = "02" v, isValid := the.GnssMap[sensorData.SensorName] if !isValid { return } code = v case models.AXY_FactorType_Rain_New: typeCode = "03" v, isValid := the.RainMap[sensorData.SensorName] if !isValid { code = "" } code = v case models.AXY_FactorType_SBSPWY: typeCode = "04" v, isValid := the.NBWYMap[sensorData.SensorName] if !isValid { code = "" } code = v case models.AXY_FactorType_DXSW: typeCode = "09" v, isValid := the.DXSWMap[sensorData.SensorName] if !isValid { code = "" } code = v } return } func (the Adaptor_AXYES_HBKS) 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_HBKS) 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_HBKS) 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_HBKS) 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 } func jwdDXY(sensorName string) (longitude, latitude, height string) { switch sensorName { //大斜阳矿 case "JC01": longitude = "115.243554" latitude = "40.114834" height = "1297" case "JC02": longitude = "115.241985" latitude = "40.115445" height = "1317" case "JC03": longitude = "115.241080" latitude = "40.115698" height = "1317" case "雨量": longitude = "115.244209" latitude = "40.123344" height = "1217" } return } func jwdTPB(sensorName string) (longitude, latitude, height string) { switch sensorName { case "JC01": longitude = "115.370653" latitude = "40.119892" height = "1085" case "JC02": longitude = "115.366018" latitude = "40.117782" height = "1115" case "雨量": longitude = "115.366922" latitude = "40.117217" height = "1107" } return } func jwdTSJD(sensorName string) (longitude, latitude, height string) { //传感器所在孔 holeName := []rune(sensorName) holeIndex := string(holeName[:len(holeName)-1]) switch holeIndex { case "Y": longitude = "118.249646" latitude = "39.934143" height = "100" case "BC190_10": longitude = "118.238283" latitude = "39.946949" height = "190" case "BC170_30": longitude = "118.242235" latitude = "39.945843" height = "170" case "BC170_40": longitude = "118.244229" latitude = "39.945138" height = "170" case "BC150_10": longitude = "118.237151" latitude = "39.946451" height = "150" case "BC150_20": longitude = "118.239612" latitude = "39.945954" height = "150" case "BC130_30": longitude = "118.240133" latitude = "39.945304" height = "130" case "BC130_40": longitude = "118.242631" latitude = "39.944875" height = "130" case "BC115_10": longitude = "118.238570" latitude = "39.944695" height = "115" case "BC115_20": longitude = "118.243008" latitude = "39.943796" height = "115" case "BC115_30": longitude = "118.243000" latitude = "39.944419" height = "115" case "BC100_30": longitude = "118.242954" latitude = "39.942911" height = "100" case "BC100_40": longitude = "118.244194" latitude = "39.940684" height = "100" case "NC110_40": longitude = "118.249610" latitude = "39.944820" height = "110" case "NC120_10": longitude = "118.249305" latitude = "39.934018" height = "120" case "NC120_20": longitude = "118.249700" latitude = "39.932248" height = "120" case "NC120_30": longitude = "118.250113" latitude = "39.929592" height = "120" case "NC140_20": longitude = "118.251119" latitude = "39.932524" height = "140" case "NC140_30": longitude = "118.251605" latitude = "39.929896" height = "140" } return }