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.

182 lines
4.7 KiB

package common_models
import (
"encoding/json"
"fmt"
"gitea.anxinyun.cn/container/common_models/constant/logTag"
"math"
"strings"
"time"
)
//type mStation struct {
// Name string
// Id int
// Structure Structure
// Factor Factor
// ManualData bool `json:"manual_data"`
// Params map[string]any
// Group StationGroup
// DeviceProto DeviceProto
// CorrGroups []StationGroup //关联的分组(沉降级联)
// Labels string
// //CombineInfo // 测点数据组装信息
//}
// Station 测点模型 = 基本信息 + 数据 + 阈值
type Station struct {
// 测点基本信息:名称、监测因素、监测原型、结构物、IOT-Things信息等
Info StationInfo
// 测点数据包括:设备数据 + 主题数据
Data StationData
// 测点阈值
Threshold *Threshold
}
type StationData struct {
//测点包含-计算后的设备单点数据
DeviceCalcData map[string]any
//测点最终数据(主题数据)
ThemeData map[string]any
PhyData map[string]any
CollectTime time.Time
AlarmLevel int
}
// GetThemeFields 获取主题数据的监测项
func (s *StationData) GetThemeFields() []string {
keys := make([]string, 0, len(s.ThemeData))
for key := range s.ThemeData {
keys = append(keys, key)
}
return keys
}
// GetProtoFields 获取监测原型的监测项
func (the *Station) GetProtoFields() []string {
var fields []string
for _, protoItem := range the.Info.Factor.Items {
fields = append(fields, protoItem.FieldName)
}
return fields
}
// GetValidThemeData 获取有效的主题数据(数据规整为:非空float64)
func (s *StationData) GetValidThemeData() (map[string]float64, bool) {
processedData := make(map[string]float64)
for key, value := range s.ThemeData {
switch v := value.(type) {
case int:
processedData[key] = float64(v)
case float64:
processedData[key] = v
default:
processedData[key] = math.NaN()
}
}
// 过滤掉值为NaN的条目
filteredData := make(map[string]float64)
for key, value := range processedData {
if !math.IsNaN(value) {
filteredData[key] = value
}
}
return filteredData, len(filteredData) > 0
}
func (the *Station) LogMsg() string {
logTagThing := fmt.Sprintf("[%s:%s][%s:%d]", logTag.Thing, the.Info.ThingId, logTag.Station, the.Info.Id)
var deviceLogs []string
for _, device := range the.Info.Devices {
deviceLogs = append(deviceLogs, device.LogMsg())
}
deviceLogStr := strings.Join(deviceLogs, ",")
return logTagThing + deviceLogStr
}
// redis序列化
func (the *Station) MarshalBinary() (data []byte, err error) {
return json.Marshal(the)
}
// redis序列化
func (the *Station) UnmarshalBinary(data []byte) error {
return json.Unmarshal(data, the)
}
type StationArrayObj []Station
// redis序列化
func (m *StationArrayObj) MarshalBinary() (data []byte, err error) {
return json.Marshal(m)
}
// redis序列化
func (m *StationArrayObj) UnmarshalBinary(data []byte) error {
return json.Unmarshal(data, m)
}
type DeviceProto struct {
Formula int
FieldVal map[string]string
MultiFormula int
multiFields int
UnitConversion map[string]float64
}
type StationInfo struct {
Id int `json:"id"`
Name string `json:"name"`
StructureId int `json:"structure"`
ThingId string `json:"thingId"`
StructureName string `json:"struct_name"`
FactorId int `json:"factor"`
IsManualData bool `json:"manual_data"`
FormulaId int `json:"formula"`
ParamsValue map[string]any `json:"params_value"`
Factor Factor
ProtoCode string `json:"proto"`
Proto Proto
Devices []SecureStationDevice
Labels string
CombineInfo string
Group StationGroup `json:"group,omitempty"`
CorrGroups []StationGroup `json:"corr_group_ids,omitempty"` // 关联的分组ID
}
// redis序列化
func (m *StationInfo) MarshalBinary() (data []byte, err error) {
return json.Marshal(m)
}
// redis序列化
func (m *StationInfo) UnmarshalBinary(data []byte) error {
return json.Unmarshal(data, m)
}
func (the *StationInfo) GetDeviceIdArray() []string {
var deviceIdArray []string
for _, device := range the.Devices {
deviceIdArray = append(deviceIdArray, device.IotaDeviceId)
}
return deviceIdArray
}
type SecureStationDevice struct {
FormulaId int `json:"formula_id"` //单设备测点 公式id redis里面原本没有 20240326后加
Params map[string]any `json:"params"`
IotaDeviceId string `json:"iota_device_id"`
IotaDeviceSerial int `json:"iota_device_serial"`
FormulaInfo Formula
DeviceFactorProto DeviceFactorProto
}
func (s *SecureStationDevice) LogMsg() string {
return fmt.Sprintf("[%s:%s]", logTag.Device, s.IotaDeviceId)
}