| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372 |
- package com.mes.ui;
- import com.alibaba.fastjson2.JSON;
- import com.github.xingshuangs.iot.protocol.s7.service.S7PLC;
- import com.mes.util.DateLocalUtils;
- import com.mes.util.JdbcUtils;
- import java.util.ArrayList;
- import java.util.List;
- public class PlcUtil {
- private static final String DB = "DB200";
- private static final String A_ENABLE = DB + ".0.1";
- private static final String B_ENABLE = DB + ".38.0";
- private static final String A_STATUS = DB + ".34";
- private static final String B_STATUS = DB + ".72";
- private static final String A_POSITION = DB + ".156.3";
- private static final String B_POSITION = DB + ".156.4";
- private static final WeldPoint[] WELD_POINTS = new WeldPoint[]{
- new WeldPoint("R1", 76.1, 78, 80, 84, 88, 92),
- new WeldPoint("R2", 96.2, 98, 100, 104, 108, 112),
- new WeldPoint("R3", 116.2, 118, 120, 124, 128, 132),
- new WeldPoint("R4", 136.2, 138, 140, 144, 148, 152)
- };
- // 缓存上一次的参数值,用于检测变化
- private static String lastParamValues = null;
- public static void getParamDyDl(S7PLC s7PLC) {
- if (MesClient.tjFlaga == 2 || MesClient.tjFlagb == 2) {
- List<String> values = readWeldParams(s7PLC);
- String recordTime = DateLocalUtils.getCurrentTime();
- values.add("time=" + recordTime);
- MesClient.hjparams.add(String.join("|", values));
- refreshParamText(values);
- // 只在参数变化时打印(排除 time 字段)
- String currentParams = String.join("|", values.subList(0, values.size() - 1));
- if (!currentParams.equals(lastParamValues)) {
- System.out.println("cmt:" + String.join("|", values));
- lastParamValues = currentParams;
- }
- if (MesClient.hjparams.size() == 60) {
- saveCurrentParams();
- }
- } else if (MesClient.hjparams.size() > 0) {
- saveCurrentParams();
- lastParamValues = null; // 重置缓存
- }
- }
- private static List<String> readWeldParams(S7PLC s7PLC) {
- List<String> values = new ArrayList<>();
- for (WeldPoint point : WELD_POINTS) {
- boolean finished = readBoolean(s7PLC, point.finishAddress());
- int seq = readInt16(s7PLC, point.seqAddress());
- float current = readFloat32(s7PLC, point.currentAddress());
- float voltage = readFloat32(s7PLC, point.voltageAddress());
- float wireSpeed = readFloat32(s7PLC, point.wireSpeedAddress());
- float weldSpeed = readFloat32(s7PLC, point.weldSpeedAddress());
- values.add(point.name + ":finish=" + finished);
- values.add(point.name + ":seq=" + seq);
- values.add(point.name + ":current=" + current);
- values.add(point.name + ":voltage=" + voltage);
- values.add(point.name + ":wireSpeed=" + wireSpeed);
- values.add(point.name + ":weldSpeed=" + weldSpeed);
- }
- return values;
- }
- private static void refreshParamText(List<String> values) {
- float r1Current = valueOf(values, "R1:current=");
- float r1Voltage = valueOf(values, "R1:voltage=");
- float r1WireSpeed = valueOf(values, "R1:wireSpeed=");
- float r1WeldSpeed = valueOf(values, "R1:weldSpeed=");
- int r1Seq = (int) valueOf(values, "R1:seq=");
- if ("B".equals(MesClient.curFlag)) {
- MesClient.param21.setText(String.valueOf(r1Voltage));
- MesClient.param22.setText(String.valueOf(r1Current));
- MesClient.param23.setText(String.valueOf(r1WireSpeed));
- MesClient.param24.setText(String.valueOf(r1Seq));
- MesClient.param25.setText(String.valueOf(r1WeldSpeed));
- } else {
- MesClient.param1.setText(String.valueOf(r1Voltage));
- MesClient.param2.setText(String.valueOf(r1Current));
- MesClient.param3.setText(String.valueOf(r1WireSpeed));
- MesClient.param4.setText(String.valueOf(r1Seq));
- MesClient.param5.setText(String.valueOf(r1WeldSpeed));
- }
- }
- private static float valueOf(List<String> values, String prefix) {
- for (String value : values) {
- if (value.startsWith(prefix)) {
- try {
- return Float.parseFloat(value.substring(prefix.length()));
- } catch (Exception ignored) {
- return 0;
- }
- }
- }
- return 0;
- }
- private static void saveCurrentParams() {
- try {
- String oprno = MesClient.mes_gw + "A";
- String sn = MesClient.curSna;
- if ("B".equals(MesClient.curFlag)) {
- oprno = MesClient.mes_gw + "B";
- sn = MesClient.curSnb;
- }
- // 检查工件码和参数列表都不为空
- if (sn != null && !sn.trim().isEmpty()) {
- if (MesClient.hjparams != null && !MesClient.hjparams.isEmpty()) {
- JdbcUtils.insertCmtData(oprno, MesClient.mes_line_sn, sn, JSON.toJSONString(MesClient.hjparams));
- System.out.println("cmt:" + MesClient.curFlag + " sn:" + sn + " params_count:" + MesClient.hjparams.size());
- } else {
- System.out.println("weld params is empty, skip save (sn:" + sn + ")");
- }
- } else {
- System.out.println("workpiece sn is empty, skip cmt params save");
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- MesClient.hjparams = new ArrayList<>();
- }
- }
- public static Boolean getAStart(S7PLC s7PLC) {
- return getWeldStatus(s7PLC, "A") == 1;
- }
- public static Boolean getAFinish(S7PLC s7PLC) {
- return getWeldStatus(s7PLC, "A") == 2;
- }
- public static void getStatusA(S7PLC s7PLC) {
- int status = getWeldStatus(s7PLC, "A");
- if (MesClient.tjFlaga == 1) {
- if (status == 1) {
- MesClient.curFlag = "A";
- MesClient.tjFlaga = 2;
- MesClient.pxstatus1.setText("A:\u8bbe\u5907\u8fd0\u884c\u4e2d");
- if (MesClient.curSna == null || MesClient.curSna.isEmpty()) {
- MesClient.curSna = MesClient.product_sn.getText();
- }
- changeEnable(s7PLC, false, "A");
- } else if (!getEnable(s7PLC, "A")) {
- changeEnable(s7PLC, true, "A");
- }
- } else if (MesClient.tjFlaga == 2 && status == 2) {
- finishA(s7PLC);
- }
- }
- private static void finishA(S7PLC s7PLC) {
- MesClient.tjFlaga = 3;
- MesClient.pxstatus1.setText("A:\u8bbe\u5907\u8fd0\u884c\u7ed3\u675f,\u63d0\u4ea4\u7ed3\u679c\u4e2d");
- MesClient.finish_ok_bt.setEnabled(true);
- changeEnable(s7PLC, false, "A");
- ackWeldStatus(s7PLC, "A");
- saveCurrentParams();
- Boolean sendret = DataUtil.sendQuality(MesClient.nettyClient, MesClient.product_sn.getText(), "OK", MesClient.user20, "A");
- if (!sendret) {
- MesClient.pxstatus1.setText("A:\u7ed3\u679c\u4e0a\u4f20MES\u5931\u8d25");
- MesClient.tjStatusa = 1;
- }
- }
- public static Boolean getBStart(S7PLC s7PLC) {
- return getWeldStatus(s7PLC, "B") == 1;
- }
- public static Boolean getBFinish(S7PLC s7PLC) {
- return getWeldStatus(s7PLC, "B") == 2;
- }
- public static void getStatusB(S7PLC s7PLC) {
- int status = getWeldStatus(s7PLC, "B");
- if (MesClient.tjFlagb == 1) {
- if (status == 1) {
- MesClient.curFlag = "B";
- MesClient.tjFlagb = 2;
- MesClient.pxstatus2.setText("B:\u8bbe\u5907\u8fd0\u884c\u4e2d");
- if (MesClient.curSnb == null || MesClient.curSnb.isEmpty()) {
- MesClient.curSnb = MesClient.product_sn2.getText();
- }
- changeEnable(s7PLC, false, "B");
- } else if (!getEnable(s7PLC, "B")) {
- changeEnable(s7PLC, true, "B");
- }
- } else if (MesClient.tjFlagb == 2 && status == 2) {
- finishB(s7PLC);
- }
- }
- private static void finishB(S7PLC s7PLC) {
- MesClient.tjFlagb = 3;
- MesClient.pxstatus2.setText("B:\u8bbe\u5907\u8fd0\u884c\u7ed3\u675f,\u63d0\u4ea4\u7ed3\u679c\u4e2d");
- MesClient.finish_ng_bt.setEnabled(true);
- changeEnable(s7PLC, false, "B");
- ackWeldStatus(s7PLC, "B");
- saveCurrentParams();
- Boolean sendret = DataUtil.sendQuality(MesClient.nettyClient, MesClient.product_sn2.getText(), "OK", MesClient.user20, "B");
- if (!sendret) {
- MesClient.pxstatus2.setText("B:\u7ed3\u679c\u4e0a\u4f20MES\u5931\u8d25");
- MesClient.tjStatusb = 1;
- }
- }
- public static Boolean changeEnable(S7PLC s7PLC, Boolean result) {
- return changeEnable(s7PLC, result, getCurAside(s7PLC));
- }
- public static Boolean changeEnable(S7PLC s7PLC, Boolean result, String side) {
- try {
- s7PLC.writeBoolean(enableAddress(side), result);
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
- public static Boolean getEnable(S7PLC s7PLC) {
- return getEnable(s7PLC, getCurAside(s7PLC));
- }
- public static Boolean getEnable(S7PLC s7PLC, String side) {
- return readBoolean(s7PLC, enableAddress(side));
- }
- public static String getReserveSide(S7PLC s7PLC) {
- int aStatus = getWeldStatus(s7PLC, "A");
- int bStatus = getWeldStatus(s7PLC, "B");
- if (aStatus == 1 || MesClient.tjFlaga == 2) {
- return "B";
- }
- if (bStatus == 1 || MesClient.tjFlagb == 2) {
- return "A";
- }
- return getCurAside(s7PLC);
- }
- public static String getCurAside(S7PLC s7PLC) {
- try {
- boolean aPosition = s7PLC.readBoolean(A_POSITION);
- boolean bPosition = s7PLC.readBoolean(B_POSITION);
- if (aPosition) {
- return "A";
- }
- if (bPosition) {
- return "B";
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- if (MesClient.lastDetectedPage != null && !MesClient.lastDetectedPage.isEmpty()) {
- return MesClient.lastDetectedPage;
- }
- return "A";
- }
- public static int getProgranNoA(S7PLC s7PLC) {
- return 0;
- }
- public static int getProgranNoB(S7PLC s7PLC) {
- return 0;
- }
- private static int getWeldStatus(S7PLC s7PLC, String side) {
- return readInt16(s7PLC, statusAddress(side));
- }
- private static void ackWeldStatus(S7PLC s7PLC, String side) {
- try {
- s7PLC.writeInt16(statusAddress(side), (short) 0);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- private static String enableAddress(String side) {
- return "B".equals(side) ? B_ENABLE : A_ENABLE;
- }
- private static String statusAddress(String side) {
- return "B".equals(side) ? B_STATUS : A_STATUS;
- }
- private static boolean readBoolean(S7PLC s7PLC, String address) {
- try {
- return s7PLC.readBoolean(address);
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
- private static int readInt16(S7PLC s7PLC, String address) {
- try {
- return s7PLC.readInt16(address);
- } catch (Exception e) {
- e.printStackTrace();
- return 0;
- }
- }
- private static float readFloat32(S7PLC s7PLC, String address) {
- try {
- return s7PLC.readFloat32(address);
- } catch (Exception e) {
- e.printStackTrace();
- return 0;
- }
- }
- private static class WeldPoint {
- private final String name;
- private final double finishOffset;
- private final int seqOffset;
- private final int currentOffset;
- private final int voltageOffset;
- private final int wireSpeedOffset;
- private final int weldSpeedOffset;
- private WeldPoint(String name, double finishOffset, int seqOffset, int currentOffset, int voltageOffset,
- int wireSpeedOffset, int weldSpeedOffset) {
- this.name = name;
- this.finishOffset = finishOffset;
- this.seqOffset = seqOffset;
- this.currentOffset = currentOffset;
- this.voltageOffset = voltageOffset;
- this.wireSpeedOffset = wireSpeedOffset;
- this.weldSpeedOffset = weldSpeedOffset;
- }
- private String finishAddress() {
- return DB + "." + String.valueOf(finishOffset);
- }
- private String seqAddress() {
- return DB + "." + seqOffset;
- }
- private String currentAddress() {
- return DB + "." + currentOffset;
- }
- private String voltageAddress() {
- return DB + "." + voltageOffset;
- }
- private String wireSpeedAddress() {
- return DB + "." + wireSpeedOffset;
- }
- private String weldSpeedAddress() {
- return DB + "." + weldSpeedOffset;
- }
- }
- }
|