xuxiuxi
2017-06-02 3cceac30dc1c5a7cf5bd4f95e327e855cdcf304d
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package cn.com.basic.face.service;
 
import android.content.Context;
import android.content.SharedPreferences;
import android.widget.Toast;
 
import cn.com.basic.face.base.BaseApplication;
import cn.com.basic.face.base.MainActivity;
import cn.com.basic.face.discern.common.CommonVariables;
import cn.com.basic.face.util.AppApi;
 
import org.xutils.http.RequestParams;
import org.xutils.x;
 
import java.util.List;
import java.util.Map;
 
import cn.com.basic.face.discern.common.BaseCommonCallBack;
import cn.com.basic.face.discern.entity.Device;
import cn.com.basic.face.util.SharedPreferencesUtils;
 
public class DeviceMng {
    public static DeviceMng instance = new DeviceMng();
 
    public static String authorationId = "101";
 
    public static DeviceMng getInstance() {
        return instance;
    }
 
    public void findDevice() {
        new Thread() {
            @Override
            public void run() {
                try {
                    while (true) {
                        if (!BaseApplication.getInstance().deviceAvailable) {
                            load ();
                        } else {
                            break;
                        }
                        Thread.sleep(3000);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
 
    public void load () {
        try {
//            if (authorationId == null || authorationId.trim().length() <= 1) {
//                getAuthorationId();
//                MainActivity.getInstance().runOnUiThread(new Runnable() {
//                    @Override
//                    public void run() {
//                        Toast.makeText(MainActivity.getInstance(), "请填写设备序列号", Toast.LENGTH_SHORT).show();
//                    }
//                });
//                return;
//            }
            RequestParams params = new RequestParams(AppApi.BASE_URL + AppApi.Query.DEVICE_QUERY);
            params.addBodyParameter(Device.FieldNames.sn, authorationId);
            params.addBodyParameter(Device.FieldNames.type, CommonVariables.DeviceType.ANDROID_DEVICE);
            x.http().post(params, new BaseCommonCallBack() {
                public void success() {
                    List<Device> places = resultBean.getListBeanOfType(Device.class);
                    if (places.size() > 0) {
                        BaseApplication.getInstance().setDevice(places.get(0));
                    } else {
                        Toast.makeText(BaseApplication.getInstance(), "数据库中找不到该设备", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        } catch (Throwable e) {
            e.printStackTrace();
            Toast.makeText(BaseApplication.getInstance(), "数据库中找不到该设备", Toast.LENGTH_SHORT).show();
        }
    }
 
    public void getAuthorationId() {
        SharedPreferences school = DeviceMng.getInstance().loadSharedPreferences(CommonVariables.DeviceSharedPreferences.SCHOOL);
        if (!"".equals(school.getString("fragment_device_right_device_info_sn", ""))) {
            authorationId = school.getString("fragment_device_right_device_info_sn", "");
        }
    }
 
    public void saveWifiSetting(Map<String, String> fields) {
        SharedPreferencesUtils.save(CommonVariables.DeviceSharedPreferences.WIFI, fields);
    }
 
    public void saveLanSetting(Map<String, String> fields) {
 
        RequestParams params = new RequestParams(AppApi.BASE_URL + AppApi.Query.DEVICE_QUERY);
        params.addBodyParameter(Device.FieldNames.sn, authorationId);
        params.addBodyParameter(Device.FieldNames.type, CommonVariables.DeviceType.ANDROID_DEVICE);
        x.http().post(params, new BaseCommonCallBack() {
            public void success() {
                List<Device> places = resultBean.getListBeanOfType(Device.class);
                if (places.size() > 0) {
                    BaseApplication.getInstance().setDevice(places.get(0));
                } else {
                    Toast.makeText(BaseApplication.getInstance(), "数据库中找不到该设备", Toast.LENGTH_SHORT).show();
                }
            }
        });
 
 
        SharedPreferencesUtils.save(CommonVariables.DeviceSharedPreferences.LAN, fields);
    }
 
    public void saveSchoolSetting(Map<String, String> fields) {
        SharedPreferencesUtils.save(CommonVariables.DeviceSharedPreferences.SCHOOL, fields);
        getAuthorationId();
    }
 
    public void saveCamera1Setting(Map<String, String> fields) {
        SharedPreferencesUtils.save(CommonVariables.DeviceSharedPreferences.CAMERA1, fields);
    }
 
    public void saveCamera2Setting(Map<String, String> fields) {
        SharedPreferencesUtils.save(CommonVariables.DeviceSharedPreferences.CAMERA2, fields);
    }
 
    public SharedPreferences loadSharedPreferences(String name) {
        return MainActivity.getInstance().getSharedPreferences(name, Context.MODE_PRIVATE);
    }
 
    public String get(SharedPreferences sharedPreferences, String key) {
        return sharedPreferences.getString(key, "");
    }
 
    public String getCamera1Address() {
        String username = "admin";
        String password = "admin12345";
        String ip = "192.168.1.70";
        String port = "554";
 
//        SharedPreferences camera1 = DeviceMng.getInstance().loadSharedPreferences(CommonVariables.DeviceSharedPreferences.CAMERA1);
//        if (!"".equals(camera1.getString("fragment_device_right_camera1_username", ""))) {
//            username = camera1.getString("fragment_device_right_camera1_username", "");
//        }
//        if (!"".equals(camera1.getString("fragment_device_right_camera1_password", ""))) {
//            password = camera1.getString("fragment_device_right_camera1_password", "");
//        }
//        if (!"".equals(camera1.getString("fragment_device_right_camera1_ip", ""))) {
//            ip = camera1.getString("fragment_device_right_camera1_ip", "");
//        }
//        if (!"".equals(camera1.getString("fragment_device_right_camera1_port", ""))) {
//            port = camera1.getString("fragment_device_right_camera1_port", "");
//        }
 
        return "rtsp://"+username+":"+password+"@"+ip+":"+port+"/h264/ch1/sub/av_stream";
    }
 
    public String getCamera2Address() {
        String username = "admin";
        String password = "a1234567";
        String ip = "192.168.1.68";
        String port = "554";
 
//        SharedPreferences camera1 = DeviceMng.getInstance().loadSharedPreferences(CommonVariables.DeviceSharedPreferences.CAMERA2);
//        if (!"".equals(camera1.getString("fragment_device_right_camera1_username", ""))) {
//            username = camera1.getString("fragment_device_right_camera1_username", "");
//        }
//        if (!"".equals(camera1.getString("fragment_device_right_camera1_password", ""))) {
//            password = camera1.getString("fragment_device_right_camera1_password", "");
//        }
//        if (!"".equals(camera1.getString("fragment_device_right_camera1_ip", ""))) {
//            ip = camera1.getString("fragment_device_right_camera1_ip", "");
//        }
//        if (!"".equals(camera1.getString("fragment_device_right_camera1_port", ""))) {
//            port = camera1.getString("fragment_device_right_camera1_port", "");
//        }
 
        return "rtsp://"+username+":"+password+"@"+ip+":"+port+"/h264/ch1/sub/av_stream";
    }
 
}