diff --git a/acs/nladmin-system/src/main/java/org/nl/acs/device_driver/basedriver/casing_station/CasingStationConveyorDeviceDriver.java b/acs/nladmin-system/src/main/java/org/nl/acs/device_driver/basedriver/casing_station/CasingStationConveyorDeviceDriver.java new file mode 100644 index 000000000..48c33ca87 --- /dev/null +++ b/acs/nladmin-system/src/main/java/org/nl/acs/device_driver/basedriver/casing_station/CasingStationConveyorDeviceDriver.java @@ -0,0 +1,408 @@ +package org.nl.acs.device_driver.basedriver.casing_station; + +import cn.hutool.core.date.DateUtil; +import cn.hutool.core.util.IdUtil; +import cn.hutool.core.util.ObjectUtil; +import cn.hutool.core.util.StrUtil; +import com.alibaba.fastjson.JSONObject; +import lombok.Data; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.nl.acs.agv.server.AgvService; +import org.nl.acs.device.service.DeviceService; +import org.nl.acs.device_driver.DeviceDriver; +import org.nl.acs.device_driver.RouteableDeviceDriver; +import org.nl.acs.device_driver.driver.AbstractOpcDeviceDriver; +import org.nl.acs.device_driver.driver.ExecutableDeviceDriver; +import org.nl.acs.ext.wms.data.ApplyPaperActionRequest; +import org.nl.acs.ext.wms.data.ApplyPaperActionResponse; +import org.nl.acs.ext.wms.service.AcsToWmsService; +import org.nl.acs.ext.wms.service.impl.AcsToWmsServiceImpl; +import org.nl.acs.history.ErrorUtil; +import org.nl.acs.history.service.DeviceErrorLogService; +import org.nl.acs.history.service.dto.DeviceErrorLogDto; +import org.nl.acs.history.service.impl.DeviceErrorLogServiceImpl; +import org.nl.acs.instruction.service.InstructionService; +import org.nl.acs.instruction.service.dto.Instruction; +import org.nl.acs.log.service.DeviceExecuteLogService; +import org.nl.acs.monitor.DeviceStageMonitor; +import org.nl.acs.opc.Device; +import org.nl.acs.opc.DeviceAppService; +import org.nl.acs.route.service.RouteLineService; +import org.nl.acs.task.service.TaskService; +import org.nl.acs.task.service.dto.TaskDto; +import org.nl.modules.system.service.ParamService; +import org.nl.modules.wql.util.SpringContextHolder; +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.*; + +/** + * 套管工位 + */ +@Slf4j +@Data +@RequiredArgsConstructor +public class CasingStationConveyorDeviceDriver extends AbstractOpcDeviceDriver implements DeviceDriver, ExecutableDeviceDriver, RouteableDeviceDriver, DeviceStageMonitor { + protected ItemProtocol itemProtocol = new ItemProtocol(this); + @Autowired + DeviceAppService deviceAppservice = SpringContextHolder.getBean(DeviceAppService.class); + @Autowired + InstructionService instructionService = SpringContextHolder.getBean(InstructionService.class); + @Autowired + DeviceService deviceservice = SpringContextHolder.getBean(DeviceService.class); + @Autowired + TaskService taskserver = SpringContextHolder.getBean(TaskService.class); + @Autowired + RouteLineService routeLineService = SpringContextHolder.getBean(RouteLineService.class); + @Autowired + AcsToWmsService acsToWmsService = SpringContextHolder.getBean(AcsToWmsServiceImpl.class); + @Autowired + ParamService paramService = SpringContextHolder.getBean(ParamService.class); + @Autowired + DeviceExecuteLogService logServer = SpringContextHolder.getBean(DeviceExecuteLogService.class); + @Autowired + AgvService agvService = SpringContextHolder.getBean(AgvService.class); + @Autowired + DeviceErrorLogService deviceErrorLogService = SpringContextHolder.getBean(DeviceErrorLogServiceImpl.class); + + private Date instruction_update_time = new Date(); + private Date require_apply_casing_time = new Date(); + private int instruction_update_time_out = 500; + private Date instruction_require_time = new Date(); + private Date instruction_finished_time = new Date(); + private Date instruction_apply_time = new Date(); + private int instruction_require_time_out = 3000; + + //心跳 + int heartbeat = 0; + int last_heartbeat = 0; + //工作模式 + int mode = 0; + int last_mode = 0; + //光电信号 + int move = 0; + int last_move = 0; + //托盘方向 + int carrier_direction = 0; + int last_carrier_direction = 0; + //报警 + int error = 0; + int last_error = 0; + //任务号 + int task = 0; + int last_task = 0; + + String material1 = null; + String last_material1 = null; + + String material2 = null; + String last_material2 = null; + + + //下发命令 + int to_command = 0; + int last_to_command = 0; + + //当前指令 + Instruction inst = null; + + Boolean isonline = true; + + Boolean iserror = false; + //请求成功标记 + Boolean requireSucess = false; + + String message = null; + String device_code; + + + @Override + public Device getDevice() { + return this.device; + } + + @Override + public void execute() { + try { + String message = null; + device_code = this.getDeviceCode(); + heartbeat = this.itemProtocol.getHeartbeat(); + mode = this.itemProtocol.getMode(); + move = this.itemProtocol.getMove(); + carrier_direction = this.itemProtocol.getCarrier_direction(); + error = this.itemProtocol.getError(); + task = this.itemProtocol.getTask(); + material1 = this.itemProtocol.getMaterial1(); + material2 = this.itemProtocol.getMaterial2(); + + to_command = this.itemProtocol.getTo_command(); + + + if (to_command != last_to_command) { + logServer.deviceExecuteLog(this.device_code, "", "", "信号to_command:" + last_to_command + "->" + to_command); + } + if (!material1.equals(last_material1)) { + logServer.deviceExecuteLog(this.device_code, "", "", "信号material1:" + last_material1 + "->" + material1); + } + if (!material2.equals(last_material2)) { + logServer.deviceExecuteLog(this.device_code, "", "", "信号material2:" + last_material2 + "->" + material2); + } + + if (carrier_direction != last_carrier_direction) { + logServer.deviceItemValue(this.device_code, "carrier_direction", String.valueOf(carrier_direction)); + logServer.deviceExecuteLog(this.device_code, "", "", "信号carrier_direction:" + last_carrier_direction + "->" + carrier_direction); + } + if (task != last_task) { + logServer.deviceItemValue(this.device_code, "task", String.valueOf(task)); + logServer.deviceExecuteLog(this.device_code, "", "", "信号task:" + last_task + "->" + task); + } + if (error != last_error) { + if (error != 0) { + DeviceErrorLogDto dto = new DeviceErrorLogDto(); + dto.setDevice_code(device_code); + dto.setError_code(String.valueOf(error)); + String errorInfo = ErrorUtil.getDictDetail("ssx_error_type", String.valueOf(error)); + dto.setError_info(errorInfo); + deviceErrorLogService.create(dto); + } + logServer.deviceItemValue(this.device_code, "error", String.valueOf(error)); + logServer.deviceExecuteLog(this.device_code, "", "", "信号error:" + last_error + "->" + error); + } + if (mode != last_mode) { + JSONObject param = new JSONObject(); + param.put("device_code", this.device_code); + param.put("mode", Math.min(mode, 3)); + param.put("device_name", this.getDevice().getDevice_name()); + param.put("device_type", "1"); + param.put("product_area", paramService.findByCode("productArea").getValue()); + acsToWmsService.sendDeviceStatus(param); + logServer.deviceExecuteLog(this.device_code, "", "", "信号mode:" + last_mode + "->" + mode + "复位请求标记:" + requireSucess); + logServer.deviceExecuteLog(this.device_code, "", "", "信号mode:" + last_mode + "->" + mode); + } + if (move != last_move) { + logServer.deviceItemValue(this.device_code, "move", String.valueOf(move)); + logServer.deviceExecuteLog(this.device_code, "", "", "信号move:" + last_move + "->" + move); + } + + if (move != 0 && task > 1) { + update_instruction_status(); + } + + } catch (Exception var17) { + var17.printStackTrace(); + logServer.deviceExecuteLog(this.device_code, "", "", "读取信号值时出现异常:" + var17.getMessage() + ",this.itemProtocol is null:" + ObjectUtil.isEmpty(this.itemProtocol)); + } + + if (mode == 0) { + this.setIsonline(false); + message = "未联机"; + } else if (error != 0) { + this.setIserror(true); + message = "有报警"; + } else { + this.setIsonline(true); + this.setIserror(false); + message = ""; + Instruction instruction = null; + List toInstructions; + + //纸管库申请任务 + switch (mode) { + case 1: + log.debug("弃用(留作兼容)"); + break; + case 2: + //申请任务 + /*if (move > 0 && !requireSucess) { + instruction_require(); + }*/ + break; + case 3: + log.info("运行中"); + break; + default: + if (move == 1 && !requireSucess) { + //申请套管校验 + apply_for_casing_inspection(material1, material2); + } + break; + } + } + last_heartbeat = heartbeat; + last_mode = mode; + last_move = move; + last_error = error; + last_carrier_direction = carrier_direction; + last_task = task; + last_material1 = material1; + last_material2 = material2; + last_to_command = to_command; + } + + + /** + * 更新指令状态 + */ + public synchronized void update_instruction_status() throws Exception { + Date date = new Date(); + if (date.getTime() - this.instruction_update_time.getTime() < (long) this.instruction_update_time_out) { + log.trace("触发时间因为小于{}毫秒,而被无视", this.instruction_update_time_out); + + } else { + this.instruction_update_time = date; + inst = checkInst(); + if (inst != null) { + if (StrUtil.equals(inst.getInstruction_status(), "0") && StrUtil.equals(this.getDeviceCode(), inst.getStart_device_code())) { + inst.setInstruction_status("1"); + inst.setExecute_device_code(this.device_code); + instructionService.update(inst); + logServer.deviceExecuteLog(device_code, "", "", "入库输送线任务开始反馈执行中状态,反馈成功,指令号:" + task); + } + if (StrUtil.equals(inst.getInstruction_status(), "1") || StrUtil.equals(inst.getInstruction_status(), "0")) { + if (StrUtil.equals(this.getDeviceCode(), inst.getNext_device_code())) { + inst.setExecute_device_code(this.device_code); + finish_instruction(); + logServer.deviceExecuteLog(device_code, "", "", "入库输送线任务开始反馈完成状态,反馈成功,指令号:" + task); + } + } + } + } + } + + /** + * 申请套管校验 + */ + public synchronized void apply_for_casing_inspection(String material1, String material2) { + Date date = new Date(); + if (date.getTime() - this.require_apply_casing_time.getTime() < (long) this.instruction_require_time_out) { + log.trace("触发时间因为小于{}毫秒,而被无视", this.instruction_require_time_out); + } else { + this.require_apply_casing_time = date; + ApplyPaperActionRequest applyPaperActionRequest = new ApplyPaperActionRequest(); + applyPaperActionRequest.setDevice_code(this.device_code); + applyPaperActionRequest.setType("2"); + applyPaperActionRequest.setTask_code(String.valueOf(task)); + //获取出库顺序 + applyPaperActionRequest.setMaterial1(material1); + applyPaperActionRequest.setMaterial2(material2); + ApplyPaperActionResponse applyPaperActionResponse = acsToWmsService.applyPaperActionRequest(applyPaperActionRequest); + Map map = new HashMap(); + if (applyPaperActionResponse.getstatus() == 200) { + map.put("code", "to_command"); + map.put("value", "4"); + this.writing(map); + requireSucess = true; + logServer.deviceExecuteLog(this.device_code, "", "", "申请套管,返回参数:" + applyPaperActionResponse); + message = "申请套管成功"; + } else { + message = applyPaperActionResponse.getMessage(); + map.put("code", "to_command"); + map.put("value", "5"); + this.writing(map); + requireSucess = false; + message = "纸管不匹配"; + logServer.deviceExecuteLog(this.device_code, "", "", "申请套管,返回参数:" + applyPaperActionResponse); + } + } + } + + + /** + * 申请任务 + */ + public synchronized void instruction_require() { + Date date = new Date(); + if (date.getTime() - this.instruction_require_time.getTime() < (long) this.instruction_require_time_out) { + log.trace("触发时间因为小于{}毫秒,而被无视", this.instruction_require_time_out); + return; + } else { + //没有就绪状态的任务,查询就绪状态的指令 + TaskDto taskdto = taskserver.findByStartCodeAndReady(device_code); + Instruction inst = instructionService.findByDeviceCodeFromCache(this.device_code); + if (ObjectUtil.isNull(taskdto) && ObjectUtil.isNotNull(inst)) { + + } + } + } + + /** + * 完成指令 + * + * @return + * @throws Exception + */ + public synchronized boolean finish_instruction() throws Exception { + instructionService.finish(inst); + return true; + } + + @Override + public JSONObject getDeviceStatusName() throws Exception { + return null; + } + + @Override + public void setDeviceStatus(JSONObject data) { + + } + + public void writing(List list) { + Map itemMap = new HashMap(); + for (int i = 0; i < list.size(); i++) { + Object ob = list.get(i); + JSONObject json = (JSONObject) JSONObject.toJSON(ob); + if (!StrUtil.isEmpty(json.getString("value"))) { + String to_param = this.getDevice().getOpc_server_code() + "." + this.getDevice().getOpc_plc_code() + "." + this.getDevice().getDevice_code() + + "." + json.getString("code"); + itemMap.put(to_param, json.getString("value")); + } + } + logServer.deviceExecuteLog(device_code, "", "", "下发电气信号:" + itemMap); + try { + this.checkcontrol(itemMap); + } catch (Exception e) { + e.printStackTrace(); + try { + this.checkcontrol(itemMap); + } catch (Exception e1) { + e1.printStackTrace(); + } + } + } + + public void writing(Map map) { + DeviceExecuteLogService logServer = SpringContextHolder.getBean("deviceExecuteLogServiceImpl"); + Map itemMap = new LinkedHashMap<>(); + map.forEach((key, value) -> { + if (ObjectUtil.isNotEmpty(value)) { + itemMap.put(getToParam() + key, value); + } + }); + if (ObjectUtil.isNotEmpty(itemMap)) { + this.control(itemMap); + logServer.deviceExecuteLog(this.getDevice().getDevice_code(), "", "", "下发多个电气信号:" + itemMap); + } + } + + public String getToParam() { + return this.getDevice().getOpc_server_code() + "." + this.getDevice().getOpc_plc_code() + "." + this.getDevice().getDevice_code() + "."; + } + + public Instruction checkInst() { + if (ObjectUtil.isNotEmpty(this.inst)) { + if (this.task > 0) { + if (this.inst.getInstruction_code().equals(String.valueOf(this.task))) { + return this.inst; + } else { + inst = instructionService.findByCodeFromCache(String.valueOf(task)); + return inst; + } + } + } else { + inst = instructionService.findByCodeFromCache(String.valueOf(task)); + return inst; + } + return null; + } +} diff --git a/acs/nladmin-system/src/main/java/org/nl/acs/device_driver/basedriver/casing_station/CasingStationDefination.java b/acs/nladmin-system/src/main/java/org/nl/acs/device_driver/basedriver/casing_station/CasingStationDefination.java new file mode 100644 index 000000000..158e279d5 --- /dev/null +++ b/acs/nladmin-system/src/main/java/org/nl/acs/device_driver/basedriver/casing_station/CasingStationDefination.java @@ -0,0 +1,59 @@ +package org.nl.acs.device_driver.basedriver.casing_station; + +import org.nl.acs.device.device_driver.standard_inspect.ItemDto; +import org.nl.acs.device_driver.DeviceDriver; +import org.nl.acs.device_driver.defination.OpcDeviceDriverDefination; +import org.nl.acs.opc.Device; +import org.nl.acs.opc.DeviceType; +import org.springframework.stereotype.Service; + +import java.util.LinkedList; +import java.util.List; + +/** + * 套管工位 + */ +@Service +public class CasingStationDefination implements OpcDeviceDriverDefination { + @Override + public String getDriverCode() { + return "casing_station"; + } + + @Override + public String getDriverName() { + return "套管工位"; + } + + @Override + public String getDriverDescription() { + return "套管工位"; + } + + @Override + public DeviceDriver getDriverInstance(Device device) { + return (new CasingStationConveyorDeviceDriver().setDevice(device).setDriverDefination(this)); + } + + @Override + public Class getDeviceDriverType() { + return CasingStationConveyorDeviceDriver.class; + } + + @Override + public List getFitDeviceTypes() { + List types = new LinkedList(); + types.add(DeviceType.conveyor); + return types; + } + + @Override + public List getReadableItemDtos() { + return ItemProtocol.getReadableItemDtos(); + } + + @Override + public List getWriteableItemDtos() { + return ItemProtocol.getWriteableItemDtos(); + } +} diff --git a/acs/nladmin-system/src/main/java/org/nl/acs/device_driver/basedriver/casing_station/ItemProtocol.java b/acs/nladmin-system/src/main/java/org/nl/acs/device_driver/basedriver/casing_station/ItemProtocol.java new file mode 100644 index 000000000..feb67bc19 --- /dev/null +++ b/acs/nladmin-system/src/main/java/org/nl/acs/device_driver/basedriver/casing_station/ItemProtocol.java @@ -0,0 +1,125 @@ +package org.nl.acs.device_driver.basedriver.casing_station; + +import cn.hutool.core.util.StrUtil; +import lombok.Data; +import lombok.extern.slf4j.Slf4j; +import org.nl.acs.device.device_driver.standard_inspect.ItemDto; + +import java.util.ArrayList; +import java.util.List; + +@Slf4j +@Data +public class ItemProtocol { + + //心跳 + public static String item_heartbeat = "heartbeat"; + //工作模式 + public static String item_mode = "mode"; + //光电信号 + public static String item_move = "move"; + //托盘方向 + public static String item_carrier_direction = "carrier_direction"; + //报警 + public static String item_error = "error"; + //任务号 + public static String item_task = "task"; + //物料1 + public static String item_material1 = "material1"; + //物料2 + public static String item_material2 = "material2"; + + //下发命令 + public static String item_to_command = "to_command"; + + public ItemProtocol(CasingStationConveyorDeviceDriver driver) { + this.driver = driver; + } + + public int getHeartbeat() { + return this.getOpcIntegerValue(item_heartbeat); + } + + public int getMode() { + return this.getOpcIntegerValue(item_mode); + } + + public int getMove() { + return this.getOpcIntegerValue(item_move); + } + + public int getCarrier_direction() { + return this.getOpcIntegerValue(item_carrier_direction); + } + + public int getError() { + return this.getOpcIntegerValue(item_error); + } + + public int getTask() { + return this.getOpcIntegerValue(item_task); + } + + public String getMaterial1() { + return this.getOpcStringValue(item_material1); + } + + public String getMaterial2() { + return this.getOpcStringValue(item_material2); + } + + public int getTo_command() { + return this.getOpcIntegerValue(item_to_command); + } + + Boolean isonline; + + private CasingStationConveyorDeviceDriver driver; + + public int getOpcIntegerValue(String protocol) { + Integer value = this.driver.getIntegeregerValue(protocol); + if (value == null) { + setIsonline(false); + } else { + setIsonline(true); + return value; + } + return 0; + + } + + public String getOpcStringValue(String protocol) { + String value = this.driver.getStringValue(protocol); + if (StrUtil.isBlank(value)) { + + } else { + return value; + } + return "0"; + } + + public static List getReadableItemDtos() { + ArrayList list = new ArrayList(); + list.add(new ItemDto(item_heartbeat, "心跳", "DB101.W0")); + list.add(new ItemDto(item_mode, "工作模式", "DB101.W2")); + list.add(new ItemDto(item_move, "光电信号", "DB101.W3")); + list.add(new ItemDto(item_carrier_direction, "托盘方向", "DB101.W4")); + list.add(new ItemDto(item_error, "报警", "DB101.W58")); + list.add(new ItemDto(item_task, "任务号", "DB101.W6")); + list.add(new ItemDto(item_material1, "物料1", "DB101.W7")); + list.add(new ItemDto(item_material2, "物料1", "DB101.W8")); + return list; + } + + public static List getWriteableItemDtos() { + ArrayList list = new ArrayList(); + list.add(new ItemDto(item_to_command, "下发命令", "DB102.W4")); + return list; + } + + @Override + public String toString() { + return ""; + } + +} diff --git a/acs/nladmin-system/src/main/java/org/nl/acs/device_driver/basedriver/paper_tube_device2/Paper.java b/acs/nladmin-system/src/main/java/org/nl/acs/device_driver/basedriver/paper_tube_device2/Paper.java new file mode 100644 index 000000000..e088a576e --- /dev/null +++ b/acs/nladmin-system/src/main/java/org/nl/acs/device_driver/basedriver/paper_tube_device2/Paper.java @@ -0,0 +1,19 @@ +package org.nl.acs.device_driver.basedriver.paper_tube_device2; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class Paper implements Serializable { + //设备号 + private String device_code; + // + private String meterial_code; + private String qty; + +}