Browse Source

修改获取地磅配置、加解密

985653680@qq.com 11 months ago
parent
commit
42e8865c65

+ 8 - 4
src/main/java/com/gzlh/background/client/BackgroundClientHandler.java

@@ -46,15 +46,16 @@ public class BackgroundClientHandler extends SimpleChannelInboundHandler<String>
     protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
 //        System.out.println("Client received: " + msg);
         if (!"back message".equalsIgnoreCase(msg)){
-            log.info(msg);
+            log.info("后端 客户端收到的信息:{}",msg);
             CommandBO commandBO = JSONUtil.toBean(msg, CommandBO.class);
 //            log.info(commandBO.toString());
             CommandBO.Command command = commandBO.getCommand();
-            eventBus.startEvent(command.getModule()+"."+command.getCommand());
             if (!StringUtils.isEmpty(command.getExtra())){
                 LedOptions options = new LedOptions().setLine("04").setColor("03").setShowType("00");
                 SystemObject.ledFactory.handler(SysConfig.serialSetting.getLed().getBrand())
                             .sendMsg(command.getExtra(),options);
+            }else{
+                eventBus.startEvent(command.getModule()+"."+command.getCommand());
             }
         }
     }
@@ -68,13 +69,16 @@ public class BackgroundClientHandler extends SimpleChannelInboundHandler<String>
             Map<String,Object> captrue = new HashMap<String,Object>();
             int userId = HKCacheManager.getUserId(ModuleEnum.CAPTURE_MODULE);
             boolean status = hkUtils.getStatus(hcNetSDK, userId);
-
             int loginV40=0;
             if (!status){
                 short port= (short) caputreSetting.getPort();
                 String ip=caputreSetting.getHost();
                 loginV40 = hkUtils.Login_V40(ip, port, caputreSetting.getUsername(), caputreSetting.getPwd(), hcNetSDK);
-            }
+                if (loginV40!=-1){
+                    hkUtils.SetAlarm(hcNetSDK,loginV40);
+                    HKCacheManager.add(ModuleEnum.CAPTURE_MODULE,loginV40);
+                }
+             }
             DeviceCache.changeDeviceStatus(ModuleEnum.CAPTURE_MODULE.getModuleEn(), loginV40!=-1?1:0);
         }
 

+ 16 - 12
src/main/java/com/gzlh/bus/EventThread.java

@@ -19,12 +19,15 @@ import com.gzlh.device.led.factory.LedFactory;
 import com.gzlh.device.led.utils.LedOptions;
 import com.gzlh.device.plc.event.PLCEvent;
 import com.gzlh.device.plc.handler.PLCHadnler;
-import com.gzlh.device.weighbridge.config.WeighbridgePropertiesConfig;
 import com.gzlh.device.weighbridge.factory.WeighbridgeFactory;
 import com.gzlh.entity.CommandBO;
+import com.gzlh.entity.ReqBO;
 import com.gzlh.utils.DeviceCache;
+import com.gzlh.utils.WordHandlerUtils;
 import lombok.extern.slf4j.Slf4j;
 
+import java.util.ArrayList;
+import java.util.Date;
 import java.util.List;
 
 @Slf4j
