DESKTOP-SADJPBG\47462 %!s(int64=5) %!d(string=hai) anos
pai
achega
0c0a08a79b

+ 17 - 2
app/build.gradle

@@ -48,6 +48,13 @@ android {
         }
         }
 
 
     }
     }
+    packagingOptions {
+        exclude 'META-INF/DEPENDENCIES'
+        exclude 'META-INF/NOTICE'
+        exclude 'META-INF/LICENSE'
+        exclude 'META-INF/LICENSE.txt'
+        exclude 'META-INF/NOTICE.txt'
+    }
     compileOptions {
     compileOptions {
         sourceCompatibility = 1.8
         sourceCompatibility = 1.8
         targetCompatibility = 1.8
         targetCompatibility = 1.8
@@ -70,6 +77,14 @@ dependencies {
     implementation 'com.github.maybesix:Android-XHLibrary:v1.0.0'
     implementation 'com.github.maybesix:Android-XHLibrary:v1.0.0'
     implementation 'com.tencent.bugly:crashreport:3.1.0'
     implementation 'com.tencent.bugly:crashreport:3.1.0'
     implementation 'com.tencent.bugly:nativecrashreport:latest.release'
     implementation 'com.tencent.bugly:nativecrashreport:latest.release'
-    implementation files('src/main/libs/aliyun-java-sdk-core-4.1.0.jar')
-    implementation files('src/main/libs/aliyun-java-sdk-dysmsapi-1.0.0.jar')
+    implementation 'com.aliyun:aliyun-java-sdk-core:4.0.6'
+    implementation 'javax.xml.bind:jaxb-api:2.3.0'
+    implementation 'com.sun.xml.bind:jaxb-core:2.3.0'
+    implementation 'com.sun.xml.bind:jaxb-impl:2.3.0'
+    implementation 'com.aliyun:aliyun-java-sdk-dysmsapi:1.1.0'
+    implementation 'com.squareup.okhttp3:okhttp:3.10.0'
+    implementation 'com.zhy:okhttputils:2.6.2'
+//    compile group: 'xerces', name: 'xercesImpl', version: '2.12.0'
+//    implementation files('src/main/libs//aliyun-java-sdk-core-4.1.0.jar')
+//    implementation files('src/main/libs/aliyun-java-sdk-dysmsapi-1.0.0.jar')
 }
 }

+ 3 - 0
app/src/main/AndroidManifest.xml

@@ -76,7 +76,10 @@
                 android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
                 android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
                 android:resource="@xml/device_filter" />
                 android:resource="@xml/device_filter" />
         </activity>
         </activity>
+
+
         <service android:name=".service.MyService" />
         <service android:name=".service.MyService" />
+
         <receiver android:name=".receiver.BootReceiver"  android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
         <receiver android:name=".receiver.BootReceiver"  android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
             <intent-filter>
             <intent-filter>
                 <action android:name="android.intent.action.BOOT_COMPLETED" />
                 <action android:name="android.intent.action.BOOT_COMPLETED" />

+ 3 - 1
app/src/main/java/com/siwei/recyclebox/application/AppApplication.java

@@ -77,6 +77,8 @@ public class AppApplication extends BaseApplication implements CustomActivityOnC
         super.onCreate();
         super.onCreate();
         SerialPortUtil.getInstance().init(this);        //初+始化串口
         SerialPortUtil.getInstance().init(this);        //初+始化串口
 
 
+        SPUtils.getInstance().put("RunningState",0);
+        SPUtils.getInstance().put("TaskSwitch",1);
         CrashReport.initCrashReport(getApplicationContext(), "40c3954b93", true);//测试阶段为true  发布时改成false
         CrashReport.initCrashReport(getApplicationContext(), "40c3954b93", true);//测试阶段为true  发布时改成false
         CrashReport.setUserId(this,MainViewModel.getDeviceId(this));
         CrashReport.setUserId(this,MainViewModel.getDeviceId(this));
         Log.i(TAG,"AppApplication------------------------");
         Log.i(TAG,"AppApplication------------------------");
@@ -105,7 +107,7 @@ public class AppApplication extends BaseApplication implements CustomActivityOnC
 //        DeviceInfo deviceInfo = new DeviceInfo();
 //        DeviceInfo deviceInfo = new DeviceInfo();
 //        deviceInfo.productKey = "a13H8L6bDyf";// 产品类型
 //        deviceInfo.productKey = "a13H8L6bDyf";// 产品类型
 //        deviceInfo.deviceName = "9pD3trz6OaDV8GF7yRsb";// 设备名称
 //        deviceInfo.deviceName = "9pD3trz6OaDV8GF7yRsb";// 设备名称
-////        deviceInfo.deviceSecret = "A08BEZ1GvOXZodAASIogQBi97f3Le1Cu";// 设备密钥
+////        deviceInfo.deviceSecret = "lzR12lxbbuV3c4tZMxrIPl1Vh9K9Ssv4";// 设备密钥
 //        deviceInfo.deviceSecret = SPUtils.getInstance().getString("deviceSecret");
 //        deviceInfo.deviceSecret = SPUtils.getInstance().getString("deviceSecret");
         /**
         /**
          * 设置设备当前的初始状态值,属性需要和云端创建的物模型属性一致
          * 设置设备当前的初始状态值,属性需要和云端创建的物模型属性一致

+ 62 - 96
app/src/main/java/com/siwei/recyclebox/deviceUtils/OtherDevice.java

@@ -8,7 +8,6 @@ import com.aliyuncs.exceptions.ClientException;
 import com.siwei.recyclebox.ui.main.MainViewModel;
 import com.siwei.recyclebox.ui.main.MainViewModel;
 import com.hoho.android.usbserial.driver.UsbSerialDriver;
 import com.hoho.android.usbserial.driver.UsbSerialDriver;
 import com.hoho.android.usbserial.driver.UsbSerialPort;
 import com.hoho.android.usbserial.driver.UsbSerialPort;
-import com.siwei.recyclebox.ui.main.WarnActivity;
 
 
 import android.os.Handler;
 import android.os.Handler;
 
 
@@ -112,7 +111,6 @@ public class OtherDevice extends BaseDeviceEntity {
      */
      */
     public  Integer queryMagDoor(){
     public  Integer queryMagDoor(){
         try {
         try {
-            SPUtils.getInstance().put("RunningState","0");
             byte[] pushRodOrderBytes = new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x55, 0x04, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD};
             byte[] pushRodOrderBytes = new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x55, 0x04, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD};
             port.write(pushRodOrderBytes, 150);//
             port.write(pushRodOrderBytes, 150);//
             byte[] response = new byte[12];
             byte[] response = new byte[12];
@@ -128,17 +126,14 @@ public class OtherDevice extends BaseDeviceEntity {
                 Log.i(TAG,"response[6] 门磁读取到"+response[6]);
                 Log.i(TAG,"response[6] 门磁读取到"+response[6]);
                 return 1;
                 return 1;
             }
             }
-            MainViewModel.reportDeviceEvent("queryMagDoor");
+//            MainViewModel.reportDeviceEvent("queryMagDoor");
             return 0;
             return 0;
-        }catch ( Exception e ){
 
 
-            MainViewModel.reportDeviceEvent("queryMagDoor");
-            try {
-                WarnActivity.SendSmsResponse("queryMagDoor门磁");
-            } catch (ClientException ex) {
-                ex.printStackTrace();
-            }
+        }catch ( Exception e ){
             e.printStackTrace();
             e.printStackTrace();
+            SPUtils.getInstance().put("RunningState",1);
+            MainViewModel.SendSmsResponse("其他设备未检测到");
+
             Log.e(TAG,"读取门磁错误",e);
             Log.e(TAG,"读取门磁错误",e);
             return 3;
             return 3;
         }
         }
@@ -149,7 +144,6 @@ public class OtherDevice extends BaseDeviceEntity {
     * */
     * */
     public Integer queryCurrent(){
     public Integer queryCurrent(){
         try{
         try{
-            SPUtils.getInstance().put("RunningState","0");
 //            AA BB 0A 01 55 05 FF FF FF FF CC DD  查询电流过大标志  回复00 没有 01 有
 //            AA BB 0A 01 55 05 FF FF FF FF CC DD  查询电流过大标志  回复00 没有 01 有
             port.write(new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x55, 0x05, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD}, 150);//
             port.write(new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x55, 0x05, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD}, 150);//
             byte[] response = new byte[12];
             byte[] response = new byte[12];
@@ -158,11 +152,12 @@ public class OtherDevice extends BaseDeviceEntity {
                 Log.i(TAG,"电流过大返回值response[6]========"+response[6]);
                 Log.i(TAG,"电流过大返回值response[6]========"+response[6]);
                 return (int) response[6];
                 return (int) response[6];
             }
             }
-            MainViewModel.reportDeviceEvent("queryCurrent");
+//            MainViewModel.reportDeviceEvent("queryCurrent");
             return 0;
             return 0;
         }catch (Exception e){
         }catch (Exception e){
             e.printStackTrace();
             e.printStackTrace();
-            MainViewModel.reportDeviceEvent("queryCurrent");
+//            MainViewModel.reportDeviceEvent("queryCurrent");
+
             Log.e(TAG,"查询电流过大失败");
             Log.e(TAG,"查询电流过大失败");
             return 3;
             return 3;
         }
         }
@@ -173,11 +168,9 @@ public class OtherDevice extends BaseDeviceEntity {
      * */
      * */
     public void clearCurrent(){
     public void clearCurrent(){
         try{
         try{
-            SPUtils.getInstance().put("RunningState","0");
-            port.write(new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x55, 0x06, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD}, 150);//
+            port.write(new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x66, 0x06, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD}, 150);//
         }catch (Exception e){
         }catch (Exception e){
             e.printStackTrace();
             e.printStackTrace();
-            MainViewModel.reportDeviceEvent("clearCurrent");
             Log.i(TAG,"电流过大标志清零失败");
             Log.i(TAG,"电流过大标志清零失败");
         }
         }
 
 
@@ -188,18 +181,7 @@ public class OtherDevice extends BaseDeviceEntity {
      */
      */
     public  Integer queryInfrared(){
     public  Integer queryInfrared(){
         try {
         try {
-            SPUtils.getInstance().put("RunningState","0");
-//            Thread thread=new Thread(new Runnable() {
-//                @Override
-//                public void run() {
-//                    try {
-//                        port.write(new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x55, 0x03, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD}, 100);//
-//                    } catch (IOException e) {
-//                        e.printStackTrace();
-//                    }
-//                }
-//            });
-//            thread.start();
+
             port.write(new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x55, 0x03, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD}, 100);//
             port.write(new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x55, 0x03, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD}, 100);//
             byte[] response = new byte[12];
             byte[] response = new byte[12];
             int readLen = port.read(response,200);
             int readLen = port.read(response,200);
@@ -212,22 +194,27 @@ public class OtherDevice extends BaseDeviceEntity {
                 Log.i(TAG,"response[6]"+response[6]);
                 Log.i(TAG,"response[6]"+response[6]);
                 return (int) 2;
                 return (int) 2;
             }
             }
-            MainViewModel.reportDeviceEvent("queryInfrared");
+            int num=SPUtils.getInstance().getInt("queryInfraredError");
+            SPUtils.getInstance().put("queryInfraredError",num+1);
             return 0;
             return 0;
         }catch ( Exception e ){
         }catch ( Exception e ){
-
-            MainViewModel.reportDeviceEvent("queryInfrared");
-            try {
-                WarnActivity.SendSmsResponse("queryInfrared红外线");
-            } catch (ClientException ex) {
-                ex.printStackTrace();
-            }
             Log.e(TAG,"读取红外线读数错误",e);
             Log.e(TAG,"读取红外线读数错误",e);
+            SPUtils.getInstance().put("RunningState",1);
+            MainViewModel.SendSmsResponse("其他设备未检测到");
             e.printStackTrace();
             e.printStackTrace();
-            return 3;
+            return 0;
         }
         }
     }
     }
 
 
+    public void setPower(){
+        try {
+            byte[] setPower = new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x66, 0x0A, 0x02, 0x20, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD};
+            port.write(setPower, 2000);
+        }catch(Exception e){
+            e.printStackTrace();
+        }
+
+    }
     /**
     /**
      * 伸杆关门
      * 伸杆关门
      */
      */
