|
@@ -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);
|
|
|
+ // log.info("plc发送消息:{}", packData);
|
|
|
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);
|
|
|
+ // 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);
|
|
|
+ // 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);
|
|
|
+ // 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);
|
|
|
+ // 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);
|
|
|
+ // 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 {
|
|
|
+ // log.info("plc:{}", msg);
|
|
|
// 不是返回的plc状态信息就中断
|
|
|
- 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();
|
|
|
+ // log.info("oldPlcInfo status:{}", oldPlcInfo);
|
|
|
// 将plc的设备信息补到8存入缓存
|
|
|
String newPlcInfo = String.format("%8s", result).replace(" ", "0");
|
|
|
- DeviceCache.setPlcStatus(newPlcInfo);
|
|
|
+// log.info("new plc status:{}", newPlcInfo);
|
|
|
+ String newReverse = new StringBuffer(newPlcInfo).reverse().toString();
|
|
|
+// log.info("new plc reverse status:{}", newReverse);
|
|
|
+ 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++){
|
|
|
-// 第i位设备的值
|
|
|
- String value = String.valueOf(newReverse.charAt(i));
|
|
|
-// 设备被触发
|
|
|
- if (value.equals("1")&&!value.equals(String.valueOf(oldReverse.charAt(i)))){
|
|
|
-// 记录时间 配置的设备地址为map的key
|
|
|
- for (Field field:status.getClass().getDeclaredFields()){
|
|
|
- field.setAccessible(true);
|
|
|
- if (Integer.parseInt(String.valueOf(field.get(status)))==i){
|
|
|
-// String name = fields[i].getName();
|
|
|
- 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();//默认是1
|
|
|
+ //状态变化
|
|
|
+ if (!newPointStatus.equals(oldPointStatus)) {
|
|
|
+// log.info("newReverse:{}", newReverse);
|
|
|
+ //看下是哪个状态发生变化
|
|
|
+ if (radarPoint.equals(judgePoint)) {
|
|
|
+ //雷达变化0--->1不管 1--->0,判断是否是退出
|
|
|
+ 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)) {
|
|
|
+ //前红外发生变化 1--->0时,判断是否开始读地磅数;0--->1时不管
|
|
|
+ if (newPointStatus.equals("0")) {
|
|
|
+ String radarPointStatus = newReverse.charAt(Integer.parseInt(radarPoint)) + "";
|
|
|
+ //判断是否跟车
|
|
|
+ // if (radarPointStatus.equals("1")) {
|
|
|
+ // log.info("init----------");
|
|
|
+ // eventBus.startEvent(ModuleEnum.PLC_MODULE.getModuleEn() + "." + PLCEvent.IDLE);
|
|
|
+ // break;
|
|
|
+ // }
|
|
|
+ //开始称重逻辑:存在车牌,并且雷达状态是0
|
|
|
+ if (CommonWeighbridgeHandler.hsCar
|
|
|
+ && radarPointStatus.equals("0")) {
|
|
|
+ log.info("read----------");
|
|
|
+ eventBus.startEvent(ModuleEnum.WEIGHBRIDGE_MODULE.getModuleEn() + "." + WeighbridgeEvent.READ);
|
|
|
}
|
|
|
- }).start();
|
|
|
+ }
|
|
|
+ } else if (bRedPoint.equals(judgePoint)) {
|
|
|
+ //后红外发生变化 0--->1:看下是否是下到位=0时车头碰到红外
|
|
|
+ String downStatus = newReverse.charAt(Integer.parseInt(downPoint)) + "";
|
|
|
+// log.info("new point:{},{}", newPointStatus, downStatus);
|
|
|
+ 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();
|
|
|
+ //后退进行称重 1--->0
|
|
|
+ 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);
|
|
|
+// 将plc的设备信息补到8存入缓存
|
|
|
+ String newPlcInfo = String.format("%8s", result).replace(" ", "0");
|
|
|
+ System.out.println(newPlcInfo);
|
|
|
+ String newReverse = new StringBuffer(newPlcInfo).reverse().toString();
|
|
|
+ System.out.println(newReverse);
|
|
|
+ }
|
|
|
}
|