package data import ( "MeterService/core/logger" "MeterService/core/utils" "MeterService/core/utils/httpHelper" "MeterService/dataStruct" "MeterService/database/appApi" "encoding/json" "time" ) func initDevice() error { appApiMap := dataStruct.NewMapAppApi() apiMap, ok := appApiMap.Get(dataStruct.GetDevices) if !ok { logger.Error("获取 设备加载API 错误") } for k, v := range apiMap { go loadDevice(k, v) } return nil } func loadDevice(appId int, api *appApi.AppApi) { if api == nil { logger.Error("应用[%d] 获取 设备加载API 错误", appId) return } time.Sleep(time.Second * 2) http := httpHelper.NewHTTPClientHelper() url := api.Url if !utils.HasPrefix(&url, "http") { url = utils.IsPrefix(url, "/") url = api.Host + url } res, err := http.Get(url) if err != nil { logger.Error("应用[%d] 获取 设备加载API 错误", appId) return } devices := &[]dataStruct.DtuDevice{} err = json.Unmarshal(res, devices) if err != nil { return } for _, device := range *devices { dtu, err := device.ToDtu() if err != nil { return } dtuState := &dataStruct.DTUDeviceState{ Info: dtu, Online: false, PwrOff: false, } DtuMap.Add(dtu.SN, dtuState) logger.Debug("应用[%d] 加载设备 [ %s ]", appId, dtu.SN) } }