device.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. Status int `json:"status" gorm:"type:tinyint(1);comment:启用状态"`
  22. Dsn string `json:"dsn" gorm:"type:text;comment:设备Dsn"`
  23. Description string `json:"description" gorm:"type:varchar(255);comment:设备描述"`
  24. Protocol string `json:"protocol" gorm:"type:varchar(50);comment:表计协议"`
  25. Address int `json:"address" gorm:"type:bigint(20);comment:表计地址"`
  26. BmYz string `json:"bmYz" gorm:"type:text;comment:编码因子"`
  27. OtherConfig string `json:"otherConfig" gorm:"type:text;comment:其他配置"`
  28. OnlineStatus int `json:"onlineStatus" gorm:"type:tinyint(1);comment:在线状态"`
  29. TimeOnline sql.NullTime `json:"timeOnline" gorm:"type:datetime;comment:上线时间"`
  30. TimeOffline sql.NullTime `json:"timeOffline" gorm:"type:datetime;comment:离线时间"`
  31. Group *IotGroup `json:"-"`
  32. GroupName string `json:"groupName" gorm:"-"`
  33. ReportConfig *[]iotStruct.ReportConfig `json:"reportConfig" gorm:"-"`
  34. Last *IotDevice `json:"-" gorm:"-"`
  35. models.ControlBy
  36. models.ModelTime
  37. }
  38. func (*IotDevice) TableName() string {
  39. return "iot_device"
  40. }
  41. func (e *IotDevice) Generate() models.ActiveRecord {
  42. o := *e
  43. return &o
  44. }
  45. func (e *IotDevice) GetId() interface{} {
  46. return e.Id
  47. }
  48. func (e *IotDevice) BeforeCreate(_ *gorm.DB) error {
  49. var err error
  50. e.OnlineStatus = constant.IotDeviceOffline
  51. return err
  52. }
  53. //
  54. //func (e *IotDevice) BeforeUpdate(_ *gorm.DB) error {
  55. // var err error
  56. //
  57. // return err
  58. //}
  59. func (e *IotDevice) AfterFind(_ *gorm.DB) error {
  60. if e.Group != nil {
  61. e.GroupName = e.Group.Name
  62. }
  63. if e.Type == constant.IotDeviceTypeGateway || e.Dsn == "" {
  64. return nil
  65. }
  66. configs := make([]iotStruct.ReportConfig, 0)
  67. arr := strings.Split(e.Dsn, constant.DsnSplitString)
  68. bmYzMap, err := (&iotStruct.BmYz{}).BuildBmYzArray(e.BmYz)
  69. if err != nil {
  70. logger.Error("BuildBmYzArray error:", err)
  71. return err
  72. }
  73. for _, v := range arr {
  74. cfg := iotStruct.ReportConfig{}
  75. arr := strings.Split(v, constant.DsnChildSplitString)
  76. if len(arr) < 4 {
  77. continue
  78. }
  79. cfg.Host = arr[0]
  80. cfg.Protocol = arr[1]
  81. cfg.ST = arr[2]
  82. cfg.MN = arr[3]
  83. if len(arr) == 6 {
  84. cfg.User = arr[4]
  85. cfg.Pwd = arr[5]
  86. }
  87. bmYz := (*bmYzMap)[cfg.Protocol]
  88. cfg.BmYz = &bmYz
  89. configs = append(configs, cfg)
  90. }
  91. e.ReportConfig = &configs
  92. if e.OtherConfig == "" {
  93. e.OtherConfig = "{\"lvRef\":220,\"pvRef\":380}"
  94. }
  95. return nil
  96. }
  97. func (e *IotDevice) ToDtuConfig(children *[]IotDevice) *iotStruct.DtuConfig {
  98. if len(*children) <= 0 {
  99. return nil
  100. }
  101. slaveConfigs := make([]iotStruct.SlaveConfig, 0)
  102. dtuConfig := &iotStruct.DtuConfig{
  103. Enable: e.Status == constant.IotDeviceEnabled,
  104. Sn: e.Sn,
  105. Name: e.Name,
  106. Mode: e.Mode,
  107. Cycle: e.Cycle,
  108. SlaveConfig: &slaveConfigs,
  109. }
  110. slaveConfigs = make([]iotStruct.SlaveConfig, 0)
  111. for _, device := range *children {
  112. slaveConfig := iotStruct.SlaveConfig{
  113. Enable: device.Status == constant.IotDeviceEnabled,
  114. No: device.Sn,
  115. Addr: device.Address,
  116. Protocol: device.Protocol,
  117. OtherConfig: device.OtherConfig,
  118. ReportConfig: device.ReportConfig,
  119. }
  120. slaveConfigs = append(slaveConfigs, slaveConfig)
  121. }
  122. dtuConfig.SlaveConfig = &slaveConfigs
  123. return dtuConfig
  124. }