| | |
| | | import ( |
| | | "apsClient/conf" |
| | | "apsClient/model" |
| | | "fmt" |
| | | "apsClient/model/response" |
| | | "apsClient/pkg/logx" |
| | | "errors" |
| | | "github.com/jinzhu/gorm" |
| | | "os" |
| | | "strings" |
| | | ) |
| | | |
| | | func GetDeviceIDList() (deviceIds []string, err error) { |
| | | devices, err := model.NewDeviceSearch().SetDeviceMac(conf.Conf.System.DeviceId).FindNotTotal() |
| | | if err == gorm.ErrRecordNotFound { |
| | | return nil, nil |
| | | } |
| | | if err != nil { |
| | | return nil, err |
| | | } |
| | |
| | | return deviceIds, nil |
| | | } |
| | | |
| | | func GetDeviceList() (deviceList []*response.Device, err error) { |
| | | devices, err := model.NewDeviceSearch().SetDeviceMac(conf.Conf.System.DeviceId).FindNotTotal() |
| | | if err == gorm.ErrRecordNotFound { |
| | | return nil, nil |
| | | } |
| | | if err != nil { |
| | | return nil, err |
| | | } |
| | | deviceList = make([]*response.Device, 0, len(devices)) |
| | | for _, device := range devices { |
| | | deviceList = append(deviceList, &response.Device{ |
| | | DeviceID: device.DeviceID, |
| | | DeviceName: device.DeviceName, |
| | | NeedSetProcessParams: device.NeedSetProcessParams, |
| | | }) |
| | | } |
| | | return deviceList, nil |
| | | } |
| | | |
| | | func InitCurrentDeviceID() (err error) { |
| | | currentDeviceID := ReadDeviceIDFromFile() |
| | | if currentDeviceID != "" { |
| | | conf.Conf.CurrentDeviceID = currentDeviceID |
| | | return |
| | | } |
| | | deviceList, err := GetDeviceIDList() |
| | | if err != nil { |
| | | return err |
| | | } |
| | | if len(deviceList) == 0 { |
| | | conf.Conf.CurrentDeviceID = conf.Conf.System.DeviceId |
| | | } else { |
| | | conf.Conf.CurrentDeviceID = deviceList[0] |
| | | } |
| | | SetDeviceIDToFile(conf.Conf.CurrentDeviceID) |
| | | return nil |
| | | } |
| | | |
| | | const deviceIDFile = "currentDeviceID.txt" |
| | | |
| | | func SetDeviceIDToFile(deviceID string) { |
| | | err := os.WriteFile(deviceIDFile, []byte(deviceID), 0644) |
| | | if err != nil { |
| | | fmt.Printf("无法写入设备ID到文件: %v\n", err) |
| | | logx.Errorf("无法写入设备ID到文件: %v\n", err) |
| | | } else { |
| | | fmt.Println("设备ID已写入文件") |
| | | logx.Infof("设备ID已写入文件") |
| | | } |
| | | } |
| | | |
| | | func ReadDeviceIDFromFile() string { |
| | | data, err := os.ReadFile(deviceIDFile) |
| | | if err != nil { |
| | | fmt.Printf("无法读取设备ID文件: %v\n", err) |
| | | logx.Errorf("无法读取设备ID文件: %v\n", err) |
| | | return "" |
| | | } |
| | | return string(data) |
| | | deviceId := string(data) |
| | | deviceId = strings.TrimSpace(deviceId) |
| | | deviceId = strings.Trim(deviceId, "\n") |
| | | return deviceId |
| | | } |
| | | |
| | | func UpdateDevice(deviceId string, needSetProcessParams bool) (err error) { |
| | | device, err := model.NewDeviceSearch().SetDeviceId(deviceId).First() |
| | | if err == gorm.ErrRecordNotFound { |
| | | return errors.New("设备不存在") |
| | | } |
| | | device.NeedSetProcessParams = needSetProcessParams |
| | | return model.NewDeviceSearch().SetID(device.ID).Save(device) |
| | | } |