From 759ccbfc701c60800da901d8b5822b3528a5b002 Mon Sep 17 00:00:00 2001
From: doum <doum>
Date: 星期二, 10 三月 2026 11:23:58 +0800
Subject: [PATCH] 更改头部

---
 server/visits/device_service/src/main/java/com/doumee/tcp/WaterElectricityUtil.java |  109 +++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 102 insertions(+), 7 deletions(-)

diff --git a/server/visits/device_service/src/main/java/com/doumee/tcp/WaterElectricityUtil.java b/server/visits/device_service/src/main/java/com/doumee/tcp/WaterElectricityUtil.java
index ebbfc0f..7b1f654 100644
--- a/server/visits/device_service/src/main/java/com/doumee/tcp/WaterElectricityUtil.java
+++ b/server/visits/device_service/src/main/java/com/doumee/tcp/WaterElectricityUtil.java
@@ -60,6 +60,7 @@
         return (byte) (sum & 0xFF);
     }
 
+
     public static byte[] getRequestParam(int feCount, byte[] address, byte control, byte[] data) throws IOException {
 
         ///FEFEFE    68  999999999999    68  01  02  65  F3C1  16
@@ -170,6 +171,17 @@
         return hexResult;
 
     }
+    private static String addByte(String value, byte add) {
+        byte b = (byte) Integer.parseInt(value, 16);
+        int result = (b & 0xFF) + (add & 0xFF);
+        // 纭繚缁撴灉鍦�0-255鑼冨洿鍐咃紙澶勭悊璐熸暟锛�
+        if (result < 0) {
+            result += 256;
+        }
+        String hexResult = String.format("%02X", result & 0xFF);
+        return hexResult;
+
+    }
 
 
     private static String[] parseSub33Reverse(String msg, int n) {
@@ -240,6 +252,83 @@
         map.put("total", total);
     }
 
+    /**
+     * 鐢佃〃璺抽椄銆佸悎闂�
+     * @param ip
+     * @param port
+     * @param addressBuf
+     * @param type 锛�0璺抽椄 1鍚堥椄锛�
+     * @param date 鏈夋晥鎴鏃堕棿
+     * @throws IOException
+     */
+    private static boolean electricityControl(String ip, int port, byte[] addressBuf,int type,  String date)   {
+        /**
+         * N1涓烘帶鍒跺懡浠ょ被鍨嬶紝N1=1AH浠h〃璺抽椄4D锛孨1=1BH浠h〃鍚堥椄4F鍏佽N2淇濈暀
+         * 鏉冮檺瀵嗙爜鎿嶄綔浠g爜锛歅AP2P1P0C3C2C1C0(02/223203/111111H)
+         * 4F
+         * TCP/IP鐩存帴鍚堥椄鎸囦护[2026骞�03鏈�09鏃� 10:57:39]
+         * Tx ->FEFEFEFE68615121010000681C1035366555776655444F33568843433659E016
+         * TCP/IP鐩存帴鍚堥椄鎸囦护[2026骞�03鏈�09鏃� 10:57:40]
+         * Rx <-FEFEFEFE68615121010000689C004016
+         * 鐩存帴鍚堥椄鎵ц鎴愬姛锛� 9C
+         */
+        try {
+            byte control = 0x1C;
+            byte n1 =  0x4D;
+            if(type==1){
+                n1 =  0x4F;
+            }
+//        byte[] data0 = {0x02, 0x03, 0x32, 0x22,0x44,0x33,0x22,0x11};
+            byte[] data = {0x35, 0x36, 0x65, 0x55,0x77,0x66,0x55,0x44,n1,0x33,0,0,0,0,0,0};
+            byte[] data2 = getDateBytes(date);
+            for (int i = 0; i < data2.length; i++) {
+                data[10+i]=data2[i];
+            }
+            byte[] bufReq = getRequestParam(4, addressBuf, control, data);
+            String reqt = bytesToHex(bufReq);
+            System.out.println(reqt);
+            byte[] respBuf = readDeviceData(ip, port, bufReq);
+            String resp = bytesToHex(respBuf);
+            System.out.println(resp);
+            //FEFEFEFE68379707010000689C004216
+             String msg = resp.substring(24, 26);
+             return msg.equals("9C");
+        }catch (Exception e){
+
+        }
+        return  false;
+    }
+
+    private static byte[] getDateBytes(String dateStr) {
+//        String dateStr = new SimpleDateFormat("yyyyMMddHHmmss").format(date);
+        dateStr = dateStr.substring(2);
+        // 1. 楠岃瘉杈撳叆
+        if (dateStr == null || dateStr.length() != 12) {
+            throw new IllegalArgumentException("鏃堕棿蹇呴』鏄�12浣嶅崄杩涘埗鏁�");
+        }
+        if (!dateStr.matches("\\d{12}")) {
+            throw new IllegalArgumentException("鏃堕棿蹇呴』鍏ㄩ儴鏄暟瀛�");
+        }
+        // 2. 鍑嗗缁撴灉鏁扮粍锛�6瀛楄妭锛�
+        byte[] result = new byte[6];
+        // 3. 浠庡彸鍚戝乏姣�2浣嶄竴缁勫鐞嗭紙灏忕搴忥級
+        byte add = 0x33;
+        for (int i = 0; i < 6; i++) {
+            // 璁$畻鍦ㄥ瓧绗︿覆涓殑浣嶇疆锛堜粠鍙冲悜宸︼級
+            int strIndex = 10 - (i * 2);  // 鍥犱负瑕佸彇涓や綅锛屾墍浠ユ槸10,8,6,4,2,0
+            String twoDigits = dateStr.substring(strIndex, strIndex + 2);
+            // 灏嗕袱浣嶅崄杩涘埗鏁拌浆鎹负BCD瀛楄妭
+            // 渚嬪锛�"25" -> 0x25
+            int t = Integer.parseInt(twoDigits, 16);
+            int t1 =   ( t& 0xFF) + (add & 0xFF);
+            System.out.println(t1+":");
+            result[i] =(byte) t1 ;
+        }
+        // 娉ㄦ剰锛氫笂闈㈢殑寰幆椤哄簭宸茬粡鏄皬绔簭锛宺esult[0]瀛樼殑鏄渶浣庝袱浣�
+        return result;
+    }
+
+
     private static void electricityStatus(String ip, int port, byte[] addressBuf, Map<String, Object> map) throws IOException {
         byte control = 0x11;
         byte[] data = {0x36, 0x38, 0x33, 0x37};
@@ -266,8 +355,10 @@
         String ts = hexToBinary1(nArr);
         String time = "20"+ts.substring(0,6)+ts.substring(8);
         Date date = getDateByStr(time);
+        System.out.println( formatData(date));
 //        String resp = "FEFEFEFE   68  615121010000    68  91  06  36383337    3333    7916";
-        map.put("currentTime", formatData(date));
+        map.put("time", date);
+//        map.put("currentTime", formatData(date));
     }
     public static Date getDateByStr(String date)  {
         TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
@@ -296,7 +387,7 @@
         return null;
     }
 
