From d343b71cd89f59a87e85c46ce7a04d47c357462d Mon Sep 17 00:00:00 2001
From: xuxiuxi <xuxiuxi@454eff88-639b-444f-9e54-f578c98de674>
Date: 星期二, 25 七月 2017 15:46:45 +0800
Subject: [PATCH] 

---
 VisitFace/DemoForBsk/app/src/main/java/cn/com/basic/face/service/DeviceMng.java |  137 +++++++++++++++++++++++++++++----------------
 1 files changed, 89 insertions(+), 48 deletions(-)

diff --git a/VisitFace/DemoForBsk/app/src/main/java/cn/com/basic/face/service/DeviceMng.java b/VisitFace/DemoForBsk/app/src/main/java/cn/com/basic/face/service/DeviceMng.java
index 2e8b6e7..c8a4d1d 100644
--- a/VisitFace/DemoForBsk/app/src/main/java/cn/com/basic/face/service/DeviceMng.java
+++ b/VisitFace/DemoForBsk/app/src/main/java/cn/com/basic/face/service/DeviceMng.java
@@ -9,6 +9,7 @@
 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;
@@ -20,9 +21,12 @@
 import cn.com.basic.face.discern.common.BaseCommonCallBack;
 import cn.com.basic.face.discern.entity.Device;
 import cn.com.basic.face.util.SharedPreferencesUtils;
+import cn.com.basic.face.util.SyncUpAndDownThread;
 
 public class DeviceMng {
     public static DeviceMng instance = new DeviceMng();
+
+    private SyncUpAndDownThread syncUpAndDownThread = null;
 
     public static String authorationId = "101";
 
@@ -36,7 +40,7 @@
             public void run() {
                 try {
                     while (true) {
-                        AttendanceDao.getInstance().reloadLeftList(null, 0, false);
+                        //AttendanceDao.getInstance().reloadLeftList(null, 0, false);
                         if (!BaseApplication.getInstance().deviceAvailable) {
                             load ();
                         } else {
@@ -63,58 +67,90 @@
 //                });
 //                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> devices = resultBean.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);
 
-                                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.addBodyParameter(Device.FieldNames.sn, authorationId);
+                params.addBodyParameter(Device.FieldNames.type, CommonVariables.DeviceType.ANDROID_DEVICE);
+                params.addBodyParameter("deviceCompanyId", BaseApplication.getInstance().getAndroidDevice().getCompanyId());
+                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);
 
-                                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);
-                                        }
-                                    }
-                                });
+                    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);
+                        params.addBodyParameter("deviceCompanyId", BaseApplication.getInstance().getAndroidDevice().getCompanyId());
+                        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);
+                        params.addBodyParameter("deviceCompanyId", BaseApplication.getInstance().getAndroidDevice().getCompanyId());
+                        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();
+            synchronized (this) {
+                if (syncUpAndDownThread == null) {
+                    syncUpAndDownThread = new SyncUpAndDownThread();
+                syncUpAndDownThread.start();
+            }
+            }
+        } else {
             Toast.makeText(BaseApplication.getInstance(), "鏁版嵁搴撲腑鎵句笉鍒拌璁惧", Toast.LENGTH_SHORT).show();
         }
     }
@@ -171,7 +207,7 @@
         return sharedPreferences.getString(key, "");
     }
 
-    public String getCamera1Address() {
+    public String getCamera2Address() {
         String username = "admin";
         String password = "admin12345";
         String ip = "192.168.1.70";
@@ -193,10 +229,12 @@
 
 //        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";
+//        return "rtsp://admin:a1234567@192.168.1.132:554/h264/ch1/sub/av_stream";
+        return "rtsp://admin:admin12345@192.168.1.202:554/h264/ch1/main/av_stream";
+//        return "rtsp://admin:a1234567@192.168.1.132:554/h264/ch1/main/av_stream";
     }
 
-    public String getCamera2Address() {
+    public String getCamera1Address() {
         String username = "admin";
         String password = "a1234567";
         String ip = "192.168.1.68";
@@ -219,7 +257,10 @@
 
         //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";
+//        return "rtsp://admin:a1234567@192.168.1.69:554/h264/ch1/sub/av_stream";
+//        return "rtsp://Admin:1234@192.168.1.22/h264";
+        return "rtsp://admin:a1234567@192.168.1.201:554/h264/ch1/main/av_stream";
     }
 
+
 }

--
Gitblit v1.8.0