package common_models import ( "encoding/json" "errors" "fmt" "gitea.anxinyun.cn/container/common_models/constant/logTag" "gitea.anxinyun.cn/container/common_models/constant/settlementParam" "log" "strconv" ) //var group36 = ` //RedisKey = group:36 的值: //{ // "id": 36, // "name": "上游沉降", // "group_type": "202", // "items": [ // { // "sensor": 26, // "params_value": { // "base": false // } // }, // { // "sensor": 43, // "params_value": { // "base": false // } // }, // { // "sensor": 192, // "params_value": { // "base": true // } // } // ], // "params": { // "ref_base": 193, // "ref_point": 23 // } //} //` type StationGroup struct { Id int `json:"id"` Name string `json:"name"` GroupType string `json:"group_type"` Items []GroupItem `json:"items"` Params map[string]interface{} } // AllCorrItems 分组的关联项 = Items + 该分组的级联 ref_base、ref_point(可能有多层) func (g *StationGroup) AllCorrItems() []GroupItem { var allCorrItems []GroupItem for _, item := range g.Items { allCorrItems = append(allCorrItems, item.CorrItems()...) } return allCorrItems } // GetSettlementBaseItem 获取沉降分组的基点 func (g *StationGroup) GetSettlementBaseItem() *GroupItem { for i, item := range g.Items { if item.ParamsValue[settlementParam.Base] == true { return &g.Items[i] } } return nil } func (g *StationGroup) GetItem(stationId int) *GroupItem { for i, item := range g.Items { if item.StationId == stationId { return &g.Items[i] } } return nil } func (g *StationGroup) R() string { return fmt.Sprintf("[%s:%d]", logTag.Group, g.Id) } // redis序列化 func (g *StationGroup) MarshalBinary() (data []byte, err error) { return json.Marshal(g) } // redis序列化 func (g *StationGroup) UnmarshalBinary(data []byte) error { return json.Unmarshal(data, g) } type GroupItem struct { StationId int `json:"sensor"` ParamsValue map[string]interface{} `json:"params_value"` SubItems map[string]GroupItem `json:"subItems,omitempty"` } // GetDoubleParam 返回param的float64值 func (gi *GroupItem) GetDoubleParam(key string) (float64, error) { value := gi.ParamsValue[key] switch v := value.(type) { case int: return float64(v), nil case float64: return v, nil case string: if floatValue, err := strconv.ParseFloat(v, 64); err == nil { return floatValue, nil } else { log.Printf("[GroupItem.getDoubleParam] key=%s value=%v ,value parse to float64 error: %v\n", key, value, err) return 0.0, err } } var noKeyMsg = fmt.Sprintf("[GroupItem.getDoubleParam] no key=%s\n", key) log.Println(noKeyMsg) return 0.0, errors.New(noKeyMsg) } // CorrItems 基点的级联参考基点和参考测点 func (gi *GroupItem) CorrItems() []GroupItem { if gi.SubItems == nil { return []GroupItem{*gi} } var result []GroupItem for _, item := range gi.SubItems { result = append(result, item.CorrItems()...) } result = append(result, *gi) return result } // StationGroupInfo 测点的分组信息(对应 ET3.0 的 secure_station_group ) type StationGroupInfo struct { StationId int `json:"station"` GroupId int `json:"group"` Params map[string]interface{} `json:"params"` Name string `json:"name"` GroupType string `json:"group_type"` } // redis序列化 func (g *StationGroupInfo) MarshalBinary() (data []byte, err error) { return json.Marshal(g) } // redis序列化 func (g *StationGroupInfo) UnmarshalBinary(data []byte) error { return json.Unmarshal(data, g) }