@@ -236,11 +223,13 @@ public class OtherDevice extends BaseDeviceEntity {
             @Override
             @Override
             public void run() {
             public void run() {
                 try {
                 try {
-                    SPUtils.getInstance().put("RunningState","0");
                     SPUtils.getInstance().put("RodSwitch","0");
                     SPUtils.getInstance().put("RodSwitch","0");
                     //门  66后面 01 表示杆 01表示伸杆  1c表示多少秒  02 1c 表示电流
                     //门  66后面 01 表示杆 01表示伸杆  1c表示多少秒  02 1c 表示电流
                     System.out.println("------------push rod------------------------");
                     System.out.println("------------push rod------------------------");
-                    byte[] pushRodOrderBytes = new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x66, 0x01, 0x01,(byte)0x0B, (byte) 0x02, (byte) 0x1c, (byte) 0xCC, (byte) 0xDD};
+                    SPUtils.getInstance().put("queryInfraredError",0);
+//                    byte[] setPower = new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x66, 0x0A, 0x02, 0x1C, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD};
+//                    port.write(setPower, 100);
+                    byte[] pushRodOrderBytes = new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x66, 0x01, 0x01,(byte)0x0B, (byte) 0xff, (byte) 0xff, (byte) 0xCC, (byte) 0xDD};
                     port.write(pushRodOrderBytes, 100);
                     port.write(pushRodOrderBytes, 100);
                     System.out.println("bytes:"+ByteUtil.bytesToString(pushRodOrderBytes));
                     System.out.println("bytes:"+ByteUtil.bytesToString(pushRodOrderBytes));
 //                    byte[] pushRodOrderBytes2 = new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x66, 0x01, 0x01,0x1c, (byte) 0xFF,0x1c, (byte) 0xCC, (byte) 0xDD};
 //                    byte[] pushRodOrderBytes2 = new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x66, 0x01, 0x01,0x1c, (byte) 0xFF,0x1c, (byte) 0xCC, (byte) 0xDD};
@@ -248,22 +237,27 @@ public class OtherDevice extends BaseDeviceEntity {
                     getRodOpenNumber();
                     getRodOpenNumber();
                     byte[] response = new byte[ 12];
                     byte[] response = new byte[ 12];
                     port.read(response,200);
                     port.read(response,200);
-                    byte[] readPushOrderBytes=new byte[]{(byte)0xAA, (byte) 0xBB,0x0A, 0x01, 0x55 ,0x01 , (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD};
-                    port.write(readPushOrderBytes, 100);
-                    byte[] responseRead = new byte[ 12];
-                    port.read(responseRead,200);
+//                    byte[] readPushOrderBytes=new byte[]{(byte)0xAA, (byte) 0xBB,0x0A, 0x01, 0x55 ,0x01 , (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD};
+//                    port.write(readPushOrderBytes, 100);
+//                    byte[] responseRead = new byte[ 12];
+//                    port.read(responseRead,200);
                     System.out.println("push response:"+ByteUtil.bytesToString(response));
                     System.out.println("push response:"+ByteUtil.bytesToString(response));
-                    System.out.println("pushRead response:"+ByteUtil.bytesToString(responseRead));
-//                    SPUtils.getInstance().put("infraredNum1",0);
-//                    SPUtils.getInstance().getInt("infraredNum1",0);
+//                    System.out.println("pushRead response:"+ByteUtil.bytesToString(responseRead));
+                    SPUtils.getInstance().put("infraredNum1",0);
+                    SPUtils.getInstance().getInt("infraredNum1",0);
                     long curr=System.currentTimeMillis();
                     long curr=System.currentTimeMillis();
-                    while (System.currentTimeMillis() - curr < 8000) {
+                    Thread.sleep(500);
+                    while (System.currentTimeMillis() - curr < 6000) {
                         Thread.sleep(300);
                         Thread.sleep(300);
                         Integer infrared = queryInfrared();
                         Integer infrared = queryInfrared();
+                        int ErrorNum=SPUtils.getInstance().getInt("queryInfraredError");
+                        if(ErrorNum>15){
+                            MainViewModel.SendSmsResponse("门磁读取失灵");
+                        }
                         if (infrared == 1) {//有障碍物
                         if (infrared == 1) {//有障碍物
                             //停止关门
                             //停止关门
                             Log.i(TAG, "停止关门");
                             Log.i(TAG, "停止关门");
-                            port.write(new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x66, 0x01, 0x03, (byte) 0x02, 0x02, (byte) 0x1C, (byte) 0xCC, (byte) 0xDD}, 200);
+                            port.write(new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x66, 0x01, 0x03, (byte) 0x05, (byte) 0xff, (byte) 0xff, (byte) 0xCC, (byte) 0xDD}, 200);
                             int num=SPUtils.getInstance().getInt("infraredNum1")+1;
                             int num=SPUtils.getInstance().getInt("infraredNum1")+1;
                             SPUtils.getInstance().put("infraredNum1",num);
                             SPUtils.getInstance().put("infraredNum1",num);
                             if(num<3){
                             if(num<3){
@@ -290,13 +284,9 @@ public class OtherDevice extends BaseDeviceEntity {
                         }
                         }
                     }
                     }
                 }catch (Exception e ){
                 }catch (Exception e ){
-                    MainViewModel.reportDeviceEvent("pushRod");
-                    try {
-                        WarnActivity.SendSmsResponse("pushRod关门");
-                    } catch (ClientException ex) {
-                        ex.printStackTrace();
-                    }
                     e.printStackTrace();
                     e.printStackTrace();
+                    SPUtils.getInstance().put("RunningState",1);
+                    MainViewModel.SendSmsResponse("其他设备未检测到");
                     Log.i(TAG,"关门失败");
                     Log.i(TAG,"关门失败");
                 }
                 }
             }
             }
@@ -311,10 +301,9 @@ public class OtherDevice extends BaseDeviceEntity {
             @Override
             @Override
             public void run() {
             public void run() {
                 try {
                 try {
-                    SPUtils.getInstance().put("RunningState","0");
                     SPUtils.getInstance().put("RodSwitch","1");
                     SPUtils.getInstance().put("RodSwitch","1");
                     //伸杆  66后面 01 表示杆 02表示缩杆  1c表示多少秒  02 1c 表示电流
                     //伸杆  66后面 01 表示杆 02表示缩杆  1c表示多少秒  02 1c 表示电流
-                    byte[] pullRodOrderBytes = new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x66, 0x01, 0x02,0x0B, (byte) 0xff, (byte) 0x1c, (byte) 0xCC, (byte) 0xDD};
+                    byte[] pullRodOrderBytes = new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x66, 0x01, 0x02,0x0B, (byte) 0xff, (byte) 0xff, (byte) 0xCC, (byte) 0xDD};
                     int writeLen = port.write(pullRodOrderBytes, 100);
                     int writeLen = port.write(pullRodOrderBytes, 100);
                     System.out.println("pullRod "+writeLen);
                     System.out.println("pullRod "+writeLen);
                     byte[] response = new byte[12];
                     byte[] response = new byte[12];
@@ -327,13 +316,9 @@ public class OtherDevice extends BaseDeviceEntity {
                     System.out.println("pullRead response:"+ByteUtil.bytesToString(responseRead));
                     System.out.println("pullRead response:"+ByteUtil.bytesToString(responseRead));
                     System.out.println("response:"+ByteUtil.bytesToString(response));
                     System.out.println("response:"+ByteUtil.bytesToString(response));
                 }catch ( Exception e ){
                 }catch ( Exception e ){
-                    MainViewModel.reportDeviceEvent("pullRod");
-                    try {
-                        WarnActivity.SendSmsResponse("pullRod开门");
-                    } catch (ClientException ex) {
-                        ex.printStackTrace();
-                    }
                     e.printStackTrace();
                     e.printStackTrace();
+                    SPUtils.getInstance().put("RunningState",1);
+                    MainViewModel.SendSmsResponse("其他设备未检测到");
                     Log.i(TAG,"开门失败");
                     Log.i(TAG,"开门失败");
                 }
                 }
             }
             }
@@ -346,7 +331,6 @@ public class OtherDevice extends BaseDeviceEntity {
      */
      */
     public  Integer openLock(){
     public  Integer openLock(){
         try {
         try {
-            SPUtils.getInstance().put("RunningState","0");
 //            byte[] openLockOrderBytes = new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x66, 0x02, 0x01,0x03, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD};
 //            byte[] openLockOrderBytes = new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x66, 0x02, 0x01,0x03, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD};
           byte[] openLockOrderBytes = new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x66, 0x02, 0x01,0x07, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD};
           byte[] openLockOrderBytes = new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x66, 0x02, 0x01,0x07, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD};
             port.write(openLockOrderBytes,200);
             port.write(openLockOrderBytes,200);
@@ -357,12 +341,12 @@ public class OtherDevice extends BaseDeviceEntity {
             if(response[4]==102&&response[5]==2&&response[6]==1){
             if(response[4]==102&&response[5]==2&&response[6]==1){
                 return 0;
                 return 0;
             }else {
             }else {
-                MainViewModel.reportDeviceEvent("openLock");
                 return 0;
                 return 0;
             }
             }
         }catch ( Exception e ){
         }catch ( Exception e ){
             e.printStackTrace();
             e.printStackTrace();
-            MainViewModel.reportDeviceEvent("openLock");
+            SPUtils.getInstance().put("RunningState",1);
+            MainViewModel.SendSmsResponse("其他设备未检测到");
             Log.i(TAG,"开收运门失败");
             Log.i(TAG,"开收运门失败");
             return 0;
             return 0;
         }
         }
@@ -374,7 +358,6 @@ public class OtherDevice extends BaseDeviceEntity {
      */
      */
     public Integer closeLock(){
     public Integer closeLock(){
         try {
         try {
-            SPUtils.getInstance().put("RunningState","0");
             byte[] closeLockOrderBytes = new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x66, 0x02, 0x02, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD};
             byte[] closeLockOrderBytes = new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x66, 0x02, 0x02, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD};
             port.write(closeLockOrderBytes, 200);
             port.write(closeLockOrderBytes, 200);
             byte[] response=new byte[12];
             byte[] response=new byte[12];
@@ -383,12 +366,11 @@ public class OtherDevice extends BaseDeviceEntity {
             if(response[4]==102&&response[5]==2&&response[6]==2){
             if(response[4]==102&&response[5]==2&&response[6]==2){
                 return 0;
                 return 0;
             }else {
             }else {
-                MainViewModel.reportDeviceEvent("closeLock");
                 return 0;
                 return 0;
             }
             }
         }catch ( Exception e ){
         }catch ( Exception e ){
             e.printStackTrace();
             e.printStackTrace();
-            MainViewModel.reportDeviceEvent("closeLock");
+
             Log.i(TAG,"关收运门失败");
             Log.i(TAG,"关收运门失败");
             return 0;
             return 0;
         }
         }
@@ -400,7 +382,6 @@ public class OtherDevice extends BaseDeviceEntity {
      */
      */
     public  Integer getDistance(){
     public  Integer getDistance(){
         try {
         try {
-            SPUtils.getInstance().put("RunningState","0");
             byte[] bytes = new byte[]{(byte)0xAA ,(byte)0xBB ,(byte)0x0C ,(byte)0x01 ,(byte)0x55 ,(byte)0x01 ,(byte)0xFF ,(byte)0xFF,(byte)0xFF ,(byte)0xFF ,(byte)0xCC ,(byte)0xDD};
             byte[] bytes = new byte[]{(byte)0xAA ,(byte)0xBB ,(byte)0x0C ,(byte)0x01 ,(byte)0x55 ,(byte)0x01 ,(byte)0xFF ,(byte)0xFF,(byte)0xFF ,(byte)0xFF ,(byte)0xCC ,(byte)0xDD};
             port.write(bytes,200);
             port.write(bytes,200);
             byte[] response = new byte[12];
             byte[] response = new byte[12];
@@ -414,13 +395,13 @@ public class OtherDevice extends BaseDeviceEntity {
             if(response[4]==85&&response[5]==1){
             if(response[4]==85&&response[5]==1){
                 return disInt;
                 return disInt;
             }else {
             }else {
-                MainViewModel.reportDeviceEvent("getDistance");
-                return 1000;
+                SPUtils.getInstance().put("RunningState",5);
+                return 0;
             }
             }
         }catch ( Exception e ){
         }catch ( Exception e ){
             e.printStackTrace();
             e.printStackTrace();
-            MainViewModel.reportDeviceEvent("getDistance");
-            Log.i(TAG,"测距离失败");
+            SPUtils.getInstance().put("RunningState",1);
+            MainViewModel.SendSmsResponse("其他设备未检测到");
             return 0;
             return 0;
         }
         }
     }
     }
@@ -430,7 +411,6 @@ public class OtherDevice extends BaseDeviceEntity {
      */
      */
     public Integer geTemperature(){//获取温度
     public Integer geTemperature(){//获取温度
         try{
         try{
-            SPUtils.getInstance().put("RunningState","0");
 //            byte[] bytes=new byte[]{(byte) 0xAA, (byte) 0xBB,0x0D,0x01,0x55, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD};
 //            byte[] bytes=new byte[]{(byte) 0xAA, (byte) 0xBB,0x0D,0x01,0x55, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD};
             byte[] bytes = new byte[]{(byte)0xAA ,(byte)0xBB ,(byte)0x0C ,(byte)0x01 ,(byte)0x55 ,(byte)0x02 ,(byte)0xFF ,(byte)0xFF,(byte)0xFF ,(byte)0xFF ,(byte)0xCC ,(byte)0xDD};
             byte[] bytes = new byte[]{(byte)0xAA ,(byte)0xBB ,(byte)0x0C ,(byte)0x01 ,(byte)0x55 ,(byte)0x02 ,(byte)0xFF ,(byte)0xFF,(byte)0xFF ,(byte)0xFF ,(byte)0xCC ,(byte)0xDD};
             port.write(bytes,200);
             port.write(bytes,200);
@@ -451,12 +431,13 @@ public class OtherDevice extends BaseDeviceEntity {
             if(response[4]==85&&response[5]==2){
             if(response[4]==85&&response[5]==2){
                 return temInt;
                 return temInt;
             }else {
             }else {
-                MainViewModel.reportDeviceEvent("geTemperature");
-                return 123456789;
+                SPUtils.getInstance().put("RunningState",5);
+                return 0;
             }
             }
         }catch ( Exception e ){
         }catch ( Exception e ){
             e.printStackTrace();
             e.printStackTrace();
-            MainViewModel.reportDeviceEvent("geTemperature");
+            SPUtils.getInstance().put("RunningState",1);
+            MainViewModel.SendSmsResponse("其他设备未检测到");
             Log.i(TAG,"测温度失败");
             Log.i(TAG,"测温度失败");
             return 0;
             return 0;
         }
         }
@@ -466,88 +447,72 @@ public class OtherDevice extends BaseDeviceEntity {
      * */
      * */
     public void openLight1(){
     public void openLight1(){
         try{
         try{
-            SPUtils.getInstance().put("RunningState","0");
             byte[] bytes=new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0D, 0x01, 0x66, 0x01, 0x01, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD};
             byte[] bytes=new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0D, 0x01, 0x66, 0x01, 0x01, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD};
             port.write(bytes,200);
             port.write(bytes,200);
         }catch (Exception e){
         }catch (Exception e){
-            MainViewModel.reportDeviceEvent("openLight1");
             e.printStackTrace();
             e.printStackTrace();
             Log.i(TAG,"开灯1失败");
             Log.i(TAG,"开灯1失败");
         }
         }
     }
     }
     public void closeLight1(){
     public void closeLight1(){
         try{
         try{
-            SPUtils.getInstance().put("RunningState","0");
             byte[] bytes=new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0D, 0x01, 0x66, 0x01, 0x00, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD};
             byte[] bytes=new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0D, 0x01, 0x66, 0x01, 0x00, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD};
             port.write(bytes,200);
             port.write(bytes,200);
         }catch (Exception e){
         }catch (Exception e){
-            MainViewModel.reportDeviceEvent("closeLight1");
             e.printStackTrace();
             e.printStackTrace();
             Log.i(TAG,"关灯1失败");
             Log.i(TAG,"关灯1失败");
         }
         }
     }
     }
     public void openLight2(){
     public void openLight2(){
         try{
         try{
-            SPUtils.getInstance().put("RunningState","0");
             byte[] bytes=new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0D, 0x01, 0x66, 0x02, 0x01, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD};
             byte[] bytes=new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0D, 0x01, 0x66, 0x02, 0x01, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD};
             port.write(bytes,200);
             port.write(bytes,200);
         }catch (Exception e){
         }catch (Exception e){
-            MainViewModel.reportDeviceEvent("openLight2");
             e.printStackTrace();
             e.printStackTrace();
             Log.i(TAG,"开灯2失败");
             Log.i(TAG,"开灯2失败");
         }
         }
     }
     }
     public void closeLight2(){
     public void closeLight2(){
         try{
         try{
-            SPUtils.getInstance().put("RunningState","0");
             byte[] bytes=new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0D, 0x01, 0x66, 0x02, 0x00, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD};
             byte[] bytes=new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0D, 0x01, 0x66, 0x02, 0x00, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD};
             port.write(bytes,200);
             port.write(bytes,200);
         }catch (Exception e){
         }catch (Exception e){
-            MainViewModel.reportDeviceEvent("closeLight2");
             e.printStackTrace();
             e.printStackTrace();
             Log.i(TAG,"关灯2失败");
             Log.i(TAG,"关灯2失败");
         }
         }
     }
     }
     public void openLight3(){
     public void openLight3(){
         try{
         try{
-            SPUtils.getInstance().put("RunningState","0");
             byte[] bytes=new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0D, 0x01, 0x66, 0x03, 0x01, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD};
             byte[] bytes=new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0D, 0x01, 0x66, 0x03, 0x01, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD};
             port.write(bytes,200);
             port.write(bytes,200);
         }catch (Exception e){
         }catch (Exception e){
-            MainViewModel.reportDeviceEvent("openLight3");
             e.printStackTrace();
             e.printStackTrace();
             Log.i(TAG,"开灯3失败");
             Log.i(TAG,"开灯3失败");
         }
         }
     }
     }
     public void closeLight3(){
     public void closeLight3(){
         try{
         try{
-            SPUtils.getInstance().put("RunningState","0");
             byte[] bytes=new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0D, 0x01, 0x66, 0x03, 0x00, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD};
             byte[] bytes=new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0D, 0x01, 0x66, 0x03, 0x00, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD};
             port.write(bytes,200);
             port.write(bytes,200);
         }catch (Exception e){
         }catch (Exception e){
-            MainViewModel.reportDeviceEvent("closeLight3");
             e.printStackTrace();
             e.printStackTrace();
             Log.i(TAG,"关灯3失败");
             Log.i(TAG,"关灯3失败");
         }
         }
     }
     }
     public void openLight4(){
     public void openLight4(){
         try{
         try{
-            SPUtils.getInstance().put("RunningState","0");
             byte[] bytes=new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0D, 0x01, 0x66, 0x04, 0x01, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD};
             byte[] bytes=new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0D, 0x01, 0x66, 0x04, 0x01, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD};
             port.write(bytes,200);
             port.write(bytes,200);
         }catch (Exception e){
         }catch (Exception e){
-            MainViewModel.reportDeviceEvent("openLight4");
             e.printStackTrace();
             e.printStackTrace();
             Log.i(TAG,"开灯4失败");
             Log.i(TAG,"开灯4失败");
         }
         }
     }
     }
     public void closeLight4(){
     public void closeLight4(){
         try{
         try{
-            SPUtils.getInstance().put("RunningState","0");
             byte[] bytes=new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0D, 0x01, 0x66, 0x04, 0x00, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD};
             byte[] bytes=new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0D, 0x01, 0x66, 0x04, 0x00, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD};
             port.write(bytes,200);
             port.write(bytes,200);
         }catch (Exception e){
         }catch (Exception e){
-            MainViewModel.reportDeviceEvent("closeLight4");
             e.printStackTrace();
             e.printStackTrace();
             Log.i(TAG,"关灯4失败");
             Log.i(TAG,"关灯4失败");
         }
         }