-    public static Map<String, Object> electricity(String ip, int port, String address) throws IOException {
+    public static Map<String, Object> electricityData(String ip, int port, String address) throws IOException {
         Map<String, Object> r = new HashMap<>();
         byte[] addressBuf = convertToBCDAddress(address);
         electricityTotal(ip, port, addressBuf, r);
@@ -304,7 +395,11 @@
         electricityTime(ip, port, addressBuf, r);
         return r;
     }
-
+    public static boolean electricityAct(String ip, int port, String address,int type,String dateStr) {
+        Map<String, Object> r = new HashMap<>();
+        byte[] addressBuf = convertToBCDAddress(address);
+       return electricityControl(ip,port,addressBuf,type,dateStr);
+    }
     private static String hexToBinary(String hex) {
         StringBuilder sb = new StringBuilder();
         for (char c : hex.toCharArray()) {
@@ -328,7 +423,6 @@
         }
         return sb.toString();
     }
-
 
     private static String addHex(String hex1, String hex2) {
         int num1 = Integer.parseInt(hex1.replace("0x", ""), 16);
@@ -494,9 +588,10 @@
     public static void main(String[] args) throws IOException {
 //        testWater();
 //        electricityTest();
-        water("192.168.1.78",1030,"000152462599");
-        Map<String, Object> map = electricity("192.168.1.78", 1030, "000001215161");
-        System.out.println(JSONObject.toJSONString(map));
+//        water("192.168.1.78",1030,"000152462599");
+//        Map<String, Object> map = electricityData("192.168.1.78", 1030, "000001215161");
+//        System.out.println(JSONObject.toJSONString(map));
+        electricityAct("192.168.1.78", 1030, "000001215161",0,"20260309162655");
 //        FEFEFE6899254652010068810 843C3333433333333   E9 16
 //        FEFEFE6899254652010068810 84  3C3333433333333E916
 

--
Gitblit v1.9.3