device.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package models
  2. import (
  3. "IotAdmin/common/models"
  4. "IotAdmin/core/logger"
  5. "IotAdmin/iot/constant"
  6. iotStruct "IotAdmin/iot/struct"
  7. "database/sql"
  8. "strings"
  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:设备SN"`
  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 sql.NullTime `json:"timeOnline" gorm:"type:datetime;comment:上线时间"`
  29. TimeOffline sql.NullTime `json:"timeOffline" gorm:"type:datetime;comment:离线时间"`
  30. Group *IotGroup `json:"-"`
  31. GroupName string `json:"groupName" gorm:"-"`
  32. ReportConfig *[]iotStruct.ReportConfig `json:"reportConfig" gorm:"-"`
  33. Last *IotDevice `json:"-" gorm:"-"`
  34. models.ControlBy
  35. models.ModelTime
  36. }
  37. func (*IotDevice) TableName() string {
  38. return "iot_device"
  39. }
  40. func (e *IotDevice) Generate() models.ActiveRecord {
  41. o := *e
  42. return &o
  43. }
  44. func (e *IotDevice) GetId() interface{} {
  45. return e.Id
  46. }
  47. func (e *IotDevice) BeforeCreate(_ *gorm.DB) error {
  48. var err error
  49. e.OnlineStatus = constant.IotDeviceOffline
  50. return err
  51. }
  52. //
  53. //func (e *IotDevice) BeforeUpdate(_ *gorm.DB) error {
  54. // var err error
  55. //
  56. // return err
  57. //}
  58. func (e *IotDevice) AfterFind(_ *gorm.DB) error {
  59. if e.Group != nil {
  60. e.GroupName = e.Group.Name
  61. }
  62. if e.Type == constant.IotDeviceTypeGateway || e.Dsn == "" {
  63. return nil
  64. }
  65. configs := make([]iotStruct.ReportConfig, 0)
  66. arr := strings.Split(e.Dsn, constant.DsnSplitString)
  67. bmYzMap, err := (&iotStruct.BmYz{}).BuildBmYzArray(e.BmYz)
  68. if err != nil {
  69. logger.Error("BuildBmYzArray error:", err)
  70. return err
  71. }
  72. for _, v := range arr {
  73. cfg := iotStruct.ReportConfig{}
  74. arr := strings.Split(v, constant.DsnChildSplitString)
  75. if len(arr) < 4 {
  76. continue
  77. }
  78. cfg.Host = arr[0]
  79. cfg.Protocol = arr[1]
  80. cfg.ST = arr[2]
  81. cfg.MN = arr[3]
  82. if len(arr) == 6 {
  83. cfg.User = arr[4]
  84. cfg.Pwd = arr[5]
  85. }
  86. bmYz := (*bmYzMap)[cfg.Protocol]
  87. cfg.BmYz = &bmYz
  88. configs = append(configs, cfg)
  89. }
  90. e.ReportConfig = &configs
  91. return nil
  92. }
  93. func (e *IotDevice) ToDtuConfig(children *[]IotDevice) *iotStruct.DtuConfig {
  94. if len(*children) <= 0 {
  95. return nil
  96. }
  97. slaveConfigs := make([]iotStruct.SlaveConfig, 0)
  98. dtuConfig := &iotStruct.DtuConfig{
  99. Enable: true,
  100. Sn: e.Sn,
  101. Name: e.Name,
  102. Mode: e.Mode,
  103. Cycle: e.Cycle,
  104. SlaveConfig: &slaveConfigs,
  105. }
  106. slaveConfigs = make([]iotStruct.SlaveConfig, 0)
  107. for _, device := range *children {
  108. slaveConfig := iotStruct.SlaveConfig{
  109. No: device.Sn,
  110. Addr: device.Address,
  111. Protocol: device.Protocol,
  112. OtherConfig: device.OtherConfig,
  113. ReportConfig: device.ReportConfig,
  114. }
  115. slaveConfigs = append(slaveConfigs, slaveConfig)
  116. }
  117. dtuConfig.SlaveConfig = &slaveConfigs
  118. return dtuConfig
  119. }