@@ -558,7 +523,6 @@ public class OtherDevice extends BaseDeviceEntity {
     * */
     * */
     public Integer readWeight(){
     public Integer readWeight(){
         try{
         try{
-            SPUtils.getInstance().put("RunningState","0");
 //            主机发送 : AA BB 0B 01 55 FF FF FF FF FF CC DD
 //            主机发送 : AA BB 0B 01 55 FF FF FF FF FF CC DD
 //            设备回复: AA BB 0B 01 55 00 00 C9 FF FF CC DD
 //            设备回复: AA BB 0B 01 55 00 00 C9 FF FF CC DD
 //            00 00 C9 为数据
 //            00 00 C9 为数据
@@ -579,11 +543,13 @@ public class OtherDevice extends BaseDeviceEntity {
                 }
                 }
                 return weightInt;
                 return weightInt;
             }else {
             }else {
+                SPUtils.getInstance().put("RunningState",4);
+                MainViewModel.SendSmsResponse("readWeight称重失灵");
                 return 0;
                 return 0;
             }
             }
-
         }catch(Exception e){
         }catch(Exception e){
-            MainViewModel.reportDeviceEvent("readWeight");
+            SPUtils.getInstance().put("RunningState",1);
+            MainViewModel.SendSmsResponse("其他设备未检测到");
             e.printStackTrace();
             e.printStackTrace();
             Log.i(TAG,"称重失败");
             Log.i(TAG,"称重失败");
             return 0;
             return 0;

+ 28 - 3
app/src/main/java/com/siwei/recyclebox/deviceUtils/SerialPortUtil.java

@@ -9,15 +9,19 @@ import android.hardware.usb.UsbManager;
 import android.util.Log;
 import android.util.Log;
 import android.widget.Toast;
 import android.widget.Toast;
 
 
+import com.aliyuncs.exceptions.ClientException;
 import com.hoho.android.usbserial.driver.Ch34xSerialDriver;
 import com.hoho.android.usbserial.driver.Ch34xSerialDriver;
 import com.hoho.android.usbserial.driver.ProlificSerialDriver;
 import com.hoho.android.usbserial.driver.ProlificSerialDriver;
 import com.hoho.android.usbserial.driver.UsbSerialDriver;
 import com.hoho.android.usbserial.driver.UsbSerialDriver;
 import com.hoho.android.usbserial.driver.UsbSerialPort;
 import com.hoho.android.usbserial.driver.UsbSerialPort;
 import com.hoho.android.usbserial.driver.UsbSerialProber;
 import com.hoho.android.usbserial.driver.UsbSerialProber;
 
 
+
 import java.util.ArrayList;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.List;
 
 
+import me.goldze.mvvmhabit.utils.SPUtils;
+
 /**
 /**
  * 串口读写工具。
  * 串口读写工具。
  */
  */
@@ -54,15 +58,32 @@ public class SerialPortUtil {
            if (driver instanceof Ch34xSerialDriver){
            if (driver instanceof Ch34xSerialDriver){
                BaseDeviceEntity weightDevice= isWeightDevice(driver);
                BaseDeviceEntity weightDevice= isWeightDevice(driver);
                if( weightDevice!= null){
                if( weightDevice!= null){
+                   SPUtils.getInstance().put("RunningState",1);
                    System.out.println("是称重设备");
                    System.out.println("是称重设备");
                    deviceList.add(weightDevice);
                    deviceList.add(weightDevice);
                    continue;
                    continue;
                }
                }
+//               else{
+//                   try {
+//                       SPUtils.getInstance().put("RunningState","1");
+//                       WarnActivity.SendSmsResponse("称重设备未检测到");
+//                   } catch (ClientException ex) {
+//                       ex.printStackTrace();
+//                   }
+//               }
                BaseDeviceEntity otherDevicde = isOtherDevice(driver);
                BaseDeviceEntity otherDevicde = isOtherDevice(driver);
                if(otherDevicde != null ){
                if(otherDevicde != null ){
                    System.out.println("是otherDevice");
                    System.out.println("是otherDevice");
                    deviceList.add(otherDevicde);
                    deviceList.add(otherDevicde);
+                   SerialPortUtil.getInstance().getOtherDevice().setPower();//设置电量阈值
                }
                }
+//               else {
+//                   try {
+//                       WarnActivity.SendSmsResponse("杆、门磁、锁、温度、红外、超声波设备未检测到");
+//                   } catch (ClientException ex) {
+//                       ex.printStackTrace();
+//                   }
+//               }
 
 
 
 
 //               deviceList.add(new PushRodDevice(mUsbManager,driver));
 //               deviceList.add(new PushRodDevice(mUsbManager,driver));
@@ -157,8 +178,10 @@ public class SerialPortUtil {
     private BaseDeviceEntity isOtherDevice( UsbSerialDriver driver ){
     private BaseDeviceEntity isOtherDevice( UsbSerialDriver driver ){
         OtherDevice otherDevice = new OtherDevice(mUsbManager,driver);
         OtherDevice otherDevice = new OtherDevice(mUsbManager,driver);
         Integer infrared = otherDevice.queryInfrared();
         Integer infrared = otherDevice.queryInfrared();
-        if(infrared == null) {
+        Log.i("otherDevice","---------------------------------------");
+        if(infrared == 0) {
             otherDevice.close();
             otherDevice.close();
+            Log.i("otherDevice","==null");
             return null;
             return null;
         }
         }
         return otherDevice;
         return otherDevice;
@@ -166,8 +189,10 @@ public class SerialPortUtil {
 
 
     private BaseDeviceEntity isWeightDevice(UsbSerialDriver driver){
     private BaseDeviceEntity isWeightDevice(UsbSerialDriver driver){
         WeightDevice weightDevice = new WeightDevice(mUsbManager,driver);
         WeightDevice weightDevice = new WeightDevice(mUsbManager,driver);
-        Integer weight = weightDevice.readWeight();
-        if(weight== null){
+        Integer weight = weightDevice.readIsWeight();
+        Log.i("weightDevice","---------------------------------------");
+        if(weight== 1){
+            Log.i("weightDevice","==NULL");
             weightDevice.close();
             weightDevice.close();
             return null;
             return null;
         }
         }

+ 104 - 46
app/src/main/java/com/siwei/recyclebox/deviceUtils/WeightDevice.java

@@ -3,10 +3,12 @@ package com.siwei.recyclebox.deviceUtils;
 import android.hardware.usb.UsbManager;
 import android.hardware.usb.UsbManager;
 import android.util.Log;
 import android.util.Log;
 
 
+import com.aliyuncs.exceptions.ClientException;
 import com.hoho.android.usbserial.driver.UsbSerialDriver;
 import com.hoho.android.usbserial.driver.UsbSerialDriver;
 import com.hoho.android.usbserial.driver.UsbSerialPort;
 import com.hoho.android.usbserial.driver.UsbSerialPort;
 import com.siwei.recyclebox.ui.main.MainViewModel;
 import com.siwei.recyclebox.ui.main.MainViewModel;
 
 
+
 import java.io.IOException;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.List;
@@ -25,74 +27,130 @@ public class WeightDevice extends BaseDeviceEntity {
      * 读取称重器的读数
      * 读取称重器的读数
      * return weight 单位:g
      * return weight 单位:g
      */
      */
+    public synchronized Integer readIsWeight(){
+        try {
+//            SPUtils.getInstance().put("RunningState",0);//1F 03 00 2A 00 01 A6 7C
+            port.write(new byte[]{0x1F, 0x03, 0x00, 0x2A,0x00, 0x01, (byte) 0xA6, 0x7C},150);//称重器读取数值的请求
+            byte[] response = new byte[7];
+            port.read(response,200);
+            Log.i(TAG,"返回称重"+ByteUtil.bytesToString(response));
+            if(response[1]==3) {
+                byte[] weightBytes = new byte[4];
+                weightBytes[2] = response[3];
+                weightBytes[3] = response[4];
+                int weightInt = ByteUtil.bytes2Int(weightBytes);
+                weightInt=weightInt*5*2;
+                System.out.println("称重读数:"+weightInt);
+                if(weightInt>=327670){
+                    weightInt=0;
+                }
+                return (int) weightInt;
+            }else {
+                return 1;
+            }
+        }catch ( Exception e ){
+            e.printStackTrace();
+            return 1;
+        }
+
+    }
     public synchronized Integer readWeight(){
     public synchronized Integer readWeight(){
         try {
         try {
-            SPUtils.getInstance().put("RunningState","0");
+//            SPUtils.getInstance().put("RunningState","0");//1F 03 00 2A 00 01 A6 7C
             port.write(new byte[]{0x1F, 0x03, 0x00, 0x2A,0x00, 0x01, (byte) 0xA6, 0x7C},150);//称重器读取数值的请求
             port.write(new byte[]{0x1F, 0x03, 0x00, 0x2A,0x00, 0x01, (byte) 0xA6, 0x7C},150);//称重器读取数值的请求
 //            port.write(new byte[]{(byte) 0xAA, (byte) 0xBB,0x0C ,0x01 ,0x55 , (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD},150);//称重器读取数值的请求
 //            port.write(new byte[]{(byte) 0xAA, (byte) 0xBB,0x0C ,0x01 ,0x55 , (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD},150);//称重器读取数值的请求
 //            port.write(new byte[]{(byte) 0xAA, (byte) 0xBB,0x0C ,0x02 ,0x55 , (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD},150);//超声波读取数值的请求
 //            port.write(new byte[]{(byte) 0xAA, (byte) 0xBB,0x0C ,0x02 ,0x55 , (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD},150);//超声波读取数值的请求
 //            Thread.sleep(100);
 //            Thread.sleep(100);
             byte[] response = new byte[7];
             byte[] response = new byte[7];
-            byte[] responseCache = new byte[7];
-            long curTimeMiles = System.currentTimeMillis();
-            int readLengthCache = 0;
-            while (System.currentTimeMillis()-curTimeMiles<200){
-                int len = port.read(response,10);
-                System.out.println("读取到:"+ByteUtil.bytesToString(response)+"  len:"+len);
-                if(len>0){
-                    if(readLengthCache == 0 && response[0] != 0x1f){//一个包的起始为0x22
-                        continue;
-                    }
-                    for(int i = 0 ;i<len;i++){
-                        if(readLengthCache>=7)
-                            break;
-                        responseCache[readLengthCache] = response[i];
-                        readLengthCache++;
-                    }
+            port.read(response,200);
+            Log.i(TAG,"返回称重"+ByteUtil.bytesToString(response));
+//            byte[] responseCache = new byte[7];
+//            long curTimeMiles = System.currentTimeMillis();
+//            int readLengthCache = 0;
+//            while (System.currentTimeMillis()-curTimeMiles<200){
+//                int len = port.read(response,10);
+//                System.out.println("读取到:"+ByteUtil.bytesToString(response)+"  len:"+len);
+//                if(len>0){
+//                    if(readLengthCache == 0 && response[0] != 0x1f){//一个包的起始为0x22
+//                        continue;
+//                    }
+//                    for(int i = 0 ;i<len;i++){
+//                        if(readLengthCache>=7)
+//                            break;
+//                        responseCache[readLengthCache] = response[i];
+//                        readLengthCache++;
+//                    }
+//                }
+//            }
+//            if(readLengthCache == 0) {
+//                return 1;
+//            }
+//            if(readLengthCache >= 7 && responseCache[0] == 0x1f && responseCache[1] == 0x03){
+//                byte[] weightBytes = new byte[4];
+//                weightBytes[2] = responseCache[3];
+//                weightBytes[3] = responseCache[4];
+//                int weightInt = ByteUtil.bytes2Int(weightBytes);
+//                weightInt=weightInt*5*2;
+//                System.out.println("称重读数:"+weightInt);
+//                if(weightInt>=327670){
+//                    weightInt=0;
+//                }
+//                return (int) weightInt;
+//            }else{
+//                SPUtils.getInstance().put("RunningState","4");
+//                try {
+//                    WarnActivity.SendSmsResponse("readWeight称重故障");
+//                } catch (ClientException ex) {
+//                    ex.printStackTrace();
+//                }
+//            }
+
+            if(response[1]==3) {
+                long curTimeMiles = System.currentTimeMillis();
+                int num=0,a=0;
+                while (System.currentTimeMillis()-curTimeMiles<200){
+                    port.read(response,10);
+                    byte[] weightBytes = new byte[4];
+                    weightBytes[2] = response[3];
+                    weightBytes[3] = response[4];
+                    int weightInt = ByteUtil.bytes2Int(weightBytes);
+//                    weightInt=weightInt*5*2;
+                    num += (weightInt * 5 * 2);
+                    a++;
+                    Log.i("while","********"+a);
                 }
                 }
-            }
-            if(readLengthCache == 0)
-                return null;
-            if(readLengthCache >= 7 && responseCache[0] == 0x1f && responseCache[1] == 0x03){
+                Log.i("num",num+"****"+a+"");
                 byte[] weightBytes = new byte[4];
                 byte[] weightBytes = new byte[4];
-                weightBytes[2] = responseCache[3];
-                weightBytes[3] = responseCache[4];
+                weightBytes[2] = response[3];
+                weightBytes[3] = response[4];
                 int weightInt = ByteUtil.bytes2Int(weightBytes);
                 int weightInt = ByteUtil.bytes2Int(weightBytes);
                 weightInt=weightInt*5*2;
                 weightInt=weightInt*5*2;
                 System.out.println("称重读数:"+weightInt);
                 System.out.println("称重读数:"+weightInt);
+                int weightNumber=num/a++;
                 if(weightInt>=327670){
                 if(weightInt>=327670){
-                    weightInt=0;
+                    weightNumber=0;
                 }
                 }
-                return (int) weightInt;
+                return (int) weightNumber;
+            }else {
+                Log.i("返回称重","错误");
+                SPUtils.getInstance().put("RunningState",1);
+                MainViewModel.SendSmsResponse("readWeight称重失灵");
+                return 1;
             }
             }
             /*int readLen = port.read(response,200);
             /*int readLen = port.read(response,200);
             System.out.println("readweight len:"+readLen+"  bytes;"+ByteUtil.bytesToString(response));
             System.out.println("readweight len:"+readLen+"  bytes;"+ByteUtil.bytesToString(response));
             */
             */
-            return 0;
-//            return latestWeight;
-            /*byte[] response = new byte[7];
-            int len = port.read(response,200);
-            System.out.println("readLength:"+len);
-            System.out.println("response:"+ ByteString.of(response));
-            if(len<7){
-                Log.e(TAG,"读取数据错误,读取字节数:"+len+" 应读取字节数:7");
-            }
-            byte[] weightBytes = new byte[2];
-            weightBytes[0] = response[3];
-            weightBytes[1] = response[4];
-            short weightShort = ByteUtil.byte2short(weightBytes);
-            return weightShort;*/
+//            return 1;
+//
         }catch ( Exception e ){
         }catch ( Exception e ){
             e.printStackTrace();
             e.printStackTrace();
-            MainViewModel.reportDeviceEvent("readWeight");
+            SPUtils.getInstance().put("RunningState",1);
+            MainViewModel.SendSmsResponse("readWeight称重设备未检测到");
+
             Log.e(TAG,"读取称重器读数错误",e);
             Log.e(TAG,"读取称重器读数错误",e);
-            return null;
+            return 1;
         }
         }
-//        try {
-//            port.close();
-//        } catch (IOException e) {
-//            e.printStackTrace();
-//        }
+
     }
     }
     /**
     /**
      * 读数清零
      * 读数清零

+ 5 - 4
app/src/main/java/com/siwei/recyclebox/ui/main/MainActivity.java

@@ -34,18 +34,19 @@ public class MainActivity extends BaseActivity<ViewDataBinding,MainViewModel> {
     @Override
     @Override
     public void initParam() {
     public void initParam() {
         super.initParam();
         super.initParam();
-        Intent service = new Intent(getApplication(), MyService.class);
-        getApplication().startService(service);
+//        Intent service = new Intent(getApplication(), MyService.class);
+//        getApplication().startService(service);
     }
     }
 
 
     //页面数据初始化方法。
     //页面数据初始化方法。
     @Override
     @Override
     public void initData() {
     public void initData() {
-        super.initData();
 //        viewModel.btnClick.onClick();
 //        viewModel.btnClick.onClick();
 //        viewModel.btnText.set("");
 //        viewModel.btnText.set("");
         viewModel.registerAliIoTListener();
         viewModel.registerAliIoTListener();
-        viewModel.startBLEServer();
+//        viewModel.repeatTask();
+        Log.i("repeatTask","*****************************************");
+//        viewModel.startBLEServer();
 //        viewModel.netWorkInfo();
 //        viewModel.netWorkInfo();
 //        viewModel.openWeight();
 //        viewModel.openWeight();
 //        System.out.println("serialPortUtil.readControllerStatus:"+ SerialPortUtil.getInstance().readRelayControllerStatus());
 //        System.out.println("serialPortUtil.readControllerStatus:"+ SerialPortUtil.getInstance().readRelayControllerStatus());

+ 248 - 191
app/src/main/java/com/siwei/recyclebox/ui/main/MainViewModel.java

@@ -9,7 +9,6 @@ import android.app.PendingIntent;
 import android.bluetooth.BluetoothAdapter;
 import android.bluetooth.BluetoothAdapter;
 import android.bluetooth.BluetoothDevice;
 import android.bluetooth.BluetoothDevice;
 import android.bluetooth.BluetoothGatt;
 import android.bluetooth.BluetoothGatt;
-import android.bluetooth.BluetoothGattCallback;
 import android.bluetooth.BluetoothGattCharacteristic;
 import android.bluetooth.BluetoothGattCharacteristic;
 import android.bluetooth.BluetoothGattDescriptor;
 import android.bluetooth.BluetoothGattDescriptor;
 import android.bluetooth.BluetoothGattServer;
 import android.bluetooth.BluetoothGattServer;
@@ -23,43 +22,29 @@ import android.bluetooth.le.BluetoothLeAdvertiser;
 import android.content.BroadcastReceiver;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Context;
 import android.content.Intent;
 import android.content.Intent;
-import android.content.IntentFilter;
 import android.content.pm.PackageManager;
 import android.content.pm.PackageManager;
 import android.content.res.AssetFileDescriptor;
 import android.content.res.AssetFileDescriptor;
 import android.databinding.ObservableField;
 import android.databinding.ObservableField;
-import android.hardware.usb.UsbDevice;
-import android.hardware.usb.UsbDeviceConnection;
 import android.hardware.usb.UsbManager;
 import android.hardware.usb.UsbManager;
 import android.media.MediaPlayer;
 import android.media.MediaPlayer;
 import android.net.ConnectivityManager;
 import android.net.ConnectivityManager;
 import android.net.NetworkInfo;
 import android.net.NetworkInfo;
-import android.net.Uri;
 import android.net.wifi.WifiInfo;
 import android.net.wifi.WifiInfo;
 import android.net.wifi.WifiManager;
 import android.net.wifi.WifiManager;
 import android.os.BatteryManager;
 import android.os.BatteryManager;
 import android.os.Build;
 import android.os.Build;
-import android.os.Bundle;
-import android.os.Environment;
 import android.os.Handler;
 import android.os.Handler;
-import android.os.Message;
 import android.os.ParcelUuid;
 import android.os.ParcelUuid;
-import android.os.SystemClock;
-import android.speech.tts.TextToSpeech;
-import android.speech.tts.TextToSpeechService;
 import android.support.annotation.NonNull;
 import android.support.annotation.NonNull;
 import android.support.annotation.RequiresApi;
 import android.support.annotation.RequiresApi;
 import android.support.v4.app.ActivityCompat;
 import android.support.v4.app.ActivityCompat;
-import android.support.v4.content.FileProvider;
-import android.support.v7.app.AppCompatActivity;
 import android.telephony.TelephonyManager;
 import android.telephony.TelephonyManager;
 import android.util.Log;
 import android.util.Log;
 import android.view.View;
 import android.view.View;
-import android.widget.Toast;
 
 
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.alibaba.fastjson.JSONObject;
 import com.aliyun.alink.dm.api.IOta;
 import com.aliyun.alink.dm.api.IOta;
-import com.aliyun.alink.dm.api.ResultCallback;
 import com.aliyun.alink.linkkit.api.LinkKit;
 import com.aliyun.alink.linkkit.api.LinkKit;
 import com.aliyun.alink.linksdk.cmp.connect.channel.MqttPublishRequest;
 import com.aliyun.alink.linksdk.cmp.connect.channel.MqttPublishRequest;
 import com.aliyun.alink.linksdk.cmp.connect.channel.MqttRrpcRegisterRequest;
 import com.aliyun.alink.linksdk.cmp.connect.channel.MqttRrpcRegisterRequest;
@@ -69,35 +54,28 @@ import com.aliyun.alink.linksdk.cmp.core.base.ARequest;
 import com.aliyun.alink.linksdk.cmp.core.base.AResponse;
 import com.aliyun.alink.linksdk.cmp.core.base.AResponse;
 import com.aliyun.alink.linksdk.cmp.core.base.ConnectState;
 import com.aliyun.alink.linksdk.cmp.core.base.ConnectState;
 import com.aliyun.alink.linksdk.cmp.core.listener.IConnectNotifyListener;
 import com.aliyun.alink.linksdk.cmp.core.listener.IConnectNotifyListener;
-import com.aliyun.alink.linksdk.cmp.core.listener.IConnectRrpcHandle;
-import com.aliyun.alink.linksdk.cmp.core.listener.IConnectRrpcListener;
 import com.aliyun.alink.linksdk.cmp.core.listener.IConnectSendListener;
 import com.aliyun.alink.linksdk.cmp.core.listener.IConnectSendListener;
 import com.aliyun.alink.linksdk.cmp.core.listener.IConnectSubscribeListener;
 import com.aliyun.alink.linksdk.cmp.core.listener.IConnectSubscribeListener;
 import com.aliyun.alink.linksdk.tmp.api.OutputParams;
 import com.aliyun.alink.linksdk.tmp.api.OutputParams;
 import com.aliyun.alink.linksdk.tmp.device.payload.ValueWrapper;
 import com.aliyun.alink.linksdk.tmp.device.payload.ValueWrapper;
 import com.aliyun.alink.linksdk.tmp.listener.IPublishResourceListener;
 import com.aliyun.alink.linksdk.tmp.listener.IPublishResourceListener;
-import com.aliyun.alink.linksdk.tmp.listener.ITResRequestHandler;
-import com.aliyun.alink.linksdk.tmp.listener.ITResResponseCallback;
-import com.aliyun.alink.linksdk.tmp.utils.ErrorInfo;
 import com.aliyun.alink.linksdk.tools.AError;
 import com.aliyun.alink.linksdk.tools.AError;
-import com.hoho.android.usbserial.driver.Ch34xSerialDriver;
-import com.hoho.android.usbserial.driver.ProlificSerialDriver;
-import com.hoho.android.usbserial.driver.UsbSerialDriver;
+import com.aliyuncs.CommonRequest;
+import com.aliyuncs.CommonResponse;
+import com.aliyuncs.DefaultAcsClient;
+import com.aliyuncs.IAcsClient;
+import com.aliyuncs.exceptions.ClientException;
+import com.aliyuncs.http.MethodType;
+import com.aliyuncs.profile.DefaultProfile;
 import com.hoho.android.usbserial.driver.UsbSerialPort;
 import com.hoho.android.usbserial.driver.UsbSerialPort;
-import com.hoho.android.usbserial.driver.UsbSerialProber;
-import com.hoho.android.usbserial.util.SerialInputOutputManager;
 import com.siwei.recyclebox.BuildConfig;
 import com.siwei.recyclebox.BuildConfig;
 import com.siwei.recyclebox.deviceUtils.SerialPortUtil;
 import com.siwei.recyclebox.deviceUtils.SerialPortUtil;
-import com.siwei.recyclebox.deviceUtils.WeightDevice;
 
 
-import java.io.BufferedReader;
-import java.io.DataOutputStream;
 import java.io.File;
 import java.io.File;
 import java.io.IOException;
 import java.io.IOException;
-import java.io.InputStreamReader;
-import java.nio.charset.Charset;
 import java.util.ArrayList;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Arrays;
+import java.util.Calendar;
 import java.util.HashMap;
 import java.util.HashMap;
 import java.util.List;
 import java.util.List;
 import java.util.Locale;
 import java.util.Locale;
@@ -106,54 +84,58 @@ import java.util.Spliterator;
 import java.util.Timer;
 import java.util.Timer;
 import java.util.TimerTask;
 import java.util.TimerTask;
 import java.util.UUID;
 import java.util.UUID;
-import java.util.concurrent.CompletableFuture;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import java.util.concurrent.atomic.AtomicInteger;
 
 
 import me.goldze.mvvmhabit.base.BaseModel;
 import me.goldze.mvvmhabit.base.BaseModel;
 import me.goldze.mvvmhabit.base.BaseViewModel;
 import me.goldze.mvvmhabit.base.BaseViewModel;
-import me.goldze.mvvmhabit.binding.command.BindingAction;
-import me.goldze.mvvmhabit.binding.command.BindingCommand;
 import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
 import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
-import me.goldze.mvvmhabit.http.ApiDisposableObserver;
+import me.goldze.mvvmhabit.crash.CaocConfig;
+import me.goldze.mvvmhabit.crash.CustomActivityOnCrash;
+import me.goldze.mvvmhabit.crash.DefaultErrorActivity;
 import me.goldze.mvvmhabit.utils.SPUtils;
 import me.goldze.mvvmhabit.utils.SPUtils;
 import me.goldze.mvvmhabit.utils.StringUtils;
 import me.goldze.mvvmhabit.utils.StringUtils;
+import okhttp3.OkHttpClient;
+import okhttp3.Response;
 import okio.ByteString;
 import okio.ByteString;
 import top.maybesix.xhlibrary.serialport.ComPortData;
 import top.maybesix.xhlibrary.serialport.ComPortData;
 import top.maybesix.xhlibrary.serialport.SerialPortHelper;
 import top.maybesix.xhlibrary.serialport.SerialPortHelper;
 import top.maybesix.xhlibrary.util.HexStringUtils;
 import top.maybesix.xhlibrary.util.HexStringUtils;
 
 
-import com.siwei.recyclebox.entity.DataEntity;
-import com.siwei.recyclebox.service.MyService;
 import com.siwei.recyclebox.utils.DemoOne;
 import com.siwei.recyclebox.utils.DemoOne;
 import com.siwei.recyclebox.utils.SilentInstall;
 import com.siwei.recyclebox.utils.SilentInstall;
 import com.siwei.recyclebox.utils.unZipFileDemo;
 import com.siwei.recyclebox.utils.unZipFileDemo;
-import com.tencent.bugly.crashreport.CrashReport;
+import com.zhy.http.okhttp.OkHttpUtils;
 
 
 import static android.content.Context.BATTERY_SERVICE;
 import static android.content.Context.BATTERY_SERVICE;
-import static android.content.Context.EUICC_SERVICE;
 import static java.lang.Thread.sleep;
 import static java.lang.Thread.sleep;
 
 
 public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnSerialPortReceivedListener{
 public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnSerialPortReceivedListener{
 
 
     Integer lockSwitch=0;//锁的状态
     Integer lockSwitch=0;//锁的状态
     private static Context context;//上下文
     private static Context context;//上下文
+    private final Timer timer=new Timer();
+    private TimerTask task1;
+    private TimerTask task2;
+    private TimerTask task3;
     @RequiresApi(api = Build.VERSION_CODES.O)
     @RequiresApi(api = Build.VERSION_CODES.O)
     public void onCreate(){
     public void onCreate(){
         super.onCreate();
         super.onCreate();
-        Intent service = new Intent(getApplication(), MyService.class);
-        getApplication().startService(service);//打开服务,把程序写在服务里不容易被系统kill
+//        Intent service = new Intent(getApplication(), MyService.class);
+//        getApplication().startService(service);//打开服务,把程序写在服务里不容易被系统kill
 
 
         MainViewModel.context = getApplication();
         MainViewModel.context = getApplication();
-        SPUtils.getInstance().put("RunningState","0");//设备运行状态
+        SPUtils.getInstance().put("RunningState",0);//设备运行状态
         SPUtils.getInstance().put("magDoorSwitch","0");//门磁状态
         SPUtils.getInstance().put("magDoorSwitch","0");//门磁状态
-        SPUtils.getInstance().put("type","0");//小程序传送数据类型
 
 
         BatteryManager batteryManager = (BatteryManager)getSystemService(BATTERY_SERVICE);//电量管理
         BatteryManager batteryManager = (BatteryManager)getSystemService(BATTERY_SERVICE);//电量管理
         int battery = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);//查询电量值
         int battery = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);//查询电量值
         Log.i(TAG,"电量值="+battery);
         Log.i(TAG,"电量值="+battery);
 
 
+
+        if(SPUtils.getInstance().getInt("TaskSwitch")==1){
+            repeatTask();
+        }
+        SPUtils.getInstance().put("TaskSwitch",0);
+
     }
     }
     private MediaPlayer mMediaPlay=null;
     private MediaPlayer mMediaPlay=null;
     public ObservableField<String> textReadWeightData = new ObservableField<>();//称重数据显示
     public ObservableField<String> textReadWeightData = new ObservableField<>();//称重数据显示
@@ -212,7 +194,7 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
 
 
     int a=1;
     int a=1;
     public View.OnClickListener btnClickOpenLight1234 = new View.OnClickListener() {
     public View.OnClickListener btnClickOpenLight1234 = new View.OnClickListener() {
-        //打开关闭灯4按钮
+        //打开关闭灯4按钮 开灯1
         @Override
         @Override
         public void onClick(View view) {
         public void onClick(View view) {
             if(a%2==0){
             if(a%2==0){
@@ -268,13 +250,15 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
         //打开关闭灯2按钮
         //打开关闭灯2按钮
         @Override
         @Override
         public void onClick(View view) {
         public void onClick(View view) {
-            if(a%2==0){
-                SerialPortUtil.getInstance().getOtherDevice().openLight2();
-            }
-            else {
-                SerialPortUtil.getInstance().getOtherDevice().closeLight2();
-            }
-            a++;
+//            if(a%2==0){
+//                SerialPortUtil.getInstance().getOtherDevice().openLight2();
+//            }
+//            else {
+//                SerialPortUtil.getInstance().getOtherDevice().closeLight2();
+//            }
+//            a++;
+            int hour=readTime();
+            Log.i("当前时间hour是:",hour+"dian");
         }
         }
     };
     };
     public View.OnClickListener btnClickOpenLight1 = new View.OnClickListener() {
     public View.OnClickListener btnClickOpenLight1 = new View.OnClickListener() {
@@ -288,7 +272,8 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
                 SerialPortUtil.getInstance().getOtherDevice().closeLight1();
                 SerialPortUtil.getInstance().getOtherDevice().closeLight1();
             }
             }
             a++;*/
             a++;*/
-            reportDeviceEvent("haoge");
+//            reportDeviceEvent("haoge");
+            SendSmsResponse("(测试123)");
         }
         }
     };
     };
     public View.OnClickListener btnClickreadWeight = new View.OnClickListener() {
     public View.OnClickListener btnClickreadWeight = new View.OnClickListener() {
@@ -354,7 +339,7 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
         @Override
         @Override
         public void onClick(View view) {
         public void onClick(View view) {
             System.out.println("btnClickReport");
             System.out.println("btnClickReport");
-            reportProperty(1);
+            reportProperty();
         }
         }
     };
     };
     public View.OnClickListener btnQeryIMEI = new View.OnClickListener() {
     public View.OnClickListener btnQeryIMEI = new View.OnClickListener() {
@@ -424,7 +409,6 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
         mHandler.postDelayed(new Runnable() {
         mHandler.postDelayed(new Runnable() {
             @Override
             @Override
             public void run() {
             public void run() {
-                SPUtils.getInstance().put("infraredNum1",0);
                 SerialPortUtil.getInstance().getOtherDevice().pushRod();
                 SerialPortUtil.getInstance().getOtherDevice().pushRod();
             }
             }
         },60000);
         },60000);
@@ -545,15 +529,8 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
     }
     }
     private void pushRodMethod(){
     private void pushRodMethod(){
         //有网下关门
         //有网下关门
-        SPUtils.getInstance().put("infraredNum1",0);
         SerialPortUtil.getInstance().getOtherDevice().pushRod();//关门
         SerialPortUtil.getInstance().getOtherDevice().pushRod();//关门
 
 
-        mHandler.postDelayed(new Runnable() {
-            @Override
-            public void run() {
-                avgWeight();
-            }
-        },500);
 
 
         mHandler.postDelayed(new Runnable() {
         mHandler.postDelayed(new Runnable() {
             @Override
             @Override
@@ -609,12 +586,12 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
         mHandler.postDelayed(new Runnable() {
         mHandler.postDelayed(new Runnable() {
             @Override
             @Override
             public void run() {
             public void run() {
-                reportProperty(2);
+                reportProperty();
             }
             }
         },4000);
         },4000);
     }
     }
 
 
-    private void avgWeight(){
+   /* private void avgWeight(){
         Thread thread=new Thread(new Runnable() {
         Thread thread=new Thread(new Runnable() {
             @Override
             @Override
             public void run() {
             public void run() {
@@ -630,9 +607,9 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
 //                        e.printStackTrace();
 //                        e.printStackTrace();
 //                    }
 //                    }
 //                    arr[n]=n;
 //                    arr[n]=n;
-                    Log.i("n=", String.valueOf(n));
+//                    Log.i("n=", String.valueOf(n));
 //                arr[n]=SerialPortUtil.getInstance().getOtherDevice().readWeight();
 //                arr[n]=SerialPortUtil.getInstance().getOtherDevice().readWeight();
-                    Log.i("weight=", String.valueOf(arr[n]));
+//                    Log.i("weight=", String.valueOf(arr[n]));
                     weight=arr[n]+weight;
                     weight=arr[n]+weight;
                     if(n==3){
                     if(n==3){
                         Log.i("weight=", Arrays.toString(arr));
                         Log.i("weight=", Arrays.toString(arr));
@@ -652,12 +629,11 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
             }
             }
         });
         });
         thread.start();
         thread.start();
-    }
+    }*/
 
 
 
 
     private void pushRodMethodNonet(){
     private void pushRodMethodNonet(){
         //无网下关门
         //无网下关门
-        SPUtils.getInstance().put("infraredNum1",0);
         SerialPortUtil.getInstance().getOtherDevice().pushRod();//关门
         SerialPortUtil.getInstance().getOtherDevice().pushRod();//关门
         mHandler.postDelayed(new Runnable() {
         mHandler.postDelayed(new Runnable() {
             @Override
             @Override
@@ -746,7 +722,7 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
 //                SerialPortUtil.getInstance().getWeightDevice().weightClear();//去皮清零
 //                SerialPortUtil.getInstance().getWeightDevice().weightClear();//去皮清零
 //                SerialPortUtil.getInstance().getOtherDevice().clearWeight();//去皮清零
 //                SerialPortUtil.getInstance().getOtherDevice().clearWeight();//去皮清零
                 pullonNetRodMethod();
                 pullonNetRodMethod();
-                reportProperty(1);
+                reportProperty();
                 Log.i(TAG,"欢迎使用智能回收箱");
                 Log.i(TAG,"欢迎使用智能回收箱");
                 Log.i(TAG,"收到开门=============");
                 Log.i(TAG,"收到开门=============");
                 Thread thread=new Thread(new Runnable() {
                 Thread thread=new Thread(new Runnable() {
@@ -819,8 +795,8 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
                                 public void run() {
                                 public void run() {
                                     try{
                                     try{
                                         long curr=System.currentTimeMillis();
                                         long curr=System.currentTimeMillis();
-                                        while(System.currentTimeMillis()-curr<1800000) {
-                                            Thread.sleep(1000);
+                                        while(System.currentTimeMillis()-curr<300000) {
+                                            Thread.sleep(2000);
                                             Integer magDoorSwitch=SerialPortUtil.getInstance().getOtherDevice().queryMagDoor();//通过门磁感应判断收运门是否关闭
                                             Integer magDoorSwitch=SerialPortUtil.getInstance().getOtherDevice().queryMagDoor();//通过门磁感应判断收运门是否关闭
                                             if(magDoorSwitch==0){
                                             if(magDoorSwitch==0){
                                                 SerialPortUtil.getInstance().getWeightDevice().weightClear();//若关闭,称重清零
                                                 SerialPortUtil.getInstance().getWeightDevice().weightClear();//若关闭,称重清零
@@ -828,9 +804,9 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
                                                 mHandler.postDelayed(new Runnable() {
                                                 mHandler.postDelayed(new Runnable() {
                                                     @Override
                                                     @Override
                                                     public void run() {
                                                     public void run() {
-                                                        reportProperty(1);
+                                                        reportProperty();
                                                     }
                                                     }
-                                                },1000);
+                                                },3000);
                                                 SPUtils.getInstance().put("magDoorSwitch","0");
                                                 SPUtils.getInstance().put("magDoorSwitch","0");
                                                 break;
                                                 break;
                                             }
                                             }
@@ -849,6 +825,18 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
                     e.printStackTrace();
                     e.printStackTrace();
                 }
                 }
             }
             }
+            if(topic.endsWith("thing/service/RemoteAgentClose")) {//关收运门
+                Log.i(TAG, "关运收门");
+                SPUtils.getInstance().put("magDoorSwitch", "0");
+                SerialPortUtil.getInstance().getWeightDevice().weightClear();
+//                                                SerialPortUtil.getInstance().getOtherDevice().clearWeight();
+                mHandler.postDelayed(new Runnable() {
+                    @Override
+                    public void run() {
+                        reportProperty();
+                    }
+                },3000);
+            }
             if(topic.endsWith("thing/service/RemoteOpenPort2")){//开继电器2号端口
             if(topic.endsWith("thing/service/RemoteOpenPort2")){//开继电器2号端口
                 short i = 2;
                 short i = 2;
                 try {
                 try {
@@ -1036,36 +1024,30 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
      * 数据上行
      * 数据上行
      * 上报状态
      * 上报状态
      */
      */
-    public void reportProperty(int num) {
+    public void reportProperty() {
         Log.i(TAG,"上报数据");
         Log.i(TAG,"上报数据");
         try{
         try{
-//            SerialPortUtil.getInstance().getWeightDevice().weightClear();//去皮清零
-    //                SerialPortUtil.getInstance().getOtherDevice().clearWeight();//去皮清零
             Integer weight = 0;
             Integer weight = 0;
-            if(num==1){
-                Log.i("num=1","读一次称重");
+            Log.i("num=1","读一次称重");
+            weight = SerialPortUtil.getInstance().getWeightDevice().readWeight();//读称重
+            if(weight==1){
                 weight = SerialPortUtil.getInstance().getWeightDevice().readWeight();//读称重
                 weight = SerialPortUtil.getInstance().getWeightDevice().readWeight();//读称重
-            }else if(num==2){
-                Log.i("num=2","读平均称重或最后");
-                weight=SPUtils.getInstance().getInt("avgWeight");
             }
             }
-
 //            weight=SerialPortUtil.getInstance().getOtherDevice().readWeight();//称重
 //            weight=SerialPortUtil.getInstance().getOtherDevice().readWeight();//称重
             Integer capacity=0;
             Integer capacity=0;
             capacity=SerialPortUtil.getInstance().getOtherDevice().getDistance();//超声波距离
             capacity=SerialPortUtil.getInstance().getOtherDevice().getDistance();//超声波距离
+            if(capacity==0){
+                capacity=SerialPortUtil.getInstance().getOtherDevice().getDistance();//超声波距离
+            }
             Integer magDoorSwitch=0;
             Integer magDoorSwitch=0;
             magDoorSwitch=SerialPortUtil.getInstance().getOtherDevice().queryMagDoor();//门磁
             magDoorSwitch=SerialPortUtil.getInstance().getOtherDevice().queryMagDoor();//门磁
             Integer currentTemperature=0;
             Integer currentTemperature=0;
             currentTemperature=SerialPortUtil.getInstance().getOtherDevice().geTemperature();//温度
             currentTemperature=SerialPortUtil.getInstance().getOtherDevice().geTemperature();//温度
-
-
+            if(currentTemperature==0){
+                currentTemperature=SerialPortUtil.getInstance().getOtherDevice().geTemperature();//温度
+            }
             Log.i(TAG,"----------------------------------------");
             Log.i(TAG,"----------------------------------------");
 
 
-    //                Integer capacity=0;
-    //                Integer magDoorSwitch=0;
-    //                Integer currentTemperature=0;
-
-
             Map<String, ValueWrapper> locationMap = new HashMap<>();
             Map<String, ValueWrapper> locationMap = new HashMap<>();
             Map<String, ValueWrapper> reportData = new HashMap<>();
             Map<String, ValueWrapper> reportData = new HashMap<>();
 //            SPUtils.getInstance().put("RodNumber","4721");
 //            SPUtils.getInstance().put("RodNumber","4721");
@@ -1086,11 +1068,7 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
             }
             }
             int lockNumberInt=Integer.parseInt(lockNumberStr);
             int lockNumberInt=Integer.parseInt(lockNumberStr);
 
 
-            String runningStateStr=SPUtils.getInstance().getString("RunningState");
-            if(runningStateStr.equals("")){
-                runningStateStr="0";
-            }
-            int runningStateInt=Integer.parseInt(runningStateStr);
+            int runningStateInt=SPUtils.getInstance().getInt("RunningState");
             reportData.put("RodOpenNum",new ValueWrapper.IntValueWrapper(rodNumberInt));
             reportData.put("RodOpenNum",new ValueWrapper.IntValueWrapper(rodNumberInt));
             reportData.put("LockOpenNum",new ValueWrapper.IntValueWrapper(lockNumberInt));
             reportData.put("LockOpenNum",new ValueWrapper.IntValueWrapper(lockNumberInt));
 
 
@@ -1102,7 +1080,12 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
             reportData.put("LockSwitch", new ValueWrapper.BooleanValueWrapper(lockSwitch));   //锁开关状态:0-关锁  1-解锁  解锁后3s就关锁
             reportData.put("LockSwitch", new ValueWrapper.BooleanValueWrapper(lockSwitch));   //锁开关状态:0-关锁  1-解锁  解锁后3s就关锁
             reportData.put("RodSwitch", new ValueWrapper.BooleanValueWrapper(rodSwitchInt));   //推杆状态:0-关闭状态  1-打开  开门
             reportData.put("RodSwitch", new ValueWrapper.BooleanValueWrapper(rodSwitchInt));   //推杆状态:0-关闭状态  1-打开  开门
 
 
-            reportData.put("RunningState", new ValueWrapper.BooleanValueWrapper(runningStateInt)); // 0 正常 1 故障
+            reportData.put("RunningState", new ValueWrapper.BooleanValueWrapper(runningStateInt)); // 0正常
+//            1设备故障 未识别到称重设备 报警  未识别到杆、门磁、锁、温度、红外、超声波设备 报警
+//            2温度过高 报警
+//            3收运门意外打开报警
+//            4称重失灵报警 错误一次就再读一次  超过两次读取错误就报警
+//            5温度和超声波失灵警告 失灵显示的000000000
 
 
             System.out.println(IMEI+"DeviceId");
             System.out.println(IMEI+"DeviceId");
             reportData.put("IMEI", new ValueWrapper.StringValueWrapper(IMEI));
             reportData.put("IMEI", new ValueWrapper.StringValueWrapper(IMEI));
@@ -1111,13 +1094,14 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
             locationMap.put("altitude", new ValueWrapper.DoubleValueWrapper(523.1));
             locationMap.put("altitude", new ValueWrapper.DoubleValueWrapper(523.1));
             reportData.put("GeoLocation", new ValueWrapper.StructValueWrapper(locationMap)); //
             reportData.put("GeoLocation", new ValueWrapper.StructValueWrapper(locationMap)); //
 
 
+            int queryCurrent=SPUtils.getInstance().getInt("queryCurrent");
             Log.i(TAG, "称重" + weight.toString() + "距离" + capacity.toString() + "温度" + currentTemperature);
             Log.i(TAG, "称重" + weight.toString() + "距离" + capacity.toString() + "温度" + currentTemperature);
             textReportData.set("称重 " + weight.toString() + ";距离 " + capacity.toString() + "mm;温度 " +
             textReportData.set("称重 " + weight.toString() + ";距离 " + capacity.toString() + "mm;温度 " +
-                    currentTemperature+"度;门磁 "+magDoorSwitch+";杆状态 "+rodSwitchInt+";锁状态 "+lockSwitch+";杆开门次数 "+rodNumberInt+";开锁次数 "+lockNumberInt
+                    currentTemperature+"度;门磁 "+magDoorSwitch+";杆状态 "+rodSwitchInt+";锁状态 "+lockSwitch+";杆开门次数 "+rodNumberInt+";开锁次数 "+lockNumberInt+";被夹次数"+queryCurrent
             );
             );
 
 
             String  text = "称重 " + weight.toString() + ";距离 " + capacity.toString() + "mm;温度 " + currentTemperature+"度;门磁 "+magDoorSwitch+";杆状态 "+
             String  text = "称重 " + weight.toString() + ";距离 " + capacity.toString() + "mm;温度 " + currentTemperature+"度;门磁 "+magDoorSwitch+";杆状态 "+
-                    rodSwitchInt+";锁状态 "+lockSwitch+";杆开门次数 "+rodNumberInt+";开锁次数 "+lockNumberInt+"------------------------------";
+                    rodSwitchInt+";锁状态 "+lockSwitch+";杆开门次数 "+rodNumberInt+";开锁次数 "+lockNumberInt+";被夹次数"+queryCurrent+"------------------------------";
             Log.i(TAG, text + "=textReportData---------------------");
             Log.i(TAG, text + "=textReportData---------------------");
                     mHandler.postDelayed(new Runnable() {
                     mHandler.postDelayed(new Runnable() {
                         @Override
                         @Override
@@ -1126,6 +1110,7 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
                                 @Override
                                 @Override
                                 public void onSuccess(String s, Object o) {
                                 public void onSuccess(String s, Object o) {
                                     Log.d("", "onSuccess() called with: s = [" + s + "], o = [" + o + "]");
                                     Log.d("", "onSuccess() called with: s = [" + s + "], o = [" + o + "]");
+                                    SPUtils.getInstance().put("RunningState",0);
                                 }
                                 }
                                 @Override
                                 @Override
                                 public void onError(String s, AError aError) {
                                 public void onError(String s, AError aError) {
@@ -1139,37 +1124,33 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
                     e.printStackTrace();
                     e.printStackTrace();
                 }
                 }
     }
     }
-   /* public View.OnClickListener btnClickclearCurrent = new View.OnClickListener() {//清除
-        @Override
-        public void onClick(View view) {
-            System.out.println("btnClickclearCurrent");
-            String reportDataToString=reportDataMethod();
-            try {
-                String base64Str = DemoOne.java_openssl_encrypt("SSSS", "96e79218965eb72c92a549dd", "1234567891234567");//加密
-                Log.i(TAG,base64Str+"+++++base64Str");
-            } catch (Exception e) {
-                e.printStackTrace();
-            }
-        }
-    };*/
    /*
    /*
    * 上报给蓝牙的数据格式
    * 上报给蓝牙的数据格式
    * */
    * */
     public JSONObject reportDataMethod(){
     public JSONObject reportDataMethod(){
         Integer capacity=0;
         Integer capacity=0;
         capacity=SerialPortUtil.getInstance().getOtherDevice().getDistance();//超声波距离/
         capacity=SerialPortUtil.getInstance().getOtherDevice().getDistance();//超声波距离/
+        if(capacity==0){
+            capacity=SerialPortUtil.getInstance().getOtherDevice().getDistance();//超声波距离/
+        }
         Integer magDoorSwitch=0;
         Integer magDoorSwitch=0;
         magDoorSwitch=SerialPortUtil.getInstance().getOtherDevice().queryMagDoor();//门磁
         magDoorSwitch=SerialPortUtil.getInstance().getOtherDevice().queryMagDoor();//门磁
         Integer currentTemperature=0;
         Integer currentTemperature=0;
         currentTemperature=SerialPortUtil.getInstance().getOtherDevice().geTemperature();//温度
         currentTemperature=SerialPortUtil.getInstance().getOtherDevice().geTemperature();//温度
+        if(currentTemperature==0){
+            currentTemperature=SerialPortUtil.getInstance().getOtherDevice().geTemperature();//温度
+        }
         Integer weight = 0;
         Integer weight = 0;
         weight=SerialPortUtil.getInstance().getWeightDevice().readWeight();
         weight=SerialPortUtil.getInstance().getWeightDevice().readWeight();
+        if(weight==1){
+            weight=SerialPortUtil.getInstance().getWeightDevice().readWeight();
+        }
         //        weight=SerialPortUtil.getInstance().getOtherDevice().readWeight();//称重
         //        weight=SerialPortUtil.getInstance().getOtherDevice().readWeight();//称重
 
 
         String  text = "称重 " + weight.toString() + ";距离 " + capacity.toString() + "mm;温度 " + currentTemperature+"度;门磁 "+magDoorSwitch+";杆状态 "+";锁状态 "+lockSwitch;
         String  text = "称重 " + weight.toString() + ";距离 " + capacity.toString() + "mm;温度 " + currentTemperature+"度;门磁 "+magDoorSwitch+";杆状态 "+";锁状态 "+lockSwitch;
         Log.i(TAG, text + "=textReportData---------------------");
         Log.i(TAG, text + "=textReportData---------------------");
 
 
-        JSONObject locationMap =new JSONObject();
+//        JSONObject locationMap =new JSONObject();
         String rodNumberStr= SPUtils.getInstance().getString("RodNumber");
         String rodNumberStr= SPUtils.getInstance().getString("RodNumber");
         if(rodNumberStr.equals("")){
         if(rodNumberStr.equals("")){
             rodNumberStr="0";
             rodNumberStr="0";
@@ -1187,11 +1168,8 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
         }
         }
         int lockNumberInt=Integer.parseInt(lockNumberStr);
         int lockNumberInt=Integer.parseInt(lockNumberStr);
 
 
-        String runningStateStr=SPUtils.getInstance().getString("RunningState");
-        if(runningStateStr.equals("")){
-            runningStateStr="0";
-        }
-        int runningStateInt=Integer.parseInt(runningStateStr);
+
+        int runningStateInt=SPUtils.getInstance().getInt("RunningState");
 //        JSONObject tem1 =new JSONObject();
 //        JSONObject tem1 =new JSONObject();
 //        tem1.put("V",IMEI);
 //        tem1.put("V",IMEI);
 //        tem1.put("I","IMEI");
 //        tem1.put("I","IMEI");
@@ -1251,12 +1229,10 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
         array.add(tem9);
         array.add(tem9);
         array.add(tem10);
         array.add(tem10);
 //        array.add(tem11);
 //        array.add(tem11);
-
         JSONObject reportData =new JSONObject();
         JSONObject reportData =new JSONObject();
         reportData.put("P",array);
         reportData.put("P",array);
-
-
         Log.i(TAG,"reportData=+++++"+reportData.toString());
         Log.i(TAG,"reportData=+++++"+reportData.toString());
+        SPUtils.getInstance().put("RunningState",0);
         return reportData;
         return reportData;
 //        return (String) reportData;
 //        return (String) reportData;
     }
     }
@@ -1280,7 +1256,6 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
     }
     }
     //设备事件上报
     //设备事件上报
     public static void reportDeviceEvent(String identifier){
     public static void reportDeviceEvent(String identifier){
-        SPUtils.getInstance().put("RunningState","1");
         HashMap<String, ValueWrapper> hashMap = new HashMap<>();
         HashMap<String, ValueWrapper> hashMap = new HashMap<>();
         // TODO 用户根据实际情况设置
         // TODO 用户根据实际情况设置
         // hashMap.put("ErrorCode", new ValueWrapper.IntValueWrapper(0));
         // hashMap.put("ErrorCode", new ValueWrapper.IntValueWrapper(0));
@@ -1304,7 +1279,6 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
 //            }
 //            }
 //        });
 //        });
     }
     }
-
     private void getDeviceProperty(String identifier){
     private void getDeviceProperty(String identifier){
         // 根据 identifier 获取当前物模型中该属性的值
         // 根据 identifier 获取当前物模型中该属性的值
         LinkKit.getInstance().getDeviceThing().getPropertyValue(identifier);
         LinkKit.getInstance().getDeviceThing().getPropertyValue(identifier);
@@ -1331,14 +1305,7 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
                 System.out.println("checkOnlineStatus subscribe Fail");
                 System.out.println("checkOnlineStatus subscribe Fail");
             }
             }
         });
         });
-        mHandler.postDelayed(new Runnable() {
-            @Override
-            public void run() {
-                publish();
-                publishRepeat();
-
-            }
-        },600000);
+        publish();
     }
     }
     private void reportVersion(){
     private void reportVersion(){
         //上报版本
         //上报版本
@@ -1366,21 +1333,6 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
                 }else{
                 }else{
                     Log.i("安装","no");
                     Log.i("安装","no");
                 }
                 }
-
-//                Intent intent = new Intent(Intent.ACTION_VIEW);
-////      安装完成后,启动app
-//                Log.i(TAG,"安装");
-//                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-////                Uri uri = Uri.parse("file://" + apkFile.toString());
-//                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
-//                    intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
-//                    Uri uri = FileProvider.getUriForFile(getApplication(), getApplication().getPackageName() + ".fileprovider", apkFile3);
-//                    intent.setDataAndType(uri, "application/vnd.android.package-archive");
-//                } else {
-//                    intent.setDataAndType(Uri.fromFile(apkFile3), "application/vnd.android.package-archive");
-//                }
-////                intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
-//                getApplication().startActivity(intent);
             }
             }
         },1000);
         },1000);
     }
     }
@@ -1434,7 +1386,7 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
     }
     }
     private int disconnectTimes = 0;
     private int disconnectTimes = 0;
 
 
-    private void publish (){
+    private void publish(){
         //一个循环方法,用来更新数据
         //一个循环方法,用来更新数据
         System.out.println("checkOnlineStatus");
         System.out.println("checkOnlineStatus");
         MqttPublishRequest publishRequest = new MqttPublishRequest();
         MqttPublishRequest publishRequest = new MqttPublishRequest();
@@ -1442,22 +1394,23 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
         publishRequest.topic = "/a13H8L6bDyf/device2/user/checkOnlineStatus";
         publishRequest.topic = "/a13H8L6bDyf/device2/user/checkOnlineStatus";
         publishRequest.payloadObj = "{\"id\":"+ publishRequest.msgId+", \"version\":\"1.0\",\"time\":" + System.currentTimeMillis() + "}";
         publishRequest.payloadObj = "{\"id\":"+ publishRequest.msgId+", \"version\":\"1.0\",\"time\":" + System.currentTimeMillis() + "}";
         Integer networkInt=netWorkInfo();
         Integer networkInt=netWorkInfo();
+
         Log.i(TAG,"network网络"+networkInt);
         Log.i(TAG,"network网络"+networkInt);
+
             LinkKit.getInstance().publish(publishRequest, new IConnectSendListener() {
             LinkKit.getInstance().publish(publishRequest, new IConnectSendListener() {
                 @Override
                 @Override
                 public void onResponse(ARequest aRequest, AResponse aResponse) {
                 public void onResponse(ARequest aRequest, AResponse aResponse) {
                     disconnectTimes = 0;
                     disconnectTimes = 0;
-                    System.out.println("publish on Response :" + aResponse.getData());
                     mHandler.postDelayed(new Runnable() {
                     mHandler.postDelayed(new Runnable() {
                         @Override
                         @Override
                         public void run() {
                         public void run() {
-                            publish();
                             if(networkInt!=0){
                             if(networkInt!=0){
-                                reportProperty(1);
-                                Log.i(TAG,"循环上报数据");
+                                reportProperty();
                             }
                             }
                         }
                         }
-                    }, 1800000);
+                    },5000);
+
+                    System.out.println("publish on Response :" + aResponse.getData());
                 }
                 }
                 @Override
                 @Override
                 public void onFailure(ARequest aRequest, AError aError) {
                 public void onFailure(ARequest aRequest, AError aError) {
@@ -1473,49 +1426,13 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
                         public void run() {
                         public void run() {
                             publish();
                             publish();
                         }
                         }
-                    }, 1800000);
+                    },100000);
+
                 }
                 }
             });
             });
     }
     }
 
 
-    private void publishRepeat(){
-        //每隔一段时间检测磁控和温度 如果异常就上报(前提是有网条件下)
-        Thread thread=new Thread(new Runnable() {
-            @Override
-            public void run() {
-                Integer currentTemperature=SerialPortUtil.getInstance().getOtherDevice().geTemperature();//温度
-                Integer magDoorSwitch=SerialPortUtil.getInstance().getOtherDevice().queryMagDoor();
-                if(currentTemperature>100){
-                    SPUtils.getInstance().put("RunningState","2");//温度过高
-                    Log.i(TAG,"温度过高");
-                    Integer networkInt=netWorkInfo();
-                    if(networkInt!=0){
-                        reportProperty(1);
-                    }
-                }
-                if(magDoorSwitch==1){
-                    if(SPUtils.getInstance().getString("magDoorSwitch").equals("0")){
-                        SPUtils.getInstance().put("RunningState","3");
-                        Log.i(TAG,"收运门意外打开");
-                        Integer networkInt=netWorkInfo();
-                        if(networkInt!=0){
-                            reportProperty(1);
-                        }
-                    }
-                }
-                if(currentTemperature<100&magDoorSwitch==0) {
-                    SPUtils.getInstance().put("RunningState","0");
-                }
-            }
-        });
-        thread.start();
-        mHandler.postDelayed(new Runnable() {
-            @Override
-            public void run() {
-                publishRepeat();
-            }
-        }, 420000);
-    }
+
     public void unRegisterIoTListener(){
     public void unRegisterIoTListener(){
         // 取消注册 notifyListener,notifyListener对象需和注册的时候是同一个对象
         // 取消注册 notifyListener,notifyListener对象需和注册的时候是同一个对象
         LinkKit.getInstance().unRegisterOnPushListener(notifyListener);
         LinkKit.getInstance().unRegisterOnPushListener(notifyListener);
@@ -1646,6 +1563,7 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
                                     timeInt=82;
                                     timeInt=82;
                                 }
                                 }
                                 int finalTimeInt = timeInt;
                                 int finalTimeInt = timeInt;
+                                Log.i("计时",""+finalTimeInt);
                                 Thread thread=new Thread(new Runnable() {
                                 Thread thread=new Thread(new Runnable() {
                                     @Override
                                     @Override
                                     public void run() {
                                     public void run() {
@@ -1655,6 +1573,7 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
                                             try {
                                             try {
                                                 sleep(1000);
                                                 sleep(1000);
                                                 numInt++;
                                                 numInt++;
+                                                Log.i("倒计时时间",numInt+"");
                                                 if(SPUtils.getInstance().getString("RodSwitch").equals("0")){
                                                 if(SPUtils.getInstance().getString("RodSwitch").equals("0")){
                                                     break;
                                                     break;
                                                 }
                                                 }
@@ -1707,7 +1626,7 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
                                                 public void run() {
                                                 public void run() {
                                                     try{
                                                     try{
                                                     long curr=System.currentTimeMillis();
                                                     long curr=System.currentTimeMillis();
-                                                    while(System.currentTimeMillis()-curr<1800000) {
+                                                    while(System.currentTimeMillis()-curr<3000000) {
                                                         Thread.sleep(1000);
                                                         Thread.sleep(1000);
                                                         Integer magDoorSwitch=SerialPortUtil.getInstance().getOtherDevice().queryMagDoor();//通过门磁感应判断收运门是否关闭
                                                         Integer magDoorSwitch=SerialPortUtil.getInstance().getOtherDevice().queryMagDoor();//通过门磁感应判断收运门是否关闭
                                                         if(magDoorSwitch==0){
                                                         if(magDoorSwitch==0){
@@ -1744,6 +1663,9 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
                     }
                     }
                     if(requestStr.equals("close2")){
                     if(requestStr.equals("close2")){
                         Log.i(TAG,"+++++收运门关");
                         Log.i(TAG,"+++++收运门关");
+                        SerialPortUtil.getInstance().getWeightDevice().weightClear();//若关闭,称重清零
+                        SPUtils.getInstance().put("magDoorSwitch","0");
+//                          SerialPortUtil.getInstance().getOtherDevice().clearWeight();
                         SPUtils.getInstance().put("type","close");
                         SPUtils.getInstance().put("type","close");
                         String response=reportDataMethodEncrypt();
                         String response=reportDataMethodEncrypt();
                         SPUtils.getInstance().put("response",response);
                         SPUtils.getInstance().put("response",response);
@@ -1995,6 +1917,141 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
         return getApplication().getSystemService(name);
         return getApplication().getSystemService(name);
     }
     }
 
 
+    public void repeatTask() {
+        Log.i("----------------------","ok");
+        task3 =new TimerTask(){
+            public void run(){
+                publishRepeat();
+                Log.i("Task","300秒循环");
+            }
+        };
+        timer.schedule(task3,60000,300000);
+        Calendar calendar = Calendar.getInstance();
+        calendar.setTimeInMillis(System.currentTimeMillis());
+        calendar.set(Calendar.HOUR_OF_DAY, 11);
+        calendar.set(Calendar.MINUTE, 26);
+        task2=new TimerTask() {
+            @Override
+            public void run() {
+                openLight();
+            }
+        };
+        timer.schedule(task2,0,1800000);
+
+        task1=new TimerTask() {
+            @Override
+            public void run() {
+                publish();
+            }
+        };
+        timer.schedule(task1,600000,600000);
+    }
+    private void publishRepeat(){
+        Integer currentTemperature= SerialPortUtil.getInstance().getOtherDevice().geTemperature();//温度
+        Integer magDoorSwitch=SerialPortUtil.getInstance().getOtherDevice().queryMagDoor();
+        Integer queryCurrent=SerialPortUtil.getInstance().getOtherDevice().queryCurrent();
+        if(queryCurrent==1){
+            int num=SPUtils.getInstance().getInt("queryCurrent");
+            SPUtils.getInstance().put("queryCurrent",num+1);//记录被夹次数
+            SerialPortUtil.getInstance().getOtherDevice().clearCurrent();
+        }
+        if(currentTemperature>90){
+            SPUtils.getInstance().put("RunningState",2);//温度过高
+            Log.i(TAG,"温度过高");
+            Integer networkInt=netWorkInfo();
+            if(networkInt!=0){
+                reportProperty();
+                //短信通知
+                SendSmsResponse("温度过高");
+
+            }
+        }
+        if(currentTemperature>40){
+//            SerialPortUtil.getInstance().getOtherDevice().openuFan();//打开分钟
+        }else{
+            //            SerialPortUtil.getInstance().getOtherDevice().closeFan();//打开分钟
+        }
 
 
+        if(magDoorSwitch==1){
+            if(SPUtils.getInstance().getString("magDoorSwitch").equals("0")){
+                SPUtils.getInstance().put("RunningState",3);
+                Log.i(TAG,"收运门意外打开");
+                Integer networkInt=netWorkInfo();
+                      /*  if(networkInt!=0){
+                            reportProperty();
+                            //短信通知
+                            try {
+                                SendSmsResponse("收运门意外打开");
+                            } catch (ClientException e) {
+                                e.printStackTrace();
+                            }
+                        }*/
+            }
+        }
+//                if(currentTemperature<100&magDoorSwitch==0) {
+//                    SPUtils.getInstance().put("RunningState","0");
+//                }
+//            }
+//        });
+
+    }
+    public int readTime(){
+        Calendar calendar = Calendar.getInstance();
+        int hour = calendar.get(Calendar.HOUR_OF_DAY);
+        Log.i("Time hour=","现在是"+hour+"点");
+        return hour;
+    }
+
+    public void openLight(){
+        Log.i("d定时","ok");
+        if(readTime()>19||readTime()<1){
+            Log.i("night","开灯");
+//            SerialPortUtil.getInstance().getOtherDevice().openLight1();
+        }else {
+            Log.i("day","关灯");
+//            SerialPortUtil.getInstance().getOtherDevice().closeLight1();
+        }
+    }
+
+
+    public static void SendSmsResponse(String event){
+        Log.i("短信","发送"+event);
+        /*DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4FcFSGUX9ngzehwMMU6M", "SxG0xU9U7bqpCDoK0nMGo4pSXAdNHc");
+        IAcsClient client = new DefaultAcsClient(profile);
+        CommonRequest request = new CommonRequest();
+        request.setMethod(MethodType.POST);
+        request.setDomain("dysmsapi.aliyuncs.com");
+        request.setVersion("2017-05-25");
+        request.setAction("SendSms");
+        request.putQueryParameter("RegionId", "cn-hangzhou");
+        request.putQueryParameter("PhoneNumbers", "18116680579");
+        request.putQueryParameter("SignName", "思维定制");
+        request.putQueryParameter("TemplateCode", "SMS_185242509");
+        JSONObject jsono=new JSONObject();
+//        jsono.put("DeviceId", SPUtils.getInstance().getString("IMEI"));
+        jsono.put("DeviceName", event);
+
+        request.putQueryParameter("TemplateParam", jsono.toJSONString());
+        try {
+            CommonResponse response = client.getCommonResponse(request);
+            System.out.println(response.getData());
+        } catch (ClientException e) {
+            e.printStackTrace();
+        }*/
+        /*try {
+            Response response = OkHttpUtils.post()
+                    .url("")
+                    .addParams("event", event)
+                    .build().execute();
+            if (response.isSuccessful()) {
+                String jsonStr = response.body().string();
+                Log.i("RESPONSE==", "JSONstr=" + jsonStr);
+            } else {
+                throw new IOException(response.message());
+            }
+        }catch (IOException e){
+            e.printStackTrace();
+        }*/
+    }
 
 
 }
 }

+ 1 - 0
build.gradle

@@ -4,6 +4,7 @@ buildscript {
     repositories {
     repositories {
         maven{url 'http://maven.aliyun.com/nexus/content/groups/public/'}
         maven{url 'http://maven.aliyun.com/nexus/content/groups/public/'}
         maven { url 'https://jitpack.io' }
         maven { url 'https://jitpack.io' }
+
         google()
         google()
         jcenter()
         jcenter()
     }
     }

+ 2 - 0
mvvmhabit/src/main/java/me/goldze/mvvmhabit/base/ContainerActivity.java

@@ -6,6 +6,7 @@ import android.os.PersistableBundle;
 import android.support.v4.app.Fragment;
 import android.support.v4.app.Fragment;
 import android.support.v4.app.FragmentManager;
 import android.support.v4.app.FragmentManager;
 import android.support.v4.app.FragmentTransaction;
 import android.support.v4.app.FragmentTransaction;
+import android.util.Log;
 import android.view.View;
 import android.view.View;
 import android.view.ViewGroup;
 import android.view.ViewGroup;
 import android.view.WindowManager;
 import android.view.WindowManager;
@@ -34,6 +35,7 @@ public class ContainerActivity extends RxAppCompatActivity {
     protected void onCreate(Bundle savedInstanceState) {
     protected void onCreate(Bundle savedInstanceState) {
         getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
         getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
         super.onCreate(savedInstanceState);
         super.onCreate(savedInstanceState);
+        Log.i("error_activity","start");
         setContentView(R.layout.activity_container);
         setContentView(R.layout.activity_container);
         FragmentManager fm = getSupportFragmentManager();
         FragmentManager fm = getSupportFragmentManager();
         Fragment fragment = null;
         Fragment fragment = null;

+ 2 - 2
mvvmhabit/src/main/java/me/goldze/mvvmhabit/utils/SPUtils.java

@@ -92,10 +92,10 @@ public final class SPUtils {
      * SP中读取int
      * SP中读取int
      *
      *
      * @param key 键
      * @param key 键
-     * @return 存在返回对应值,不存在返回默认值-1
+     * @return 存在返回对应值,不存在返回默认值0
      */
      */
     public int getInt(@NonNull final String key) {
     public int getInt(@NonNull final String key) {
-        return getInt(key, -1);
+        return getInt(key, 0);
     }
     }
 
 
     /**
     /**