Jelajahi Sumber

称重系数

Newspaper 1 tahun lalu
induk
melakukan
7ab2f75d4b

+ 2 - 0
input/src/main/java/com/huimv/input/entity/EnvDevice.java

@@ -67,6 +67,8 @@ public class EnvDevice implements Serializable {
      */
     private Integer deviceStatus;
 
+    private Double ratio;
+
     private Integer farmId;
 
 

+ 2 - 0
input/src/main/java/com/huimv/input/entity/RawData.java

@@ -36,6 +36,8 @@ public class RawData implements Serializable {
 
     private String data;
 
+    private String originData;
+
     private Date createTime;
     private BigDecimal lastWeight;
     private String battery;

+ 38 - 3
input/src/main/java/com/huimv/input/server/EnvInputServerHandler.java

@@ -22,6 +22,7 @@ import org.springframework.transaction.annotation.Transactional;
 import javax.lang.model.element.VariableElement;
 import java.io.IOException;
 import java.math.BigDecimal;
+import java.math.BigInteger;
 import java.nio.channels.SocketChannel;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
@@ -88,7 +89,7 @@ public class EnvInputServerHandler extends ChannelInboundHandlerAdapter   {
 
         String str =
 
-                "55 86 38 00 4A 00 01 52 00 78 BC 56 B9 56 BC 56 BC 56 BA 56 BB 56 BB 56 BB 56 BA 56 BC 56 BA 56 BB 56 B9 56 BA 56 BC 56 BE 56 BB 56 BB 56 BB 56 BA 56 BA 56 BB 56 B9 56 B7 56 B8 56 B7 56 CC 56 CB 56 C9 56 CB 56 CB 56 CC 56 CB 56 CB 56 CB 56 CB 56 CA 56 CA 56 C9 56 CA 56 CB 56 CB 56 C9 56 C9 56 C8 56 CB 56 C9 56 C9 56 C9 56 C8 56 C8 56 CA 56 C9 56 C9 56 C8 56 C9 56 C9 56 C9 56 CB 56 CA 56 17 0C 07 15 0F 03 AA ";
+                "55 86 56 00 49 00 01 50 00 78 f1 88 f7 88 f8 88 f6 88 f2 88 f2 88 ef 88 04 89 18 89 64 8d 19 8d 15 87 12 87 fc 88 fb 84 31 85 29 85 2c 85 2b 85 25 85 23 85 1d 85 35 85 3d 85 38 85 33 85 30 85 2c 85 28 85 29 85 2e 85 31 85 34 85 34 85 36 85 38 85 3a 85 3f 85 44 85 42 85 44 85 48 85 4a 85 48 85 44 85 3e 85 44 85 4a 85 48 85 30 86 3c 8a 96 90 88 82 03 82 fb 81 e9 81 03 82 5d 83 ea 81 e4 81 18 01 1C 0B 11 02 AA ";
         String[] s = str.split(" ");
         String  dateTimeString = "2023-12-07 23:22:54";
         // 创建 DateTimeFormatter 对象,指定日期时间格式
@@ -110,7 +111,30 @@ public class EnvInputServerHandler extends ChannelInboundHandlerAdapter   {
             String str1 = s[i * 2 + 11] + s[i * 2 + 10];
             BigDecimal weight = new BigDecimal(Integer.parseInt(str1, 16));
             System.out.println("" +  sdf.format(thenTime)+"  重量:" +weight  );
+//            Integer multiply = weight.multiply(BigDecimal.valueOf(0.8)).intValue();
+//            System.out.println("计算后重量:"+ multiply);
+//            // 将 BigInteger 转换为 16 进制 String
+//            String s1 = Integer.toHexString(multiply);
+//            s[i * 2 + 11] = s1.substring(0,2);
+//            s[i * 2 + 10] = s1.substring(s1.length() - 2, s1.length());
         }
+//        String result = String.join(" ", s);
+//        System.out.println(result);
+    }
+
+    public String replace(String str, double ratio){
+        String[] s = str.split(" ");
+        for (int i = 0; i < 60; i++) {
+            String str1 = s[i * 2 + 11] + s[i * 2 + 10];
+            BigDecimal weight = new BigDecimal(Integer.parseInt(str1, 16));
+            Integer multiply = weight.multiply(BigDecimal.valueOf(ratio)).intValue();
+            // 将 BigInteger 转换为 16 进制 String
+            String s1 = Integer.toHexString(multiply);
+            s[i * 2 + 11] = s1.substring(0,2);
+            s[i * 2 + 10] = s1.substring(s1.length() - 2, s1.length());
+        }
+        String result = String.join(" ", s);
+        return result;
     }
 
 
@@ -237,6 +261,7 @@ public class EnvInputServerHandler extends ChannelInboundHandlerAdapter   {
         int decId = Integer.parseInt(hexId, 16);
         System.out.println("十进制id"+decId);
         long l = System.currentTimeMillis();
+        double ratio = 1;
         BigDecimal bigDecimal10 = new BigDecimal(10);
         String bat = s[8] + s[7] ;
         String batStr = new BigDecimal(Integer.parseInt(bat, 16)).divide(bigDecimal10, 1, BigDecimal.ROUND_HALF_UP).toString();
@@ -254,10 +279,21 @@ public class EnvInputServerHandler extends ChannelInboundHandlerAdapter   {
             lastWeight = lastRawData.getLastWeight();
         }
         int sLength = s.length;
+
+        //获取设备
+        EnvDevice envDevice = envDeviceMapper.selectOne(new QueryWrapper<EnvDevice>().eq("device_code", decId).eq("device_type", 3));
+        if (ObjectUtil.isNotEmpty(envDevice)){
+            if (ObjectUtil.isNotEmpty(envDevice.getRatio())){
+                ratio = envDevice.getRatio();
+            }
+        }
+
         //保存原始数据
         RawData rawData = new RawData();
         rawData.setDeviceCode(decId+"");
-        rawData.setData(str);
+        rawData.setOriginData(str);
+        String replace = replace(str, ratio);
+        rawData.setData(replace);
         rawData.setCreateTime(new Date(l));
         rawData.setLastWeight(new BigDecimal(Integer.parseInt(s[sLength -8]+s[sLength-9], 16)));
         rawData.setBattery(batStr);
@@ -281,7 +317,6 @@ public class EnvInputServerHandler extends ChannelInboundHandlerAdapter   {
         }
 
         //获取设备
-        EnvDevice envDevice = envDeviceMapper.selectOne(new QueryWrapper<EnvDevice>().eq("device_code", decId).eq("device_type", 3));
         if (ObjectUtil.isEmpty(envDevice)){
             System.out.println("固定电子称不存在,数据抛弃");
             return;