@@ -45,7 +48,6 @@ public class EventThread implements Runnable{
         LedFactory ledFactory= SystemObject.ledFactory;
         WeighbridgeFactory weighbridgeFactory=SystemObject.weighbridgeFactory;
         ElectronFactory electronFactory= SystemObject.electronFactory;
-        WeighbridgePropertiesConfig weighbridgePropertiesConfig=SystemObject.weighbridgePropertiesConfig;
         PLCHadnler plcHadnler= SpringUtil.getBean(PLCHadnler.class);
         CaptureFactory captureFactory=SystemObject.captureFactory;
         CaputreSetting caputreSetting=SysConfig.caputreSetting;
@@ -69,7 +71,7 @@ public class EventThread implements Runnable{
                     ledFactory.handler(serialSetting.getLed().getBrand()).handlerAction(command);
                 } else if (StrUtil.equals(module, ModuleEnum.WEIGHBRIDGE_MODULE.getModuleEn())) {
                     //动作属于地磅
-                    weighbridgeFactory.handler(weighbridgePropertiesConfig.getBrand()).handlerAction(command);
+                    weighbridgeFactory.handler(serialSetting.getWeighbridge().getBrand()).handlerAction(command);
                 }else if (StrUtil.equals(module, ModuleEnum.CAPTURE_MODULE.getModuleEn())) {
                     //动作属于抓拍单元--车牌识别
                     captureFactory.handler(caputreSetting.getBrand()).handlerAction(command);
@@ -125,14 +127,16 @@ public class EventThread implements Runnable{
         String api="/open/submit";
         HttpResponse response = request(address+api, JSONUtil.toJsonStr(EventDataManager.getCacheData()));
         EventBus eventBus= SpringUtil.getBean(EventBus.class);
-        log.info("提交通道数据-返回结果 code:{},body:{}",response.getStatus(),response.body());
-        if (response.getStatus() == 200 || response.getStatus() == 500){
-            JSONObject jsonObject = new JSONObject(response.body());
-            String data = jsonObject.get("data").toString();
-            log.info("提交通道数据-返回结果:{}",data);
+        log.info("提交通道数据-返回结果:{}",response.body());
+        if (response.getStatus() == 200){
+            String body = response.body();
+            JSONObject jsonObject = new JSONObject(body);
+//            返回数据解密
+            String data = WordHandlerUtils.AESDecrypt(jsonObject.get("data").toString());
+//            String data = jsonObject.get("data").toString();
             CommandBO commandBO = JSONUtil.toBean(data, CommandBO.class);
             LedOptions options = new LedOptions().setLine("04").setShowType("00");
-            if (response.getStatus()==200){
+            if ("200".equals(jsonObject.get("code"))){
                 options.setColor("02");
                 if (ModuleEnum.LED_MODULE.getModuleEn().equalsIgnoreCase(commandBO.getCommand().getModule())){
                     SystemObject.ledFactory.handler(SysConfig.serialSetting.getLed().getBrand())
@@ -165,8 +169,8 @@ public class EventThread implements Runnable{
 //        url = "http://192.168.1.8:9191/open/submit";
         long startTime = System.currentTimeMillis();
 //        System.out.println(startTime);
-
-        String body = json;
+//        数据加密
+        String body = WordHandlerUtils.AESEncrypt(json);
         try {
 //            System.out.printf("请求地址:%s, 请求体:%s ", url,body);
 
@@ -174,7 +178,7 @@ public class EventThread implements Runnable{
                     .header("Content-Type", "application/json;character=utf-8;")
                     .body(body)
                     .execute();
-            String bodyResult = res.body();
+//            String bodyResult = res.body();
 //            System.out.printf("ReqStatus:%s,返回结果:{%s} ",res.getStatus(), bodyResult);
 
         } catch (Exception e) {

+ 1 - 0
src/main/java/com/gzlh/config/ApplicationConfig.java

@@ -11,5 +11,6 @@ public class ApplicationConfig {
   private String serverUrl;
   private String channelName;
   private String channelCode;
+//  0 货运通道 1 行政通道
   private int channelType;
 }

+ 11 - 11
src/main/java/com/gzlh/config/SystemObject.java

@@ -2,9 +2,7 @@ package com.gzlh.config;
 
 import com.gzlh.device.capture.factory.CaptureFactory;
 import com.gzlh.device.electron.factory.ElectronFactory;
-
 import com.gzlh.device.led.factory.LedFactory;
-import com.gzlh.device.weighbridge.config.WeighbridgePropertiesConfig;
 import com.gzlh.device.weighbridge.factory.WeighbridgeFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
@@ -18,14 +16,15 @@ public class SystemObject {
 
 
     public static WeighbridgeFactory weighbridgeFactory;
-    public static WeighbridgePropertiesConfig weighbridgePropertiesConfig;
 
     public static LedFactory ledFactory;
 
     public static CaptureFactory captureFactory;
+
     public static ElectronFactory electronFactory;
+
     @Autowired
-      void setElectronFactory(ElectronFactory electronFactory) {
+    void setElectronFactory(ElectronFactory electronFactory) {
         SystemObject.electronFactory = electronFactory;
     }
 
@@ -33,23 +32,24 @@ public class SystemObject {
     void setApplicationConfig(ApplicationConfig applicationConfig) {
         SystemObject.applicationConfig = applicationConfig;
     }
+
     @Autowired
-   void setFilePropertiesConfig(FilePropertiesConfig filePropertiesConfig) {
+    void setFilePropertiesConfig(FilePropertiesConfig filePropertiesConfig) {
         SystemObject.filePropertiesConfig = filePropertiesConfig;
     }
 
     @Autowired
-     void setWeighbridgeFactory(WeighbridgeFactory weighbridgeFactory) {
+    void setWeighbridgeFactory(WeighbridgeFactory weighbridgeFactory) {
         SystemObject.weighbridgeFactory = weighbridgeFactory;
     }
-    @Autowired
-     void setWeighbridgePropertiesConfig(WeighbridgePropertiesConfig weighbridgePropertiesConfig) {
-        SystemObject.weighbridgePropertiesConfig = weighbridgePropertiesConfig;
-    }
+
     @Autowired
     void setLedFactory(LedFactory ledFactory) {
         SystemObject.ledFactory = ledFactory;
     }
 
-
+    @Autowired
+    void setCaptureFactory(CaptureFactory captureFactory) {
+        SystemObject.captureFactory = captureFactory;
+    }
 }

+ 3 - 9
src/main/java/com/gzlh/device/camera/controller/CameraController.java

@@ -4,11 +4,8 @@ import cn.hutool.core.bean.BeanUtil;
 import cn.hutool.core.util.XmlUtil;
 import com.gzlh.bus.EventDataManager;
 import com.gzlh.config.SystemObject;
-import com.gzlh.device.camera.factory.CameraFactory;
-import com.gzlh.device.camera.properties.CameraPropertiesConfig;
 import com.gzlh.entity.ReqBO;
 import com.gzlh.utils.FileUtils;
-import org.springframework.beans.factory.annotation.Value;
 import org.springframework.boot.autoconfigure.web.ServerProperties;
 import org.springframework.util.StringUtils;
 import org.springframework.web.bind.annotation.PostMapping;
@@ -28,10 +25,6 @@ import java.util.*;
 @RequestMapping("/camera")
 public class CameraController {
     @Resource
-    private CameraFactory cameraFactory;
-    @Resource
-    private CameraPropertiesConfig cameraPropertiesConfig;
-    @Resource
     private ServerProperties serverProperties;
 
     @PostMapping("/recv")
@@ -55,9 +48,10 @@ public class CameraController {
         Map<String, Object> seal = BeanUtil.beanToMap(StringObjectMap.get("SEAL"));
 
 
-        Map<String, String> boxNoMap = new HashMap<>();
-
         ReqBO.Box box = new ReqBO.Box();
+        if (!StringUtils.isEmpty((String) conta.get("CONTA_NUM"))) {
+            box.setBoxNum((String) conta.get("CONTA_NUM"));
+        }
         if (!StringUtils.isEmpty((String) conta.get("CONTA_ID_F"))) {
             box.setBoxF((String) conta.get("CONTA_ID_F"));
         }

+ 0 - 15
src/main/java/com/gzlh/device/camera/properties/CameraPropertiesConfig.java

@@ -1,15 +0,0 @@
-package com.gzlh.device.camera.properties;
-
-import lombok.Data;
-import org.springframework.boot.context.properties.ConfigurationProperties;
-import org.springframework.context.annotation.Configuration;
-
-@ConfigurationProperties(prefix = "led")
-@Configuration
-@Data
-public class CameraPropertiesConfig {
-    private String host;
-    private int port;
-    private int brand;
-    private boolean enable;
-}

+ 1 - 1
src/main/java/com/gzlh/device/led/client/LedNettyConfig.java

@@ -76,7 +76,7 @@ public class LedNettyConfig {
             if (future1.isSuccess()) {
                 channel = future1.channel();
                 log.info("led 串口服务器连接成功,{},{}", host, port);
-                DeviceCache.changeDeviceStatus(ModuleEnum.PLC_MODULE.getModuleEn(),1);
+                DeviceCache.changeDeviceStatus(ModuleEnum.LED_MODULE.getModuleEn(),1);
             } else {
                 log.error("-------------led 连接服务器失败,{},{}-----------,进行重连", host, port);
                 future1.channel().eventLoop().schedule(this::connect, 5, TimeUnit.SECONDS);

+ 0 - 4
src/main/java/com/gzlh/device/plc/service/PLCService.java

@@ -56,9 +56,6 @@ public class PLCService {
         if (newFRed.equals("0") && bRed.equals("0")) {
             Date fRedTime = DeviceCache.getTime(status.getFRedPoint());
             Date radarTime = DeviceCache.getTime(status.getRadarPoint());
-            System.out.println("radarTime"+radarTime);
-            System.out.println("fRedTime"+fRedTime);
-            System.out.println("雷达时间小于前红外时间"+radarTime.before(fRedTime));
 //            雷达时间小于前红外时间为进,车辆前后红外之间
             if (radarTime.before(fRedTime)) {
                 eventBus.startEvent(ModuleEnum.WEIGHBRIDGE_MODULE.getModuleEn() + "." + WeighbridgeEvent.READ);
@@ -138,7 +135,6 @@ public class PLCService {
         StringBuilder sb = new StringBuilder(downOnly);
         sb.setCharAt(Integer.parseInt(status.getDownPoint()), '1');
         downOnly = sb.toString();
-//        System.out.println(downOnly);
 //        判断当前PLC状态
         if (downOnly.equals(newStatus)) {
             EventDataManager.cleanData();

+ 4 - 3
src/main/java/com/gzlh/device/weighbridge/client/WeighbridgeClientHandler.java

@@ -1,8 +1,9 @@
 package com.gzlh.device.weighbridge.client;
 
 import cn.hutool.extra.spring.SpringUtil;
+import com.gzlh.bus.SysConfig;
 import com.gzlh.config.ModuleEnum;
-import com.gzlh.device.weighbridge.config.WeighbridgePropertiesConfig;
+import com.gzlh.config.dto.SerialSetting;
 import com.gzlh.device.weighbridge.factory.WeighbridgeFactory;
 import com.gzlh.utils.DeviceCache;
 import io.netty.channel.ChannelHandler;
@@ -22,10 +23,10 @@ public class WeighbridgeClientHandler extends SimpleChannelInboundHandler<String
 
     @Override
     protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
+        SerialSetting.Weighbridge weighbridge = SysConfig.serialSetting.getWeighbridge();
         DeviceCache.changeDeviceStatus(ModuleEnum.WEIGHBRIDGE_MODULE.getModuleEn(),1);
-        WeighbridgePropertiesConfig weighbridgePropertiesConfig = SpringUtil.getBean(WeighbridgePropertiesConfig.class);
         WeighbridgeFactory weighbridgeFactory = SpringUtil.getBean(WeighbridgeFactory.class);
-        weighbridgeFactory.handler(weighbridgePropertiesConfig.getBrand()).handlerMsg(msg);
+        weighbridgeFactory.handler(weighbridge.getBrand()).handlerMsg(msg);
     }
 
     @Override

+ 0 - 20
src/main/java/com/gzlh/device/weighbridge/config/WeighbridgePropertiesConfig.java

@@ -1,20 +0,0 @@
-package com.gzlh.device.weighbridge.config;
-
-import lombok.Data;
-import org.springframework.boot.context.properties.ConfigurationProperties;
-import org.springframework.context.annotation.Configuration;
-
-@ConfigurationProperties(prefix = "weighbridge")
-@Data
-@Configuration
-public class WeighbridgePropertiesConfig {
-    private String host;
-    private int port;
-    /**
-     * 品牌
-     */
-    private int brand;
-    private int minKg;
-
-    private boolean enable;
-}

+ 6 - 6
src/main/java/com/gzlh/device/weighbridge/handler/impl/CommonWeighbridgeHandler.java

@@ -10,7 +10,6 @@ import com.gzlh.bus.EventBus;
 import com.gzlh.config.SystemObject;
 import com.gzlh.config.dto.SerialSetting;
 import com.gzlh.device.led.utils.LedOptions;
-import com.gzlh.device.weighbridge.config.WeighbridgePropertiesConfig;
 import com.gzlh.device.weighbridge.event.WeighbridgeEvent;
 import com.gzlh.utils.DeviceCache;
 
@@ -59,7 +58,8 @@ public class CommonWeighbridgeHandler {
         public void run() {
             int averaWeight = 0;
             String deviceStatus= null;
-            SerialSetting.PlcDTO.StatusDTO status = SysConfig.serialSetting.getPlc().getStatus();
+            SerialSetting serialSetting = SysConfig.serialSetting;
+            SerialSetting.PlcDTO.StatusDTO status = serialSetting.getPlc().getStatus();
             EventBus eventBus = SpringUtil.getBean(EventBus.class);
             while (time > 0) {
                 time = time - 1000;
@@ -69,7 +69,6 @@ public class CommonWeighbridgeHandler {
                 String fRed = String.valueOf(deviceStatus.charAt(Integer.parseInt(status.getFRedPoint())));
                 String bRed = String.valueOf(deviceStatus.charAt(Integer.parseInt(status.getBRedPoint())));
                 String radar = String.valueOf(deviceStatus.charAt(Integer.parseInt(status.getRadarPoint())));
-                System.out.println(fRed+","+bRed+","+radar);
                 if (fRed.equals("1")||bRed.equals("1")||radar.equals("1")){
                     DeviceCache.setInterrupt(true);
                     LedOptions options = new LedOptions().setLine("03").setColor("04").setShowType("00");
@@ -83,14 +82,15 @@ public class CommonWeighbridgeHandler {
                 }
                 ThreadUtil.sleep(1000);
             }
-            WeighbridgePropertiesConfig weighbridgePropertiesConfig = SpringUtil.getBean(WeighbridgePropertiesConfig.class);
-            if (averaWeight == 0 || weighbridgePropertiesConfig.getMinKg() > averaWeight) {
+            isStart=false;
+            if (averaWeight == 0 || serialSetting.getWeighbridge().getMinKg() > averaWeight) {
                 //todo read timeout
                 //读取超时
+                DeviceCache.setInterrupt(true);
+                ThreadUtil.sleep(500);
                 eventBus.startEvent(ModuleEnum.WEIGHBRIDGE_MODULE.getModuleEn() + "." + WeighbridgeEvent.TIMEOUT);
                 return;
             }
-            isStart=false;
             ApplicationConfig applicationConfig = SpringUtil.getBean(ApplicationConfig.class);
             //上传地磅数据
             String channelName = applicationConfig.getChannelName();

+ 0 - 4
src/main/java/com/gzlh/device/weighbridge/handler/impl/WeighbridgeHandlerKeLi.java

@@ -3,7 +3,6 @@ package com.gzlh.device.weighbridge.handler.impl;
 import cn.hutool.core.util.StrUtil;
 import com.gzlh.device.weighbridge.action.WeighbridgeCommonAction;
 import com.gzlh.device.weighbridge.brand.WeighbridgeBrandType;
-import com.gzlh.device.weighbridge.config.WeighbridgePropertiesConfig;
 import com.gzlh.device.weighbridge.handler.IWeighbridgeHandler;
 
 import lombok.extern.slf4j.Slf4j;
@@ -19,9 +18,6 @@ import java.util.Arrays;
 @Slf4j
 public class WeighbridgeHandlerKeLi implements IWeighbridgeHandler {
 
-    @Resource
-    private WeighbridgePropertiesConfig weighbridgePropertiesConfig;
-
     @Override
     public WeighbridgeBrandType brandType() {
         return WeighbridgeBrandType.NINGBO_KELI;

+ 4 - 0
src/main/java/com/gzlh/entity/ReqBO.java

@@ -72,6 +72,10 @@ public class ReqBO implements Serializable {
          * 后箱型
          */
         private String modelB;
+        /**
+         * 箱数量
+         */
+        private String boxNum;
     }
 
 

+ 57 - 1
src/main/java/com/gzlh/utils/WordHandlerUtils.java

@@ -5,12 +5,22 @@ import cn.hutool.core.util.StrUtil;
 import com.gzlh.device.led.utils.ColorConver;
 import com.gzlh.device.led.utils.YangBandPackage;
 
+import javax.crypto.BadPaddingException;
+import javax.crypto.Cipher;
+import javax.crypto.IllegalBlockSizeException;
+import javax.crypto.NoSuchPaddingException;
+import javax.crypto.spec.SecretKeySpec;
+import javax.xml.bind.DatatypeConverter;
 import java.math.BigDecimal;
 import java.math.RoundingMode;
 import java.nio.charset.Charset;
+import java.security.InvalidKeyException;
+import java.security.NoSuchAlgorithmException;
+import java.util.Arrays;
 
 public class WordHandlerUtils {
 
+    static String key ="SC35fdGrQofSMT5a";
 
     public static String msgToASCII(String msg){
         int len=msg.length();
@@ -34,8 +44,10 @@ public class WordHandlerUtils {
     }
 
 
-    public static void main(String[] args) {
+    public static void main(String[] args) throws Exception {
         System.out.println(msgToASCII("你好ssss"));
+        String ssss = AESEncrypt("你好ssss");
+        AESDecrypt(ssss);
     }
 
     /**
@@ -95,5 +107,49 @@ public class WordHandlerUtils {
         }
         return sb.toString();
     }
+
+    public static String AESEncrypt(String data)  {
+
+        SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "AES");
+
+        Cipher cipher = null; // 选择AES算法、工作模式为ECB、填充方式为PKCS5Padding
+        byte[] encryptedBytes =null;
+        try {
+            cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
+            cipher.init(Cipher.ENCRYPT_MODE, secretKey); // 初始化加密器
+            encryptedBytes = cipher.doFinal(data.getBytes()); // 对明文进行加密操作
+        } catch (NoSuchAlgorithmException | NoSuchPaddingException | BadPaddingException
+                | IllegalBlockSizeException | InvalidKeyException e) {
+            e.printStackTrace();
+        }
+
+//        System.out.println("Encrypted Text: " + DatatypeConverter.printHexBinary(encryptedBytes));
+        return DatatypeConverter.printHexBinary(encryptedBytes);
+    }
+
+    public static String AESDecrypt(String data)  {
+        byte[] result = new byte[data.length()/2];
+        for (int i = 0; i < data.length()/2; i++) {
+            result[i] =Integer.valueOf(data.substring(i * 2, i * 2 + 2), 16).byteValue();
+        }
+
+        SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "AES");
+
+        Cipher cipher = null; // 选择AES算法、工作模式为ECB、填充方式为PKCS5Padding
+        byte[] decryptedBytes = null;
+        String decryptStr =null;
+        try {
+            cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
+            cipher.init(Cipher.DECRYPT_MODE, secretKey); // 初始化解密器
+            decryptedBytes = cipher.doFinal(result); // 对密文进行解密操作
+//            System.out.println("Decrypted Text: " + new String(decryptedBytes));
+            decryptStr=new String(decryptedBytes);
+        } catch (NoSuchAlgorithmException | NoSuchPaddingException | BadPaddingException
+                | IllegalBlockSizeException | InvalidKeyException e) {
+            e.printStackTrace();
+        }
+        return decryptStr;
+    }
+
 }
 

+ 2 - 1
src/main/resources/application.yml

@@ -4,8 +4,9 @@ application:
   server-url: http://192.168.1.3:9191 #后端服务器地址
   channel-name: "A1地磅通道"
   channel-code: "A1001"
+  #0 货运通道 1 行政通道
   channel-type: 0
-
+#后端SOCKET地址
 background:
   host: 192.168.1.3
   port: 8050