| | |
| | | package com.zy.core.network.real; |
| | | |
| | | import HslCommunication.Core.Types.OperateResult; |
| | | import HslCommunication.Core.Types.OperateResultExOne; |
| | | import HslCommunication.Profinet.Siemens.SiemensPLCS; |
| | | import HslCommunication.Profinet.Siemens.SiemensS7Net; |
| | | |
| | | import java.text.MessageFormat; |
| | | import java.util.Date; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.core.common.DateUtils; |
| | | import com.core.common.SpringUtils; |
| | | import com.zy.asrs.entity.BasCrnpOpt; |
| | | import com.zy.asrs.entity.BasRgv; |
| | | import com.zy.asrs.entity.BasRgvOpt; |
| | | import com.zy.asrs.entity.DeviceConfig; |
| | | import com.zy.asrs.entity.DeviceDataLog; |
| | | import com.zy.asrs.service.BasCrnpOptService; |
| | | import com.zy.asrs.service.BasRgvOptService; |
| | | import com.zy.asrs.service.BasRgvService; |
| | | import com.zy.asrs.service.DeviceDataLogService; |
| | | import com.zy.asrs.utils.Utils; |
| | | import com.zy.core.News; |
| | | import com.zy.core.cache.OutputQueue; |
| | | import com.zy.core.enums.RgvStatusType; |
| | | import com.zy.core.model.CommandResponse; |
| | | import com.zy.core.model.command.RgvCommand; |
| | | import com.zy.core.model.protocol.RgvProtocol; |
| | | import com.zy.core.network.api.ZyRgvConnectApi; |
| | | import com.zy.core.network.entity.ZyRgvStatusEntity; |
| | | |
| | | public class ZyRgvRealConnect implements ZyRgvConnectApi { |
| | | private final SiemensS7Net siemensNet; |
| | | private final DeviceConfig deviceConfig; |
| | | |
| | | public ZyRgvRealConnect(SiemensS7Net siemensNet, DeviceConfig deviceConfig) { |
| | | this.siemensNet = siemensNet; |
| | | private SiemensS7Net siemensNet; |
| | | private DeviceConfig deviceConfig; |
| | | |
| | | public ZyRgvRealConnect(DeviceConfig deviceConfig) { |
| | | this.deviceConfig = deviceConfig; |
| | | } |
| | | |
| | | @Override |
| | | public boolean connect() { |
| | | return true; |
| | | boolean result = false; |
| | | siemensNet = new SiemensS7Net(SiemensPLCS.S1200, deviceConfig.getIp()); |
| | | OperateResult connect = siemensNet.ConnectServer(); |
| | | if(connect.IsSuccess){ |
| | | result = true; |
| | | OutputQueue.RGV.offer(MessageFormat.format( "【{0}】RGV plc连接成功 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), deviceConfig.getId(), deviceConfig.getIp(), deviceConfig.getPort())); |
| | | News.info("RGV plc连接成功 ===>> [id:{}] [ip:{}] [port:{}]", deviceConfig.getId(), deviceConfig.getIp(), deviceConfig.getPort()); |
| | | } else { |
| | | OutputQueue.RGV.offer(MessageFormat.format("【{0}】RGV plc连接失败!!! ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), deviceConfig.getId(), deviceConfig.getIp(), deviceConfig.getPort())); |
| | | News.error("RGV plc连接失败!!! ===>> [id:{}] [ip:{}] [port:{}]", deviceConfig.getId(), deviceConfig.getIp(), deviceConfig.getPort()); |
| | | } |
| | | // siemensNet.ConnectClose(); |
| | | return result; |
| | | } |
| | | |
| | | @Override |
| | | public boolean disconnect() { |
| | | siemensNet.ConnectClose(); |
| | | return true; |
| | | } |
| | | |
| | | @Override |
| | | public ZyRgvStatusEntity getStatus() { |
| | | try { |
| | | OperateResultExOne<byte[]> result = siemensNet.Read("DB101.0", (short) 30); |
| | | if (result.IsSuccess) { |
| | | ZyRgvStatusEntity zyRgvStatusEntity = new ZyRgvStatusEntity(); |
| | | zyRgvStatusEntity.setRgvNo(deviceConfig.getDeviceNo()); |
| | | zyRgvStatusEntity.setMode((int) siemensNet.getByteTransform().TransInt16(result.Content, 0)); |
| | | zyRgvStatusEntity.setStatus((int) siemensNet.getByteTransform().TransInt16(result.Content, 2)); |
| | | zyRgvStatusEntity.setTaskNo((int) siemensNet.getByteTransform().TransInt16(result.Content, 4)); |
| | | // zyRgvStatusEntity.setStatus((int) siemensNet.getByteTransform().TransInt16(result.Content, 6)); |
| | | zyRgvStatusEntity.setLoaded((int) siemensNet.getByteTransform().TransInt16(result.Content, 8)); |
| | | zyRgvStatusEntity.setRgvPos((int) siemensNet.getByteTransform().TransInt16(result.Content, 10)); |
| | | zyRgvStatusEntity.setWalkPos((int) siemensNet.getByteTransform().TransInt16(result.Content, 12)); |
| | | zyRgvStatusEntity.setAlarm((int) siemensNet.getByteTransform().TransInt16(result.Content, 14)); |
| | | |
| | | OutputQueue.RGV.offer(MessageFormat.format("【{0}】[id:{1}] <<<<< 实时数据更新成功", DateUtils.convert(new Date()), deviceConfig.getDeviceNo())); |
| | | |
| | | return zyRgvStatusEntity; |
| | | } else { |
| | | OutputQueue.RGV.offer(MessageFormat.format("【{0}】读取RGV plc状态信息失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), deviceConfig.getId(), deviceConfig.getIp(), deviceConfig.getPort())); |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | OutputQueue.RGV.offer(MessageFormat.format("【{0}】读取RGV plc状态信息失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), deviceConfig.getId(), deviceConfig.getIp(), deviceConfig.getPort())); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | @Override |
| | | public CommandResponse sendCommand(RgvCommand command) { |
| | | return new CommandResponse(false, "not implemented"); |
| | | CommandResponse response = new CommandResponse(false); |
| | | try { |
| | | if (null == command) { |
| | | response.setMessage("RGV写入命令为空"); |
| | | return response; |
| | | } |
| | | |
| | | short[] array = new short[5]; |
| | | array[0] = command.getAckFinish().shortValue(); |
| | | array[1] = command.getTaskNo().shortValue(); |
| | | array[2] = command.getTaskMode().shortValue(); |
| | | array[3] = command.getSourcePos().shortValue(); |
| | | array[4] = command.getTargetPos().shortValue(); |
| | | OperateResult result = siemensNet.Write("DB100.0", array); |
| | | |
| | | if (result != null && result.IsSuccess) { |
| | | News.info("RGV 命令下发[id:{}] >>>>> {}", deviceConfig.getDeviceNo(), JSON.toJSON(command)); |
| | | OutputQueue.RGV.offer(MessageFormat.format("【{0}】[id:{1}] >>>>> 命令下发: {2}", DateUtils.convert(new Date()), deviceConfig.getDeviceNo(), JSON.toJSON(command))); |
| | | response.setResult(true); |
| | | return response; |
| | | } else { |
| | | OutputQueue.RGV.offer(MessageFormat.format("【{0}】写入RGV plc数据失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), deviceConfig.getId(), deviceConfig.getIp(), deviceConfig.getPort())); |
| | | News.error("写入RGV plc数据失败 ===>> [id:{}] [ip:{}] [port:{}]", deviceConfig.getId(), deviceConfig.getIp(), deviceConfig.getPort()); |
| | | response.setMessage("写入RGV plc数据失败"); |
| | | return response; |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } finally { |
| | | // 日志记录 |
| | | BasRgvOptService bean = SpringUtils.getBean(BasRgvOptService.class); |
| | | BasRgvOpt basRgvOpt = new BasRgvOpt( |
| | | command.getTaskNo(), // 任务号 |
| | | command.getRgvNo(), // RGV[非空] |
| | | new Date(), // 下发时间 |
| | | String.valueOf(command.getTaskMode()), // 模式 |
| | | String.valueOf(command.getSourcePos()), //源库位 |
| | | String.valueOf(command.getTargetPos()), //目标库位 |
| | | null, // 修改时间 |
| | | null, // 修改人员 |
| | | null, // 备注 |
| | | JSON.toJSONString(command), // 指令 |
| | | JSON.toJSONString(getStatus()), // 系统状态 |
| | | 1, // 下发状态{0:未下发,1:已下发} |
| | | JSON.toJSONString(response) // 响应 |
| | | ); |
| | | bean.insert(basRgvOpt); |
| | | } |
| | | return response; |
| | | } |
| | | } |