device.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package models
  2. import (
  3. "IotAdmin/common/models"
  4. "IotAdmin/core/logger"
  5. "IotAdmin/iot/constant"
  6. iotStruct "IotAdmin/iot/struct"
  7. "strings"
  8. "time"
  9. "gorm.io/gorm"
  10. )
  11. // IotDevice 设备实体
  12. type IotDevice struct {
  13. models.Model
  14. ParentId int `json:"parentId" gorm:"rel(fk);type:bigint(20);comment:父ID"`
  15. GroupId int `json:"groupId" gorm:"type:bigint(20);comment:分组ID"`
  16. Sn string `json:"sn" gorm:"type:varchar(50);comment:设备编码"`
  17. Name string `json:"name" gorm:"type:varchar(50);comment:设备名称"`
  18. Type int `json:"type" gorm:"type:bigint(20);comment:设备类型 1:网关 2:表计"`
  19. Mode int `json:"mode" gorm:"type:bigint(20);comment:设备模式 1:下发指令查询 2:表计主动上报"`
  20. Cycle int `json:"cycle" gorm:"type:bigint(20);comment:上报周期"`
  21. Dsn string `json:"dsn" gorm:"type:text;comment:设备Dsn"`
  22. Description string `json:"description" gorm:"type:varchar(255);comment:设备描述"`
  23. Protocol string `json:"protocol" gorm:"type:varchar(50);comment:表计协议"`
  24. Address int `json:"address" gorm:"type:bigint(20);comment:表计地址"`
  25. BmYz string `json:"bmYz" gorm:"type:text;comment:编码因子"`
  26. OtherConfig string `json:"otherConfig" gorm:"type:text;comment:其他配置"`
  27. OnlineStatus int `json:"onlineStatus" gorm:"type:tinyint(1);comment:在线状态"`
  28. TimeOnline time.Time `json:"timeOnline" gorm:"type:datetime;comment:上线时间"`
  29. TimeOffline time.Time `json:"timeOffline" gorm:"type:datetime;comment:离线时间"`
  30. GroupName string `json:"groupName" `
  31. ReportConfig *[]iotStruct.ReportConfig `json:"reportConfigs" gorm:"-"`
  32. models.ControlBy
  33. models.ModelTime
  34. }
  35. func (*IotDevice) TableName() string {
  36. return "iot_device"
  37. }
  38. func (e *IotDevice) Generate() models.ActiveRecord {
  39. o := *e
  40. return &o
  41. }
  42. func (e *IotDevice) GetId() interface{} {
  43. return e.Id
  44. }
  45. func (e *IotDevice) BeforeCreate(_ *gorm.DB) error {
  46. var err error
  47. e.OnlineStatus = constant.IotDeviceOffline
  48. return err
  49. }
  50. //
  51. //func (e *IotDevice) BeforeUpdate(_ *gorm.DB) error {
  52. // var err error
  53. //
  54. // return err
  55. //}
  56. func (e *IotDevice) AfterFind(_ *gorm.DB) error {
  57. if e.Type == constant.IotDeviceTypeGateway || e.Dsn == "" {
  58. return nil
  59. }
  60. configs := make([]iotStruct.ReportConfig, 0)
  61. arr := strings.Split(e.Dsn, constant.DsnSplitString)
  62. bmYzMap, err := (&iotStruct.BmYz{}).BuildBmYzArray(e.BmYz)
  63. if err != nil {
  64. logger.Error("BuildBmYzArray error:", err)
  65. return err
  66. }
  67. for _, v := range arr {
  68. cfg := iotStruct.ReportConfig{}
  69. arr := strings.Split(v, constant.DsnChildSplitString)
  70. if len(arr) < 4 {
  71. continue
  72. }
  73. cfg.Host = arr[0]
  74. cfg.Protocol = arr[1]
  75. cfg.ST = arr[2]
  76. cfg.MN = arr[3]
  77. if len(arr) == 6 {
  78. cfg.User = arr[4]
  79. cfg.Pwd = arr[5]
  80. }
  81. bmYz := (*bmYzMap)[cfg.Protocol]
  82. cfg.BmYz = &bmYz
  83. configs = append(configs, cfg)
  84. }
  85. e.ReportConfig = &configs
  86. return nil
  87. }
  88. func (e *IotDevice) ToDtuConfig(children *[]IotDevice) *iotStruct.DtuConfig {
  89. if len(*children) <= 0 {
  90. return nil
  91. }
  92. slaveConfigs := make([]iotStruct.SlaveConfig, 0)
  93. dtuConfig := &iotStruct.DtuConfig{
  94. Enable: true,
  95. Sn: e.Sn,
  96. Name: e.Name,
  97. Mode: e.Mode,
  98. Cycle: e.Cycle,
  99. SlaveConfig: &slaveConfigs,
  100. }
  101. slaveConfigs = make([]iotStruct.SlaveConfig, 0)
  102. for _, device := range *children {
  103. slaveConfig := iotStruct.SlaveConfig{
  104. No: device.Sn,
  105. Addr: device.Address,
  106. Protocol: device.Protocol,
  107. OtherConfig: device.OtherConfig,
  108. ReportConfig: device.ReportConfig,
  109. }
  110. slaveConfigs = append(slaveConfigs, slaveConfig)
  111. }
  112. dtuConfig.SlaveConfig = &slaveConfigs
  113. return dtuConfig
  114. }