package iotDb import ( "IotAdmin/app/iot/models" "IotAdmin/core/logger" "IotAdmin/core/sdk" "IotAdmin/core/storage" "IotAdmin/iot/constant" iotLog "IotAdmin/iot/log" iotMap "IotAdmin/iot/map" iotStruct "IotAdmin/iot/struct" "encoding/json" "errors" "fmt" "gorm.io/gorm" ) // LoadDtuDeviceMap 加载DTU设备 func LoadDtuDeviceMap() error { iotMap.MapDtuDevice.Clean() deviceArr := make([]models.IotDevice, 0) for _, db := range dbMap { gatewayArr := make([]models.IotDevice, 0) d := &models.IotDevice{} err := db.Model(d).Select("id,sn,name,type,mode,status,cycle").Where("type = ? AND status = ?", constant.IotDeviceTypeGateway, constant.IotDeviceOnline).Find(&gatewayArr).Error if err != nil { continue } deviceArr = append(deviceArr, gatewayArr...) err = loadDtuDeviceMap(db, &deviceArr) if err != nil { return err } } return nil } func loadDtuDeviceMap(db *gorm.DB, arr *[]models.IotDevice) error { if len(*arr) == 0 { return nil } for _, v := range *arr { err := addDtuDevice(db, v) if err != nil { return err } } return nil } func addDtuDevice(db *gorm.DB, device models.IotDevice) error { if device.Type != constant.IotDeviceTypeGateway { return errors.New("type is not gateway") } children := make([]models.IotDevice, 0) db.Model(&models.IotDevice{}).Select("id,sn,status,dsn,protocol,address,bm_yz,other_config").Where("parent_id = ? AND status = ?", device.Id, constant.IotDeviceOnline).Find(&children) if len(children) == 0 { return nil } cfg := device.ToDtuConfig(&children) var dtu = &iotStruct.DtuDevice{ SN: device.Sn, Name: device.Name, Config: cfg, Logger: logger.NewHelper(iotLog.SetupDeviceLogger(device.Sn)), } logger.Infof("加载DTU设备: %s %v", dtu.SN, dtu.Config) dtu.Logger.Infof("加载DTU设备 %v", *dtu.Config) iotMap.MapDtuDevice.Add(device.Sn, dtu) return nil } func DtuChangeStatus(message storage.Message) (err error) { db := sdk.Runtime.GetDbByKey(message.GetPrefix()) if db == nil { err = errors.New("db not exist") logger.Errorf("host[%s]'s %s", message.GetPrefix(), err.Error()) return } var rb []byte rb, err = json.Marshal(message.GetValues()) if err != nil { err = fmt.Errorf("json Marshal error,%s, %v", string(rb), err.Error()) return } status := iotStruct.DtuStatus{} err = json.Unmarshal(rb, &status) if err != nil { err = fmt.Errorf("json Unmarshal error,%s, %v", string(rb), err.Error()) return } mp := make(map[string]interface{}) mp["online_status"] = status.Status if status.Status == constant.IotDeviceOffline { mp["time_offline"] = status.Time } else { mp["time_online"] = status.Time } err = db.Model(&models.IotDevice{}).Where("sn = ?", status.SN).Updates(mp).Error return err } func DtuDeviceChange(message storage.Message) (err error) { db := sdk.Runtime.GetDbByKey(message.GetPrefix()) if db == nil { err = errors.New("db not exist") logger.Errorf("host[%s]'s %s", message.GetPrefix(), err.Error()) return nil } var rb []byte rb, err = json.Marshal(message.GetValues()) if err != nil { err = fmt.Errorf("json Marshal error,%s, %v", string(rb), err.Error()) return err } val := &iotStruct.DtuChange{} err = json.Unmarshal(rb, val) if err != nil { err = fmt.Errorf("json Unmarshal error,%s, %v", string(rb), err.Error()) return err } if val.Type == constant.IotDeviceRefresh { err = LoadDtuDeviceMap() } else if val.Type == constant.IotDeviceDelete { device := &models.IotDevice{} err = db.Model(device).Where("id = ? ", val.Id).Find(device).Error if err != nil { return } iotMap.MapDtuDevice.Remove(device.Sn) } else { device := &models.IotDevice{} err = db.Model(device).Where("id = ? AND status = ?", val.Id, constant.IotDeviceEnabled).Find(device).Error if err != nil { return } err = addDtuDevice(db, *device) } return }