Ver Fonte

消息队列通信,开关门上报事件信息。

DESKTOP-SADJPBG\47462 há 5 anos atrás
pai
commit
d6dc115327

+ 2 - 1
app/build.gradle

xqd xqd
@@ -14,7 +14,7 @@ android {
             storePassword '159753'
             storeFile file('\\box.keystore')
         }
-        debug{
+        debug {
             keyAlias 'box.keystore'
             keyPassword '159753'
             storePassword '159753'
@@ -63,6 +63,7 @@ android {
         sourceCompatibility = 1.8
         targetCompatibility = 1.8
     }
+    sourceSets { main { jni.srcDirs = ['src/main/jni', 'src/main/jni/2'] } }
 }
 
 dependencies {

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

xqd xqd
@@ -171,11 +171,11 @@ public class AppApplication extends BaseApplication implements CustomActivityOnC
          *   调用动态注册接口去获取deviceSecret
          */
         DeviceInfo myDeviceInfo = new DeviceInfo();
-        myDeviceInfo.productKey = "a1EtqHmfpNk";//正式服
-//        myDeviceInfo.productKey = "a13H8L6bDyf";//测试服
+//        myDeviceInfo.productKey = "a1EtqHmfpNk";//正式服
+        myDeviceInfo.productKey = "a13H8L6bDyf";//测试服
 //
-        myDeviceInfo.productSecret = "x6wHbrbXUMFYtvLl";//正式服
-//        myDeviceInfo.productSecret = "Nc4Y4KsjofejCy27";//测试服
+//        myDeviceInfo.productSecret = "x6wHbrbXUMFYtvLl";//正式服
+        myDeviceInfo.productSecret = "Nc4Y4KsjofejCy27";//测试服
 
         System.out.println(MainViewModel.getDeviceId(this)+"----------------------------+");
         myDeviceInfo.deviceName = MainViewModel.getDeviceId(this);
@@ -184,8 +184,8 @@ public class AppApplication extends BaseApplication implements CustomActivityOnC
 //        myDeviceInfo.deviceName="123456";
 
 //        String secret = SPUtils.getInstance().getString("deviceSecret3");//读
-        String secret = SPUtils.getInstance().getString("deviceSecret2");//读
-//        String secret ="WMClVkIJipSunIY4fOSd2Be3xo42TfVs";
+//        String secret = SPUtils.getInstance().getString("deviceSecret2");//读
+        String secret ="WMClVkIJipSunIY4fOSd2Be3xo42TfVs";
         Log.i(TAG,"secret=="+secret);
 
         if(secret != null &&  !secret.equals("")){

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

xqd xqd xqd
@@ -133,7 +133,6 @@ public class OtherDevice extends BaseDeviceEntity {
             e.printStackTrace();
             SPUtils.getInstance().put("RunningState",1);
             MainViewModel.SendSmsResponse("其他设备未检测到");
-
             Log.e(TAG,"读取门磁错误",e);
             return 3;
         }
@@ -205,10 +204,14 @@ public class OtherDevice extends BaseDeviceEntity {
             return 0;
         }
     }
-
+    /*
+    * 设置电流阈值
+    * */
     public void setPower(){
         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, 0x03, 0x60, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD};
+
             port.write(setPower, 500);
             byte[] response = new byte[12];
             port.read(response,500);
@@ -216,8 +219,9 @@ public class OtherDevice extends BaseDeviceEntity {
         }catch(Exception e){
             e.printStackTrace();
         }
-
     }
+
+
     /**
      * 伸杆关门
      */

+ 12 - 11
app/src/main/java/com/siwei/recyclebox/deviceUtils/SerialPortUtil.java

xqd xqd xqd
@@ -27,11 +27,12 @@ import me.goldze.mvvmhabit.utils.SPUtils;
  */
 public class SerialPortUtil {
     private Context mContext ;
-    private SerialPortUtil(){
-    }
+
 
     private static final String TAG = "SerialPortUtil";
     private static final String INTENT_ACTION_GRANT_USB = "com.siwei.recyclebox.GRANT_USB";
+    private SerialPortUtil(){
+    }
     private static SerialPortUtil instance;
     public static SerialPortUtil getInstance(){
         if(instance == null){
@@ -141,7 +142,7 @@ public class SerialPortUtil {
 
     public WeightDevice getWeightDevice(){
         for(BaseDeviceEntity deviceEntity  : deviceList){
-            if(deviceEntity instanceof WeightDevice)
+            if(deviceEntity instanceof WeightDevice)//用来测试一个对象是否为一个类的实例
                 return (WeightDevice) deviceEntity;
         }
         return null;
@@ -178,19 +179,19 @@ public class SerialPortUtil {
 
     private BaseDeviceEntity isOtherDevice( UsbSerialDriver driver ){
         OtherDevice otherDevice = new OtherDevice(mUsbManager,driver);
-//        Integer infrared = otherDevice.queryInfrared();
-        Integer  magDoor = otherDevice.queryMagDoor();
+        Integer infrared = otherDevice.queryInfrared();
+//        Integer  magDoor = otherDevice.queryMagDoor();
         Log.i("otherDevice","---------------------------------------");
-//        if(infrared == 0) {
-//            otherDevice.close();
-//            Log.i("otherDevice","==null");
-//            return null;
-//        }
-        if(magDoor == 3) {
+        if(infrared == 0) {
             otherDevice.close();
             Log.i("otherDevice","==null");
             return null;
         }
+//        if(magDoor == 3) {
+//            otherDevice.close();
+//            Log.i("otherDevice","==null");
+//            return null;
+//        }
         return otherDevice;
     }
 

+ 25 - 15
app/src/main/java/com/siwei/recyclebox/deviceUtils/WeightDevice.java

xqd xqd xqd xqd xqd
@@ -115,7 +115,8 @@ public class WeightDevice extends BaseDeviceEntity {
                     weightBytes[3] = response[4];
                     int weightInt = ByteUtil.bytes2Int(weightBytes);
 //                    weightInt=weightInt*5*2;
-                    num += (weightInt * 5 * 2);
+                    num += (weightInt * 10);
+//                    num += weightInt;
                     a++;
                     Log.i("while","********"+a);
                 }
@@ -124,7 +125,7 @@ public class WeightDevice extends BaseDeviceEntity {
                 weightBytes[2] = response[3];
                 weightBytes[3] = response[4];
                 int weightInt = ByteUtil.bytes2Int(weightBytes);
-                weightInt=weightInt*5*2;
+                weightInt=weightInt*10;
                 System.out.println("称重读数:"+weightInt);
                 int weightNumber=num/a++;
                 if(weightInt>=327670){
@@ -157,39 +158,44 @@ public class WeightDevice extends BaseDeviceEntity {
      */
     public synchronized void weightClear(){
         byte[] cleanOrderBytes = new byte[]{0x1F,0x06 ,0x00 ,0x24,0x00 ,0x00, (byte) 0xCA,0x7F};
-        System.out.println("称重器读数清零!!!!!");
+        System.out.println();
         try {
             port.write(cleanOrderBytes,150);
             byte[] response=new byte[8];
             port.read(response,200);
+            Log.i(TAG,"称重器读数清零!!!!!:"+ByteUtil.bytesToString(response));
         } catch (IOException e) {
             e.printStackTrace();
         }
     }
+
     /*
-     * 设置分度值
+     * 空载
      * */
-    public synchronized void setScale(){
-        byte[] cleanOrderBytes = new byte[]{0x1F, 0x06 ,0x00 ,0x04 ,0x00 ,0x01 ,0x0A ,0x75};
+    public synchronized void zeroCalibration(){
+        byte[] cleanOrderBytes = new byte[]{0x1F ,0x10 ,0x00 ,0x08 ,0x00 ,0x02 ,0x04 ,0x00 ,0x00 ,0x00 ,0x00 , (byte) 0x92,0x21 };
         try {
             port.write(cleanOrderBytes,150);
             byte[] response=new byte[8];
             port.read(response,200);
-            Log.i(TAG,"设置分度值");
+            Log.i(TAG,"空载:"+ByteUtil.bytesToString(response));
         } catch (IOException e) {
             e.printStackTrace();
         }
     }
     /*
-     * 空载
+     * 设置分度值
      * */
-    public synchronized void zeroCalibration(){
-        byte[] cleanOrderBytes = new byte[]{0x1F ,0x10 ,0x00 ,0x08 ,0x00 ,0x02 ,0x04 ,0x00 ,0x00 ,0x00 ,0x00 , (byte) 0x92,0x21 };
+    public synchronized void setScale(){//1F 06 00 04 00 01 0A 75              02  4A 74
+
+//        byte[] cleanOrderBytes = new byte[]{0x1F, 0x06 ,0x00 ,0x04 ,0x00 ,0x02 ,0x0A ,0x75};
+        byte[] cleanOrderBytes = new byte[]{0x1F, 0x06 ,0x00 ,0x04 ,0x00 ,0x02 ,0x4A ,0x74};
+
         try {
             port.write(cleanOrderBytes,150);
             byte[] response=new byte[8];
             port.read(response,200);
-            Log.i(TAG,"空载");
+            Log.i(TAG,"设置分度值2:"+ByteUtil.bytesToString(response));
         } catch (IOException e) {
             e.printStackTrace();
         }
@@ -197,13 +203,15 @@ public class WeightDevice extends BaseDeviceEntity {
     /*
      * 量程设定
      * */
-    public synchronized void weightSet(){
+    public synchronized void weightSet(){//1F 10 00 00 00 02 04 27 10 00 00 98 F6
         byte[] cleanOrderBytes = new byte[]{0x1F ,0x10 ,0x00 ,0x00 ,0x00 ,0x02 ,0x04 , 0x27, 0x10,0x00 ,0x00 , (byte) 0x98, (byte) 0xF6};
+//        byte[] cleanOrderBytes = new byte[]{0x1F ,0x10 ,0x00 ,0x00 ,0x00 ,0x02 ,0x04 , 0x4E, 0x20,0x00 ,0x00 , (byte) 0x98, (byte) 0xF6};
+
         try {
             port.write(cleanOrderBytes,150);
             byte[] response=new byte[8];
             port.read(response,200);
-            Log.i(TAG,"l量程设定");
+            Log.i(TAG,"l0000量程设定:"+ByteUtil.bytesToString(response));
         } catch (IOException e) {
             e.printStackTrace();
         }
@@ -213,13 +221,15 @@ public class WeightDevice extends BaseDeviceEntity {
      * */
     public synchronized void weightCalibration(){
         //1F 10 00 10 00 02 04 03 E8 00 00 12 FB
-        byte[] cleanOrderBytes = new byte[]{0x1F ,0x10 ,0x00 ,0x10 ,0x00 ,0x02 ,0x04 ,0x03 , (byte) 0xE8,0x00 ,0x00, (byte) 0x96, (byte) 0xE4};
+        byte[] cleanOrderBytes = new byte[]{0x1F ,0x10 ,0x00 ,0x10 ,0x00 ,0x02 ,0x04 ,0x03 , (byte) 0xE8,0x00 ,0x00, 0x12, (byte) 0xFB};
+//        byte[] cleanOrderBytes = new byte[]{0x1F ,0x10 ,0x00 ,0x10 ,0x00 ,0x02 ,0x04 ,0x27 , 0x10,0x00 ,0x00, (byte) 0x96, (byte) 0xE4};
+
 //        byte[] cleanOrderBytes = new byte[]{0x1F ,0x10 ,0x00 ,0x10 ,0x00 ,0x02 ,0x04 ,0x27 ,0x10 ,0x00 ,0x00, (byte) 0x96, (byte) 0xE4};
         try {
             port.write(cleanOrderBytes,150);
             byte[] response=new byte[8];
             port.read(response,200);
-            Log.i(TAG,"砝码校准kg");
+            Log.i(TAG,"砝码校准10kg:"+ByteUtil.bytesToString(response));
         } catch (IOException e) {
             e.printStackTrace();
         }

+ 74 - 24
app/src/main/java/com/siwei/recyclebox/ui/main/MainViewModel.java

xqd xqd xqd xqd xqd xqd xqd xqd xqd xqd xqd xqd
@@ -184,12 +184,15 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
     * 按钮模拟指令
     * */
     public View.OnClickListener btnClickqueryCurrent = new View.OnClickListener() {
-        //电流过大查询
+        //查询电流过大值
         @Override
         public void onClick(View view) {
-            System.out.println("btnClickqueryCurrent");
-            Integer queryCurrentData=SerialPortUtil.getInstance().getOtherDevice().queryCurrent();
-            textqueryCurrentData.set(String.valueOf(queryCurrentData));
+//            System.out.println("btnClickqueryCurrent");
+//            Integer queryCurrentData=SerialPortUtil.getInstance().getOtherDevice().queryCurrent();
+//            textqueryCurrentData.set(String.valueOf(queryCurrentData));
+
+
+
         }
     };
     public void onSerialPortDataReceived(ComPortData comPortData) {
@@ -316,6 +319,8 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
         public void onClick(View view) {
 //            SendSmsResponse("(测试12355656)");
             SendSmsResponse("温度超声波失灵");
+            SendSmsResponse("温度超声波失灵");
+            reportDeviceEvent("commoneEvent","open",3);
         }
     };
     public View.OnClickListener btnClickGetDeviceValue = new View.OnClickListener() {
@@ -343,10 +348,17 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
         @Override
         public void onClick(View view) {
             System.out.println("btnClicksetScale");
-//            SerialPortUtil.getInstance().getOtherDevice().setScale();
-            SerialPortUtil.getInstance().getWeightDevice().setScale();
-//            SerialPortUtil.getInstance().getOtherDevice().weightSet();
+            //            SerialPortUtil.getInstance().getOtherDevice().weightSet();
             SerialPortUtil.getInstance().getWeightDevice().weightSet();
+//            SerialPortUtil.getInstance().getOtherDevice().setScale();
+            mHandler.postDelayed(new Runnable() {
+                @Override
+                public void run() {
+                    SerialPortUtil.getInstance().getWeightDevice().setScale();
+                }
+            },1000);
+//            SerialPortUtil.getInstance().getWeightDevice().setScale();
+
         }
     };
     public View.OnClickListener btnClickclearWeight = new View.OnClickListener() {
@@ -676,10 +688,11 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
             }
         },13000);
 
-        mHandler.postDelayed(new Runnable() {
+        mHandler.postDelayed(new Runnable() {//关门过程中称重会有波动,暂时不上报
             @Override
             public void run() {
                 reportProperty();
+                reportDeviceEvent("commoneEvent","close",3);
             }
         },4000);
     }
@@ -822,8 +835,15 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
 //                SerialPortUtil.getInstance().getOtherDevice().clearWeight();//去皮清零
                     SPUtils.getInstance().put("clearWight",2);
                 }
-                pullonNetRodMethod();
-                reportProperty();
+                mHandler.postDelayed(new Runnable() {
+                    @Override
+                    public void run() {
+                        reportProperty();
+                        pullonNetRodMethod();
+                        reportDeviceEvent("commoneEvent","open",3);
+                    }
+                },500);
+
                 Log.i(TAG,"欢迎使用智能回收箱");
                 Log.i(TAG,"收到开门=============");
                 Thread thread=new Thread(new Runnable() {
@@ -840,6 +860,7 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
                                 }
                                 if(numInt==82){
                                     pushRodMethod();
+                                    reportDeviceEvent("commoneEvent","close",3);
                                     break;
                                 }
                             } catch (InterruptedException e) {
@@ -1162,16 +1183,16 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
 //            weight=SerialPortUtil.getInstance().getOtherDevice().readWeight();//称重
                         Integer capacity = 0;
                         capacity = SerialPortUtil.getInstance().getOtherDevice().getDistance();//超声波距离
-                        if (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();//温度
-                        }
+//                        if (currentTemperature == 0) {
+//                            currentTemperature = SerialPortUtil.getInstance().getOtherDevice().geTemperature();//温度
+//                        }
                         Log.i(TAG, "----------------------------------------");
 
                         Map<String, ValueWrapper> locationMap = new HashMap<>();
@@ -1394,15 +1415,25 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
     //设备事件上报
     public static void reportDeviceEvent(String identifier, String event, int num){
         HashMap<String, ValueWrapper> hashMap = new HashMap<>();
+        HashMap<String, ValueWrapper> hashMapWeight = new HashMap<>();
         // TODO 用户根据实际情况设置
         // hashMap.put("ErrorCode", new ValueWrapper.IntValueWrapper(0));
         if(num==1){
             hashMap.put("warnEvent",new ValueWrapper.StringValueWrapper(event));
         }else if(num==2){
             hashMap.put("errorEvent",new ValueWrapper.StringValueWrapper(event));
+        }else if(num==3){
+            int weight =SerialPortUtil.getInstance().getWeightDevice().readWeight();
+            String weightS=weight+"";
+            hashMap.put("action",new ValueWrapper.StringValueWrapper(event));
+            hashMap.put("weight",new ValueWrapper.StringValueWrapper(weightS));
+//            hashMapWeight.put("weight",new ValueWrapper.IntValueWrapper(weight));
+//            hashMapWeight.put("action",new ValueWrapper.StringValueWrapper(event));
+//            hashMapWeight.put("weight",new ValueWrapper.IntValueWrapper(weight));
+//            hashMap.put("commoneEvent", new ValueWrapper.StructValueWrapper(hashMapWeight));
         }
         OutputParams params = new OutputParams(hashMap);
-        Log.i("设备故障",identifier+"====="+params);
+        Log.i("事件上报",identifier+"====="+event);
         int networkNum=SPUtils.getInstance().getInt("netWork",1);
         if(networkNum!=0){
             LinkKit.getInstance().getDeviceThing().thingEventPost(identifier, params, new IPublishResourceListener() {
@@ -1447,9 +1478,13 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
         // 注册下行监听,包括长连接的状态和云端下行的数据
         LinkKit.getInstance().registerOnPushListener(notifyListener);
         MqttRrpcRegisterRequest rrpcRegisterRequest = new MqttRrpcRegisterRequest();
+//        rrpcRegisterRequest.topic = "/a1EtqHmfpNk/9pD3trz6OaDV8GF7yRsb/thing/service/currentWeight";
         rrpcRegisterRequest.topic = "/a13H8L6bDyf/9pD3trz6OaDV8GF7yRsb/thing/service/currentWeight";
+
         MqttSubscribeRequest subscribeRequest = new MqttSubscribeRequest();
+//        subscribeRequest.topic = "/a1EtqHmfpNk/9pD3trz6OaDV8GF7yRsb/user/checkOnlineStatus";
         subscribeRequest.topic = "/a13H8L6bDyf/9pD3trz6OaDV8GF7yRsb/user/checkOnlineStatus";
+
         subscribeRequest.isSubscribe = true;
         subscribeRequest.qos = 0;
         LinkKit.getInstance().subscribe(subscribeRequest, new IConnectSubscribeListener() {
@@ -1548,7 +1583,9 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
         System.out.println("checkOnlineStatus");
         MqttPublishRequest publishRequest = new MqttPublishRequest();
         publishRequest.qos = 0;
-        publishRequest.topic = "/a13H8L6bDyf/device2/user/checkOnlineStatus";
+//        publishRequest.topic = "/a1EtqHmfpNk/"+IMEI+"/user/checkOnlineStatus";//a1EtqHmfpNk
+        publishRequest.topic = "/a13H8L6bDyf/"+IMEI+"/user/checkOnlineStatus";//a1EtqHmfpNk
+
         publishRequest.payloadObj = "{\"id\":"+ publishRequest.msgId+", \"version\":\"1.0\",\"time\":" + System.currentTimeMillis() + "}";
 //        Integer networkInt=netWorkInfo();
 //        Log.i(TAG,"network网络"+networkInt);
@@ -2170,8 +2207,7 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
 //            }
         }
         if(currentTemperature>25){
-//            SerialPortUtil.getInstance().getOtherDevice().openuFan();//打开风扇
-//            SerialPortUtil.getInstance().getOtherDevice().openLight1();
+//            SerialPortUtil.getInstance().getOtherDevice().openLight1();/打开风扇
         }else{
             //            SerialPortUtil.getInstance().getOtherDevice().closeFan();//打开风扇
 //            SerialPortUtil.getInstance().getOtherDevice().closeLight1();
@@ -2216,11 +2252,25 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
 
     public static void SendSmsResponse(String event){//
         if(event.equals("温度超声波失灵")){
-            reportDeviceEvent("errorEvent","温度超声波失灵",2);
-            Log.i("警告","发送"+event);
+            int errorNum=SPUtils.getInstance().getInt("errorEvent",1)+1;
+            SPUtils.getInstance().put("errorEvent",errorNum);
+            if(errorNum==20){//过滤,每20次再上报一次,因为超声和温度是同一个设备,读取了2次,实际上是10次上报一次
+                reportDeviceEvent("errorEvent","温度超声波失灵",2);
+                Log.i("警告","发送"+event);
+                SPUtils.getInstance().put("errorEvent",0);
+            }
+            Log.i("警告","次数"+errorNum);
+//            reportDeviceEvent("errorEvent","温度超声波失灵",2);
+//            Log.i("警告","发送"+event);
         }else {
-            reportDeviceEvent("warnEvent",event,1);
-            Log.i("报警","发送"+event);
+            int warnEvent=SPUtils.getInstance().getInt("warnEvent",1)+1;
+            SPUtils.getInstance().put("errorEvent",warnEvent);
+            if(warnEvent==3){//过滤
+                reportDeviceEvent("warnEvent",event,1);
+                Log.i("报警","发送"+event);
+                SPUtils.getInstance().put("errorEvent",0);
+            }
+            Log.i("报警","次数"+warnEvent);
         }
 
     }

+ 1 - 0
gradle.properties

xqd
@@ -11,5 +11,6 @@ org.gradle.jvmargs=-Xmx1536m
 # This option should only be used with decoupled projects. More details, visit
 # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
 # org.gradle.parallel=true
+android.useDeprecatedNdk=true