| | |
| | | |
| | | import HslCommunication.Core.Types.OperateResult; |
| | | import HslCommunication.Core.Types.OperateResultExOne; |
| | | import HslCommunication.Profinet.Siemens.SiemensPLCS; |
| | | import HslCommunication.Profinet.Siemens.SiemensS7Net; |
| | | import com.zy.common.utils.RedisUtil; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | |
| | | import com.zy.core.News; |
| | | import com.zy.core.cache.OutputQueue; |
| | | import com.zy.core.model.CommandResponse; |
| | | import com.zy.core.model.StationObjModel; |
| | | import com.zy.core.model.command.StationCommand; |
| | | import com.zy.core.network.api.ZyStationConnectApi; |
| | | import com.zy.core.network.entity.ZyStationStatusEntity; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Collections; |
| | | import java.util.Comparator; |
| | | import java.util.List; |
| | | |
| | | import java.text.MessageFormat; |
| | |
| | | public class ZyStationRealConnect implements ZyStationConnectApi { |
| | | |
| | | private List<ZyStationStatusEntity> statusList; |
| | | private List<ZyStationStatusEntity> barcodeStatusList; |
| | | private final SiemensS7Net siemensNet; |
| | | private final DeviceConfig deviceConfig; |
| | | private List<StationObjModel> barcodeOriginList; |
| | | private SiemensS7Net siemensNet; |
| | | private DeviceConfig deviceConfig; |
| | | private RedisUtil redisUtil; |
| | | |
| | | public ZyStationRealConnect(SiemensS7Net siemensNet, DeviceConfig deviceConfig) { |
| | | this.siemensNet = siemensNet; |
| | | public ZyStationRealConnect(DeviceConfig deviceConfig, RedisUtil redisUtil) { |
| | | this.deviceConfig = deviceConfig; |
| | | this.redisUtil = redisUtil; |
| | | } |
| | | |
| | | @Override |
| | | public boolean connect() { |
| | | boolean connected = false; |
| | | siemensNet = new SiemensS7Net(SiemensPLCS.S1200, deviceConfig.getIp()); |
| | | OperateResult connect = siemensNet.ConnectServer(); |
| | | if (connect.IsSuccess) { |
| | | connected = true; |
| | |
| | | } |
| | | |
| | | @Override |
| | | public List<ZyStationStatusEntity> getStatus() { |
| | | public List<ZyStationStatusEntity> getStatus(Integer deviceNo) { |
| | | if (statusList == null) { |
| | | BasDevpService basDevpService = SpringUtils.getBean(BasDevpService.class); |
| | | if (basDevpService == null) { |
| | |
| | | } |
| | | |
| | | statusList = JSONObject.parseArray(basDevp.getStationList(), ZyStationStatusEntity.class); |
| | | barcodeStatusList = JSONObject.parseArray(basDevp.getBarcodeStationList(), ZyStationStatusEntity.class); |
| | | if (statusList != null) { |
| | | statusList.sort(Comparator.comparing(ZyStationStatusEntity::getStationId)); |
| | | } |
| | | barcodeOriginList = basDevp.getBarcodeStationList$(); |
| | | } |
| | | |
| | | OperateResultExOne<byte[]> result = siemensNet.Read("DB101.0", (short) (statusList.size() * 2)); |
| | | OperateResultExOne<byte[]> result = siemensNet.Read("DB101.0", (short) (statusList.size() * 8)); |
| | | if (result.IsSuccess) { |
| | | for (int i = 0; i < statusList.size(); i++) { |
| | | ZyStationStatusEntity statusEntity = statusList.get(i); // 站点编号 |
| | | boolean[] status = siemensNet.getByteTransform().TransBool(result.Content, i * 2, 2); |
| | | statusEntity.setTaskNo((int) siemensNet.getByteTransform().TransInt16(result.Content, i * 8)); // 工作号 |
| | | statusEntity.setTargetStaNo((int) siemensNet.getByteTransform().TransInt16(result.Content, i * 8 + 4)); // 目标站 |
| | | |
| | | boolean[] status = siemensNet.getByteTransform().TransBool(result.Content, i * 8 + 6, 2); |
| | | statusEntity.setAutoing(status[0]); // 自动 |
| | | statusEntity.setLoading(status[1]); // 有物 |
| | | statusEntity.setInEnable(status[2]); // 可入 |
| | |
| | | } |
| | | } |
| | | |
| | | OperateResultExOne<byte[]> result1 = siemensNet.Read("DB100.0", (short) (statusList.size() * 4)); |
| | | if (result1.IsSuccess) { |
| | | for (int i = 0; i < statusList.size(); i++) { |
| | | ZyStationStatusEntity statusEntity = statusList.get(i); |
| | | statusEntity.setTaskNo((int) siemensNet.getByteTransform().TransInt16(result1.Content, i * 4)); // 工作号 |
| | | statusEntity.setTargetStaNo((int) siemensNet.getByteTransform().TransInt16(result1.Content, i * 4 + 2)); // 目标站 |
| | | // 条码扫描器 |
| | | OperateResultExOne<byte[]> result2 = siemensNet.Read("DB101.840", (short) (barcodeOriginList.size() * 8)); |
| | | if (result2.IsSuccess) { |
| | | for (int i = 0; i < barcodeOriginList.size(); i++) { |
| | | ZyStationStatusEntity barcodeEntity = findStatusEntityByBarcodeIdx(i + 1); |
| | | if (barcodeEntity == null) { |
| | | continue; |
| | | } |
| | | String barcode = siemensNet.getByteTransform().TransString(result2.Content, i * 8, 8, "UTF-8"); |
| | | barcode = barcode.trim(); |
| | | barcodeEntity.setBarcode(barcode); |
| | | } |
| | | } |
| | | |
| | | // 条码扫描器 |
| | | OperateResultExOne<byte[]> result2 = siemensNet.Read("DB101.100", (short) (statusList.size() * 10)); |
| | | if (result2.IsSuccess) { |
| | | for (int i = 0; i < barcodeStatusList.size(); i++) { |
| | | ZyStationStatusEntity barcodeEntity = barcodeStatusList.get(i); |
| | | ZyStationStatusEntity statusEntity = findStatusEntity(barcodeEntity.getStationId()); |
| | | if (statusEntity == null) { |
| | | // 报警信息 |
| | | OperateResultExOne<byte[]> result3 = siemensNet.Read("DB101.800", (short) (barcodeOriginList.size() * 3)); |
| | | if (result3.IsSuccess) { |
| | | for (int i = 0; i < barcodeOriginList.size(); i++) { |
| | | ZyStationStatusEntity barcodeEntity = findStatusEntityByBarcodeIdx(i + 1); |
| | | if (barcodeEntity == null) { |
| | | continue; |
| | | } |
| | | String barcode = siemensNet.getByteTransform().TransString(result2.Content, i * 10 + 2, 8, "UTF-8"); |
| | | barcode = barcode.trim(); |
| | | barcodeEntity.setBarcode(barcode); |
| | | |
| | | barcodeEntity.setPalletHeight((int) siemensNet.getByteTransform().TransInt16(result3.Content, i * 3)); // 高低信号 |
| | | |
| | | StringBuilder sb = new StringBuilder(); |
| | | boolean[] status1 = siemensNet.getByteTransform().TransBool(result3.Content, i * 3 + 2, 1); |
| | | |
| | | if(status1[0]){ |
| | | sb.append("前超限报警;"); |
| | | } |
| | | if(status1[1]) { |
| | | sb.append("后超限报警;"); |
| | | } |
| | | if(status1[2]) { |
| | | sb.append("超高报警;"); |
| | | } |
| | | if(status1[3]) { |
| | | sb.append("左超报警;"); |
| | | } |
| | | if(status1[4]) { |
| | | sb.append("右超报警;"); |
| | | } |
| | | if(status1[5]) { |
| | | sb.append("超重报警;"); |
| | | } |
| | | if(status1[6]) { |
| | | sb.append("扫码失败;"); |
| | | } |
| | | |
| | | if(sb.length() > 0) { |
| | | barcodeEntity.setError(1); |
| | | }else { |
| | | barcodeEntity.setError(0); |
| | | } |
| | | barcodeEntity.setErrorMsg(sb.toString()); |
| | | } |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | @Override |
| | | public CommandResponse sendCommand(StationCommand command) { |
| | | public CommandResponse sendCommand(Integer deviceNo, StationCommand command) { |
| | | CommandResponse commandResponse = new CommandResponse(false); |
| | | if (null == command) { |
| | | commandResponse.setMessage("命令为空"); |
| | |
| | | return commandResponse; |
| | | } |
| | | |
| | | @Override |
| | | public CommandResponse sendOriginCommand(String address, short[] data) { |
| | | CommandResponse commandResponse = new CommandResponse(false); |
| | | if (null == data || data.length == 0) { |
| | | commandResponse.setMessage("数据为空"); |
| | | return commandResponse; |
| | | } |
| | | |
| | | OperateResult write = siemensNet.Write(address, data); |
| | | if (write.IsSuccess) { |
| | | log.info("写入原始命令成功。地址={},数据={}", address, JSON.toJSON(data)); |
| | | commandResponse.setResult(true); |
| | | } else { |
| | | log.error("写入原始命令失败。地址={},数据={}", address, JSON.toJSON(data)); |
| | | commandResponse.setResult(false); |
| | | } |
| | | return commandResponse; |
| | | } |
| | | |
| | | @Override |
| | | public byte[] readOriginCommand(String address, int length) { |
| | | OperateResultExOne<byte[]> result = siemensNet.Read(address, (short) length); |
| | | if (result.IsSuccess) { |
| | | return result.Content; |
| | | } |
| | | return new byte[0]; |
| | | } |
| | | |
| | | private ZyStationStatusEntity findStatusEntity(Integer stationId) { |
| | | for (ZyStationStatusEntity statusEntity : statusList) { |
| | | if (statusEntity.getStationId().equals(stationId)) { |
| | |
| | | } |
| | | return -1; |
| | | } |
| | | } |
| | | |
| | | private ZyStationStatusEntity findStatusEntityByBarcodeIdx(Integer barcodeIdx) { |
| | | Integer stationId = null; |
| | | for (StationObjModel stationObjModel : barcodeOriginList) { |
| | | if (stationObjModel.getBarcodeIdx().equals(barcodeIdx)) { |
| | | stationId = stationObjModel.getStationId(); |
| | | break; |
| | | } |
| | | } |
| | | |
| | | for (ZyStationStatusEntity zyStationStatusEntity : statusList) { |
| | | if(zyStationStatusEntity.getStationId().equals(stationId)) { |
| | | return zyStationStatusEntity; |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | } |