|
|
@ -2,11 +2,15 @@ package common_utils |
|
|
|
|
|
|
|
import ( |
|
|
|
"context" |
|
|
|
"encoding/json" |
|
|
|
"gitea.anxinyun.cn/container/common_models/constant/settlementParam" |
|
|
|
"strings" |
|
|
|
|
|
|
|
//"encoding/json"
|
|
|
|
"errors" |
|
|
|
"fmt" |
|
|
|
"gitea.anxinyun.cn/container/common_models" |
|
|
|
"gitea.anxinyun.cn/container/common_models/constant/redisKey" |
|
|
|
json "github.com/bytedance/sonic" |
|
|
|
"github.com/eko/gocache/lib/v4/store" |
|
|
|
"log" |
|
|
|
"time" |
|
|
@ -23,6 +27,7 @@ var IotaDeviceCache map[string]common_models.IotaDevice |
|
|
|
var ThingStructCache map[string]common_models.ThingStruct |
|
|
|
var DeviceNodeCache map[string]common_models.IotaInstances |
|
|
|
var AlarmCodeCache map[string]common_models.AlarmCode |
|
|
|
var StructureCache map[int]common_models.Structure |
|
|
|
|
|
|
|
// var stationThreshold map[int]common_models.Threshold
|
|
|
|
// var aggThreshold map[string]common_models.AggThreshold
|
|
|
@ -62,6 +67,31 @@ func initDeviceInfoMapCache() { |
|
|
|
DeviceMetaCache = make(map[string]common_models.DeviceMeta) |
|
|
|
} |
|
|
|
|
|
|
|
if deviceStationIdsCache == nil { |
|
|
|
deviceStationIdsCache = make(map[string][]int) |
|
|
|
} |
|
|
|
|
|
|
|
if stationCache == nil { |
|
|
|
stationCache = make(map[int]common_models.Station) |
|
|
|
} |
|
|
|
|
|
|
|
if deviceFactorProtoMap == nil { |
|
|
|
deviceFactorProtoMap = make(map[string]common_models.DeviceFactorProto) |
|
|
|
} |
|
|
|
|
|
|
|
if FormulaCache == nil { |
|
|
|
FormulaCache = make(map[int]common_models.Formula) |
|
|
|
} |
|
|
|
|
|
|
|
if ProtoCache == nil { |
|
|
|
ProtoCache = make(map[string]common_models.Proto) |
|
|
|
} |
|
|
|
if AlarmCodeCache == nil { |
|
|
|
AlarmCodeCache = make(map[string]common_models.AlarmCode) |
|
|
|
} |
|
|
|
if StructureCache == nil { |
|
|
|
StructureCache = make(map[int]common_models.Structure) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// GetDeviceInfo 通过
|
|
|
@ -137,6 +167,9 @@ func (the *ConfigHelper) GetDeviceStationIds(deviceId string) ([]int, error) { |
|
|
|
k := fmt.Sprintf("%s:%s", redisKey.Device_stationIds, deviceId) |
|
|
|
//var deviceMeta common_models.DeviceMeta
|
|
|
|
s := the.redisHelper.Get(k) |
|
|
|
if s == "" { |
|
|
|
return result, errors.New(fmt.Sprintf("redis 中无key=[%s] 缓存", k)) |
|
|
|
} |
|
|
|
err = json.Unmarshal([]byte(s), &result) |
|
|
|
//err = the.redisHelper.GetObj(k, &result)
|
|
|
|
} |
|
|
@ -144,16 +177,21 @@ func (the *ConfigHelper) GetDeviceStationIds(deviceId string) ([]int, error) { |
|
|
|
} |
|
|
|
func (the *ConfigHelper) SetChainedCacheObj(k string, obj any) error { |
|
|
|
var value string |
|
|
|
if v, ok := obj.(string); !ok { |
|
|
|
v, err := json.Marshal(obj) |
|
|
|
var err error |
|
|
|
switch obj.(type) { |
|
|
|
case string: |
|
|
|
value = obj.(string) |
|
|
|
case []byte: |
|
|
|
value = string(obj.([]byte)) |
|
|
|
default: |
|
|
|
bs, err := json.Marshal(obj) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
value = string(v) |
|
|
|
} else { |
|
|
|
value = v |
|
|
|
value = string(bs) |
|
|
|
} |
|
|
|
err := the.chainedCache.LoadableChinCache.Set(the.ctx, k, value) |
|
|
|
|
|
|
|
err = the.chainedCache.LoadableChinCache.Set(the.ctx, k, value) |
|
|
|
return err |
|
|
|
} |
|
|
|
func (the *ConfigHelper) SetChainedCacheObjWithExpiration(k string, obj any, duration time.Duration) error { |
|
|
@ -170,6 +208,10 @@ func (the *ConfigHelper) SetChainedCacheObjWithExpiration(k string, obj any, dur |
|
|
|
err := the.chainedCache.LoadableChinCache.Set(the.ctx, k, value, store.WithExpiration(duration)) |
|
|
|
return err |
|
|
|
} |
|
|
|
func (the *ConfigHelper) DeleteChainedCacheObj(k string) error { |
|
|
|
err := the.chainedCache.LoadableChinCache.Delete(the.ctx, k) |
|
|
|
return err |
|
|
|
} |
|
|
|
func (the *ConfigHelper) GetCacheWindowObj(key_cacheWindow string) (common_models.CacheWindow, error) { |
|
|
|
var redisCacheWin common_models.CacheWinSave |
|
|
|
value, err := the.chainedCache.LoadableChinCache.Get(the.ctx, key_cacheWindow) |
|
|
@ -205,9 +247,13 @@ func (the *ConfigHelper) GetDeviceStationObjs(deviceId string) ([]common_models. |
|
|
|
//err = the.redisHelper.GetObj(k, &result)
|
|
|
|
value, err := the.chainedCache.LoadableChinCache.Get(the.ctx, k) |
|
|
|
if v, ok := value.(string); ok { |
|
|
|
if v == "" { |
|
|
|
log.Printf("LoadableChinCache[%s]=空", k) |
|
|
|
return result, err |
|
|
|
} |
|
|
|
err = json.Unmarshal([]byte(v), &result) |
|
|
|
if err != nil { |
|
|
|
log.Printf("json unmarshal error:%s \n", err.Error()) |
|
|
|
log.Printf("json unmarshal error:%s", err.Error()) |
|
|
|
} |
|
|
|
} |
|
|
|
return result, err |
|
|
@ -216,7 +262,11 @@ func (the *ConfigHelper) SetDeviceStationObjs(deviceId string, stations common_m |
|
|
|
var err error |
|
|
|
k := fmt.Sprintf("%s:%s", redisKey.Device_stationObjs, deviceId) |
|
|
|
|
|
|
|
err = the.SetChainedCacheObj(k, &stations) |
|
|
|
bytes, err := json.Marshal(stations) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
go the.SetChainedCacheObj(k, bytes) |
|
|
|
|
|
|
|
return err |
|
|
|
} |
|
|
@ -291,23 +341,78 @@ func (the *ConfigHelper) SetStationGroup(groupId int, group common_models.Statio |
|
|
|
k := fmt.Sprintf("%s:%d", redisKey.Group, groupId) |
|
|
|
return the.chainedCache.LoadableChinCache.Set(the.ctx, k, &group) |
|
|
|
} |
|
|
|
|
|
|
|
// 最大级联层级
|
|
|
|
const max_corr_depth int = 15 |
|
|
|
|
|
|
|
// 关联分组(e.g 沉降级联)
|
|
|
|
// depth: 递归查询深度(防止错误配置导致基点互相参考;亦限制级联的最大层级)
|
|
|
|
func (the *ConfigHelper) corrGroups(gp common_models.StationGroup, depth int) { |
|
|
|
if gp.GroupType == "202" && gp.Params != nil && len(gp.Params) == 2 { |
|
|
|
ref_base_stationId, ok := gp.Params[settlementParam.Ref_base].(float64) |
|
|
|
if !ok { |
|
|
|
log.Println("ref_base 转换异常。", gp.Params[settlementParam.Ref_base]) |
|
|
|
return |
|
|
|
} |
|
|
|
ref_point_stationId, ok := gp.Params[settlementParam.Ref_point].(float64) |
|
|
|
if !ok { |
|
|
|
log.Println("ref_base 转换异常。", gp.Params[settlementParam.Ref_point]) |
|
|
|
return |
|
|
|
} |
|
|
|
basePtr := gp.GetSettlementBaseItem() |
|
|
|
if basePtr == nil { |
|
|
|
log.Println("无基点。", gp.R()) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
sg, err := the.GetStationGroupInfo(int(ref_base_stationId)) |
|
|
|
subBase := common_models.GroupItem{ |
|
|
|
StationId: int(ref_base_stationId), |
|
|
|
ParamsValue: map[string]interface{}{"base": true}, |
|
|
|
SubItems: nil, |
|
|
|
} |
|
|
|
if err == nil { |
|
|
|
refGroup, err := the.GetStationGroup(sg.GroupId) |
|
|
|
if err == nil { |
|
|
|
if depth+1 <= max_corr_depth { |
|
|
|
the.corrGroups(refGroup, depth+1) |
|
|
|
} |
|
|
|
} |
|
|
|
subBase = *refGroup.GetItem(int(ref_base_stationId)) |
|
|
|
} |
|
|
|
|
|
|
|
subPoint := common_models.GroupItem{ |
|
|
|
StationId: int(ref_point_stationId), |
|
|
|
ParamsValue: map[string]interface{}{"base": false}, |
|
|
|
SubItems: nil, |
|
|
|
} |
|
|
|
|
|
|
|
basePtr.SubItems = map[string]common_models.GroupItem{ |
|
|
|
settlementParam.Ref_base: subBase, |
|
|
|
settlementParam.Ref_point: subPoint, |
|
|
|
} |
|
|
|
|
|
|
|
//log.Println(basePtr)
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func (the *ConfigHelper) GetStationGroup(groupId int) (common_models.StationGroup, error) { |
|
|
|
var result common_models.StationGroup |
|
|
|
var group common_models.StationGroup |
|
|
|
// group:35
|
|
|
|
k := fmt.Sprintf("%s:%d", redisKey.Group, groupId) |
|
|
|
value, err := the.chainedCache.LoadableChinCache.Get(the.ctx, k) |
|
|
|
if v, ok := value.(string); ok { |
|
|
|
if v == "" { |
|
|
|
err = errors.New("无测点group 数据") |
|
|
|
return result, err |
|
|
|
return group, err |
|
|
|
} |
|
|
|
err = json.Unmarshal([]byte(v), &result) |
|
|
|
err = json.Unmarshal([]byte(v), &group) |
|
|
|
if err != nil { |
|
|
|
log.Printf("json unmarshal error:%s \n", err.Error()) |
|
|
|
log.Printf("err => v=%s", v) |
|
|
|
} |
|
|
|
} |
|
|
|
return result, err |
|
|
|
|
|
|
|
the.corrGroups(group, 0) |
|
|
|
return group, err |
|
|
|
} |
|
|
|
|
|
|
|
// RedisKey.station_group
|
|
|
@ -315,6 +420,7 @@ func (the *ConfigHelper) SetStationGroupInfo(stationId int, info common_models.S |
|
|
|
k := fmt.Sprintf("%s:%d", redisKey.Station_group, stationId) |
|
|
|
return the.chainedCache.LoadableChinCache.Set(the.ctx, k, &info) |
|
|
|
} |
|
|
|
|
|
|
|
func (the *ConfigHelper) GetStationGroupInfo(stationId int) (common_models.StationGroupInfo, error) { |
|
|
|
// sg:193
|
|
|
|
k := fmt.Sprintf("%s:%d", redisKey.Station_group, stationId) |
|
|
@ -473,15 +579,29 @@ func (the *ConfigHelper) GetFilter(stationId int) (common_models.Filter, error) |
|
|
|
|
|
|
|
return result, err |
|
|
|
} |
|
|
|
func (the *ConfigHelper) GetFilterItem(stationId int, item string) (common_models.FilterItem, error) { |
|
|
|
|
|
|
|
result, err := the.GetFilter(stationId) |
|
|
|
|
|
|
|
if err == nil { |
|
|
|
for _, filterItem := range result.Items { |
|
|
|
if filterItem.FieldName == item { |
|
|
|
return filterItem, err |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
log.Printf("获取GetFilterItem err=%s", err.Error()) |
|
|
|
} |
|
|
|
|
|
|
|
return common_models.FilterItem{}, err |
|
|
|
} |
|
|
|
func (the *ConfigHelper) GetAlarmCode(alarmCode string) (common_models.AlarmCode, error) { |
|
|
|
//var iotaDevice common_models.IotaDevice
|
|
|
|
var err error |
|
|
|
result, ok := AlarmCodeCache[alarmCode] |
|
|
|
if !ok { //去redis查询
|
|
|
|
//thing_struct:5da9aa1b-05b7-4943-be57-dedb34f7a1bd
|
|
|
|
k := fmt.Sprintf("%s:%s", redisKey.Alarm_code, alarmCode) |
|
|
|
|
|
|
|
err = the.redisHelper.GetObj(k, &result) |
|
|
|
AlarmCodeCache[alarmCode] = result |
|
|
|
} |
|
|
|
return result, err |
|
|
|
} |
|
|
@ -497,9 +617,10 @@ func (the *ConfigHelper) GetIotaScheme(dimensionId string) (common_models.IotaSc |
|
|
|
var scheme common_models.IotaScheme |
|
|
|
value, err := the.chainedCache.LoadableChinCache.Get(the.ctx, k) |
|
|
|
if v, ok := value.(string); ok { |
|
|
|
err = json.Unmarshal([]byte(v), &scheme) |
|
|
|
formattedStr := strings.Replace(v, "+0800", "+08:00", -1) |
|
|
|
err = json.Unmarshal([]byte(formattedStr), &scheme) |
|
|
|
if err != nil { |
|
|
|
log.Printf("json unmarshal error:%s \n", err.Error()) |
|
|
|
log.Printf("【GetIotaScheme】json unmarshal error:%s \n", err.Error()) |
|
|
|
} |
|
|
|
} |
|
|
|
return scheme, err |
|
|
@ -578,3 +699,17 @@ func (the *ConfigHelper) mapToTree(source map[string]common_models.IotaInstance, |
|
|
|
} |
|
|
|
return deviceNode |
|
|
|
} |
|
|
|
|
|
|
|
func (the *ConfigHelper) GetStructure(structId int) (common_models.Structure, error) { |
|
|
|
var result common_models.Structure |
|
|
|
// structure:1
|
|
|
|
k := fmt.Sprintf("%s:%d", redisKey.Structure, structId) |
|
|
|
value, err := the.chainedCache.LoadableChinCache.Get(the.ctx, k) |
|
|
|
if v, ok := value.(string); ok { |
|
|
|
err = json.Unmarshal([]byte(v), &result) |
|
|
|
if err != nil { |
|
|
|
log.Printf("json unmarshal error:%s \n", err.Error()) |
|
|
|
} |
|
|
|
} |
|
|
|
return result, err |
|
|
|
} |
|
|
|