| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- package models
- import (
- "IotAdmin/common/models"
- "IotAdmin/core/logger"
- "IotAdmin/iot/constant"
- iotStruct "IotAdmin/iot/struct"
- "strings"
- "time"
- "gorm.io/gorm"
- )
- // IotDevice 设备实体
- type IotDevice struct {
- models.Model
- ParentId int `json:"parentId" gorm:"rel(fk);type:bigint(20);comment:父ID"`
- GroupId int `json:"groupId" gorm:"type:bigint(20);comment:分组ID"`
- Sn string `json:"sn" gorm:"type:varchar(50);comment:设备编码"`
- Name string `json:"name" gorm:"type:varchar(50);comment:设备名称"`
- Type int `json:"type" gorm:"type:bigint(20);comment:设备类型 1:网关 2:表计"`
- Mode int `json:"mode" gorm:"type:bigint(20);comment:设备模式 1:下发指令查询 2:表计主动上报"`
- Cycle int `json:"cycle" gorm:"type:bigint(20);comment:上报周期"`
- Dsn string `json:"dsn" gorm:"type:text;comment:设备Dsn"`
- Description string `json:"description" gorm:"type:varchar(255);comment:设备描述"`
- Protocol string `json:"protocol" gorm:"type:varchar(50);comment:表计协议"`
- Address int `json:"address" gorm:"type:bigint(20);comment:表计地址"`
- BmYz string `json:"bmYz" gorm:"type:text;comment:编码因子"`
- OtherConfig string `json:"otherConfig" gorm:"type:text;comment:其他配置"`
- OnlineStatus int `json:"onlineStatus" gorm:"type:tinyint(1);comment:在线状态"`
- TimeOnline time.Time `json:"timeOnline" gorm:"type:datetime;comment:上线时间"`
- TimeOffline time.Time `json:"timeOffline" gorm:"type:datetime;comment:离线时间"`
- GroupName string `json:"groupName" `
- ReportConfig *[]iotStruct.ReportConfig `json:"reportConfigs" gorm:"-"`
- models.ControlBy
- models.ModelTime
- }
- func (*IotDevice) TableName() string {
- return "iot_device"
- }
- func (e *IotDevice) Generate() models.ActiveRecord {
- o := *e
- return &o
- }
- func (e *IotDevice) GetId() interface{} {
- return e.Id
- }
- func (e *IotDevice) BeforeCreate(_ *gorm.DB) error {
- var err error
- e.OnlineStatus = constant.IotDeviceOffline
- return err
- }
- //
- //func (e *IotDevice) BeforeUpdate(_ *gorm.DB) error {
- // var err error
- //
- // return err
- //}
- func (e *IotDevice) AfterFind(_ *gorm.DB) error {
- if e.Type == constant.IotDeviceTypeGateway || e.Dsn == "" {
- return nil
- }
- configs := make([]iotStruct.ReportConfig, 0)
- arr := strings.Split(e.Dsn, constant.DsnSplitString)
- bmYzMap, err := (&iotStruct.BmYz{}).BuildBmYzArray(e.BmYz)
- if err != nil {
- logger.Error("BuildBmYzArray error:", err)
- return err
- }
- for _, v := range arr {
- cfg := iotStruct.ReportConfig{}
- arr := strings.Split(v, constant.DsnChildSplitString)
- if len(arr) < 4 {
- continue
- }
- cfg.Host = arr[0]
- cfg.Protocol = arr[1]
- cfg.ST = arr[2]
- cfg.MN = arr[3]
- if len(arr) == 6 {
- cfg.User = arr[4]
- cfg.Pwd = arr[5]
- }
- bmYz := (*bmYzMap)[cfg.Protocol]
- cfg.BmYz = &bmYz
- configs = append(configs, cfg)
- }
- e.ReportConfig = &configs
- return nil
- }
- func (e *IotDevice) ToDtuConfig(children *[]IotDevice) *iotStruct.DtuConfig {
- if len(*children) <= 0 {
- return nil
- }
- slaveConfigs := make([]iotStruct.SlaveConfig, 0)
- dtuConfig := &iotStruct.DtuConfig{
- Enable: true,
- Sn: e.Sn,
- Name: e.Name,
- Mode: e.Mode,
- Cycle: e.Cycle,
- SlaveConfig: &slaveConfigs,
- }
- slaveConfigs = make([]iotStruct.SlaveConfig, 0)
- for _, device := range *children {
- slaveConfig := iotStruct.SlaveConfig{
- No: device.Sn,
- Addr: device.Address,
- Protocol: device.Protocol,
- OtherConfig: device.OtherConfig,
- ReportConfig: device.ReportConfig,
- }
- slaveConfigs = append(slaveConfigs, slaveConfig)
- }
- dtuConfig.SlaveConfig = &slaveConfigs
- return dtuConfig
- }
|