DESKTOP-SADJPBG\47462 5 anni fa
parent
commit
f8f9cef2b3

+ 5 - 1
app/build.gradle

@@ -4,10 +4,14 @@ android {
 
 
     signingConfigs {
     signingConfigs {
         release {
         release {
+//            keyAlias 'key1'
+//            keyPassword 'fir0414'
+//            storePassword 'fir0414'
+//            storeFile file('\\recycle.jks')
+
             keyAlias 'box.keystore'
             keyAlias 'box.keystore'
             keyPassword '159753'
             keyPassword '159753'
             storePassword '159753'
             storePassword '159753'
-//            storeFile file('C:\\AndroidStudiowork\\ai-garbage-box2\\app\\box.keystore')
             storeFile file('\\box.keystore')
             storeFile file('\\box.keystore')
         }
         }
         debug{
         debug{

BIN
app/recycle.jks


+ 5 - 2
app/src/main/AndroidManifest.xml

@@ -2,8 +2,9 @@
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     xmlns:tools="http://schemas.android.com/tools"
     package="com.siwei.recyclebox"
     package="com.siwei.recyclebox"
-
     >
     >
+<!--    android:sharedUserId="android.uid.system"--><!--    系统签名需要配置-->
+
     <!--用于进行网络定位-->
     <!--用于进行网络定位-->
     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
     <!--用于访问GPS定位-->
     <!--用于访问GPS定位-->
@@ -115,7 +116,9 @@
         </activity>
         </activity>
 
 
 
 
-        <service android:name=".service.MyService" />
+        <service android:name=".service.MyService"
+            android:permission="android.permission.BIND_JOB_SERVICE"
+            />
 
 
         <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>

+ 14 - 12
app/src/main/java/com/siwei/recyclebox/application/AppApplication.java

@@ -171,14 +171,16 @@ public class AppApplication extends BaseApplication implements CustomActivityOnC
          *   调用动态注册接口去获取deviceSecret
          *   调用动态注册接口去获取deviceSecret
          */
          */
         DeviceInfo myDeviceInfo = new DeviceInfo();
         DeviceInfo myDeviceInfo = new DeviceInfo();
-        myDeviceInfo.productKey = "a13H8L6bDyf";
-        myDeviceInfo.productSecret = "Nc4Y4KsjofejCy27";
+        myDeviceInfo.productKey = "a1EtqHmfpNk";
+        myDeviceInfo.productSecret = "x6wHbrbXUMFYtvLl";
         System.out.println(MainViewModel.getDeviceId(this)+"----------------------------+");
         System.out.println(MainViewModel.getDeviceId(this)+"----------------------------+");
         myDeviceInfo.deviceName = MainViewModel.getDeviceId(this);
         myDeviceInfo.deviceName = MainViewModel.getDeviceId(this);
+        Log.i("IMEI=",MainViewModel.getDeviceId(this));
         SPUtils.getInstance().put("IMEI",MainViewModel.getDeviceId(this));
         SPUtils.getInstance().put("IMEI",MainViewModel.getDeviceId(this));
 //        myDeviceInfo.deviceName="123456";
 //        myDeviceInfo.deviceName="123456";
-//        SPUtils.getInstance().put("deviceSecret","lzR12lxbbuV3c4tZMxrIPl1Vh9K9Ssv4");//存
-        String secret = SPUtils.getInstance().getString("deviceSecret");//读
+//        SPUtils.getInstance().put("deviceSecret2","a3NRdNGf7W5uYmW4mNG7tupSq9skwm0j");//存
+        String secret = SPUtils.getInstance().getString("deviceSecret2");//读
+//        String secret ="";
         Log.i(TAG,"secret=="+secret);
         Log.i(TAG,"secret=="+secret);
 
 
         if(secret != null &&  !secret.equals("")){
         if(secret != null &&  !secret.equals("")){
@@ -207,7 +209,7 @@ public class AppApplication extends BaseApplication implements CustomActivityOnC
                             !TextUtils.isEmpty(response.data.get("deviceSecret"))) {
                             !TextUtils.isEmpty(response.data.get("deviceSecret"))) {
                         System.out.println("deviceSecret:"+response.data.get("deviceSecret"));
                         System.out.println("deviceSecret:"+response.data.get("deviceSecret"));
                         myDeviceInfo.deviceSecret = response.data.get("deviceSecret");
                         myDeviceInfo.deviceSecret = response.data.get("deviceSecret");
-                        SPUtils.getInstance().put("deviceSecret",myDeviceInfo.deviceSecret);//存
+                        SPUtils.getInstance().put("deviceSecret2",myDeviceInfo.deviceSecret);//存
                         initAliIoT(myDeviceInfo);
                         initAliIoT(myDeviceInfo);
                     }
                     }
                 }
                 }
@@ -240,19 +242,19 @@ public class AppApplication extends BaseApplication implements CustomActivityOnC
                 .errorDrawable(R.mipmap.ic_launcher) //错误图标
                 .errorDrawable(R.mipmap.ic_launcher) //错误图标
                 .restartActivity(MainActivity.class) //重新启动后的activity
                 .restartActivity(MainActivity.class) //重新启动后的activity
 //                .errorActivity(YourCustomErrorActivity.class) //崩溃后的错误activity
 //                .errorActivity(YourCustomErrorActivity.class) //崩溃后的错误activity
-//                .eventListener(this) //崩溃后的错误监听
+                .eventListener(this) //崩溃后的错误监听
                 .apply();
                 .apply();
 
 
     }
     }
     @Override
     @Override
     public void onLaunchErrorActivity() {
     public void onLaunchErrorActivity() {
 //        Log.i(TAG,"1111+++");
 //        Log.i(TAG,"1111+++");
-//            Intent mStartActivity = new Intent(this,MainActivity.class);
-//            int mPendingIntentId = 123456;
-//            PendingIntent mPendingIntent = PendingIntent.getActivity(this, mPendingIntentId,    mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT);
-//            AlarmManager mgr = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
-//            mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
-//            System.exit(0);
+            Intent mStartActivity = new Intent(this,MainActivity.class);
+            int mPendingIntentId = 123456;
+            PendingIntent mPendingIntent = PendingIntent.getActivity(this, mPendingIntentId,    mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT);
+            AlarmManager mgr = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
+            mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
+            System.exit(0);
 
 
     }
     }
 
 

+ 9 - 7
app/src/main/java/com/siwei/recyclebox/deviceUtils/OtherDevice.java

@@ -208,7 +208,7 @@ public class OtherDevice extends BaseDeviceEntity {
 
 
     public void setPower(){
     public void setPower(){
         try {
         try {
-            byte[] setPower = new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x66, 0x0A, 0x02, 0x60, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD};
+            byte[] setPower = new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x66, 0x0A, 0x02, 0x40, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD};
             port.write(setPower, 500);
             port.write(setPower, 500);
             byte[] response = new byte[12];
             byte[] response = new byte[12];
             port.read(response,500);
             port.read(response,500);
@@ -246,8 +246,8 @@ public class OtherDevice extends BaseDeviceEntity {
                     port.read(responseRead,200);
                     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));
                     System.out.println("pushRead response:"+ByteUtil.bytesToString(responseRead));
-                    SPUtils.getInstance().put("infraredNum1",0);
-                    SPUtils.getInstance().getInt("infraredNum1",0);
+
+//                    SPUtils.getInstance().getInt("infraredNum1",0);
                     long curr=System.currentTimeMillis();
                     long curr=System.currentTimeMillis();
                     Thread.sleep(500);
                     Thread.sleep(500);
                     while (System.currentTimeMillis() - curr < 6000) {
                     while (System.currentTimeMillis() - curr < 6000) {
@@ -255,13 +255,13 @@ public class OtherDevice extends BaseDeviceEntity {
                         Integer infrared = queryInfrared();
                         Integer infrared = queryInfrared();
                         int ErrorNum=SPUtils.getInstance().getInt("queryInfraredError");
                         int ErrorNum=SPUtils.getInstance().getInt("queryInfraredError");
                         if(ErrorNum>15){
                         if(ErrorNum>15){
-                            MainViewModel.SendSmsResponse("门磁读取失灵");
+                            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) 0x05, (byte) 0xff, (byte) 0xff, (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",0)+1;
                             SPUtils.getInstance().put("infraredNum1",num);
                             SPUtils.getInstance().put("infraredNum1",num);
                             if(num<3){
                             if(num<3){
                                 mHandler.postDelayed(new Runnable() {
                                 mHandler.postDelayed(new Runnable() {
@@ -269,7 +269,7 @@ public class OtherDevice extends BaseDeviceEntity {
                                     public void run() {
                                     public void run() {
                                             pushRod();
                                             pushRod();
                                     }
                                     }
-                                },10000);
+                                },5000);
                                 break;
                                 break;
                             }else {
                             }else {
                                 mHandler.postDelayed(new Runnable() {
                                 mHandler.postDelayed(new Runnable() {
@@ -277,11 +277,12 @@ public class OtherDevice extends BaseDeviceEntity {
                                     public void run() {
                                     public void run() {
                                         try {
                                         try {
                                             port.write(pushRodOrderBytes, 100);
                                             port.write(pushRodOrderBytes, 100);
+
                                         } catch (IOException e) {
                                         } catch (IOException e) {
                                             e.printStackTrace();
                                             e.printStackTrace();
                                         }
                                         }
                                     }
                                     }
-                                },10000);
+                                },5000);
                                 break;
                                 break;
                             }
                             }
                         }
                         }
@@ -304,6 +305,7 @@ public class OtherDevice extends BaseDeviceEntity {
             @Override
             @Override
             public void run() {
             public void run() {
                 try {
                 try {
+                    SPUtils.getInstance().put("infraredNum1",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) 0xff, (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};

+ 7 - 7
app/src/main/java/com/siwei/recyclebox/service/MyService.java

@@ -22,7 +22,7 @@ public class MyService extends Service {
     public int onStartCommand(Intent intent, int flags, int startId) {
     public int onStartCommand(Intent intent, int flags, int startId) {
         // Let it continue running until it is stopped.
         // Let it continue running until it is stopped.
 //        Toast.makeText(this, "服务已经启动", Toast.LENGTH_LONG).show();
 //        Toast.makeText(this, "服务已经启动", Toast.LENGTH_LONG).show();
-        Log.i("","服务已经启动");
+        Log.i("server","服务已经启动");
         return START_STICKY;
         return START_STICKY;
     }
     }
 
 
@@ -32,12 +32,12 @@ public class MyService extends Service {
 //            Intent intent=new Intent(this,MyService.class);
 //            Intent intent=new Intent(this,MyService.class);
 //            this.startService(intent);
 //            this.startService(intent);
 
 
-//        Intent mStartActivity = new Intent(this, MainActivity.class);
-//        int mPendingIntentId = 123456;
-//        PendingIntent mPendingIntent = PendingIntent.getActivity(this, mPendingIntentId,    mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT);
-//        AlarmManager mgr = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
-//        mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
-//        System.exit(0);
+        Intent mStartActivity = new Intent(this, MainActivity.class);
+        int mPendingIntentId = 123456;
+        PendingIntent mPendingIntent = PendingIntent.getActivity(this, mPendingIntentId,    mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT);
+        AlarmManager mgr = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
+        mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
+        System.exit(0);
         super.onDestroy();
         super.onDestroy();
 //        Toast.makeText(this, "服务已经停止", Toast.LENGTH_LONG).show();
 //        Toast.makeText(this, "服务已经停止", Toast.LENGTH_LONG).show();
 //        Log.i("","服务已经停止");
 //        Log.i("","服务已经停止");

+ 25 - 3
app/src/main/java/com/siwei/recyclebox/ui/main/MainActivity.java

@@ -1,5 +1,9 @@
 package com.siwei.recyclebox.ui.main;
 package com.siwei.recyclebox.ui.main;
 
 
+import android.app.AlarmManager;
+import android.app.PendingIntent;
+import android.bluetooth.BluetoothAdapter;
+import android.content.Context;
 import android.content.Intent;
 import android.content.Intent;
 import android.databinding.ViewDataBinding;
 import android.databinding.ViewDataBinding;
 import android.os.Bundle;
 import android.os.Bundle;
@@ -30,14 +34,32 @@ public class MainActivity extends BaseActivity<ViewDataBinding,MainViewModel> {
     public void initData() {
     public void initData() {
 //        viewModel.btnClick.onClick();
 //        viewModel.btnClick.onClick();
 //        viewModel.btnText.set("");
 //        viewModel.btnText.set("");
+        openBlueTooth();
         viewModel.registerAliIoTListener();
         viewModel.registerAliIoTListener();
+        viewModel.startJobScheduler();
 //        viewModel.repeatTask();
 //        viewModel.repeatTask();
         Log.i("repeatTask","*****************************************");
         Log.i("repeatTask","*****************************************");
         viewModel.startBLEServer();
         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());
+    }
+
+    public void openBlueTooth(){
+//      打开蓝牙(提示对话框)
+//        Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
+//        discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
+//        startActivity(discoverableIntent);
 
 
+//        打开蓝牙(静默,无提示)
+        BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
+        bluetoothAdapter.enable();//需要BLUETOOTH_ADMIN权限
+
+    }
+    public void closeBlueTooth(){
+//      关闭蓝牙
+        BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
+        bluetoothAdapter.disable();
     }
     }
 
 
     @Override
     @Override
@@ -69,16 +91,16 @@ public class MainActivity extends BaseActivity<ViewDataBinding,MainViewModel> {
     @Override
     @Override
     protected void onDestroy() {
     protected void onDestroy() {
 
 
-        viewModel.closePort();
+//        viewModel.closePort();
         viewModel.stopBleServer();
         viewModel.stopBleServer();
         viewModel.unRegisterIoTListener();
         viewModel.unRegisterIoTListener();
         Log.e("MainActivity.","unregisterIoTListener!!!");
         Log.e("MainActivity.","unregisterIoTListener!!!");
-        /*Intent mStartActivity = new Intent(getApplication(),MainActivity.class);
+        Intent mStartActivity = new Intent(getApplication(),MainActivity.class);
         int mPendingIntentId = 123456;
         int mPendingIntentId = 123456;
         PendingIntent mPendingIntent = PendingIntent.getActivity(getApplication(), mPendingIntentId,    mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT);
         PendingIntent mPendingIntent = PendingIntent.getActivity(getApplication(), mPendingIntentId,    mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT);
         AlarmManager mgr = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
         AlarmManager mgr = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
         mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
         mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
-        System.exit(0);*/
+        System.exit(0);
         super.onDestroy();
         super.onDestroy();
 
 
     }
     }

+ 294 - 255
app/src/main/java/com/siwei/recyclebox/ui/main/MainViewModel.java

@@ -1,10 +1,14 @@
 package com.siwei.recyclebox.ui.main;
 package com.siwei.recyclebox.ui.main;
 
 
 import android.Manifest;
 import android.Manifest;
+import android.annotation.SuppressLint;
+import android.annotation.TargetApi;
 import android.app.ActivityManager;
 import android.app.ActivityManager;
 import android.app.AlarmManager;
 import android.app.AlarmManager;
 import android.app.Application;
 import android.app.Application;
 import android.app.PendingIntent;
 import android.app.PendingIntent;
+import android.app.job.JobInfo;
+import android.app.job.JobScheduler;
 import android.bluetooth.BluetoothAdapter;
 import android.bluetooth.BluetoothAdapter;
 import android.bluetooth.BluetoothDevice;
 import android.bluetooth.BluetoothDevice;
 import android.bluetooth.BluetoothGatt;
 import android.bluetooth.BluetoothGatt;
@@ -19,6 +23,7 @@ import android.bluetooth.le.AdvertiseData;
 import android.bluetooth.le.AdvertiseSettings;
 import android.bluetooth.le.AdvertiseSettings;
 import android.bluetooth.le.BluetoothLeAdvertiser;
 import android.bluetooth.le.BluetoothLeAdvertiser;
 import android.content.BroadcastReceiver;
 import android.content.BroadcastReceiver;
+import android.content.ComponentName;
 import android.content.Context;
 import android.content.Context;
 import android.content.Intent;
 import android.content.Intent;
 import android.content.pm.PackageManager;
 import android.content.pm.PackageManager;
@@ -125,35 +130,37 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
     @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
-        mLocationUtils = LocationUtils.getInstance();
-        Log.i(TAG,"定位--------------------");
-        mLocationUtils.startLocationInfo();
-        mLocationUtils.onCreate(new LocationUtils.LocationListener() {
-            @Override
-            public void locationSuccess(MapLocationInfo mapLocationInfo, String message) {
-                mMapLocationInfo = mapLocationInfo;
-                Log.i("定位: ","" + message);
-                Log.i(TAG,"定位ok");
-                Log.i(TAG,mMapLocationInfo.toString());
-                Log.i(TAG,"纬度"+mMapLocationInfo.getLatitude()+"经度"+mMapLocationInfo.getLongitude());
-            }
-            @Override
-            public void locationFail(String message) {
-                Log.i("定位: ","" + message);
-            }
-        });
-        mHandler.postDelayed(new Runnable() {
-            @Override
-            public void run() {
-                mLocationUtils.stopLocationClient();
-            }
-        },5000);
+
+//        Intent service = new Intent(getApplication(), MyService.class);
+//        getApplication().startService(service);//打开服务,把程序写在服务里不容易被系统kill
+
+//        mLocationUtils = LocationUtils.getInstance();
+//        Log.i(TAG,"定位--------------------");
+//        mLocationUtils.startLocationInfo();
+//        mLocationUtils.onCreate(new LocationUtils.LocationListener() {
+//            @Override
+//            public void locationSuccess(MapLocationInfo mapLocationInfo, String message) {
+//                mMapLocationInfo = mapLocationInfo;
+//                Log.i("定位: ","" + message);
+//                Log.i(TAG,"定位ok");
+//                Log.i(TAG,mMapLocationInfo.toString());
+//                Log.i(TAG,"纬度"+mMapLocationInfo.getLatitude()+"经度"+mMapLocationInfo.getLongitude());
+//            }
+//            @Override
+//            public void locationFail(String message) {
+//                Log.i("定位: ","" + message);
+//            }
+//        });
+//        mHandler.postDelayed(new Runnable() {
+//            @Override
+//            public void run() {
+//                mLocationUtils.stopLocationClient();
+//            }
+//        },5000);
 
 
         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);//门磁状态
 
 
         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);//查询电量值
@@ -172,9 +179,7 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
     public ObservableField<String> textIMEI = new ObservableField<>();//imei显示
     public ObservableField<String> textIMEI = new ObservableField<>();//imei显示
     public ObservableField<String> textqueryCurrentData = new ObservableField<>();//电流过大查询显示
     public ObservableField<String> textqueryCurrentData = new ObservableField<>();//电流过大查询显示
 
 
-//    public void startLocation(){
-//        mLocationUtils.startLocationInfo();
-//    }
+
     /*
     /*
     * 按钮模拟指令
     * 按钮模拟指令
     * */
     * */
@@ -224,7 +229,7 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
         }
         }
     };
     };
 
 
-    int a=1;
+    int a=2;
     public View.OnClickListener btnClickOpenLight1234 = new View.OnClickListener() {
     public View.OnClickListener btnClickOpenLight1234 = new View.OnClickListener() {
         //打开关闭灯4按钮 开灯1
         //打开关闭灯4按钮 开灯1
         @Override
         @Override
@@ -433,6 +438,7 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
                         try {
                         try {
                             sleep(1000);
                             sleep(1000);
                             numInt++;
                             numInt++;
+                            Log.i(TAG,numInt+"秒");
                             if(SPUtils.getInstance().getString("RodSwitch").equals("0")){//判断是否已经关门
                             if(SPUtils.getInstance().getString("RodSwitch").equals("0")){//判断是否已经关门
                                 break;
                                 break;
                             }
                             }
@@ -461,40 +467,53 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
 
 
     int stopValue=0;
     int stopValue=0;
     public void repeatRod(){
     public void repeatRod(){
-        //循环开门推拉杆
-        stopValue=1;
-        SerialPortUtil.getInstance().getOtherDevice().pullRod();
-        mHandler.postDelayed(new Runnable() {
-            @Override
-            public void run() {
-                SerialPortUtil.getInstance().getOtherDevice().pushRod();
-            }
-        },60000);
-        mHandler.postDelayed(new Runnable() {
+        Thread thread=new Thread(new Runnable() {
             @Override
             @Override
             public void run() {
             public void run() {
-                repeatRod();
+                //循环开门推拉杆
+                stopValue=1;
+                SerialPortUtil.getInstance().getOtherDevice().pullRod();
+                mHandler.postDelayed(new Runnable() {
+                    @Override
+                    public void run() {
+                        SerialPortUtil.getInstance().getOtherDevice().pushRod();
+                    }
+                },60000);
+                mHandler.postDelayed(new Runnable() {
+                    @Override
+                    public void run() {
+                        repeatRod();
+                    }
+                },120000);
             }
             }
-        },120000);
+        });
+        thread.start();
+
     };
     };
     int stopValue2=0;
     int stopValue2=0;
     public void repeatLock(){
     public void repeatLock(){
-        //循环开关锁
-        stopValue2=1;
-        SerialPortUtil.getInstance().getOtherDevice().openLock();
-        mHandler.postDelayed(new Runnable() {
+        Thread thread=new Thread(new Runnable() {
             @Override
             @Override
             public void run() {
             public void run() {
-                SerialPortUtil.getInstance().getOtherDevice().closeLock();
-            }
-        },7000);
+                //循环开关锁
+                stopValue2 = 1;
+                SerialPortUtil.getInstance().getOtherDevice().openLock();
+                mHandler.postDelayed(new Runnable() {
+                    @Override
+                    public void run() {
+                        SerialPortUtil.getInstance().getOtherDevice().closeLock();
+                    }
+                }, 7000);
 
 
-        mHandler.postDelayed(new Runnable() {
-            @Override
-            public void run() {
-                repeatLock();
+                mHandler.postDelayed(new Runnable() {
+                    @Override
+                    public void run() {
+                        repeatLock();
+                    }
+                }, 40000);
             }
             }
-        },40000);
+        });
+        thread.start();
     };
     };
     public View.OnClickListener btnClickRodRepeat = new View.OnClickListener() {
     public View.OnClickListener btnClickRodRepeat = new View.OnClickListener() {
         //循环开门按钮
         //循环开门按钮
@@ -502,6 +521,13 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
         public void onClick(View view) {
         public void onClick(View view) {
             System.out.println("btnClickRodRepeat");
             System.out.println("btnClickRodRepeat");
             if(stopValue==0){
             if(stopValue==0){
+//                Thread thread=new Thread(new Runnable() {
+//                    @Override
+//                    public void run() {
+//                        repeatRod();
+//                    }
+//                });
+//                thread.start();
                 repeatRod();
                 repeatRod();
             }
             }
 
 
@@ -513,6 +539,13 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
         public void onClick(View view) {
         public void onClick(View view) {
             System.out.println("btnClickRodRepeat");
             System.out.println("btnClickRodRepeat");
             if(stopValue2==0){
             if(stopValue2==0){
+//                Thread thread=new Thread(new Runnable() {
+//                    @Override
+//                    public void run() {
+//                        repeatLock();
+//                    }
+//                });
+//                thread.start();
                 repeatLock();
                 repeatLock();
             }
             }
         }
         }
@@ -770,8 +803,8 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
             byte[] dataBytes = (byte[])aMessage.getData();
             byte[] dataBytes = (byte[])aMessage.getData();
 
 
             System.out.println(new String(dataBytes));
             System.out.println(new String(dataBytes));
-            if (topic.endsWith("thing/event/property/post_reply")){ //推送设备属性的回包。
-            }
+//            if (topic.endsWith("thing/event/property/post_reply")){ //推送设备属性的回包。
+//            }
             if(topic.endsWith("thing/service/restartApp")){
             if(topic.endsWith("thing/service/restartApp")){
                 restartApp();//重启app
                 restartApp();//重启app
             }
             }
@@ -828,7 +861,7 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
             }
             }
             if(topic.endsWith("thing/service/RemoteAgentOpen")){//开收运门
             if(topic.endsWith("thing/service/RemoteAgentOpen")){//开收运门
                 Log.i(TAG,"开运收门");
                 Log.i(TAG,"开运收门");
-                SPUtils.getInstance().put("magDoorSwitch","1");
+                SPUtils.getInstance().put("magDoorSwitch",1);
                 short i = 1;
                 short i = 1;
                 try {
                 try {
                     SerialPortUtil.getInstance().getOtherDevice().openLock();
                     SerialPortUtil.getInstance().getOtherDevice().openLock();
@@ -860,7 +893,7 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
                                     try{
                                     try{
                                         long curr=System.currentTimeMillis();
                                         long curr=System.currentTimeMillis();
                                         while(System.currentTimeMillis()-curr<300000) {
                                         while(System.currentTimeMillis()-curr<300000) {
-                                            Thread.sleep(2000);
+                                            Thread.sleep(10000);
                                             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();//若关闭,称重清零
@@ -871,7 +904,7 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
                                                         reportProperty();
                                                         reportProperty();
                                                     }
                                                     }
                                                 },3000);
                                                 },3000);
-                                                SPUtils.getInstance().put("magDoorSwitch","0");
+                                                SPUtils.getInstance().put("magDoorSwitch",0);
                                                 break;
                                                 break;
                                             }
                                             }
                                         }
                                         }
@@ -891,7 +924,7 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
             }
             }
             if(topic.endsWith("thing/service/RemoteAgentClose")) {//关收运门
             if(topic.endsWith("thing/service/RemoteAgentClose")) {//关收运门
                 Log.i(TAG, "关运收门");
                 Log.i(TAG, "关运收门");
-                SPUtils.getInstance().put("magDoorSwitch", "0");
+                SPUtils.getInstance().put("magDoorSwitch", 0);
                 SerialPortUtil.getInstance().getWeightDevice().weightClear();
                 SerialPortUtil.getInstance().getWeightDevice().weightClear();
 //                                                SerialPortUtil.getInstance().getOtherDevice().clearWeight();
 //                                                SerialPortUtil.getInstance().getOtherDevice().clearWeight();
                 mHandler.postDelayed(new Runnable() {
                 mHandler.postDelayed(new Runnable() {
@@ -1005,14 +1038,14 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
                     e.printStackTrace();
                     e.printStackTrace();
                 }
                 }
             }
             }
-            if(topic.endsWith("thing/service/RepeatRod")){//开灯
+            if(topic.endsWith("thing/service/RepeatRod")){//循环推杆
                 try {
                 try {
                     repeatRod();
                     repeatRod();
                 } catch (Exception e) {
                 } catch (Exception e) {
                     e.printStackTrace();
                     e.printStackTrace();
                 }
                 }
             }
             }
-            if(topic.endsWith("thing/service/RepeatLock")){//关灯
+            if(topic.endsWith("thing/service/RepeatLock")){//循环开锁
                 try {
                 try {
                     repeatLock();
                     repeatLock();
                 } catch (Exception e) {
                 } catch (Exception e) {
@@ -1094,112 +1127,117 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
             public void run() {
             public void run() {
                 //        publishRepeat();
                 //        publishRepeat();
                 Log.i(TAG,"上报数据");
                 Log.i(TAG,"上报数据");
-                try{
-                    double longitude=107.31;
-                    double latitude=23.16;
-                    if(mMapLocationInfo!=null){
-                        longitude=mMapLocationInfo.getLongitude();
-                        latitude=mMapLocationInfo.getLatitude();
-                        Log.i(TAG,mMapLocationInfo.toString()+"--地址:"+mMapLocationInfo.getAddress());
+                Integer networkNum=netWorkInfo();
+                if(networkNum!=0) {
+                    try {
+                        double longitude = 107.31;
+                        double latitude = 23.16;
+                        if (mMapLocationInfo != null) {
+                            longitude = mMapLocationInfo.getLongitude();
+                            latitude = mMapLocationInfo.getLatitude();
+                            Log.i(TAG, mMapLocationInfo.toString() + "--地址:" + mMapLocationInfo.getAddress());
 
 
-                    }
-                    Integer weight = 0;
-                    Log.i("num=1","读一次称重");
-                    weight = SerialPortUtil.getInstance().getWeightDevice().readWeight();//读称重
-                    if(weight==1){
+                        }
+                        Integer weight = 0;
+                        Log.i("num=1", "读一次称重");
                         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();//称重
-                    Integer capacity=0;
-                    capacity=SerialPortUtil.getInstance().getOtherDevice().getDistance();//超声波距离
-                    if(capacity==0){
-                        capacity=SerialPortUtil.getInstance().getOtherDevice().getDistance();//超声波距离
-                    }
-                    Integer magDoorSwitch=0;
-                    magDoorSwitch=SerialPortUtil.getInstance().getOtherDevice().queryMagDoor();//门磁
-                    Integer currentTemperature=0;
-                    currentTemperature=SerialPortUtil.getInstance().getOtherDevice().geTemperature();//温度
-                    if(currentTemperature==0){
-                        currentTemperature=SerialPortUtil.getInstance().getOtherDevice().geTemperature();//温度
-                    }
-                    Log.i(TAG,"----------------------------------------");
+                        Integer capacity = 0;
+                        capacity = SerialPortUtil.getInstance().getOtherDevice().getDistance();//超声波距离
+                        if (capacity == 0) {
+                            capacity = SerialPortUtil.getInstance().getOtherDevice().getDistance();//超声波距离
+                        }
+                        Integer magDoorSwitch = 0;
+                        magDoorSwitch = SerialPortUtil.getInstance().getOtherDevice().queryMagDoor();//门磁
+                        Integer currentTemperature = 0;
+                        currentTemperature = SerialPortUtil.getInstance().getOtherDevice().geTemperature();//温度
+                        if (currentTemperature == 0) {
+                            currentTemperature = SerialPortUtil.getInstance().getOtherDevice().geTemperature();//温度
+                        }
+                        Log.i(TAG, "----------------------------------------");
 
 
-                    Map<String, ValueWrapper> locationMap = new HashMap<>();
-                    Map<String, ValueWrapper> reportData = new HashMap<>();
+                        Map<String, ValueWrapper> locationMap = new HashMap<>();
+                        Map<String, ValueWrapper> reportData = new HashMap<>();
 //            SPUtils.getInstance().put("RodNumber","4721");
 //            SPUtils.getInstance().put("RodNumber","4721");
-                    String rodNumberStr= SPUtils.getInstance().getString("RodNumber");
-                    if(rodNumberStr.equals("")){
-                        rodNumberStr="0";
-                    }
-                    int rodNumberInt=Integer.parseInt(rodNumberStr);
-                    String rodSwitchStr=SPUtils.getInstance().getString("RodSwitch");
-                    if(rodSwitchStr.equals("")){
-                        rodSwitchStr="0";
-                    }
-                    int rodSwitchInt=Integer.parseInt(rodSwitchStr);
+                        String rodNumberStr = SPUtils.getInstance().getString("RodNumber");
+                        if (rodNumberStr.equals("")) {
+                            rodNumberStr = "0";
+                        }
+                        int rodNumberInt = Integer.parseInt(rodNumberStr);
+                        String rodSwitchStr = SPUtils.getInstance().getString("RodSwitch");
+                        if (rodSwitchStr.equals("")) {
+                            rodSwitchStr = "0";
+                        }
+                        int rodSwitchInt = Integer.parseInt(rodSwitchStr);
 //            SPUtils.getInstance().put("LockNumber","7562");
 //            SPUtils.getInstance().put("LockNumber","7562");
-                    String lockNumberStr=SPUtils.getInstance().getString("LockNumber");
-                    if(lockNumberStr.equals("")){
-                        lockNumberStr="0";
-                    }
-                    int lockNumberInt=Integer.parseInt(lockNumberStr);
+                        String lockNumberStr = SPUtils.getInstance().getString("LockNumber");
+                        if (lockNumberStr.equals("")) {
+                            lockNumberStr = "0";
+                        }
+                        int lockNumberInt = Integer.parseInt(lockNumberStr);
+                        int clampHandNum = SPUtils.getInstance().getInt("ClampHandNum", 0);
+                        int runningStateInt = SPUtils.getInstance().getInt("RunningState");
+                        reportData.put("RodOpenNum", new ValueWrapper.IntValueWrapper(rodNumberInt));
+                        reportData.put("LockOpenNum", new ValueWrapper.IntValueWrapper(lockNumberInt));
+
+                        reportData.put("ClampHandNum", new ValueWrapper.IntValueWrapper(clampHandNum));
 
 
-                    int runningStateInt=SPUtils.getInstance().getInt("RunningState");
-                    reportData.put("RodOpenNum",new ValueWrapper.IntValueWrapper(rodNumberInt));
-                    reportData.put("LockOpenNum",new ValueWrapper.IntValueWrapper(lockNumberInt));
+                        reportData.put("Weight", new ValueWrapper.IntValueWrapper(weight));    //重量  单位 --- 克
+                        reportData.put("CurrentTemperature", new ValueWrapper.IntValueWrapper(currentTemperature));//温度,范围正负 ,精确到小数点后2位
+                        reportData.put("RestCapacity", new ValueWrapper.IntValueWrapper(capacity));  //距离值,单位mm
+                        reportData.put("MagDoorSwitch", new ValueWrapper.BooleanValueWrapper(magDoorSwitch));//门磁 0-关闭状态  1-开门状态
 
 
-                    reportData.put("Weight", new ValueWrapper.IntValueWrapper(weight));    //重量  单位 --- 克
-                    reportData.put("CurrentTemperature", new ValueWrapper.IntValueWrapper(currentTemperature));//温度,范围正负 ,精确到小数点后2位
-                    reportData.put("RestCapacity", new ValueWrapper.IntValueWrapper(capacity));  //距离值,单位mm
-                    reportData.put("MagDoorSwitch", new ValueWrapper.BooleanValueWrapper(magDoorSwitch));//门磁 0-关闭状态  1-开门状态
+                        reportData.put("LockSwitch", new ValueWrapper.BooleanValueWrapper(lockSwitch));   //锁开关状态:0-关锁  1-解锁  解锁后3s就关锁
+                        reportData.put("RodSwitch", new ValueWrapper.BooleanValueWrapper(rodSwitchInt));   //推杆状态:0-关闭状态  1-打开  开门
 
 
-                    reportData.put("LockSwitch", new ValueWrapper.BooleanValueWrapper(lockSwitch));   //锁开关状态:0-关锁  1-解锁  解锁后3s就关锁
-                    reportData.put("RodSwitch", new ValueWrapper.BooleanValueWrapper(rodSwitchInt));   //推杆状态:0-关闭状态  1-打开  开门
 
 
-                    reportData.put("RunningState", new ValueWrapper.BooleanValueWrapper(runningStateInt)); // 0正常
+                        reportData.put("RunningState", new ValueWrapper.BooleanValueWrapper(runningStateInt)); // 0正常
 //            1设备故障 未识别到称重设备 报警  未识别到杆、门磁、锁、温度、红外、超声波设备 报警
 //            1设备故障 未识别到称重设备 报警  未识别到杆、门磁、锁、温度、红外、超声波设备 报警
 //            2温度过高 报警
 //            2温度过高 报警
 //            3收运门意外打开报警
 //            3收运门意外打开报警
 //            4称重失灵报警 错误一次就再读一次  超过两次读取错误就报警
 //            4称重失灵报警 错误一次就再读一次  超过两次读取错误就报警
 //            5温度和超声波失灵警告 失灵显示的000000000
 //            5温度和超声波失灵警告 失灵显示的000000000
 
 
-                    System.out.println(IMEI+"DeviceId");
-                    reportData.put("IMEI", new ValueWrapper.StringValueWrapper(IMEI));
-                    locationMap.put("longitude", new ValueWrapper.DoubleValueWrapper(longitude));
-                    locationMap.put("latitude", new ValueWrapper.DoubleValueWrapper(latitude));
-                    locationMap.put("altitude", new ValueWrapper.DoubleValueWrapper(523.1));
-                    reportData.put("GeoLocation", new ValueWrapper.StructValueWrapper(locationMap)); //
-
-                    int queryCurrent=SPUtils.getInstance().getInt("queryCurrent");
-                    Log.i(TAG, "称重" + weight.toString() + "距离" + capacity.toString() + "温度" + currentTemperature);
-                    textReportData.set("称重 " + weight.toString() + ";距离 " + capacity.toString() + "mm;温度 " +
-                            currentTemperature+"度;门磁 "+magDoorSwitch+";杆状态 "+rodSwitchInt+";锁状态 "+lockSwitch+";杆开门次数 "+rodNumberInt+";开锁次数 "+lockNumberInt+";被夹次数"+queryCurrent+";运行状态"+runningStateInt
-                    );
-
-                    String  text = "称重 " + weight.toString() + ";距离 " + capacity.toString() + "mm;温度 " + currentTemperature+"度;门磁 "+magDoorSwitch+";杆状态 "+
-                            rodSwitchInt+";锁状态 "+lockSwitch+";杆开门次数 "+rodNumberInt+";开锁次数 "+lockNumberInt+";被夹次数"+queryCurrent+"------------------------------";
-                    Log.i(TAG, text + "=textReportData---------------------");
-                    mHandler.postDelayed(new Runnable() {
-                        @Override
-                        public void run() {
-                            LinkKit.getInstance().getDeviceThing().thingPropertyPost(reportData, new IPublishResourceListener() {
-                                @Override
-                                public void onSuccess(String s, Object o) {
-                                    Log.d("", "onSuccess() called with: s = [" + s + "], o = [" + o + "]");
-                                    SPUtils.getInstance().put("RunningState",0);
-                                }
-                                @Override
-                                public void onError(String s, AError aError) {
-                                    Log.e("Main", s);
-                                }
-                            });
-                        }
-                    },200);
+                        System.out.println(IMEI + "DeviceId");
+                        reportData.put("IMEI", new ValueWrapper.StringValueWrapper(IMEI));
+                        locationMap.put("longitude", new ValueWrapper.DoubleValueWrapper(longitude));
+                        locationMap.put("latitude", new ValueWrapper.DoubleValueWrapper(latitude));
+                        locationMap.put("altitude", new ValueWrapper.DoubleValueWrapper(523.1));
+                        reportData.put("GeoLocation", new ValueWrapper.StructValueWrapper(locationMap)); //
+
+                        Log.i(TAG, "称重" + weight.toString() + "距离" + capacity.toString() + "温度" + currentTemperature);
+                        textReportData.set("称重 " + weight.toString() + ";距离 " + capacity.toString() + "mm;温度 " +
+                                currentTemperature + "度;门磁 " + magDoorSwitch + ";杆状态 " + rodSwitchInt + ";锁状态 " + lockSwitch + ";杆开门次数 " + rodNumberInt + ";开锁次数 " + lockNumberInt + ";被夹次数" + clampHandNum + ";运行状态" + runningStateInt
+                        );
+
+                        String text = "称重 " + weight.toString() + ";距离 " + capacity.toString() + "mm;温度 " + currentTemperature + "度;门磁 " + magDoorSwitch + ";杆状态 " +
+                                rodSwitchInt + ";锁状态 " + lockSwitch + ";杆开门次数 " + rodNumberInt + ";开锁次数 " + lockNumberInt + ";被夹次数" + clampHandNum + "------------------------------";
+                        Log.i(TAG, text + "=textReportData---------------------");
+                        mHandler.postDelayed(new Runnable() {
+                            @Override
+                            public void run() {
+                                LinkKit.getInstance().getDeviceThing().thingPropertyPost(reportData, new IPublishResourceListener() {
+                                    @Override
+                                    public void onSuccess(String s, Object o) {
+                                        Log.d("", "onSuccess() called with: s = [" + s + "], o = [" + o + "]");
+                                        SPUtils.getInstance().put("RunningState", 0);
+                                    }
 
 
-                } catch (Exception e) {
-                    e.printStackTrace();
-                }
+                                    @Override
+                                    public void onError(String s, AError aError) {
+                                        Log.e("Main", s);
+                                    }
+                                });
+                            }
+                        }, 200);
 
 
+                    } catch (Exception e) {
+                        e.printStackTrace();
+                    }
+                }
             }
             }
         });
         });
            thread.start();
            thread.start();
@@ -1337,7 +1375,7 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
     }
     }
     //设备事件上报
     //设备事件上报
     public static void reportDeviceEvent(String identifier, String event, int num){
     public static void reportDeviceEvent(String identifier, String event, int num){
-       /* 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));
         if(num==1){
         if(num==1){
@@ -1347,17 +1385,22 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
         }
         }
         OutputParams params = new OutputParams(hashMap);
         OutputParams params = new OutputParams(hashMap);
         Log.i("设备故障",identifier+"====="+params);
         Log.i("设备故障",identifier+"====="+params);
+        Integer networkNum=netWorkInfoStatic();
+        if(networkNum!=0){
+            LinkKit.getInstance().getDeviceThing().thingEventPost(identifier, params, new IPublishResourceListener() {
+                @Override
+                public void onSuccess(String resId, Object o) { // 事件上报成功
+                    Log.i("事件上报","上报成功");
+                }
+                @Override
+                public void onError(String resId, AError aError) { // 事件上报失败
+                    Log.i("事件上报","上报失败");
+                }
+            });
+        }else {
+            Log.i("上报","失败,没有网络");
+        }
 
 
-        LinkKit.getInstance().getDeviceThing().thingEventPost(identifier, params, new IPublishResourceListener() {
-            @Override
-            public void onSuccess(String resId, Object o) { // 事件上报成功
-                Log.i("事件上报","上报成功");
-            }
-            @Override
-            public void onError(String resId, AError aError) { // 事件上报失败
-                Log.i("事件上报","上报失败");
-            }
-        });*/
     }
     }
     private void getDeviceProperty(String identifier){
     private void getDeviceProperty(String identifier){
         // 根据 identifier 获取当前物模型中该属性的值
         // 根据 identifier 获取当前物模型中该属性的值
@@ -1491,6 +1534,7 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
         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);
+        if(networkInt!=0){
             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) {
@@ -1498,9 +1542,7 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
                     mHandler.postDelayed(new Runnable() {
                     mHandler.postDelayed(new Runnable() {
                         @Override
                         @Override
                         public void run() {
                         public void run() {
-                            if(networkInt!=0){
-                                reportProperty();
-                            }
+                            reportProperty();
                         }
                         }
                     },5000);
                     },5000);
 
 
@@ -1524,6 +1566,8 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
 
 
                 }
                 }
             });
             });
+        }
+
     }
     }
 
 
 
 
@@ -1656,18 +1700,19 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
                                 if(timeInt==0){
                                 if(timeInt==0){
                                     timeInt=82;
                                     timeInt=82;
                                 }
                                 }
-                                int finalTimeInt = timeInt;
+                                int finalTimeInt = timeInt*1000+5000;
                                 Log.i("计时",""+finalTimeInt);
                                 Log.i("计时",""+finalTimeInt);
                                 Thread thread=new Thread(new Runnable() {
                                 Thread thread=new Thread(new Runnable() {
                                     @Override
                                     @Override
                                     public void run() {
                                     public void run() {
                                         int numInt=0;
                                         int numInt=0;
                                         long curr=System.currentTimeMillis();
                                         long curr=System.currentTimeMillis();
-                                        while(System.currentTimeMillis()-curr<finalTimeInt+5){
+                                        while(System.currentTimeMillis()-curr<finalTimeInt){
                                             try {
                                             try {
                                                 sleep(1000);
                                                 sleep(1000);
                                                 numInt++;
                                                 numInt++;
-                                                Log.i("倒计时时间",numInt+"");
+
+                                                Log.i("倒计时时间",numInt+"---"+SPUtils.getInstance().getString("RodSwitch"));
                                                 if(SPUtils.getInstance().getString("RodSwitch").equals("0")){
                                                 if(SPUtils.getInstance().getString("RodSwitch").equals("0")){
                                                     break;
                                                     break;
                                                 }
                                                 }
@@ -1683,7 +1728,6 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
                                                 e.printStackTrace();
                                                 e.printStackTrace();
                                             }
                                             }
                                         }
                                         }
-
                                     }
                                     }
                                 });
                                 });
                                 thread.start();
                                 thread.start();
@@ -1691,7 +1735,7 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
                             if(data[1].equals("22")){
                             if(data[1].equals("22")){
                                 mBluetoothGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, requestBytes);// 响应客户端
                                 mBluetoothGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, requestBytes);// 响应客户端
                                 try {
                                 try {
-                                    SPUtils.getInstance().put("magDoorSwitch","1");
+                                    SPUtils.getInstance().put("magDoorSwitch",1);
                                     SerialPortUtil.getInstance().getOtherDevice().openLock();
                                     SerialPortUtil.getInstance().getOtherDevice().openLock();
                                     mHandler.postDelayed(new Runnable() {
                                     mHandler.postDelayed(new Runnable() {
                                         @Override
                                         @Override
@@ -1720,12 +1764,12 @@ 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<3000000) {
-                                                        Thread.sleep(1000);
+                                                    while(System.currentTimeMillis()-curr<600000) {
+                                                        Thread.sleep(10000);
                                                         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();//若关闭,称重清零
-                                                            SPUtils.getInstance().put("magDoorSwitch","0");
+                                                            SPUtils.getInstance().put("magDoorSwitch",0);
 //                                                            SerialPortUtil.getInstance().getOtherDevice().clearWeight();
 //                                                            SerialPortUtil.getInstance().getOtherDevice().clearWeight();
                                                             String response=reportDataMethodEncrypt();
                                                             String response=reportDataMethodEncrypt();
                                                             SPUtils.getInstance().put("response",response);
                                                             SPUtils.getInstance().put("response",response);
@@ -1758,7 +1802,7 @@ 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();//若关闭,称重清零
                         SerialPortUtil.getInstance().getWeightDevice().weightClear();//若关闭,称重清零
-                        SPUtils.getInstance().put("magDoorSwitch","0");
+                        SPUtils.getInstance().put("magDoorSwitch",0);
 //                          SerialPortUtil.getInstance().getOtherDevice().clearWeight();
 //                          SerialPortUtil.getInstance().getOtherDevice().clearWeight();
                         SPUtils.getInstance().put("type","close");
                         SPUtils.getInstance().put("type","close");
                         String response=reportDataMethodEncrypt();
                         String response=reportDataMethodEncrypt();
@@ -1877,15 +1921,15 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
     private List<UsbSerialPort> mEntries = new ArrayList<UsbSerialPort>();
     private List<UsbSerialPort> mEntries = new ArrayList<UsbSerialPort>();
     private BroadcastReceiver mUsbReceiver;
     private BroadcastReceiver mUsbReceiver;
     private UsbManager mUsbManager;
     private UsbManager mUsbManager;
-    UsbSerialPort port = null;
-
-    public void closePort(){
-        try {
-            port.close();
-        } catch (IOException e) {
-            e.printStackTrace();
-        }
-    }
+//    UsbSerialPort port = null;
+//
+//    public void closePort(){
+//        try {
+//            port.close();
+//        } catch (IOException e) {
+//            e.printStackTrace();
+//        }
+//    }
 
 
     public static String getDeviceId(Context context) {
     public static String getDeviceId(Context context) {
         StringBuilder deviceId = new StringBuilder();
         StringBuilder deviceId = new StringBuilder();
@@ -1995,6 +2039,28 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
             return 0;
             return 0;
         }
         }
     }
     }
+
+    /*
+     * 判断是否有网
+     * */
+    public static Integer netWorkInfoStatic(){
+        ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
+        NetworkInfo info = connectivity.getActiveNetworkInfo();
+        if(info != null && info.isAvailable()){
+            if (info.getType() == ConnectivityManager.TYPE_WIFI) {
+                return 2;
+            } else if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
+//移动网络
+                return 1;
+            } else {
+//网络错误
+                return 0;
+            }
+        }else{
+//网络错误
+            return 0;
+        }
+    }
     /*
     /*
     * 重启app方法
     * 重启app方法
     * */
     * */
@@ -2022,73 +2088,70 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
 
 
     public void repeatTask() {
     public void repeatTask() {
         Log.i("----------------------","ok");
         Log.i("----------------------","ok");
-//        task3 =new TimerTask(){
-//            public void run(){
-//                publishRepeat();
-//                Log.i("Task","300秒循环");
-//            }
-//        };
-//        timer.schedule(task3,60000,600000);
-//        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);
+        task3 =new TimerTask(){
+            public void run(){
+                publishRepeat();
+                Log.i("Task","300秒循环");
+            }
+        };
+        timer.schedule(task3,60000,600000);
+        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() {
+                if(readTime()==3){
+                    restartApp();
+                }
+            }
+        };
+        timer.schedule(task2,3300000,3300000);
 
 
         task1=new TimerTask() {
         task1=new TimerTask() {
             @Override
             @Override
             public void run() {
             public void run() {
                 publish();
                 publish();
+                openLight();
             }
             }
         };
         };
         timer.schedule(task1,600000,1800000);
         timer.schedule(task1,600000,1800000);
+
     }
     }
     private void publishRepeat(){
     private void publishRepeat(){
         Integer currentTemperature= SerialPortUtil.getInstance().getOtherDevice().geTemperature();//温度
         Integer currentTemperature= SerialPortUtil.getInstance().getOtherDevice().geTemperature();//温度
         Integer magDoorSwitch=SerialPortUtil.getInstance().getOtherDevice().queryMagDoor();
         Integer magDoorSwitch=SerialPortUtil.getInstance().getOtherDevice().queryMagDoor();
         Integer queryCurrent=SerialPortUtil.getInstance().getOtherDevice().queryCurrent();
         Integer queryCurrent=SerialPortUtil.getInstance().getOtherDevice().queryCurrent();
         if(queryCurrent==1){
         if(queryCurrent==1){
-            int num=SPUtils.getInstance().getInt("queryCurrent");
-            SPUtils.getInstance().put("queryCurrent",num+1);//记录被夹次数
+            int num=SPUtils.getInstance().getInt("ClampHandNum",0);
+            SPUtils.getInstance().put("ClampHandNum",num+1);//记录被夹次数
             SerialPortUtil.getInstance().getOtherDevice().clearCurrent();
             SerialPortUtil.getInstance().getOtherDevice().clearCurrent();
         }
         }
         if(currentTemperature>60){
         if(currentTemperature>60){
             SPUtils.getInstance().put("RunningState",2);//温度过高
             SPUtils.getInstance().put("RunningState",2);//温度过高
             Log.i(TAG,"温度过高");
             Log.i(TAG,"温度过高");
             Integer networkInt=netWorkInfo();
             Integer networkInt=netWorkInfo();
-            if(networkInt!=0){
+//            if(networkInt!=0){
 //                reportProperty();
 //                reportProperty();
                 //短信通知
                 //短信通知
                 SendSmsResponse("温度过高");
                 SendSmsResponse("温度过高");
-
-            }
+//            }
         }
         }
         if(currentTemperature>25){
         if(currentTemperature>25){
 //            SerialPortUtil.getInstance().getOtherDevice().openuFan();//打开风扇
 //            SerialPortUtil.getInstance().getOtherDevice().openuFan();//打开风扇
-            SerialPortUtil.getInstance().getOtherDevice().openLight4();
+//            SerialPortUtil.getInstance().getOtherDevice().openLight1();
         }else{
         }else{
             //            SerialPortUtil.getInstance().getOtherDevice().closeFan();//打开风扇
             //            SerialPortUtil.getInstance().getOtherDevice().closeFan();//打开风扇
-            SerialPortUtil.getInstance().getOtherDevice().closeLight4();
+//            SerialPortUtil.getInstance().getOtherDevice().closeLight1();
         }
         }
-
         if(magDoorSwitch==1){
         if(magDoorSwitch==1){
-            if(SPUtils.getInstance().getString("magDoorSwitch").equals("0")){
+            if(SPUtils.getInstance().getInt("magDoorSwitch")==0){
                 SPUtils.getInstance().put("RunningState",3);
                 SPUtils.getInstance().put("RunningState",3);
                 Log.i(TAG,"收运门意外打开");
                 Log.i(TAG,"收运门意外打开");
                 SendSmsResponse("收运门意外打开");
                 SendSmsResponse("收运门意外打开");
             }
             }
         }
         }
-//                if(currentTemperature<100&magDoorSwitch==0) {
-//                    SPUtils.getInstance().put("RunningState","0");
-//                }
-//            }
-//        });
 
 
     }
     }
     public int readTime(){
     public int readTime(){
@@ -2100,12 +2163,16 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
 
 
     public void openLight(){
     public void openLight(){
         Log.i("d定时","ok");
         Log.i("d定时","ok");
-        if(readTime()>18||readTime()<1){
+        if(readTime()>18||readTime()<2){
             Log.i("night","开灯");
             Log.i("night","开灯");
-            SerialPortUtil.getInstance().getOtherDevice().openLight4();
-        }else {
+            SerialPortUtil.getInstance().getOtherDevice().openLight1();
+        } else {
+
             Log.i("day","关灯");
             Log.i("day","关灯");
-            SerialPortUtil.getInstance().getOtherDevice().closeLight4();
+            SerialPortUtil.getInstance().getOtherDevice().closeLight1();
+            if(readTime()==3){
+                restartAndroid();
+            }
         }
         }
     }
     }
 
 
@@ -2119,43 +2186,15 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
             Log.i("报警","发送"+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("/api/v1/sendMessage/send")
-                    .addParams("code", 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();
-        }*/
+    }
+    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
+    public void startJobScheduler() {
+        int jobId = 1;
+        @SuppressLint("JobSchedulerService") JobInfo.Builder jobInfo = new JobInfo.Builder(jobId, new ComponentName(getApplication(), MyService.class));
+        jobInfo.setPeriodic(10000);
+        jobInfo.setPersisted(true);
+        JobScheduler jobScheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
+        jobScheduler.schedule(jobInfo.build());
     }
     }
 
 
 }
 }

+ 1 - 1
app/src/main/res/layout/activity_main.xml

@@ -75,7 +75,7 @@
                     android:layout_width="120dp"
                     android:layout_width="120dp"
                     android:layout_height="96dp"
                     android:layout_height="96dp"
                     android:onClick="@{viewModal.btnClickReport}"
                     android:onClick="@{viewModal.btnClickReport}"
-                    android:text="数据上报4"
+                    android:text="数据上报4.0"
                     android:textSize="24sp" />
                     android:textSize="24sp" />
                 </LinearLayout>
                 </LinearLayout>
                 <LinearLayout
                 <LinearLayout