xuxiuxi
2017-07-19 51c810c314e17aebb5935df7165ccf499dc09c31
VisitFace/DemoForBsk/app/src/main/java/cn/com/basic/face/service/DeviceMng.java
@@ -7,6 +7,9 @@
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.fragment.DeviceFragment;
import cn.com.basic.face.service.sqlite.AttendanceDao;
import cn.com.basic.face.service.sqlite.DeviceDao;
import cn.com.basic.face.util.AppApi;
import org.xutils.http.RequestParams;
@@ -34,6 +37,7 @@
            public void run() {
                try {
                    while (true) {
                        AttendanceDao.getInstance().reloadLeftList(null, 0, false);
                        if (!BaseApplication.getInstance().deviceAvailable) {
                            load ();
                        } else {
@@ -60,21 +64,81 @@
//                });
//                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));
            if (BaseApplication.getInstance().networkAvailable()) {
                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() {
                        loadAfterReturn(getList(Device.class));
                    }
                });
            } else {
                loadAfterReturn(DeviceDao.getInstance().loadMainDevice(CommonVariables.DeviceType.ANDROID_DEVICE, authorationId));
            }
        } catch (Throwable e) {
            e.printStackTrace();
            Toast.makeText(BaseApplication.getInstance(), "数据库中找不到该设备", Toast.LENGTH_SHORT).show();
        }
    }
    public void loadAfterReturn(List<Device> devices) {
//        List<Device> devices = getList(Device.class);
        if (devices.size() > 0) {
            final Device device = devices.get(0);
            BaseApplication.getInstance().setAndroidDevice(device);
            MainActivity.getInstance().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    DeviceFragment.getInstance().setAndroidDevice(device);
                    if (BaseApplication.getInstance().networkAvailable()) {
                        RequestParams params = new RequestParams(AppApi.BASE_URL + AppApi.Query.DEVICE_QUERY);
                        params.addBodyParameter(Device.FieldNames.belongId, device.getBelongId());
                        params.addBodyParameter(Device.FieldNames.type, CommonVariables.DeviceType.CAMERA1);
                        x.http().post(params, new BaseCommonCallBack() {
                            public void success() {
                                List<Device> devices = resultBean.getList(Device.class);
                                if (devices.size() > 0) {
                                    final Device camera1 = devices.get(0);
                                    BaseApplication.getInstance().setCamera1(camera1);
                                    DeviceFragment.getInstance().setCamera1(camera1);
                                }
                            }
                        });
                        params = new RequestParams(AppApi.BASE_URL + AppApi.Query.DEVICE_QUERY);
                        params.addBodyParameter(Device.FieldNames.belongId, device.getBelongId());
                        params.addBodyParameter(Device.FieldNames.type, CommonVariables.DeviceType.CAMERA2);
                        x.http().post(params, new BaseCommonCallBack() {
                            public void success() {
                                List<Device> devices = resultBean.getList(Device.class);
                                if (devices.size() > 0) {
                                    final Device camera2 = devices.get(0);
                                    BaseApplication.getInstance().setCamera2(camera2);
                                    DeviceFragment.getInstance().setCamera2(camera2);
                                }
                            }
                        });
                    } else {
                        Toast.makeText(BaseApplication.getInstance(), "数据库中找不到该设备", Toast.LENGTH_SHORT).show();
                        List<Device> camera1List = DeviceDao.getInstance().loadCamera(CommonVariables.DeviceType.CAMERA1, device.getBelongId());
                        if (camera1List != null && camera1List.size() > 0) {
                            final Device camera1 = camera1List.get(0);
                            BaseApplication.getInstance().setCamera1(camera1);
                            DeviceFragment.getInstance().setCamera1(camera1);
                        }
                        List<Device> camera2List = DeviceDao.getInstance().loadCamera(CommonVariables.DeviceType.CAMERA2, device.getBelongId());
                        if (camera2List != null && camera2List.size() > 0) {
                            final Device camera2 = camera2List.get(0);
                            BaseApplication.getInstance().setCamera2(camera2);
                            DeviceFragment.getInstance().setCamera2(camera2);
                        }
                    }
                }
            });
        } catch (Throwable e) {
            e.printStackTrace();
        } else {
            Toast.makeText(BaseApplication.getInstance(), "数据库中找不到该设备", Toast.LENGTH_SHORT).show();
        }
    }
@@ -92,22 +156,22 @@
    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);
//        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.getList(Device.class);
//                if (places.size() > 0) {
//                    BaseApplication.getInstance().setAndroidDevice(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) {
@@ -151,7 +215,9 @@
//            port = camera1.getString("fragment_device_right_camera1_port", "");
//        }
        return "rtsp://"+username+":"+password+"@"+ip+":"+port+"/h264/ch1/sub/av_stream";
//        return "rtsp://"+username+":"+password+"@"+ip+":"+port+"/h264/ch1/sub/av_stream";
//        return "rtsp://Admin:1234@192.168.1.22/h264";
        return "rtsp://admin:a1234567@192.168.1.132:554/h264/ch1/sub/av_stream";
    }
    public String getCamera2Address() {
@@ -170,11 +236,14 @@
//        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", ""))) {
//        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";
        //return "rtsp://"+username+":"+password+"@"+ip+":"+port+"/h264/ch1/sub/av_stream";
//        return "rtsp://admin:a1234567@192.168.1.68:554/h264/ch1/main/av_stream";
        return "rtsp://admin:a1234567@192.168.1.69:554/h264/ch1/sub/av_stream";
    }
}