|
@@ -5,66 +5,141 @@ import com.gzlh.bus.EventConfig;
|
|
|
import com.gzlh.config.dto.SerialSetting;
|
|
|
import com.gzlh.device.plc.action.PLCAction;
|
|
|
import com.gzlh.device.plc.client.PlcNettyConfig;
|
|
|
+import com.gzlh.device.plc.service.PLCService;
|
|
|
+import com.gzlh.utils.DeviceCache;
|
|
|
import com.gzlh.utils.ModbusUtils;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.util.Date;
|
|
|
|
|
|
@Service
|
|
|
@Slf4j
|
|
|
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";
|
|
|
+ private final String COMMAND_PREFIX = "010500";
|
|
|
+ private final String COMMAND_ON = "FF00";
|
|
|
+ private final String COMMAND_OFF = "0000";
|
|
|
|
|
|
- public void handlerAction(String action){
|
|
|
- SerialSetting serialSetting= EventConfig.serialSetting;
|
|
|
- if (StrUtil.equals(action, PLCAction.RED_LIGHT_ON)){
|
|
|
+ private PLCService plcService = new PLCService();
|
|
|
+
|
|
|
+ public void handlerAction(String action) {
|
|
|
+ SerialSetting serialSetting = EventConfig.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_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);
|
|
|
+ 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)){
|
|
|
+ } 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);
|
|
|
+ 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)){
|
|
|
+ } 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);
|
|
|
+ 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)){
|
|
|
+ } 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);
|
|
|
+ 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);
|
|
|
- }else if (StrUtil.equals(action, PLCAction.RAILING_RISE_OFF)){
|
|
|
+ } 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);
|
|
|
+ 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);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public void handlerMsg(String msg) throws Exception{
|
|
|
+
|
|
|
+ DeviceCache.put("plc",1);
|
|
|
+// 不是返回的plc状态信息就中断
|
|
|
+ if (!msg.substring(0,4).equals("0102")){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ String result = Integer.toBinaryString(Integer.parseInt(msg.substring(6, 8),16));
|
|
|
+
|
|
|
+ String oldPlcInfo = DeviceCache.getPlcStatus();
|
|
|
+// 将plc的设备信息补到8存入缓存
|
|
|
+ String newPlcInfo = String.format("%8s", result).replace(" ", "0");
|
|
|
+ DeviceCache.setPlcStatus(newPlcInfo);
|
|
|
+
|
|
|
+// 设备信息发生变化时进入
|
|
|
+ if (oldPlcInfo!=null&&!newPlcInfo.equals(oldPlcInfo)){
|
|
|
+ SerialSetting.PlcDTO.StatusDTO status = EventConfig.serialSetting.getPlc().getStatus();
|
|
|
+
|
|
|
+ String newReverse = new StringBuffer(newPlcInfo).reverse().toString();
|
|
|
+ String oldReverse = new StringBuffer(oldPlcInfo).reverse().toString();
|
|
|
+
|
|
|
+ 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());
|
|
|
+ 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();
|
|
|
+ }
|
|
|
+ }).start();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|