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.
86 lines
2.6 KiB
86 lines
2.6 KiB
2 weeks ago
|
package adaptors
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
"goUpload/models"
|
||
|
"log"
|
||
|
"strings"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
// Adaptor_TYCJ_MYX 统一采集软件数据 转换 明月峡-长江大桥
|
||
|
type Adaptor_TYCJ_MYX struct {
|
||
|
IdMap map[string]string
|
||
|
}
|
||
|
|
||
|
func (the Adaptor_TYCJ_MYX) Transform(rawMsg string) []byte {
|
||
|
tycj := models.TYCJ{}
|
||
|
json.Unmarshal([]byte(rawMsg), &tycj)
|
||
|
return the.TYCJtoMYCInflux(tycj)
|
||
|
}
|
||
|
|
||
|
func (the Adaptor_TYCJ_MYX) TYCJtoMYCInflux(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
|
||
|
}
|
||
|
|
||
|
sensorCode := the.getSensorId(tycj.SensorData.Name)
|
||
|
tableName, fields := the.getFactorFields(tycj.SensorData.FactorType)
|
||
|
if sensorCode == "" || tableName == "" || len(fields) == 0 || len(fields) > len(tycj.SensorData.Data.ThemeValues) {
|
||
|
log.Printf("统一采集 设备[%s] 数据时间 %s 无匹配 sensorCode=%s,tableName=%s,fields=%s",
|
||
|
tycj.SensorData.Name, tycj.SensorData.Time, sensorCode, tableName, fields)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
//insert ND_data,sensor_id=ND000101 valueV=0.68,valueH=290
|
||
|
var sqlBuilder strings.Builder
|
||
|
sqlBuilder.WriteString(fmt.Sprintf("%s,sensor_id=%s ", tableName, sensorCode))
|
||
|
for i, field := range fields {
|
||
|
sqlBuilder.WriteString(fmt.Sprintf("%s=%f,", field, tycj.SensorData.Data.ThemeValues[i]))
|
||
|
}
|
||
|
sqlStr := sqlBuilder.String()
|
||
|
sqlStr = sqlStr[:len(sqlStr)-1]
|
||
|
sqlStr += fmt.Sprintf(" %d", Atime.Add(-8*time.Hour).UnixNano())
|
||
|
log.Printf("TYCJ [f=%d]-[%s]-[%s]报文=%s 设备时间%v", tycj.SensorData.FactorType, tycj.SensorData.Name,
|
||
|
sensorCode, sqlStr, Atime)
|
||
|
|
||
|
return []byte(sqlStr)
|
||
|
}
|
||
|
func (the Adaptor_TYCJ_MYX) getSensorId(rawSensorName string) string {
|
||
|
v, isValid := the.IdMap[rawSensorName]
|
||
|
if !isValid {
|
||
|
v = ""
|
||
|
}
|
||
|
|
||
|
return v
|
||
|
}
|
||
|
|
||
|
func (the Adaptor_TYCJ_MYX) getFactorFields(floatCount int) (tableName string, fields []string) {
|
||
|
|
||
|
//这个对应关系 要看现场聚集的统一采集软件 进行调整
|
||
|
switch floatCount {
|
||
|
|
||
|
case 5: //温湿度 1个
|
||
|
tableName = "WSDJC"
|
||
|
fields = []string{"value", "value_2"}
|
||
|
case 18: //风速风向 共2个
|
||
|
tableName = "FSFXJC"
|
||
|
fields = []string{"value", "value_2"}
|
||
|
case 20: //塔顶倾斜 共2个 角度设备 -》对应时序库的 主塔位监测 微信群确认
|
||
|
tableName = "ZTWJC"
|
||
|
fields = []string{"value", "value_2"}
|
||
|
case 90: //桥头倾斜 -》 梁端转角
|
||
|
tableName = "LDZJJC"
|
||
|
fields = []string{"value", "value_2"}
|
||
|
case 22: //伸缩缝 2个
|
||
|
tableName = "SSFJC"
|
||
|
fields = []string{"value"}
|
||
|
default:
|
||
|
fields = []string{}
|
||
|
}
|
||
|
return tableName, fields
|
||
|
}
|