| | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import java.text.MessageFormat; |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.*; |
| | | import java.util.concurrent.ConcurrentHashMap; |
| | | |
| | | /** |
| | |
| | | add(401);add(402);add(403);add(404); |
| | | }}; |
| | | |
| | | public static final ArrayList<Integer> BarcodeList = new ArrayList<Integer>() {{ |
| | | add(1); add(2);add(3);add(4);add(5);add(6);add(7);add(8);add(9); |
| | | public static final ArrayList<Integer> BarcodeList1 = new ArrayList<Integer>() {{ |
| | | add(1); add(2);add(3);add(4);add(5); |
| | | }}; |
| | | |
| | | public static final ArrayList<Integer> BarcodeList2 = new ArrayList<Integer>() {{ |
| | | add(6);add(7);add(8);add(9); |
| | | add(10); add(11); add(12); add(13); |
| | | }}; |
| | | public static final ArrayList<Integer> staNosErrList = new ArrayList<Integer>() {{ |
| | | add(101); add(201); |
| | | public static final ArrayList<Integer> staNosErrList2 = new ArrayList<Integer>() {{ |
| | | add(101); add(104);add(106);add(108); |
| | | add(202);add(204);add(207);add(210); |
| | | }}; |
| | | |
| | | public static final ArrayList<Integer> staNosErrList1 = new ArrayList<Integer>() {{ |
| | | add(122); |
| | | add(222);add(224);add(227);add(230); |
| | | }}; |
| | | |
| | | private Integer count=0; |
| | |
| | | this.slave = slave; |
| | | } |
| | | |
| | | private ArrayList<Integer> getStaNoErr() { |
| | | switch (slave.getId()) { |
| | | case 1: |
| | | return staNosErrList1; |
| | | case 2: |
| | | return staNosErrList2; |
| | | default: |
| | | throw new CoolException("服务器异常"); |
| | | } |
| | | } |
| | | |
| | | private ArrayList<Integer> getStaNo() { |
| | | switch (slave.getId()) { |
| | | case 1: |
| | |
| | | return staNos2; |
| | | default: |
| | | throw new CoolException("服务器异常"); |
| | | } |
| | | } |
| | | private ArrayList<Integer> getBarcodeList() { |
| | | switch (slave.getId()) { |
| | | case 1: |
| | | return BarcodeList1; |
| | | case 2: |
| | | return BarcodeList2; |
| | | default: |
| | | throw new CoolException("获取站点失败!"); |
| | | } |
| | | } |
| | | |
| | |
| | | return result; |
| | | } |
| | | |
| | | public static int getOffsetFromSiteId(int siteId) { |
| | | if (siteId < 100 || siteId > 450) { |
| | | throw new IllegalArgumentException("siteId 超出有效范围: " + siteId); |
| | | } |
| | | return (siteId - 100) * 8; |
| | | } |
| | | |
| | | public static int getOffsetFromSiteId2(int siteId) { |
| | | if (siteId < 100 || siteId > 450) { |
| | | throw new IllegalArgumentException("siteId 超出有效范围: " + siteId); |
| | | } |
| | | return (siteId - 100) * 6; |
| | | } |
| | | |
| | | /** |
| | | * 读取状态 ====> 整块plc |
| | | */ |
| | |
| | | // // 更新入出库模式 |
| | | // updateIoMode(); |
| | | ArrayList<Integer> staNos = getStaNo(); |
| | | ArrayList<Integer> staNosErrList = getStaNoErr(); |
| | | int staNoSize = staNos.size(); |
| | | OperateResultExOne<byte[]> result = siemensS7Net.Read("DB101.0", (short) (staNoSize*8)); |
| | | OperateResultExOne<byte[]> result200 = siemensS7Net.Read("DB101.800", (short) (staNoSize*8)); |
| | | OperateResultExOne<byte[]> result = siemensS7Net.Read("DB101.0", (short) (500 * 8)); |
| | | |
| | | if (result.IsSuccess) { |
| | | for (int i = 0; i < staNoSize; i++) { |
| | | for(int i = 0 ; i<staNoSize;i++){ |
| | | Integer siteId = staNos.get(i); // 站点编号 |
| | | StaProtocol staProtocol = station.get(siteId); |
| | | if (null == staProtocol) { |
| | |
| | | station.put(siteId, staProtocol); |
| | | } |
| | | boolean[] status = null; |
| | | if(i < 24){ |
| | | staProtocol.setWorkNo(siemensS7Net.getByteTransform().TransInt32(result.Content, i*8)); // 工作号 |
| | | int offset = getOffsetFromSiteId(siteId);//取余获得对应db块数据 |
| | | |
| | | staProtocol.setStaNo(siemensS7Net.getByteTransform().TransInt16(result.Content, i*8 + 4)); // 目标站 |
| | | staProtocol.setWorkNo(siemensS7Net.getByteTransform().TransInt32(result.Content, offset)); // 工作号 |
| | | |
| | | status = siemensS7Net.getByteTransform().TransBool(result.Content, i*8 + 6, 2); |
| | | }else { |
| | | staProtocol.setWorkNo(siemensS7Net.getByteTransform().TransInt32(result200.Content, (i-24)*8)); // 工作号 |
| | | staProtocol.setStaNo(siemensS7Net.getByteTransform().TransInt16(result.Content, offset+ 4)); // 目标站 |
| | | |
| | | staProtocol.setStaNo(siemensS7Net.getByteTransform().TransInt16(result200.Content, (i-24)*8 + 4)); // 目标站 |
| | | |
| | | status = siemensS7Net.getByteTransform().TransBool(result200.Content, (i-24)*8 + 6, 2); |
| | | } |
| | | status = siemensS7Net.getByteTransform().TransBool(result.Content, offset+ 6, 2); |
| | | |
| | | staProtocol.setAutoing(status[0]); // 自动 |
| | | staProtocol.setLoading(status[1]); // 有物 |
| | |
| | | } |
| | | } |
| | | |
| | | //条码扫描器 |
| | | ArrayList<Integer> barcodeList = BarcodeList; |
| | | OperateResultExOne<byte[]> result2 = siemensS7Net.Read("DB101.1648", (short) (barcodeList.size() * 8)); |
| | | if (result2.IsSuccess) { |
| | | for (int i = 0; i < barcodeList.size(); i++) { |
| | | Integer barcodeId = barcodeList.get(i); |
| | | String barcode = siemensS7Net.getByteTransform().TransString(result2.Content,i*8,8, "UTF-8"); |
| | | BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, barcodeId); |
| | | if(!Cools.isEmpty(barcodeThread) && !barcodeThread.getBarcode().equals(barcode)) { |
| | | barcodeThread.setBarcode(barcode); |
| | | //条码扫描器 |
| | | ArrayList<Integer> barcodeList = getBarcodeList(); |
| | | OperateResultExOne<byte[]> result2 = siemensS7Net.Read("DB101.2848", (short) (barcodeList.size() * 8)); |
| | | if (result2.IsSuccess) { |
| | | for (int i = 0; i < barcodeList.size(); i++) { |
| | | Integer barcodeId = barcodeList.get(i); |
| | | String barcode = siemensS7Net.getByteTransform().TransString(result2.Content, i * 8, 8, "UTF-8"); |
| | | BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, barcodeId); |
| | | if (!Cools.isEmpty(barcodeThread) && !barcodeThread.getBarcode().equals(barcode)) { |
| | | barcodeThread.setBarcode(barcode); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | //外形检测 |
| | | ArrayList<Integer> staNoErrs = staNosErrList; |
| | | int staNoErrsSize = staNoErrs.size(); |
| | | OperateResultExOne<byte[]> resultErr = siemensS7Net.Read("DB101.1608", (short) (staNoErrsSize*8)); |
| | | if (resultErr.IsSuccess){ |
| | | for (int i = 0;i<2;i++){ |
| | | Integer siteId = staNoErrs.get(i); // 站点编号 |
| | | boolean[] status = siemensS7Net.getByteTransform().TransBool(resultErr.Content, i*4+2, 1); |
| | | StaProtocol staProtocol = station.get(siteId); |
| | | staProtocol.setFrontErr(status[0]); |
| | | staProtocol.setBackErr(status[1]); |
| | | staProtocol.setHighErr(status[2]); |
| | | staProtocol.setLeftErr(status[3]); |
| | | staProtocol.setRightErr(status[4]); |
| | | staProtocol.setWeightErr(status[5]); |
| | | staProtocol.setBarcodeErr(status[6]); |
| | | } |
| | | } |
| | | |
| | | //plc故障 |
| | | OperateResultExOne<byte[]> resultErr2 = siemensS7Net.Read("DB101.1728", (short) (staNoSize*4)); |
| | | if (resultErr2.IsSuccess) { |
| | | for (int i = 0; i < staNoSize; i++) { |
| | | Integer siteId = staNos.get(i); // 站点编号 |
| | | boolean[] status = siemensS7Net.getByteTransform().TransBool(resultErr2.Content, i*4, 1); |
| | | StaProtocol staProtocol = station.get(siteId); |
| | | staProtocol.setBreakerErr(status[0]); |
| | | staProtocol.setInfraredErr(status[1]); |
| | | staProtocol.setOutTimeErr(status[2]); |
| | | staProtocol.setSeizeSeatErr(status[3]); |
| | | staProtocol.setWrkYgoodsN(status[4]); |
| | | staProtocol.setInverterErr(status[5]); |
| | | staProtocol.setContactErr(status[6]); |
| | | staProtocol.setUpcontactErr(status[7]); |
| | | |
| | | } |
| | | } |
| | | OperateResultExOne<byte[]> resultErr3 = siemensS7Net.Read("DB13.400", (short) 3); |
| | | if (resultErr3.IsSuccess) { |
| | | if (staError1 == null){ |
| | | staError1 = new StaError1(); |
| | | } |
| | | boolean[] status = siemensS7Net.getByteTransform().TransBool(resultErr3.Content, 0, 3); |
| | | staError1.setEMERGENCY_Stop_Core(status[0]); |
| | | staError1.setEMERGENCY_STOP1(status[1]); |
| | | staError1.setEMERGENCY_STOP2(status[2]); |
| | | staError1.setEMERGENCY_STOP3(status[3]); |
| | | staError1.setEMERGENCY_STOP4(status[4]); |
| | | |
| | | staError1.setDoor_EMERGENCY_STOP1(status[5]); |
| | | staError1.setDoor_EMERGENCY_STOP2(status[6]); |
| | | staError1.setDoor_EMERGENCY_STOP3(status[7]); |
| | | staError1.setDoor_EMERGENCY_STOP4(status[8]); |
| | | staError1.setDoor_EMERGENCY_STOP5(status[9]); |
| | | staError1.setDoor_EMERGENCY_STOP6(status[10]); |
| | | |
| | | staError1.setDoor_Open_Err1(status[11]); |
| | | staError1.setDoor_Open_Err1(status[12]); |
| | | staError1.setDoor_Open_Err1(status[13]); |
| | | staError1.setDoor_Open_Err1(status[14]); |
| | | staError1.setDoor_Open_Err1(status[15]); |
| | | staError1.setDoor_Open_Err1(status[16]); |
| | | |
| | | } |
| | | |
| | | |
| | | if (result.IsSuccess) { |
| | | |
| | | OutputQueue.DEVP.offer(MessageFormat.format("【{0}】[id:{1}] <<<<< 实时数据更新成功",DateUtils.convert(new Date()), slave.getId())); |
| | | |
| | | // 根据实时信息更新数据库 |
| | | try { |
| | | List<BasDevp> basDevps = new ArrayList<>(); |
| | | for (Integer siteId : staNos) { |
| | | //外形检测 |
| | | ArrayList<Integer> staNoErrs = staNosErrList; |
| | | int staNoErrsSize = staNoErrs.size(); |
| | | OperateResultExOne<byte[]> resultErr = siemensS7Net.Read("DB101.2808", (short) (staNoErrsSize * 4)); |
| | | if (resultErr.IsSuccess) { |
| | | for (int i = 0; i < staNoErrsSize; i++) { |
| | | Integer siteId = staNoErrs.get(i); // 站点编号 |
| | | boolean[] status = siemensS7Net.getByteTransform().TransBool(resultErr.Content, i * 4 + 2, 1); |
| | | StaProtocol staProtocol = station.get(siteId); |
| | | basDevps.add(staProtocol.toSqlModel()); |
| | | staProtocol.setFrontErr(status[0]); |
| | | staProtocol.setBackErr(status[1]); |
| | | staProtocol.setHighErr(status[2]); |
| | | staProtocol.setLeftErr(status[3]); |
| | | staProtocol.setRightErr(status[4]); |
| | | staProtocol.setWeightErr(status[5]); |
| | | staProtocol.setBarcodeErr(status[6]); |
| | | } |
| | | BasDevpService basDevpService = SpringContextUtil.getBean(BasDevpService.class); |
| | | //BasDevpService basDevpService = SpringUtils.getBean(BasDevpService.class); |
| | | if (null != basDevpService && !basDevpService.updateBatchById(basDevps)) { |
| | | throw new Exception("更新数据库数据失败"); |
| | | } |
| | | } catch (Exception e) { |
| | | initSite(); |
| | | e.printStackTrace(); |
| | | OutputQueue.DEVP.offer(MessageFormat.format("【{0}】更新数据库数据失败 ===>> [id:{1}] [ip:{2}] [port:{3}] [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot())); |
| | | News.error("SiemensDevp"+" - 3"+" - 更新数据库数据失败 ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot()); |
| | | } |
| | | |
| | | } else { |
| | | OutputQueue.DEVP.offer(MessageFormat.format("【{0}】读取输送线plc状态信息失败 ===>> [id:{1}] [ip:{2}] [port:{3}] [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot())); |
| | | // //plc故障 |
| | | // OperateResultExOne<byte[]> resultErr2 = siemensS7Net.Read("DB101.1728", (short) (staNoSize*4)); |
| | | // if (resultErr2.IsSuccess) { |
| | | // for (int i = 0; i < staNoSize; i++) { |
| | | // Integer siteId = staNos.get(i); // 站点编号 |
| | | // boolean[] status = siemensS7Net.getByteTransform().TransBool(resultErr2.Content, i*4, 1); |
| | | // StaProtocol staProtocol = station.get(siteId); |
| | | // staProtocol.setBreakerErr(status[0]); |
| | | // staProtocol.setInfraredErr(status[1]); |
| | | // staProtocol.setOutTimeErr(status[2]); |
| | | // staProtocol.setSeizeSeatErr(status[3]); |
| | | // staProtocol.setWrkYgoodsN(status[4]); |
| | | // staProtocol.setInverterErr(status[5]); |
| | | // staProtocol.setContactErr(status[6]); |
| | | // staProtocol.setUpcontactErr(status[7]); |
| | | // |
| | | // } |
| | | // } |
| | | // OperateResultExOne<byte[]> resultErr3 = siemensS7Net.Read("DB13.400", (short) 3); |
| | | // if (resultErr3.IsSuccess) { |
| | | // if (staError1 == null){ |
| | | // staError1 = new StaError1(); |
| | | // } |
| | | // boolean[] status = siemensS7Net.getByteTransform().TransBool(resultErr3.Content, 0, 3); |
| | | // staError1.setEMERGENCY_Stop_Core(status[0]); |
| | | // staError1.setEMERGENCY_STOP1(status[1]); |
| | | // staError1.setEMERGENCY_STOP2(status[2]); |
| | | // staError1.setEMERGENCY_STOP3(status[3]); |
| | | // staError1.setEMERGENCY_STOP4(status[4]); |
| | | // |
| | | // staError1.setDoor_EMERGENCY_STOP1(status[5]); |
| | | // staError1.setDoor_EMERGENCY_STOP2(status[6]); |
| | | // staError1.setDoor_EMERGENCY_STOP3(status[7]); |
| | | // staError1.setDoor_EMERGENCY_STOP4(status[8]); |
| | | // staError1.setDoor_EMERGENCY_STOP5(status[9]); |
| | | // staError1.setDoor_EMERGENCY_STOP6(status[10]); |
| | | // |
| | | // staError1.setDoor_Open_Err1(status[11]); |
| | | // staError1.setDoor_Open_Err1(status[12]); |
| | | // staError1.setDoor_Open_Err1(status[13]); |
| | | // staError1.setDoor_Open_Err1(status[14]); |
| | | // staError1.setDoor_Open_Err1(status[15]); |
| | | // staError1.setDoor_Open_Err1(status[16]); |
| | | // |
| | | // } |
| | | |
| | | |
| | | if (result.IsSuccess) { |
| | | |
| | | OutputQueue.DEVP.offer(MessageFormat.format("【{0}】[id:{1}] <<<<< 实时数据更新成功", DateUtils.convert(new Date()), slave.getId())); |
| | | |
| | | // 根据实时信息更新数据库 |
| | | try { |
| | | List<BasDevp> basDevps = new ArrayList<>(); |
| | | for (Integer siteId : staNos) { |
| | | StaProtocol staProtocol = station.get(siteId); |
| | | basDevps.add(staProtocol.toSqlModel()); |
| | | } |
| | | BasDevpService basDevpService = SpringContextUtil.getBean(BasDevpService.class); |
| | | //BasDevpService basDevpService = SpringUtils.getBean(BasDevpService.class); |
| | | if (null != basDevpService && !basDevpService.updateBatchById(basDevps)) { |
| | | throw new Exception("更新数据库数据失败"); |
| | | } |
| | | } catch (Exception e) { |
| | | initSite(); |
| | | e.printStackTrace(); |
| | | OutputQueue.DEVP.offer(MessageFormat.format("【{0}】更新数据库数据失败 ===>> [id:{1}] [ip:{2}] [port:{3}] [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot())); |
| | | News.error("SiemensDevp" + " - 3" + " - 更新数据库数据失败 ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot()); |
| | | } |
| | | |
| | | } else { |
| | | OutputQueue.DEVP.offer(MessageFormat.format("【{0}】读取输送线plc状态信息失败 ===>> [id:{1}] [ip:{2}] [port:{3}] [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot())); |
| | | // log.error("读取输送线plc状态信息失败 ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot()); |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 写入 ID+目标站 =====> 单站点写入 |
| | |
| | | return; |
| | | } |
| | | ArrayList<Integer> staNos = getStaNo(); |
| | | int index = staNos.indexOf(staProtocol.getSiteId()); |
| | | |
| | | if(index >= 24){ |
| | | index = 76 + index; |
| | | } |
| | | // int index = staNos.indexOf(staProtocol.getSiteId()); |
| | | int offset = getOffsetFromSiteId2(staProtocol.getSiteId());//取余获得对应db块数据 |
| | | |
| | | OperateResult write = null; |
| | | OperateResult write1 = null; |
| | | //任务下发次数 |
| | | int writeCount = 0; |
| | | do { |
| | | write = siemensS7Net.Write("DB100." + index*6, staProtocol.getWorkNo()); // 工作号 |
| | | write = siemensS7Net.Write("DB100." + offset, staProtocol.getWorkNo()); // 工作号 |
| | | Thread.sleep(200); |
| | | write1 = siemensS7Net.Write("DB100." + (index*6+4), staProtocol.getStaNo().shortValue()); // 目标站 |
| | | write1 = siemensS7Net.Write("DB100." + (offset+4), staProtocol.getStaNo().shortValue()); // 目标站 |
| | | if(write.IsSuccess && write1.IsSuccess){ |
| | | log.error("写入输送线命令成功。输送线plc编号={},站点数据={},写入次数={}", slave.getId(), JSON.toJSON(staProtocol), writeCount); |
| | | break; |
| | |
| | | } |
| | | OutputQueue.DEVP.offer(MessageFormat.format("【{0}】写入输送线站点数据失败。输送线plc编号={1},站点数据={2}", slave.getId(), JSON.toJSON(staProtocol))); |
| | | News.error("SiemensDevp"+" - 4"+" - 写入输送线站点数据失败。输送线plc编号={},站点数据={}", slave.getId(), JSON.toJSON(staProtocol)); |
| | | } else { |
| | | //dang 101,201zhan xaifazhiling qingchu tiaoxingsaomaolide shuju shanchu |
| | | if (staProtocol.getSiteId()==101){ |
| | | BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, 1); |
| | | log.info("qingchutiaoma,{}",barcodeThread.getBarcode()); |
| | | barcodeThread.clearBarcode(); |
| | | } |
| | | if (staProtocol.getSiteId()==201){ |
| | | BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, 2); |
| | | log.info("qingchutiaoma,{}",barcodeThread.getBarcode()); |
| | | barcodeThread.clearBarcode(); |
| | | } |
| | | OutputQueue.DEVP.offer(MessageFormat.format("【{0}】 输送线命令下发 [id:{1}] >>>>> {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(staProtocol))); |
| | | News.info("SiemensDevp"+" - 5"+" - 输送线命令下发 [id:{}] >>>>> 命令下发: {}", slave.getId(), JSON.toJSON(staProtocol)); |
| | | } |
| | | // else { |
| | | // //dang 101,201zhan xaifazhiling qingchu tiaoxingsaomaolide shuju shanchu |
| | | // if (staProtocol.getSiteId()==101){ |
| | | // BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, 1); |
| | | // log.info("qingchutiaoma,{}",barcodeThread.getBarcode()); |
| | | // barcodeThread.clearBarcode(); |
| | | // } |
| | | // if (staProtocol.getSiteId()==201){ |
| | | // BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, 2); |
| | | // log.info("qingchutiaoma,{}",barcodeThread.getBarcode()); |
| | | // barcodeThread.clearBarcode(); |
| | | // } |
| | | // OutputQueue.DEVP.offer(MessageFormat.format("【{0}】 输送线命令下发 [id:{1}] >>>>> {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(staProtocol))); |
| | | // News.info("SiemensDevp"+" - 5"+" - 输送线命令下发 [id:{}] >>>>> 命令下发: {}", slave.getId(), JSON.toJSON(staProtocol)); |
| | | // } |
| | | } |
| | | |
| | | // 更新入出库模式 |