Compare commits

...

10 Commits
v0.0.7 ... dev

  1. 20
      IotaData.go
  2. 12
      README.md
  3. 34
      aggData.go
  4. 32
      cacheData.go
  5. 12
      constant/groupType/groupType.go
  6. 12
      dataTrace.go
  7. 46
      esGroupTheme.go
  8. 18
      esTheme.go
  9. 12
      factor.go
  10. 2
      go.mod
  11. 5
      go.sum
  12. 10
      rpc_node.go
  13. 91
      staionGroup.go
  14. 46
      station.go

20
IotaData.go

@ -54,3 +54,23 @@ type Data struct {
func (the *Data) Success() bool { func (the *Data) Success() bool {
return the.Result.Code == 0 return the.Result.Code == 0
} }
func (the *IotaData) T() time.Time {
return the.TriggerTime
}
func (the *IotaData) Q() string {
return fmt.Sprintf("iotaData:%s", the.DeviceId)
}
func (the *IotaData) R() string {
return fmt.Sprintf("[ThingId:%s] [DeviceId:%s]", the.ThingId, the.DeviceId)
}
func (the *IotaData) GetThingId() string {
return the.ThingId
}
func (the *IotaData) GetDeviceId() string {
return the.DeviceId
}

12
README.md

@ -1,26 +1,26 @@
# common_models 通用结构体模块 # common_models 通用结构体模块
### 开发语言和版本 ### 开发语言和版本
golang ,版本 go1.22.0 golang ,版本 go1.23.1
### 平台支持 ### 平台支持
安心云4.0 安心云4.0
### 使用方式: ### 使用方式:
1. 设置 go 环境 1. 设置 go 环境
set GOPRIVATE=gitea.ngaiot.com set GOPRIVATE=gitea.anxinyun.cn
2. 查询go 版本 2. 查询go 版本
go list -m -versions gitea.anxinyun.cn/container/common_models go list -m -versions gitea.anxinyun.cn/container/common_models
如若 GOPRIVATE=gitea.ngaiot.com 生效,则会返回 common_models 的版本信息。 如若 GOPRIVATE=gitea.anxinyun.cn 生效,则会返回 common_models 的版本信息。
如若无版本信息返回,有可能是没有 DevOps 的权限,也有可能环境变量设置没有生效。 如若无版本信息返回,有可能是没有 container 的权限,也有可能环境变量设置没有生效。
异常情况处理: 异常情况处理:
1. 无 https://gitea.anxinyun.cn/container 访问权限,可以联系下管理员。 1. 无 https://gitea.anxinyun.cn/container 访问权限,可以联系下管理员(刘歆毅)
2. 环境变量设置不生效 2. 环境变量设置不生效
解决方法,在GoLand工具中进行设置: 解决方法,在GoLand工具中进行设置:
1)打开 Settings 1)打开 Settings
2)Go > GOROOT,Download Go SDK 2)Go > GOROOT,Download Go SDK
3)Go > Go Modules,设置环境变量 GOPRIVATE=gitea.ngaiot.com 3)Go > Go Modules,设置环境变量 GOPRIVATE=gitea.anxinyun.cn
### 依赖包管理 ### 依赖包管理
common_models 如若有修改,必须需要同步升级 common_utils、et-go common_models 如若有修改,必须需要同步升级 common_utils、et-go

34
aggData.go

