a
554325746@qq.com
2019-12-25 603cb36a5123e46656b06a5deb8d7ac7ff81307f
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
123
124
125
126
127
128
129
130
131
132
133
134
package com.basic.security.manager;
 
import android.text.TextUtils;
 
import com.basic.security.base.BaseApplication;
import com.basic.security.model.Device;
import com.basic.security.model.ModelAdapter;
import com.basic.security.model.Sync;
import com.basic.security.utils.Constants;
import com.basic.security.utils.IpUtils;
 
import org.apache.commons.io.IOUtils;
 
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
 
public class DeviceManager extends BaseManager {
    public static String device_id = "";
    static ModelAdapter device = null;
 
    public static void saveDevice(ModelAdapter newDevice) {
        DeviceManager.device = newDevice;
        writeDeviceId(newDevice.getString(Device.device_id));
        save(newDevice);
    }
 
    public static ModelAdapter getDevice() {
        if (device == null) {
            initDevice();
        }
        return device;
    }
 
    public static String getOpenDoorTime() {
        try {
            ModelAdapter deviceSetting = getDevice();
            String open_door_time = deviceSetting.getString("open_door_time");
            if (!TextUtils.isEmpty(open_door_time)) {
                return open_door_time;
            } else {
                return "5";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "5";
    }
 
    public static String getDeviceName() {
        ModelAdapter deviceSetting = getDevice();
        String device_name = deviceSetting.getString("name");
        if (!TextUtils.isEmpty(device_name)) {
            return device_name;
        } else {
            return "admin";
        }
    }
 
    public static String getDeviceId() {
        if (1 == 1) {
            return Constants.deviceId();
        }
        if (Constants.isOutdoor) {
            return "outdoor1";
        } else {
            return "indoor1";
        }
////        return getDevice().getString(Device.device_id);
//        if (device_id != null && device_id.length() > 0) {
//            return device_id;
//        }
//        File deviceIdFile = new File("/sdcard/device_id.txt");
//        FileReader fileReader = null;
//        if (deviceIdFile.exists()) {
//            try {
//                fileReader = new FileReader(deviceIdFile);
//                List<String> lines = IOUtils.readLines(fileReader);
//                if (lines.size() > 0) {
//                    device_id = lines.get(0);
//                }
//            } catch (IOException e) {
//                e.printStackTrace();
//            } finally {
//                try {
//                    if (fileReader != null) {
//                        fileReader.close();
//                    }
//                } catch (Exception e) {
//                }
//            }
//        }
//        return device_id;
    }
 
    public static void writeDeviceId(String newDeviceId) {
        File deviceIdFile = new File("/sdcard/device_id.txt");
        if (!deviceIdFile.exists()) {
            try {
                deviceIdFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            FileWriter fileWriter = new FileWriter(deviceIdFile);
            IOUtils.write(newDeviceId.getBytes(), fileWriter);
            fileWriter.flush();
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }
    }
 
    public static String getDeviceIp() {
        return IpUtils.getIpAddress(BaseApplication.getApplication());
    }
 
    public static void initDevice() {
        device = DeviceManager.findById(Device.tableName, Device.tableName);
        if (device == null) {
            device = new ModelAdapter(Device.tableName);
            device.setString("device_id", DeviceManager.getDeviceId());
            device.setString("ip", IpUtils.getIpAddress(BaseApplication.getApplication()));
            device.setString("name", DeviceManager.getDeviceId());
            device.setString("open_door_time", "5");
            device.setString("no_face_delay", "10");
            device.setString("table", "device");
            device.setString(Sync.needSync, Constants.FALSE);
            saveDevice(device);
        }
    }
}