소스 검색

新增多路电源可以合并成一路同时控制

weizhengliang 1 년 전
부모
커밋
9b84937f23

+ 16 - 5
conversion_box/src/main/java/com/wdkl/app/ncs/conversion_box/activity/MainActivity.kt

@@ -1794,16 +1794,27 @@ class MainActivity :BaseActivity<MainActivityPresenter, MainActivityLayoutBindin
                             DeviceUtil.s485PowerResetSuccess(Constant.DEVICE_ID,tcpModel.data.toString(),tcpModel.tid)
                             if(pwcontrol.size==2){
                                 val lines = pwcontrol[1].split(",")
-                                for (line in lines) {
-                                    PowerControlUtil.controlPowerControl(pwcontrol[0].toInt(), line.toInt(), true)
+                                if (lines.size > 1) {
+                                    //多路并成一路控制
+                                    PowerControlUtil.controlPowerControlList(pwcontrol[0].toInt(), lines, true)
                                     AppTool.Time.delay(3000){
-                                        PowerControlUtil.controlPowerControl(pwcontrol[0].toInt(), line.toInt(),false)
+                                        PowerControlUtil.controlPowerControlList(pwcontrol[0].toInt(), lines,false)
+                                    }
+                                } else if (lines.size == 1) {
+                                    //单路控制
+                                    PowerControlUtil.controlPowerControl(pwcontrol[0].toInt(), lines[0].toInt(), true)
+                                    AppTool.Time.delay(3000){
+                                        PowerControlUtil.controlPowerControl(pwcontrol[0].toInt(), lines[0].toInt(),false)
                                     }
                                 }
                             }else if(pwcontrol.size==3){
                                 val lines = pwcontrol[1].split(",")
-                                for (line in lines) {
-                                    PowerControlUtil.controlPowerControl(pwcontrol[0].toInt(), line.toInt(), pwcontrol[2].toInt() == 0)
+                                if (lines.size > 1) {
+                                    //多路并成一路控制
+                                    PowerControlUtil.controlPowerControlList(pwcontrol[0].toInt(), lines, pwcontrol[2].toInt() == 0)
+                                } else if (lines.size == 1) {
+                                    //单路控制
+                                    PowerControlUtil.controlPowerControl(pwcontrol[0].toInt(), lines[0].toInt(), pwcontrol[2].toInt() == 0)
                                 }
                             }
                         }

+ 16 - 1
conversion_box/src/main/java/com/wdkl/app/ncs/conversion_box/helper/PowerControlUtil.java

@@ -2,9 +2,11 @@ package com.wdkl.app.ncs.conversion_box.helper;
 
 import android.os.CountDownTimer;
 
+import java.util.List;
+
 public class PowerControlUtil {
 
-    public static void controlPowerControl(Integer address,Integer lineNumber,Boolean off){
+    public static void controlPowerControl(final Integer address, final Integer lineNumber, final Boolean off){
         new CountDownTimer(3000,1000){
 
             @Override
@@ -18,5 +20,18 @@ public class PowerControlUtil {
         }.start();
     }
 
+    public static void controlPowerControlList(final Integer address, final List<String> lines, final Boolean off){
+        new CountDownTimer(3000,1000){
+
+            @Override
+            public void onTick(long l) { //每隔1秒发送一次控制指令,总共下发3次
+                SerialPortHelper.s485PowerControlLines(address, lines, off);
+            }
+            @Override
+            public void onFinish() {
+
+            }
+        }.start();
+    }
 
 }

+ 38 - 0
conversion_box/src/main/java/com/wdkl/app/ncs/conversion_box/helper/SerialPortHelper.java

@@ -5,6 +5,7 @@ import android.util.Log;
 import com.wdkl.ncs.android.middleware.common.Constant;
 
 import java.nio.ByteBuffer;
+import java.util.List;
 
 import serialporttest.utils.SerialPortUtil;
 
@@ -165,6 +166,43 @@ public class SerialPortHelper {
         SerialPortUtil.getInstance().send(bytes);
     }
 
+    public static void s485PowerControlLines(Integer controlAddress, List<String> lineNumbers, Boolean off){
+        if(lineNumbers.size() < 1){
+            return;
+        }
+        String address = String.format("%02x",controlAddress&0xFF);
+        StringBuilder sb = new StringBuilder();
+        sb.append("24").append(address).append("2C");
+
+        String controlStatus = "FFFFFFFF";  //默认所有电路开启
+        byte[] commandBytes = hexStringToByteArray(controlStatus);
+        if (off) {
+            for (String num : lineNumbers) {
+                int line = Integer.parseInt(num);
+                if (line < 0 || line > 32) { //传入线路序号小于0或者大于28,认为无效线路,如果是0认为是控制所有线路
+                    break;
+                }
+
+                if (line > 0) {
+                    controlStatus = POWER_CONTROL_STATUS[line - 1];
+                    byte[] temp = hexStringToByteArray(controlStatus);
+                    //多路控制,按位取&
+                    for (int i = 0; i < commandBytes.length; i++) {
+                        commandBytes[i] = (byte) (temp[i] & commandBytes[i]);
+                    }
+                }
+            }
+        }
+
+        for (int i = 0; i < commandBytes.length; i++) { //与协议相反,按位取反
+            commandBytes[i]=(byte)~commandBytes[i];
+        }
+        sb.append(byteArrToHexString(commandBytes)).append("4623");
+        byte[] bytes=hexStringToByteArray(sb.toString());
+        Log.i("s485PowerControl", "s485PowerControl: "+bytes[0]+","+bytes[1]+","+bytes[2]+","+bytes[3]+","+bytes[4]+","+bytes[5]+","+bytes[6]+","+bytes[7]+","+bytes[8]+",");
+        SerialPortUtil.getInstance().send(bytes);
+    }
+
 
     public static byte[] intToBytes(int a){
         ByteBuffer bb = ByteBuffer.allocate(4);