zhangqian
2023-11-13 6cd990b24911fadb9b7304e62317d83b2e6306a6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package service
 
import (
    "apsClient/conf"
    "apsClient/constvar"
    "apsClient/model"
    "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
    }
    deviceIds = make([]string, 0, len(devices))
    for _, device := range devices {
        deviceIds = append(deviceIds, device.DeviceID)
    }
    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(ServerID string) (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 = deviceList[0]
    } else if conf.Conf.System.DeviceId != "" {
        conf.Conf.CurrentDeviceID = conf.Conf.System.DeviceId
    } else {
        conf.Conf.CurrentDeviceID = ServerID
    }
    SetDeviceIDToFile(conf.Conf.CurrentDeviceID)
    return nil
}
 
const deviceIDFile = "currentDeviceID.txt"
 
func SetDeviceIDToFile(deviceID string) {
    err := os.WriteFile(deviceIDFile, []byte(deviceID), 0644)
    if err != nil {
        logx.Errorf("无法写入设备ID到文件: %v\n", err)
    } else {
        logx.Infof("设备ID已写入文件")
    }
}
 
func ReadDeviceIDFromFile() string {
    data, err := os.ReadFile(deviceIDFile)
    if err != nil {
        logx.Errorf("无法读取设备ID文件: %v\n", err)
        return ""
    }
    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).UpdateByMap(map[string]interface{}{"need_set_process_params": needSetProcessParams})
}
 
func GetCurrentDevice() (device *model.Device, err error) {
    if conf.Conf.CurrentDeviceID == "" {
        return nil, errors.New("当前设备ID不存在,请检查")
    }
    device, err = model.NewDeviceSearch().SetDeviceId(conf.Conf.CurrentDeviceID).First()
    if err == gorm.ErrRecordNotFound {
        return nil, errors.New("设备不存在")
    }
    return device, nil
}
 
// ReportsSystemDeviceToCloud 创建同步设备id记录
func ReportsSystemDeviceToCloud(systemDeviceID string) {
    err := model.NewReportsToCloudSearch(nil).Create(&model.ReportsToCloud{
        ReportType: constvar.ReportTypeSystemDeviceID,
        Content:    systemDeviceID,
    })
    if err != nil {
        logx.Errorf("ReportsSystemDeviceToCloud create record error:%v", err)
    }
}