|
@@ -1,11 +1,25 @@
|
|
|
package com.gzlh.device.plc.handler;
|
|
|
|
|
|
+import cn.hutool.core.util.RandomUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
+import cn.hutool.extra.spring.SpringUtil;
|
|
|
+import com.gzlh.bus.EventBus;
|
|
|
+import com.gzlh.bus.EventDataManager;
|
|
|
+import com.gzlh.bus.EventThread;
|
|
|
import com.gzlh.bus.SysConfig;
|
|
|
+import com.gzlh.config.ModuleEnum;
|
|
|
import com.gzlh.config.dto.SerialSetting;
|
|
|
+import com.gzlh.config.task.TaskService;
|
|
|
+import com.gzlh.device.led.client.LedNettyConfig;
|
|
|
+import com.gzlh.device.led.factory.LedFactory;
|
|
|
+import com.gzlh.device.led.utils.LedOptions;
|
|
|
import com.gzlh.device.plc.action.PLCAction;
|
|
|
import com.gzlh.device.plc.client.PlcNettyConfig;
|
|
|
+import com.gzlh.device.plc.event.PLCEvent;
|
|
|
import com.gzlh.device.plc.service.PLCService;
|
|
|
+import com.gzlh.device.weighbridge.event.WeighbridgeEvent;
|
|
|
+import com.gzlh.device.weighbridge.handler.impl.CommonWeighbridgeHandler;
|
|
|
+import com.gzlh.entity.ReqBO;
|
|
|
import com.gzlh.utils.DeviceCache;
|
|
|
import com.gzlh.utils.ModbusUtils;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
@@ -20,124 +34,184 @@ import java.util.Date;
|
|
|
public class PLCHadnler {
|
|
|
@Resource
|
|
|
private PlcNettyConfig plcNettyConfig;
|
|
|
- private final String COMMAND_PREFIX = "010500";
|
|
|
- private final String COMMAND_ON = "FF00";
|
|
|
- private final String COMMAND_OFF = "0000";
|
|
|
+ @Resource
|
|
|
+ private LedFactory ledFactory;
|
|
|
+ @Resource
|
|
|
+ private TaskService taskService;
|
|
|
+ public static final String COMMAND_PREFIX = "010500";
|
|
|
+ public static final String COMMAND_ON = "FF00";
|
|
|
+ public static final String COMMAND_OFF = "0000";
|
|
|
|
|
|
- private PLCService plcService = new PLCService();
|
|
|
|
|
|
public void handlerAction(String action) {
|
|
|
+ log.info("plc handler action:{}", action);
|
|
|
SerialSetting serialSetting = SysConfig.serialSetting;
|
|
|
if (StrUtil.equals(action, PLCAction.RED_LIGHT_ON)) {
|
|
|
|
|
|
String redPoint = serialSetting.getPlc().getOut().getSignalRedPoint();
|
|
|
String command = COMMAND_PREFIX + redPoint + COMMAND_ON;
|
|
|
- log.info("plc发送消息:{}", command);
|
|
|
String packData = ModbusUtils.buildRequestPacket(command);
|
|
|
+
|
|
|
plcNettyConfig.send(packData);
|
|
|
} else if (StrUtil.equals(action, PLCAction.RED_LIGHT_OFF)) {
|
|
|
|
|
|
String redPoint = serialSetting.getPlc().getOut().getSignalRedPoint();
|
|
|
String command = COMMAND_PREFIX + redPoint + COMMAND_OFF;
|
|
|
- log.info("plc发送消息:{}", command);
|
|
|
+
|
|
|
String packData = ModbusUtils.buildRequestPacket(command);
|
|
|
plcNettyConfig.send(packData);
|
|
|
} else if (StrUtil.equals(action, PLCAction.GREEN_LIGHT_ON)) {
|
|
|
|
|
|
String greenPoint = serialSetting.getPlc().getOut().getSignalGreenPoint();
|
|
|
String command = COMMAND_PREFIX + greenPoint + COMMAND_ON;
|
|
|
- log.info("plc发送消息:{}", command);
|
|
|
+
|
|
|
String packData = ModbusUtils.buildRequestPacket(command);
|
|
|
plcNettyConfig.send(packData);
|
|
|
} else if (StrUtil.equals(action, PLCAction.GREEN_LIGHT_OFF)) {
|
|
|
|
|
|
String greenPoint = serialSetting.getPlc().getOut().getSignalGreenPoint();
|
|
|
String command = COMMAND_PREFIX + greenPoint + COMMAND_OFF;
|
|
|
- log.info("plc发送消息:{}", command);
|
|
|
+
|
|
|
String packData = ModbusUtils.buildRequestPacket(command);
|
|
|
plcNettyConfig.send(packData);
|
|
|
} else if (StrUtil.equals(action, PLCAction.RAILING_RISE_ON)) {
|
|
|
|
|
|
String gatePoint = serialSetting.getPlc().getOut().getGatePoint();
|
|
|
String command = COMMAND_PREFIX + gatePoint + COMMAND_ON;
|
|
|
- log.info("plc发送消息:{}", command);
|
|
|
+
|
|
|
String packData = ModbusUtils.buildRequestPacket(command);
|
|
|
plcNettyConfig.send(packData);
|
|
|
+ taskService.addTask(new CheckUpTask(RandomUtil.randomNumbers(10),200,1));
|
|
|
} else if (StrUtil.equals(action, PLCAction.RAILING_RISE_OFF)) {
|
|
|
|
|
|
String gatePoint = serialSetting.getPlc().getOut().getGatePoint();
|
|
|
String command = COMMAND_PREFIX + gatePoint + COMMAND_OFF;
|
|
|
- log.info("plc发送消息:{}", command);
|
|
|
+
|
|
|
String packData = ModbusUtils.buildRequestPacket(command);
|
|
|
plcNettyConfig.send(packData);
|
|
|
+ taskService.addTask(new CheckDownTask(RandomUtil.randomNumbers(10),100,1));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public void handlerMsg(String msg) throws Exception{
|
|
|
+ public void handlerMsg(String msg) throws Exception {
|
|
|
+
|
|
|
|
|
|
- if (!msg.substring(0,4).equals("0102")){
|
|
|
+ if (!msg.startsWith("0102")) {
|
|
|
return;
|
|
|
}
|
|
|
- String result = Integer.toBinaryString(Integer.parseInt(msg.substring(6, 8),16));
|
|
|
+ String result = Integer.toBinaryString(Integer.parseInt(msg.substring(6, 8), 16));
|
|
|
|
|
|
String oldPlcInfo = DeviceCache.getPlcStatus();
|
|
|
+
|
|
|
|
|
|
String newPlcInfo = String.format("%8s", result).replace(" ", "0");
|
|
|
- DeviceCache.setPlcStatus(newPlcInfo);
|
|
|
+
|
|
|
+ String newReverse = new StringBuffer(newPlcInfo).reverse().toString();
|
|
|
+
|
|
|
+ doHandler(oldPlcInfo, newReverse);
|
|
|
+ }
|
|
|
|
|
|
-
|
|
|
- if (oldPlcInfo!=null&&!newPlcInfo.equals(oldPlcInfo)){
|
|
|
- SerialSetting.PlcDTO.StatusDTO status = SysConfig.serialSetting.getPlc().getStatus();
|
|
|
+ private void doHandler(String oldPlcInfo, String newReverse) {
|
|
|
+ if (StrUtil.isEmpty(oldPlcInfo)) {
|
|
|
+ DeviceCache.setPlcStatus(newReverse);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (!StrUtil.equals(newReverse, oldPlcInfo)) {
|
|
|
+ DeviceCache.setPlcStatus(newReverse);
|
|
|
|
|
|
- String newReverse = new StringBuffer(newPlcInfo).reverse().toString();
|
|
|
- String oldReverse = new StringBuffer(oldPlcInfo).reverse().toString();
|
|
|
+ SerialSetting.PlcDTO.StatusDTO status = SysConfig.serialSetting.getPlc().getStatus();
|
|
|
+ SerialSetting.LedDTO ledDTO = SysConfig.serialSetting.getLed();
|
|
|
+ int len = newReverse.length();
|
|
|
+ EventBus eventBus = SpringUtil.getBean(EventBus.class);
|
|
|
|
|
|
- for (int i=0;i<newReverse.length();i++){
|
|
|
-
|
|
|
- String value = String.valueOf(newReverse.charAt(i));
|
|
|
-
|
|
|
- if (value.equals("1")&&!value.equals(String.valueOf(oldReverse.charAt(i)))){
|
|
|
-
|
|
|
- for (Field field:status.getClass().getDeclaredFields()){
|
|
|
- field.setAccessible(true);
|
|
|
- if (Integer.parseInt(String.valueOf(field.get(status)))==i){
|
|
|
-
|
|
|
- DeviceCache.putTime((String) field.get(status),new Date());
|
|
|
+ for (int i = 0; i < len; i++) {
|
|
|
+
|
|
|
+ String newPointStatus = newReverse.charAt(i) + "";
|
|
|
+ String oldPointStatus = oldPlcInfo.charAt(i) + "";
|
|
|
+ String judgePoint = i + "";
|
|
|
+ String radarPoint = status.getRadarPoint();
|
|
|
+ String fRedPoint = status.getFRedPoint();
|
|
|
+ String bRedPoint = status.getBRedPoint();
|
|
|
+ String upPoint = status.getUpPoint();
|
|
|
+ String downPoint = status.getDownPoint();
|
|
|
+
|
|
|
+ if (!newPointStatus.equals(oldPointStatus)) {
|
|
|
+
|
|
|
+
|
|
|
+ if (radarPoint.equals(judgePoint)) {
|
|
|
+
|
|
|
+ if (newPointStatus.equals("1")) {
|
|
|
+
|
|
|
+ DeviceCache.putTime(judgePoint, new Date());
|
|
|
break;
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
- if (String.valueOf(i).equals(status.getFRedPoint())){
|
|
|
- new Thread(()-> {
|
|
|
- try {
|
|
|
- plcService.fRedTrigger(newPlcInfo);
|
|
|
- } catch (InterruptedException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }).start();
|
|
|
- }
|
|
|
-
|
|
|
- if (String.valueOf(i).equals(status.getRadarPoint())){
|
|
|
- new Thread(()-> {
|
|
|
- try {
|
|
|
- plcService.radarTrigger();
|
|
|
- } catch (InterruptedException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }).start();
|
|
|
- }
|
|
|
-
|
|
|
- if (String.valueOf(i).equals(status.getUpPoint())){
|
|
|
- new Thread(()-> {
|
|
|
- try {
|
|
|
- plcService.upTrigger();
|
|
|
- } catch (InterruptedException e) {
|
|
|
- e.printStackTrace();
|
|
|
+
|
|
|
+ if (((newReverse.charAt(Integer.parseInt(fRedPoint)) + "").equals("0"))
|
|
|
+ && !CommonWeighbridgeHandler.hsCar) {
|
|
|
+ log.info("----通道初始化-----");
|
|
|
+ eventBus.startEvent(ModuleEnum.PLC_MODULE.getModuleEn() + "." + PLCEvent.IDLE);
|
|
|
+ }
|
|
|
+ } else if (fRedPoint.equals(judgePoint)) {
|
|
|
+
|
|
|
+ if (newPointStatus.equals("0")) {
|
|
|
+ String radarPointStatus = newReverse.charAt(Integer.parseInt(radarPoint)) + "";
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ if (CommonWeighbridgeHandler.hsCar
|
|
|
+ && radarPointStatus.equals("0")) {
|
|
|
+ log.info("read----------");
|
|
|
+ eventBus.startEvent(ModuleEnum.WEIGHBRIDGE_MODULE.getModuleEn() + "." + WeighbridgeEvent.READ);
|
|
|
}
|
|
|
- }).start();
|
|
|
+ }
|
|
|
+ } else if (bRedPoint.equals(judgePoint)) {
|
|
|
+
|
|
|
+ String downStatus = newReverse.charAt(Integer.parseInt(downPoint)) + "";
|
|
|
+
|
|
|
+ if (newPointStatus.equals("1") && (downStatus.equals("1")) && CommonWeighbridgeHandler.hsCar) {
|
|
|
+ DeviceCache.setInterrupt(true);
|
|
|
+ LedOptions ledOptions = new LedOptions();
|
|
|
+ ledOptions.setLine("04").setColor("01");
|
|
|
+ String msg = "请后退";
|
|
|
+ ledFactory.handler(ledDTO.getBrand()).sendMsg(msg, ledOptions);
|
|
|
+
|
|
|
+ DeviceCache.putTime(judgePoint, new Date());
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ ReqBO reqBO = EventDataManager.getCacheData();
|
|
|
+
|
|
|
+ if (newPointStatus.equals("0")
|
|
|
+ && (downStatus.equals("1")) && reqBO != null && StrUtil.isNotEmpty(reqBO.getCarNo())) {
|
|
|
+ DeviceCache.setInterrupt(false);
|
|
|
+ eventBus.startEvent(ModuleEnum.WEIGHBRIDGE_MODULE.getModuleEn() + "." + WeighbridgeEvent.READ);
|
|
|
+ }
|
|
|
+ } else if (downPoint.equals(judgePoint)) {
|
|
|
+
|
|
|
+ String downStatus = newReverse.charAt(Integer.parseInt(downPoint)) + "";
|
|
|
+ if (downStatus.equals("1") && !CommonWeighbridgeHandler.hsCar) {
|
|
|
+ eventBus.startEvent(ModuleEnum.PLC_MODULE.getModuleEn() + "." + PLCEvent.IDLE);
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
+ DeviceCache.putTime(judgePoint, new Date());
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ String str = "10";
|
|
|
+ String result = Integer.toBinaryString(Integer.parseInt(str, 16));
|
|
|
+ System.out.println(result);
|
|
|
+
|
|
|
+ String newPlcInfo = String.format("%8s", result).replace(" ", "0");
|
|
|
+ System.out.println(newPlcInfo);
|
|
|
+ String newReverse = new StringBuffer(newPlcInfo).reverse().toString();
|
|
|
+ System.out.println(newReverse);
|
|
|
+ }
|
|
|
}
|