@ -14,18 +14,28 @@ type AggData struct {
AggMethodId int // 聚集方法 : 平均值/最大值/最小值 AggMethodId int // 聚集方法 : 平均值/最大值/最小值
Agg map[string]float64 // 聚集数据 Agg map[string]float64 // 聚集数据
Changed map[string]float64 // 变化量 Changed map[string]float64 // 变化量
ThingId string
} }
func (a *AggData) _t() time.Time { func (the *AggData) T() time.Time {
return a.Date return the.Date
} }
func (a *AggData) _q() string { func (the *AggData) Q() string {
return fmt.Sprintf("agg:%d", a.SensorId) return fmt.Sprintf("aggData:%d", the.SensorId)
} }
func (a *AggData) _r() string { func (the *AggData) R() string {
return fmt.Sprintf("[Structure:%d] [Station:%d]", a.StructId, a.SensorId) return fmt.Sprintf("[Structure:%d][Station:%d]", the.StructId, the.SensorId)
}
func (the *AggData) GetThingId() string {
return the.ThingId
}
// GetDeviceId 返回SensorId
func (the *AggData) GetDeviceId() string {
return fmt.Sprintf("%d", the.SensorId)
} }
var typeDict = map[int]string{ var typeDict = map[int]string{
@ -46,9 +56,9 @@ var typeDict = map[int]string{
3005: "sum", 3005: "sum",
} }
func GetAggTypeName(t int) string { //func GetAggTypeName(t int) string {
if val, ok := typeDict[t]; ok { // if val, ok := typeDict[t]; ok {
return val // return val
} // }
return "" // return ""
} //}

32
cacheData.go

@ -27,13 +27,14 @@ type expirationInfo struct {
// CacheWindow // CacheWindow
// 存储大量测点缓存数据 // 存储大量测点缓存数据
type CacheWindow struct { type CacheWindow struct {
Id string Id string
windowLen int //窗体长度 windowLen int //窗体长度
WindowSize int //窗体大小 WindowSize int //窗体大小
MethodId int //滑窗方法 MethodId int //滑窗方法
LatestData any CreatReason string //创建原因
ring *ring.Ring LatestData any
Params FilterParams ring *ring.Ring
Params FilterParams
//过期控制 //过期控制
Expire expirationInfo Expire expirationInfo
} }
@ -74,17 +75,18 @@ func (c *CacheWindow) MarshalBinary() (data []byte, err error) {
func (c *CacheWindow) UnmarshalBinary(data []byte) error { func (c *CacheWindow) UnmarshalBinary(data []byte) error {
return json.Unmarshal(data, c) return json.Unmarshal(data, c)
} }
func NewCacheWindow(id string, size, methodId int, params FilterParams) CacheWindow { func NewCacheWindow(id string, size, methodId int, params FilterParams, creatReason string) CacheWindow {
limit := 10 limit := 10
if size > limit { if size > limit {
size = 10 size = 10
} }
return CacheWindow{ return CacheWindow{
Id: id, Id: id,
WindowSize: size, WindowSize: size,
MethodId: methodId, MethodId: methodId,
ring: ring.New(size), ring: ring.New(size),
Params: params, Params: params,
CreatReason: creatReason,
Expire: expirationInfo{ Expire: expirationInfo{
UpdateTime: time.Now(), UpdateTime: time.Now(),
Duration: 60 * 2, Duration: 60 * 2,
@ -115,7 +117,9 @@ func (c *CacheWindow) EnQueue(d any) {
c.LatestData = d c.LatestData = d
c.ring.Value = d c.ring.Value = d
c.ring = c.ring.Next() c.ring = c.ring.Next()
log.Printf("[%s]测点缓存[m=%d] %d/%d", c.Id, c.MethodId, c.WindowSize, c.ring.Len()) if c.MethodId != 0 {
log.Printf("[%s]测点缓存[m=%d] %d/%d", c.Id, c.MethodId, c.WindowSize, c.ring.Len())
}
} }
func (c *CacheWindow) EnQueueAnalyzeData(d AnalyzeData) { func (c *CacheWindow) EnQueueAnalyzeData(d AnalyzeData) {
if c.windowLen < c.WindowSize { if c.windowLen < c.WindowSize {

12
constant/groupType/groupType.go

@ -0,0 +1,12 @@
package groupType
const (
// 深部位移
DeepDisplace = "201"
// 液压传感器测量沉降
Settlement = "202"
// 测斜测沉降或挠度
AngleToSettlement = "203"
)

12
dataTrace.go

@ -2,13 +2,17 @@ package common_models
import "time" import "time"
type DataTrace interface { type IDataTrace interface {
// 数据时间 // 数据时间
_t() time.Time T() time.Time
// 定位标记 // 定位标记
_q() string Q() string
// 定位标记 // 定位标记
_r() string R() string
GetThingId() string
GetDeviceId() string
} }

46
esGroupTheme.go

@ -1,17 +1,41 @@
package common_models package common_models
import "time" import (
"encoding/json"
"time"
)
// EsGroupTheme 分组主题数据结构体 // EsGroupTheme 分组主题数据结构体
type EsGroupTheme struct { type EsGroupTheme struct {
Structure int `json:"structure"` StructId int `json:"struct_id"`
GroupId int `json:"group_id"` GroupId int `json:"group_id"`
GroupName string `json:"group_name"` GroupName string `json:"group_name"`
Factor int `json:"factor"` GroupType string `json:"group_type"`
FactorName string `json:"factor_name"` TaskId string `json:"task_id"`
FactorProtoCode string `json:"factor_proto_code"` CorrItems []StationGroupInfo `json:"corr_items"`
FactorProtoName string `json:"factor_proto_name"` Data []CorrItemData `json:"data"`
Data map[string]any `json:"data"` CollectTime time.Time `json:"collect_time"`
CollectTime time.Time `json:"collect_time"` CreateTime time.Time `json:"create_time"`
CreateTime time.Time `json:"create_time"` }
func (the EsGroupTheme) MarshalJSON() ([]byte, error) {
type Alias EsGroupTheme
return json.Marshal(&struct {
CollectTime string `json:"collect_time"`
CreateTime string `json:"create_time"`
*Alias
}{
CollectTime: the.CollectTime.Format("2006-01-02T15:04:05.000+08:00"),
CreateTime: the.CreateTime.Format("2006-01-02T15:04:05.000+08:00"),
Alias: (*Alias)(&the),
})
}
// 级联测点数据
type CorrItemData struct {
StationId int `json:"station_id"`
StationName string `json:"station_name"`
IsBase *bool `json:"is_base,omitempty"`
PhyData map[string]interface{} `json:"phy_data"`
ThemeData map[string]interface{} `json:"theme_data"`
} }

18
esTheme.go

@ -1,6 +1,9 @@
package common_models package common_models
import "time" import (
"encoding/json"
"time"
)
type EsTheme struct { type EsTheme struct {
SensorName string `json:"sensor_name"` SensorName string `json:"sensor_name"`
@ -16,6 +19,19 @@ type EsTheme struct {
CreateTime time.Time `json:"create_time"` CreateTime time.Time `json:"create_time"`
} }
func (the EsTheme) MarshalJSON() ([]byte, error) {
type Alias EsTheme
return json.Marshal(&struct {
CollectTime string `json:"collect_time"`
CreateTime string `json:"create_time"`
*Alias
}{
CollectTime: the.CollectTime.Format("2006-01-02T15:04:05.000+08:00"),
CreateTime: the.CreateTime.Format("2006-01-02T15:04:05.000+08:00"),
Alias: (*Alias)(&the),
})
}
type EsThemeResp struct { type EsThemeResp struct {
Took int `json:"took"` Took int `json:"took"`
TimedOut bool `json:"timed_out"` TimedOut bool `json:"timed_out"`

12
factor.go

@ -15,13 +15,13 @@ type Factor struct {
} }
// redis序列化 // redis序列化
func (p *Factor) MarshalBinary() (data []byte, err error) { func (f *Factor) MarshalBinary() (data []byte, err error) {
return json.Marshal(p) return json.Marshal(f)
} }
// redis序列化 // redis序列化
func (p *Factor) UnmarshalBinary(data []byte) error { func (f *Factor) UnmarshalBinary(data []byte) error {
return json.Unmarshal(data, p) return json.Unmarshal(data, f)
} }
type ProtoItem struct { type ProtoItem struct {
@ -58,6 +58,10 @@ func (f *Factor) GetFieldNames() []string {
} }
func (p *Proto) GetProtoItem(fieldName string) *ProtoItem { func (p *Proto) GetProtoItem(fieldName string) *ProtoItem {
if fieldName == "" {
return &ProtoItem{}
}
for _, item := range p.Items { for _, item := range p.Items {
if item.FieldName == fieldName { if item.FieldName == fieldName {
return &item return &item

2
go.mod

@ -1,6 +1,6 @@
module gitea.anxinyun.cn/container/common_models module gitea.anxinyun.cn/container/common_models
go 1.22.0 go 1.23.1
require github.com/stretchr/testify v1.9.0 require github.com/stretchr/testify v1.9.0

5
go.sum

@ -1,5 +1,10 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

10
rpc_node.go

@ -8,3 +8,13 @@ type NodeArgs struct {
Addr string // 网络地址 Addr string // 网络地址
ThingIds []string // 处理的thingId ThingIds []string // 处理的thingId
} }
// RpcReply RPC方法的回复对象
type RpcReply struct {
Result bool
Message string
}
type IRpcData interface {
GetThingId() string
}

91
staionGroup.go

@ -10,38 +10,38 @@ import (
"strconv" "strconv"
) )
var group36 = ` //var group36 = `
RedisKey = group:36 的值 //RedisKey = group:36 的值:
{ //{
"id": 36, // "id": 36,
"name": "上游沉降", // "name": "上游沉降",
"group_type": "202", // "group_type": "202",
"items": [ // "items": [
{ // {
"sensor": 26, // "sensor": 26,
"params_value": { // "params_value": {
"base": false // "base": false
} // }
}, // },
{ // {
"sensor": 43, // "sensor": 43,
"params_value": { // "params_value": {
"base": false // "base": false
} // }
}, // },
{ // {
"sensor": 192, // "sensor": 192,
"params_value": { // "params_value": {
"base": true // "base": true
} // }
} // }
], // ],
"params": { // "params": {
"ref_base": 193, // "ref_base": 193,
"ref_point": 23 // "ref_point": 23
} // }
} //}
` //`
type StationGroup struct { type StationGroup struct {
Id int `json:"id"` Id int `json:"id"`
@ -62,9 +62,18 @@ func (g *StationGroup) AllCorrItems() []GroupItem {
// GetSettlementBaseItem 获取沉降分组的基点 // GetSettlementBaseItem 获取沉降分组的基点
func (g *StationGroup) GetSettlementBaseItem() *GroupItem { func (g *StationGroup) GetSettlementBaseItem() *GroupItem {
for _, item := range g.Items { for i, item := range g.Items {
if item.ParamsValue[settlementParam.Base] == true { if item.ParamsValue[settlementParam.Base] == true {
return &item 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 return nil
@ -91,8 +100,8 @@ type GroupItem struct {
} }
// GetDoubleParam 返回param的float64值 // GetDoubleParam 返回param的float64值
func (g *GroupItem) GetDoubleParam(key string) (float64, error) { func (gi *GroupItem) GetDoubleParam(key string) (float64, error) {
value := g.ParamsValue[key] value := gi.ParamsValue[key]
switch v := value.(type) { switch v := value.(type) {
case int: case int:
return float64(v), nil return float64(v), nil
@ -113,16 +122,16 @@ func (g *GroupItem) GetDoubleParam(key string) (float64, error) {
} }
// CorrItems 基点的级联参考基点和参考测点 // CorrItems 基点的级联参考基点和参考测点
func (g *GroupItem) CorrItems() []GroupItem { func (gi *GroupItem) CorrItems() []GroupItem {
if g.SubItems == nil { if gi.SubItems == nil {
return []GroupItem{*g} return []GroupItem{*gi}
} }
var result []GroupItem var result []GroupItem
for _, item := range g.SubItems { for _, item := range gi.SubItems {
result = append(result, item.CorrItems()...) result = append(result, item.CorrItems()...)
} }
result = append(result, *g) result = append(result, *gi)
return result return result
} }

46
station.go

@ -9,19 +9,19 @@ import (
"time" "time"
) )
type mStation struct { //type mStation struct {
Name string // Name string
Id int // Id int
Structure Structure // Structure Structure
Factor Factor // Factor Factor
ManualData bool `json:"manual_data"` // ManualData bool `json:"manual_data"`
Params map[string]any // Params map[string]any
Group StationGroup // Group StationGroup
DeviceProto DeviceProto // DeviceProto DeviceProto
CorrGroups []StationGroup //关联的分组(沉降级联) // CorrGroups []StationGroup //关联的分组(沉降级联)
Labels string // Labels string
//CombineInfo // 测点数据组装信息 // //CombineInfo // 测点数据组装信息
} //}
// Station 测点模型 = 基本信息 + 数据 + 阈值 // Station 测点模型 = 基本信息 + 数据 + 阈值
type Station struct { type Station struct {
@ -37,7 +37,7 @@ type StationData struct {
DeviceCalcData map[string]any DeviceCalcData map[string]any
//测点最终数据(主题数据) //测点最终数据(主题数据)
ThemeData map[string]any ThemeData map[string]any
PyhData map[string]any PhyData map[string]any
CollectTime time.Time CollectTime time.Time
AlarmLevel int AlarmLevel int
} }
@ -52,9 +52,9 @@ func (s *StationData) GetThemeFields() []string {
} }
// GetProtoFields 获取监测原型的监测项 // GetProtoFields 获取监测原型的监测项
func (s *Station) GetProtoFields() []string { func (the *Station) GetProtoFields() []string {
var fields []string var fields []string
for _, protoItem := range s.Info.Factor.Items { for _, protoItem := range the.Info.Factor.Items {
fields = append(fields, protoItem.FieldName) fields = append(fields, protoItem.FieldName)
} }
return fields return fields
@ -86,11 +86,11 @@ func (s *StationData) GetValidThemeData() (map[string]float64, bool) {
return filteredData, len(filteredData) > 0 return filteredData, len(filteredData) > 0
} }
func (s *Station) LogMsg() string { func (the *Station) LogMsg() string {
logTagThing := fmt.Sprintf("[%s:%s][%s:%s]", logTag.Thing, s.Info.ThingId, logTag.Station, s.Info.Id) logTagThing := fmt.Sprintf("[%s:%s][%s:%d]", logTag.Thing, the.Info.ThingId, logTag.Station, the.Info.Id)
var deviceLogs []string var deviceLogs []string
for _, device := range s.Info.Devices { for _, device := range the.Info.Devices {
deviceLogs = append(deviceLogs, device.LogMsg()) deviceLogs = append(deviceLogs, device.LogMsg())
} }
deviceLogStr := strings.Join(deviceLogs, ",") deviceLogStr := strings.Join(deviceLogs, ",")
@ -99,13 +99,13 @@ func (s *Station) LogMsg() string {
} }
// redis序列化 // redis序列化
func (m *Station) MarshalBinary() (data []byte, err error) { func (the *Station) MarshalBinary() (data []byte, err error) {
return json.Marshal(m) return json.Marshal(the)
} }
// redis序列化 // redis序列化
func (m *Station) UnmarshalBinary(data []byte) error { func (the *Station) UnmarshalBinary(data []byte) error {
return json.Unmarshal(data, m) return json.Unmarshal(data, the)
} }

Loading…
Cancel
Save