From 09fcc4ec56a1ce6c0fcb308348d8dd5e2c08d336 Mon Sep 17 00:00:00 2001 From: fyxc <fyxc@qq.com> Date: 星期四, 22 五月 2025 14:33:06 +0800 Subject: [PATCH] server commit files --- src/main/java/com/zy/core/thread/DevpThread.java | 247 ------------------------------------------------ 1 files changed, 3 insertions(+), 244 deletions(-) diff --git a/src/main/java/com/zy/core/thread/DevpThread.java b/src/main/java/com/zy/core/thread/DevpThread.java index 3b57b24..46122e9 100644 --- a/src/main/java/com/zy/core/thread/DevpThread.java +++ b/src/main/java/com/zy/core/thread/DevpThread.java @@ -1,255 +1,14 @@ package com.zy.core.thread; -import HslCommunication.Core.Types.OperateResult; -import HslCommunication.Core.Types.OperateResultExOne; -import HslCommunication.Profinet.Melsec.MelsecMcNet; -import com.alibaba.fastjson.JSON; -import com.core.common.DateUtils; -import com.core.common.SpringUtils; -import com.zy.asrs.entity.BasDevp; -import com.zy.asrs.service.BasDevpService; import com.zy.core.ThreadHandler; -import com.zy.core.cache.MessageQueue; -import com.zy.core.cache.OutputQueue; -import com.zy.core.enums.SlaveType; -import com.zy.core.model.DevpSlave; -import com.zy.core.model.Task; import com.zy.core.model.protocol.StaProtocol; -import lombok.Data; -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.concurrent.ConcurrentHashMap; -/** - * 杈撻�佺嚎绾跨▼ - * Created by vincent on 2020/8/4 - */ -@Data -@Slf4j -public class DevpThread implements Runnable, ThreadHandler { +public interface DevpThread extends ThreadHandler { - private DevpSlave slave; - private MelsecMcNet melsecMcNet; - private Map<Integer, StaProtocol> station = new ConcurrentHashMap<>(); - private short heartBeatVal = 1; + Map<Integer, StaProtocol> getStation(); - public DevpThread(DevpSlave slave) { - this.slave = slave; - } - - @Override - @SuppressWarnings("InfiniteLoopStatement") - public void run() { - connect(); - while (true) { - try { - int step = 1; - Task task = MessageQueue.poll(SlaveType.Devp, slave.getId()); - if (task != null) { - step = task.getStep(); - } - switch (step) { - // 璇绘暟鎹� - case 1: - read(); - break; - // 鍐欐暟鎹� ID+鐩爣绔� - case 2: - write((StaProtocol)task.getData()); - break; - // 鍐欐暟鎹� ID - case 3: - writeId((StaProtocol)task.getData()); - break; - // 鍐欐暟鎹� 鐩爣绔� - case 4: - writeStaNo((StaProtocol)task.getData()); - break; - default: - break; - } - - // 蹇冭烦 -// heartbeat(); - Thread.sleep(400); - } catch (Exception e) { - e.printStackTrace(); - } - - } - } - - @Override - public boolean connect() { - boolean result = false; - melsecMcNet = new MelsecMcNet(slave.getIp(), slave.getPort()); - OperateResult connect = melsecMcNet.ConnectServer(); - if(connect.IsSuccess){ - result = true; - OutputQueue.DEVP.offer(MessageFormat.format( "銆恵0}銆戣緭閫佺嚎plc杩炴帴鎴愬姛 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort())); - log.info("杈撻�佺嚎plc杩炴帴鎴愬姛 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); - } else { - OutputQueue.DEVP.offer(MessageFormat.format( "銆恵0}銆戣緭閫佺嚎plc杩炴帴澶辫触锛侊紒锛� ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort())); - log.error("杈撻�佺嚎plc杩炴帴澶辫触锛侊紒锛� ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); - } - melsecMcNet.ConnectClose(); - return result; - } - - /** - * 璇诲彇鐘舵�� ====> 鏁村潡plc - */ - private void read(){ - OperateResultExOne<byte[]> result = melsecMcNet.Read("D101", (short) 30); - if (result.IsSuccess) { - for (int i = 1; i <= 8; i++) { - StaProtocol staProtocol = station.get(i); - if (null == staProtocol) { - staProtocol = new StaProtocol(); - staProtocol.setSiteId(i); - station.put(i, staProtocol); - } - staProtocol.setWorkNo(melsecMcNet.getByteTransform().TransInt16(result.Content, (i-1)*2)); // 宸ヤ綔鍙� - staProtocol.setStaNo(melsecMcNet.getByteTransform().TransInt16(result.Content, (i-1)*2+40)); // 鐩爣绔� - } - } - OperateResultExOne<boolean[]> result1 = melsecMcNet.ReadBool("M800", (short) 64); - if (result1.IsSuccess) { - for (int i = 1; i <= 8; i++) { - StaProtocol staProtocol = station.get(i); - staProtocol.setAutoing(result1.Content[(i-1)*8]); // 鑷姩 - staProtocol.setLoading(result1.Content[(i-1)*8+1]); // 鏈夌墿 - staProtocol.setInEnable(result1.Content[(i-1)*8+2]); // 鍙叆 - staProtocol.setOutEnable(result1.Content[(i-1)*8+3]);// 鍙嚭 - staProtocol.setEmptyMk(result1.Content[(i-1)*8+4]); // 绌烘澘淇″彿 - } - } - if (result.IsSuccess && result1.IsSuccess) { - OutputQueue.DEVP.offer(MessageFormat.format("銆恵0}銆慬id:{1}] <<<<< 瀹炴椂鏁版嵁鏇存柊鎴愬姛",DateUtils.convert(new Date()), slave.getId())); - - // 鏍规嵁瀹炴椂淇℃伅鏇存柊鏁版嵁搴� - try { - List<BasDevp> basDevps = new ArrayList<>(); - for (int i = 1; i <= 8; i++) { - StaProtocol staProtocol = station.get(i); - basDevps.add(staProtocol.toSqlModel()); - } - BasDevpService basDevpService = SpringUtils.getBean(BasDevpService.class); - if (!basDevpService.updateBatchById(basDevps)) { - throw new Exception("鏇存柊鏁版嵁搴撴暟鎹け璐�"); - } - } catch (Exception e) { - OutputQueue.DEVP.offer(MessageFormat.format("銆恵0}銆戞洿鏂版暟鎹簱鏁版嵁澶辫触 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort())); - log.error("鏇存柊鏁版嵁搴撴暟鎹け璐� ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); - } - - } else { - OutputQueue.DEVP.offer(MessageFormat.format("銆恵0}銆戣鍙栬緭閫佺嚎plc鐘舵�佷俊鎭け璐� ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort())); - log.error("璇诲彇鍫嗗灈鏈簆lc鐘舵�佷俊鎭け璐� ===>> [id:{}] [ip:{}] [port:{}] ", slave.getId(), slave.getIp(), slave.getPort()); - } - } - - /** - * 鍐欏叆 ID+鐩爣绔� =====> 鍗曠珯鐐瑰啓鍏� - */ - private void write(StaProtocol staProtocol){ - if (null == staProtocol) { - return; - } - OperateResult write = melsecMcNet.Write("D10" + staProtocol.getSiteId(), staProtocol.getWorkNo()); // 宸ヤ綔鍙� - OperateResult write1 = melsecMcNet.Write("D12" + staProtocol.getSiteId(), staProtocol.getStaNo()); // 鐩爣绔� -// boolean[] status = new boolean[8]; -// status[0] = staProtocol.isAutoing(); -// status[1] = staProtocol.isLoading(); -// status[2] = staProtocol.isInEnable(); -// status[3] = staProtocol.isOutEnable(); -// status[4] = staProtocol.isEmptyMk(); -// OperateResult write2 = siemensS7Net.Write("DB100." + ((staProtocol.getSiteId() - 1) + 40), status); // 鐘舵�� - if (!write.IsSuccess || !write1.IsSuccess) { - OutputQueue.DEVP.offer(MessageFormat.format("銆恵0}銆戝啓鍏ヨ緭閫佺嚎绔欑偣鏁版嵁澶辫触銆傝緭閫佺嚎plc缂栧彿={1}锛岀珯鐐规暟鎹�={2}", slave.getId(), JSON.toJSON(staProtocol))); - log.error("鍐欏叆杈撻�佺嚎绔欑偣鏁版嵁澶辫触銆傝緭閫佺嚎plc缂栧彿={}锛岀珯鐐规暟鎹�={}", slave.getId(), JSON.toJSON(staProtocol)); - } else { - OutputQueue.DEVP.offer(MessageFormat.format("銆恵0}銆慬id:{1}] >>>>> 鍛戒护涓嬪彂锛� {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(staProtocol))); - } - } - - /** - * 鍐欏叆 ID =====> 鍗曠珯鐐瑰啓鍏� - */ - private void writeId(StaProtocol staProtocol){ - if (null == staProtocol) { - return; - } - OperateResult write = melsecMcNet.Write("D10" + staProtocol.getSiteId(), staProtocol.getWorkNo()); // 宸ヤ綔鍙� - if (!write.IsSuccess ) { - OutputQueue.DEVP.offer(MessageFormat.format("銆恵0}銆戝啓鍏ヨ緭閫佺嚎绔欑偣鏁版嵁澶辫触銆傝緭閫佺嚎plc缂栧彿={1}锛岀珯鐐规暟鎹�={2}", slave.getId(), JSON.toJSON(staProtocol))); - log.error("鍐欏叆杈撻�佺嚎绔欑偣鏁版嵁澶辫触銆傝緭閫佺嚎plc缂栧彿={}锛岀珯鐐规暟鎹�={}", slave.getId(), JSON.toJSON(staProtocol)); - } else { - OutputQueue.DEVP.offer(MessageFormat.format("銆恵0}銆慬id:{1}] >>>>> {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(staProtocol))); - } - } - - /** - * 鍐欏叆 鐩爣绔� =====> 鍗曠珯鐐瑰啓鍏� - */ - private void writeStaNo(StaProtocol staProtocol){ - if (null == staProtocol) { - return; - } - OperateResult write = melsecMcNet.Write("D12" + staProtocol.getSiteId(), staProtocol.getStaNo()); // 鐩爣绔� - if (!write.IsSuccess ) { - OutputQueue.DEVP.offer(MessageFormat.format("銆恵0}銆戝啓鍏ヨ緭閫佺嚎绔欑偣鏁版嵁澶辫触銆傝緭閫佺嚎plc缂栧彿={1}锛岀珯鐐规暟鎹�={2}", slave.getId(), JSON.toJSON(staProtocol))); - log.error("鍐欏叆杈撻�佺嚎绔欑偣鏁版嵁澶辫触銆傝緭閫佺嚎plc缂栧彿={}锛岀珯鐐规暟鎹�={}", slave.getId(), JSON.toJSON(staProtocol)); - } else { - OutputQueue.DEVP.offer(MessageFormat.format("銆恵0}銆慬id:{1}] >>>>> {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(staProtocol))); - } - } - - /** - * 蹇冭烦 - */ - private void heartbeat(){ - if (heartBeatVal == 1) { - heartBeatVal = 2; - } else { - heartBeatVal = 1; - } - OperateResult write = melsecMcNet.Write("DB100.50", heartBeatVal); - if (!write.IsSuccess) { - log.error("杈撻�佺嚎plc缂栧彿={} 蹇冭烦澶辫触", slave.getId()); - } - } - - @Override - public void close() { - melsecMcNet.ConnectClose(); - } - - public static void main(String[] args) { - DevpSlave slave = new DevpSlave(); - slave.setIp("192.168.3.65"); - slave.setPort(6000); - DevpThread devpThread = new DevpThread(slave); - devpThread.connect(); - devpThread.read(); - // 鍐� - StaProtocol staProtocol = devpThread.getStation().get(1); - staProtocol.setWorkNo((short) 232); - staProtocol.setStaNo((short) 6); - staProtocol.setAutoing(true); - staProtocol.setEmptyMk(true); - staProtocol.setInEnable(true); - devpThread.write(staProtocol); - System.out.println("----------------------------------------"); - // 璇� - devpThread.read(); - System.out.println(JSON.toJSONString(devpThread.station)); - - } + void setPakMk(Integer siteId, boolean pakMk); } -- Gitblit v1.9.1