| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231 |
- package com.mes.ui;
- import com.alibaba.fastjson2.JSON;
- import com.alibaba.fastjson2.JSONObject;
- import com.github.xingshuangs.iot.protocol.modbus.service.ModbusTcp;
- import com.mes.component.MesRadio;
- import com.mes.component.MesWebView;
- import com.mes.netty.NettyClient;
- import com.mes.util.DateLocalUtils;
- import com.mes.util.JdbcUtils;
- import javafx.embed.swing.JFXPanel;
- import javax.swing.*;
- import javax.swing.border.EmptyBorder;
- import javax.swing.event.ChangeEvent;
- import javax.swing.event.ChangeListener;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.util.*;
- import java.util.List;
- import java.util.Timer;
- public class MesClient extends JFrame {
- private static String Drivde = "org.sqlite.JDBC";
- public static int mes_auth = 0; // 权限级别 0=无权限 1=操作工人 2=管理员
- public static String mes_gw = ""; // 工位号
- public static String mes_gw_des = ""; // 工位名称
- public static String mes_server_ip = ""; // 服务器IP地址
- public static int mes_tcp_port = 3000; // TCP服务端口
- public static int mes_heart_beat_cycle = 10; // 心跳周期
- public static int mes_heart_icon_cycle = 1;
- public static String mes_line_sn = ""; // 产线编号
- public static boolean mes_shield_flag = false; // MES 屏蔽标志
- //TCP连接
- public static NettyClient nettyClient;
- //TCP连接状态
- public static boolean tcp_connect_flag = false;
- //TCP连接请求
- public static boolean connect_request_flag = false;
- //session
- public static String sessionid = "";
- public static JPanel contentPane;
- public static MesClient mesClientFrame;
- public static JTabbedPane tabbedPane;
- public static JScrollPane indexScrollPaneA;
- public static JScrollPane searchScrollPane;
- public static JScrollPane searchScrollPaneDj;
- public static Boolean check_quality_result = false;
- public static Integer work_status = 0;
- public static JButton heart_beat_menu;
- public static JButton status_menu;
- public static JButton user_menu;
- public static JButton finish_ok_bt;
- public static JButton finish_ng_bt;
- public static JTextField product_sn;
- public static String user20 = "";
- public static JFrame welcomeWin;
- public static MesWebView jfxPanel2 = null;
- public static JPanel indexPanelD;
- public static MesRadio mesRadioHj;
- public static JTextField param1;
- public static JTextField param2;
- public static JTextField param3;
- public static JTextField param4;
- // A/B枪实时拉力、行程显示
- public static JTextField param5;
- public static JTextField param6;
- public static JTextField param7;
- public static JTextField param8;
- public static ModbusTcp plcA;
- public static ModbusTcp plcB;
- public static String curIpA = "";
- public static String curIpB = "";
- // public static ModbusTcp plcA = new ModbusTcp(1, "192.168.1.27");
- // public static ModbusTcp plcB = new ModbusTcp(1, "192.168.1.28");
- public static Timer cjTimer;
- public static Timer cjTimer2;
- public static Timer cjTimer3;
- public static Short aSetNum = 41;
- // public static Short aSetNum = 49;
- public static Short sortA = 0;
- public static Short aMax = 0;
- public static Short aFinish = 0;
- public static List<Map> alist = new ArrayList<>();
- public static Short bSetNum = 14;
- // public static Short bSetNum = 27;
- public static Short sortB = 0;
- public static Short bMax = 0;
- public static List<Map> blist = new ArrayList<>();
- public static Short deviceControl = 0; // 0=本地 1=远程
- public static Integer tjStatus = 0; // 1=提交失败
- public static JPanel indexPanelB;
- public static MesWebView jfxPanel = null;
- public static JPanel indexPanelC;
- // 铆接参数区间配置(内存缓存)
- public static List<RivetParamRange> rivetParamRangesA = new ArrayList<>();
- public static List<RivetParamRange> rivetParamRangesB = new ArrayList<>();
- public static String tjFlagTextErr = "结果上传MES失败,请重试";
- public static String curSn = "";
- // 后台拉码定时器
- public static Timer getSnBeatTimer;
- // 已提交质量询问、等待TCP应答的工件码
- public static String backendPollingSn = "";
- // 质量检查未通过的后台码,避免重复轮询
- public static String lastRejectedBackendSn = "";
- public static JTable table;
- public static Object[] columnNames = { "物料名称", "绑定批次", "剩余次数", "操作" };
- public static Object[][] rowData = null;
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- @Override
- public void run() {
- try{
- // 读文件配置
- readProperty();
- JdbcUtils.getConn();
- initLocalConfig();
- // 显示界面
- mesClientFrame = new MesClient();
- mesClientFrame.setVisible(false);
- welcomeWin = new LoginFarme();
- welcomeWin.setVisible(true);
- getPlcParam();
- upParams();
- setDeviceStart();
- getMaterailData();
- }catch (Exception e){
- e.printStackTrace();
- }
- }
- });
- }
- public static java.util.Timer cjTimer4;
- public static void setDeviceStart() {
- if(cjTimer4!=null) {
- cjTimer4.cancel();
- }
- cjTimer4 = new Timer();
- cjTimer4.schedule(new TimerTask() {
- public void run() {
- try{
- // MES屏蔽时仅维持拉铆枪运行,不做空闲关机等干预
- if (mes_shield_flag) {
- if (work_status == 1) {
- keepGunRunningIfNeeded(plcA, sortA, aMax);
- keepGunRunningIfNeeded(plcB, sortB, bMax);
- }
- return;
- }
- if(work_status == 1){
- List<Boolean> yxstatus = plcA.readCoil(3128,1);
- if(yxstatus.size() >= 1 && !yxstatus.get(0)){
- if(!MesClient.curSn.isEmpty() && MesClient.aMax > 0 && MesClient.aMax == MesClient.sortA){
- ModbusUtil.setPowerOff(plcA);
- }else{
- ModbusUtil.setPowerOn(plcA);
- }
- }
- List<Boolean> yxstatusb = plcB.readCoil(3128,1);
- if(yxstatusb.size() >= 1 && !yxstatusb.get(0)){
- if(!MesClient.curSn.isEmpty() && MesClient.bMax > 0 && MesClient.bMax == MesClient.sortB){
- ModbusUtil.setPowerOff(plcB);
- }else{
- ModbusUtil.setPowerOn(plcB);
- }
- }
- }else{
- List<Boolean> yxstatus = plcA.readCoil(3128,1);
- if(yxstatus.size() >= 1 && yxstatus.get(0)){
- ModbusUtil.setPowerOff(plcA);
- }
- List<Boolean> yxstatusb = plcB.readCoil(3128,1);
- if(yxstatusb.size() >= 1 && yxstatusb.get(0)){
- ModbusUtil.setPowerOff(plcB);
- }
- }
- }catch (Exception e){
- e.printStackTrace();
- }
- }
- }, 1000,1000);
- }
- // MES屏蔽期间:任务未完成时保持拉铆枪运行
- private static void keepGunRunningIfNeeded(ModbusTcp plc, Short sort, Short max) {
- try {
- List<Boolean> yxstatus = plc.readCoil(3128, 1);
- if (yxstatus.size() >= 1 && !yxstatus.get(0)) {
- if (!curSn.isEmpty() && max > 0 && max != sort) {
- ModbusUtil.setPowerOn(plc);
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- //读配置文件
- private static void readProperty() throws IOException{
- String enconding = "UTF-8";
- InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
- Properties pro = new Properties();
- BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
- pro.load(br);
- mes_gw = pro.getProperty("mes.gw");
- // mes_gw_des = pro.getProperty("mes.gw_des");
- mes_server_ip = pro.getProperty("mes.server_ip");
- mes_tcp_port = Integer.parseInt(pro.getProperty("mes.tcp_port"));
- mes_heart_beat_cycle = Integer.parseInt(pro.getProperty("mes.heart_beat_cycle"));
- mes_line_sn = pro.getProperty("mes.line_sn");
- mes_gw_des = OprnoUtil.getGwDes(mes_line_sn,mes_gw);
- System.out.println(mes_gw + ";" + mes_gw_des + ";" + mes_server_ip + ";" + mes_tcp_port + ";" + mes_heart_beat_cycle);
- }
- private static void initLocalConfig() {
- Map<String, Object> config = JdbcUtils.getConfig();
- if (config.size() > 0) {
- aSetNum = (Short) config.get("a_set_num");
- bSetNum = (Short) config.get("b_set_num");
- curIpA = (String) config.get("plc_ip_a");
- curIpB = (String) config.get("plc_ip_b");
- plcA = new ModbusTcp(1, curIpA);
- plcB = new ModbusTcp(1, curIpB);
-
- // 初始化工作面板显示的预设数量
- if (param1 != null) param1.setText(String.valueOf(aSetNum));
- if (param3 != null) param3.setText(String.valueOf(bSetNum));
-
- System.out.println("加载本地配置成功:aSetNum=" + aSetNum + ", bSetNum=" + bSetNum + ", plcA=" + curIpA + ", plcB=" + curIpB);
- } else {
- // 兜底默认值
- aSetNum = 41;
- bSetNum = 14;
- curIpA = "192.168.1.7";
- curIpB = "192.168.1.8";
- plcA = new ModbusTcp(1, curIpA);
- plcB = new ModbusTcp(1, curIpB);
- System.out.println("未找到本地配置,使用默认值");
- }
- loadRivetParamConfig();
- }
- // 加载铆接参数区间配置
- public static void loadRivetParamConfig() {
- rivetParamRangesA = JdbcUtils.getRivetParams("A");
- rivetParamRangesB = JdbcUtils.getRivetParams("B");
- }
- public static void getPlcParam() {
- if(cjTimer!=null) {
- cjTimer.cancel();
- }
- cjTimer = new Timer();
- cjTimer.schedule(new TimerTask() {
- public void run() {
- try{
- if(work_status == 1 && !mes_shield_flag){
- ModbusUtil.getDataA(plcA);
- }
- }catch (Exception e){
- e.printStackTrace();
- }
- try{
- if(work_status == 1 && !mes_shield_flag){
- ModbusUtil.getDataB(plcB);
- }
- }catch (Exception e){
- e.printStackTrace();
- }
- }
- }, 1000,500);
- }
- public static void getPlcSn() {
- if(cjTimer3!=null) {
- cjTimer3.cancel();
- }
- cjTimer3 = new Timer();
- cjTimer3.schedule(new TimerTask() {
- public void run() {
- try{
- String cSn = ModbusUtil.getSn(plcA);
- if(!cSn.isEmpty()){
- if(curSn.isEmpty()){
- submitSnForQualityCheck(cSn, true);
- }else if(!curSn.isEmpty() && !cSn.equals(curSn)){
- resetScanA();
- }
- }
- }catch (Exception e){
- e.printStackTrace();
- }
- }
- }, 1000,1000);
- }
- public static void upParams() {
- if(cjTimer2!=null) {
- cjTimer2.cancel();
- }
- cjTimer2 = new Timer();
- cjTimer2.schedule(new TimerTask() {
- public void run() {
- try{
- List<ProdReq> prods = JdbcUtils.getProds();
- System.out.println("prods:"+ JSON.toJSONString(prods));
- if(prods.size() > 0){
- // 定时上传参数
- JSONObject retObj = DataUtil.upParams(JSON.toJSONString(prods));
- if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
- // 更新sqlite已同步
- for(ProdReq prodReq:prods){
- JdbcUtils.updateProdSync(prodReq.getId(),1);
- }
- }
- }
- }catch (Exception e){
- e.printStackTrace();
- }
- }
- }, 1000,30*1000);
- }
- // 初始化TCP
- public static void initTcpConnection() {
- try {
- if(nettyClient==null) {
- //初始化TCP连接
- nettyClient = new NettyClient();
- //TCP连接状态
- tcp_connect_flag = false;
- //设置TCP请求状态
- connect_request_flag = true;
- DataUtil.synrTcp(nettyClient,mes_gw);
- }
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- //启动心跳包程序
- public static java.util.Timer heartBeatTimer;
- public static java.util.Timer heartBeatIconTimer;
- public static boolean iconREDFlag = true;
- public static void startHeartBeatTimer() {
- if(heartBeatTimer!=null) {
- heartBeatTimer.cancel();
- }
- heartBeatTimer = new java.util.Timer();
- heartBeatTimer.schedule(new TimerTask() {
- public void run() {
- if(nettyClient!=null&&tcp_connect_flag) {
- //System.out.println("发送心跳报文");
- DataUtil.heartBeat(nettyClient,mes_gw);
- heart_beat_menu.setText(DateLocalUtils.getCurrentTime());
- }
- }
- }, 100,mes_heart_beat_cycle*1000);
- //心跳显示图标
- if(heartBeatIconTimer!=null) {
- heartBeatIconTimer.cancel();
- }
- heartBeatIconTimer = new Timer();
- heartBeatIconTimer.schedule(new TimerTask() {
- public void run() {
- if(tcp_connect_flag) {
- if(iconREDFlag) {
- heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png")));
- iconREDFlag = false;
- }else {
- heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/green_dot.png")));
- iconREDFlag = true;
- }
- heart_beat_menu.setText(DateLocalUtils.getCurrentTime());
- heart_beat_menu.repaint();
- }else {
- heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png")));
- heart_beat_menu.setText(DateLocalUtils.getCurrentTime());
- heart_beat_menu.repaint();
- //若未连接则尝试连接
- if(nettyClient!=null&&!tcp_connect_flag){
- System.out.println("TCP已断开");
- //TCP重新同步连接
- if(!connect_request_flag) {
- //System.out.println("TCP重新同步连接");
- //设置TCP请求状态,只重新同步连接一次
- connect_request_flag = true;
- DataUtil.synrTcp(nettyClient,mes_gw);
- }
- }
- }
- }
- }, 100,mes_heart_icon_cycle*1000);
- }
- //设置tcp连接状态显示
- public static void setTcpStatus() {
- if(tcp_connect_flag) {
- status_menu.setText("已连接MES服务器");
- heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png")));
- heart_beat_menu.repaint();
- }else {
- status_menu.setText("未连接MES服务器");
- heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png")));
- heart_beat_menu.repaint();
- }
- }
- public static void initWarehouseData(){
- resetScanA();
- }
- // 启动后台拉码轮询
- public static void startGetCurSn() {
- if (getSnBeatTimer != null) {
- getSnBeatTimer.cancel();
- }
- getSnBeatTimer = new Timer();
- getSnBeatTimer.schedule(new TimerTask() {
- @Override
- public void run() {
- pollBackendSn();
- }
- }, 100, 1000);
- }
- // 停止后台拉码轮询
- public static void stopGetCurSn() {
- if (getSnBeatTimer != null) {
- getSnBeatTimer.cancel();
- getSnBeatTimer = null;
- }
- }
- // 清除后台拉码中间状态
- public static void clearBackendSnState() {
- backendPollingSn = "";
- lastRejectedBackendSn = "";
- }
- // 清除后台工位工件码
- public static void clearBackendCurSn() {
- if (sessionid != null && !sessionid.isEmpty()) {
- DataUtil.delCurSn(mes_gw, mes_server_ip, mes_line_sn);
- }
- }
- // 轮询后台工件码
- public static void pollBackendSn() {
- try {
- if (mes_shield_flag) {
- return;
- }
- if (work_status == 1) {
- return;
- }
- if (sessionid == null || sessionid.isEmpty()) {
- return;
- }
- if (!backendPollingSn.isEmpty()) {
- return;
- }
- if (!tcp_connect_flag) {
- return;
- }
- JSONObject result = DataUtil.getCurSn(mes_gw, mes_server_ip, mes_line_sn);
- if (result == null || !result.containsKey("data")) {
- return;
- }
- String data = result.getString("data");
- if (data == null || data.trim().isEmpty()) {
- return;
- }
- data = data.trim();
- if (data.equals(lastRejectedBackendSn)) {
- return;
- }
- if (!curSn.isEmpty() && data.equals(curSn.trim())) {
- return;
- }
- submitSnForQualityCheck(data, true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- // 提交工件码进行质量询问(手动扫码与后台拉码共用,HTTP pccheck)
- public static boolean submitSnForQualityCheck(String scanBarcode, boolean fromBackend) {
- product_sn.setText(scanBarcode);
- curSn = scanBarcode;
- backendPollingSn = scanBarcode.trim();
- lastRejectedBackendSn = "";
- mesClientFrame.repaint();
- if (sessionid == null || sessionid.isEmpty()) {
- backendPollingSn = "";
- setMenuStatus("请先登录", -1);
- return false;
- }
- String user = user_menu.getText().trim();
- JSONObject retObj = DataUtil.checkQuality(scanBarcode.trim(), user);
- if (retObj != null && retObj.get("result") != null
- && retObj.get("result").toString().equalsIgnoreCase("true")) {
- backendPollingSn = "";
- status_menu.setForeground(Color.GREEN);
- check_quality_result = true;
- work_status = 1;
- tjStatus = 0;
- finish_ok_bt.setEnabled(true);
- finish_ng_bt.setEnabled(true);
- String msg = retObj.getString("message");
- if (fromBackend) {
- setMenuStatus(msg != null && !msg.isEmpty() ? msg : "已从后台获取工件码,可以加工", 0);
- } else {
- setMenuStatus(msg != null && !msg.isEmpty() ? msg : "该工件可以加工", 0);
- }
- ModbusUtil.prepareAndStartWork();
- return true;
- }
- check_quality_result = false;
- work_status = 0;
- backendPollingSn = "";
- if (!curSn.isEmpty()) {
- lastRejectedBackendSn = curSn.trim();
- }
- clearBackendCurSn();
- product_sn.setText("");
- curSn = "";
- if (retObj == null || retObj.get("result") == null) {
- setMenuStatus("质量检查请求失败,请重试", -1);
- } else if (retObj.get("result").toString().equalsIgnoreCase("false")) {
- String msg = retObj.getString("message");
- setMenuStatus(msg != null && !msg.isEmpty() ? msg : "该工件不能加工", -1);
- } else {
- setMenuStatus("质量检查失败,请重试", -1);
- }
- ModbusUtil.setPowerOff(plcA);
- ModbusUtil.setPowerOff(plcB);
- return false;
- }
- // 提交质量结果(HTTP pcresult)
- public static void submitQualityResult(String result) {
- if (work_status != 1 || !check_quality_result) {
- return;
- }
- String sn = product_sn.getText().trim();
- if (sn.isEmpty()) {
- setMenuStatus("工件码为空,请重试", -1);
- return;
- }
- if (sessionid == null || sessionid.isEmpty()) {
- setMenuStatus("请先登录", -1);
- return;
- }
- String user = user_menu.getText().trim();
- JSONObject retObj = DataUtil.sendQuality(sn, result, user);
- if (retObj != null && retObj.get("result") != null
- && retObj.get("result").toString().equalsIgnoreCase("true")) {
- resetScanA();
- setMenuStatus("结果提交成功,等待下一件", 0);
- } else {
- tjStatus = 0;
- if (retObj == null || retObj.get("result") == null) {
- setMenuStatus("结果提交失败,请重试", -1);
- } else if (retObj.get("result").toString().equalsIgnoreCase("false")) {
- String msg = retObj.getString("message");
- setMenuStatus(msg != null && !msg.isEmpty() ? msg : "结果提交失败,请重试", -1);
- } else {
- setMenuStatus("结果提交失败,请重试", -1);
- }
- }
- }
- public static void resetScanA() {
- // 闭环:提交完成或刷新时清除后台工位码
- clearBackendCurSn();
- clearBackendSnState();
- work_status = 0;
- check_quality_result = false;
- MesClient.finish_ok_bt.setEnabled(false);
- MesClient.finish_ng_bt.setEnabled(false);
- product_sn.setText("");
- curSn = "";
- MesClient.setMenuStatus("等待后台工件码",0);
- MesClient.aMax = 0;
- MesClient.bMax = 0;
- MesClient.aFinish = 0;
- MesClient.alist = new ArrayList<>();
- MesClient.blist = new ArrayList<>();
- MesClient.sortB = 0;
- MesClient.sortA = 0;
- MesClient.param1.setText(String.valueOf(aSetNum));
- MesClient.param2.setText("");
- MesClient.param3.setText(String.valueOf(bSetNum));
- MesClient.param4.setText("");
- resetForceStrokeDisplay();
- deviceControl = ModbusUtil.getControlModel(plcA);
- if(deviceControl == 1){
- }
- // plcA.writeCoil(3128,false);
- // plcB.writeCoil(3128,false);
- ModbusUtil.setPowerOff(MesClient.plcA); // 远程关机
- ModbusUtil.setPowerOff(MesClient.plcB); // 远程关机
- ModbusUtil.setTask(MesClient.plcA, MesClient.aSetNum, "A");
- ModbusUtil.setTask(MesClient.plcB, MesClient.bSetNum, "B");
- updateMaterailData();
- }
- public static void resetScanB() {
- }
- //获取用户20位
- public static void getUser() {
- user20 = user_menu.getText().toString();
- String space_tmp1 = "";
- if(user20.length()<20) {
- for(int i=0;i<20-user20.length();i++) {
- space_tmp1 = space_tmp1 + " ";
- }
- }
- user20 = user20 + space_tmp1;
- }
- //获取barcode内容36位
- public static String getBarcode(String barcodeTmp) {
- String barcodeRet = barcodeTmp;
- if(barcodeTmp.equalsIgnoreCase("")) {
- return "";
- }else {
- if(barcodeTmp.length()<36) {
- String space = "";
- for(int i=0;i<36-barcodeTmp.length();i++) {
- space = space + " ";
- }
- barcodeRet = barcodeTmp + space;
- }
- }
- return barcodeRet;
- }
- // 重置拉力、行程显示
- public static void resetForceStrokeDisplay() {
- if (param5 != null) param5.setText("-");
- if (param6 != null) param6.setText("-");
- if (param7 != null) param7.setText("-");
- if (param8 != null) param8.setText("-");
- }
- public static void logoff() {
- // welcomeWin.setVisible(true);
- stopGetCurSn();
- clearBackendCurSn();
- clearBackendSnState();
- mesClientFrame.setVisible(false);
- nettyClient = null;
- // if(heartBeatTimer!=null) {
- // heartBeatTimer.cancel();
- // }
- // if(heartBeatIconTimer!=null) {
- // heartBeatIconTimer.cancel();
- // }
- tcp_connect_flag = false;
- connect_request_flag = false;
- }
- public static int lastSelectedIndex = 0;
- public MesClient() {
- setIconImage(Toolkit.getDefaultToolkit().getImage(MesClient.class.getResource("/bg/logo.png")));
- setTitle("MES系统客户端:HEV-"+mes_gw + "- " + mes_gw_des);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(0, 0, 1024, 768);
- JMenuBar menuBar = new JMenuBar();
- menuBar.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 26));
- setJMenuBar(menuBar);
- JMenu fileMenu = new JMenu("用户");
- fileMenu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/user.png")));
- fileMenu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
- menuBar.add(fileMenu);
- JMenuItem exitMenuItem = new JMenuItem("退出");
- exitMenuItem.setIcon(new ImageIcon(MesClient.class.getResource("/bg/logoff.png")));
- exitMenuItem.setFont(new Font("微软雅黑", Font.PLAIN, 22));
- fileMenu.add(exitMenuItem);
- exitMenuItem.addMouseListener(new MouseAdapter() {
- @Override
- public void mousePressed(MouseEvent e) {//只能检测到mousePressed事件
- super.mouseClicked(e);
- //dispose();
- logoff();
- }
- });
- JMenu settingMenu = new JMenu("设置");
- //settingMenu.setVisible(false);
- settingMenu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/menu_setting.png")));
- settingMenu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
- menuBar.add(settingMenu);
- JMenuItem resetTcpMenu = new JMenuItem("\u91CD\u8FDEMES");
- resetTcpMenu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/reset_logo.png")));
- resetTcpMenu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
- resetTcpMenu.addMouseListener(new MouseAdapter() {
- @Override
- public void mousePressed(MouseEvent e) {
- super.mouseClicked(e);
- //重连mes
- nettyClient.future.channel().close();
- }
- });
- settingMenu.add(resetTcpMenu);
- JMenuItem resetTcpMenu_1 = new JMenuItem("刷新工件");
- resetTcpMenu_1.setIcon(new ImageIcon(MesClient.class.getResource("/bg/reset_logo.png")));
- resetTcpMenu_1.setFont(new Font("微软雅黑", Font.PLAIN, 20));
- resetTcpMenu_1.addMouseListener(new MouseAdapter() {
- @Override
- public void mousePressed(MouseEvent e) {
- super.mouseClicked(e);
- resetScanA();
- }
- });
- settingMenu.add(resetTcpMenu_1);
- JMenuItem shieldMesMenu = new JMenuItem("MES屏蔽: 关");
- shieldMesMenu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/menu_setting.png")));
- shieldMesMenu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
- shieldMesMenu.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- mes_shield_flag = !mes_shield_flag;
- if (mes_shield_flag) {
- shieldMesMenu.setText("MES屏蔽: 开");
- shieldMesMenu.setForeground(Color.RED);
- setMenuStatus("MES屏蔽已开启:拉铆枪保持运行,客户端暂停读取", 0);
- JOptionPane.showMessageDialog(mesClientFrame,
- "MES屏蔽已开启\n拉铆枪不会被远程关机,可继续打钉\n客户端暂停读取PLC数据及MES后台工件码",
- "提示", JOptionPane.WARNING_MESSAGE);
- } else {
- shieldMesMenu.setText("MES屏蔽: 关");
- shieldMesMenu.setForeground(Color.BLACK);
- setMenuStatus("MES屏蔽已关闭", 0);
- JOptionPane.showMessageDialog(mesClientFrame, "MES屏蔽已关闭,恢复正常读取", "提示", JOptionPane.INFORMATION_MESSAGE);
- }
- }
- });
- settingMenu.add(shieldMesMenu);
- contentPane = new JPanel();
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- setContentPane(contentPane);
- contentPane.setLayout(new BorderLayout(0, 0));
- JToolBar toolBar = new JToolBar();
- contentPane.add(toolBar, BorderLayout.NORTH);
- JLabel equipment_statu_label = new JLabel("状态:");
- equipment_statu_label.setHorizontalAlignment(SwingConstants.CENTER);
- equipment_statu_label.setForeground(Color.BLACK);
- equipment_statu_label.setFont(new Font("微软雅黑", Font.PLAIN, 20));
- equipment_statu_label.setBackground(Color.LIGHT_GRAY);
- toolBar.add(equipment_statu_label);
- status_menu = new JButton("设备未连接MES服务器");
- if(tcp_connect_flag) {
- status_menu.setText("已连接MES服务器");
- status_menu.setForeground(Color.GREEN);
- }else {
- status_menu.setText("未连接MES服务器");
- status_menu.setForeground(Color.DARK_GRAY);
- }
- status_menu.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- }
- });
- status_menu.setForeground(Color.GREEN);
- status_menu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
- status_menu.setBackground(Color.BLACK);
- toolBar.add(status_menu);
- JLabel space_1 = new JLabel(" ");
- toolBar.add(space_1);
- JLabel heart_beat_status_label = new JLabel("心跳:");
- heart_beat_status_label.setHorizontalAlignment(SwingConstants.CENTER);
- heart_beat_status_label.setForeground(Color.BLACK);
- heart_beat_status_label.setFont(new Font("微软雅黑", Font.PLAIN, 20));
- heart_beat_status_label.setBackground(Color.LIGHT_GRAY);
- toolBar.add(heart_beat_status_label);
- heart_beat_menu = new JButton("2024-02-20 23:20:10");
- heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/green_dot.png")));
- heart_beat_menu.setForeground(Color.GREEN);
- heart_beat_menu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
- heart_beat_menu.setBackground(Color.BLACK);
- toolBar.add(heart_beat_menu);
- JLabel space_2 = new JLabel(" ");
- toolBar.add(space_2);
- JLabel user_status_label = new JLabel("登录用户:");
- user_status_label.setHorizontalAlignment(SwingConstants.CENTER);
- user_status_label.setForeground(Color.BLACK);
- user_status_label.setFont(new Font("微软雅黑", Font.PLAIN, 20));
- user_status_label.setBackground(Color.LIGHT_GRAY);
- toolBar.add(user_status_label);
- user_menu = new JButton("JinJuShi");
- user_menu.setForeground(Color.GREEN);
- user_menu.setFont(new Font("微软雅黑", Font.PLAIN, 22));
- user_menu.setBackground(Color.BLACK);
- toolBar.add(user_menu);
- JLabel space_3 = new JLabel(" ");
- toolBar.add(space_3);
- JLabel space_4 = new JLabel(" ");
- toolBar.add(space_4);
- tabbedPane = new JTabbedPane(JTabbedPane.TOP);
- tabbedPane.setMinimumSize(new Dimension(400, 50));
- tabbedPane.setFont(new Font("宋体", Font.BOLD, 22));
- contentPane.add(tabbedPane);
- //首页
- JPanel indexPanelA = new JPanel();
- indexScrollPaneA = new JScrollPane(indexPanelA);
- indexPanelA.setLayout(null);
- product_sn = new JTextField();
- product_sn.setHorizontalAlignment(SwingConstants.CENTER);
- product_sn.setEditable(false);
- product_sn.setFont(new Font("微软雅黑", Font.PLAIN, 28));
- product_sn.setBounds(81, 70, 810, 70);
- indexPanelA.add(product_sn);
- product_sn.setColumns(10);
- // String[] hjtitles = new String[]{"焊机1","焊机2","焊机3"};
- // String[] hjvals = new String[]{"HJ001","HJ002","HJ003"};
- // mesRadioHj = new MesRadio(hjtitles,hjvals);
- // mesRadioHj.setSize(500,50);
- // mesRadioHj.setBounds(190,170,500,50);
- // indexPanelA.add(mesRadioHj);
- finish_ok_bt = new JButton("OK");
- finish_ok_bt.setEnabled(false);
- finish_ok_bt.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- submitQualityResult("OK");
- }
- });
- finish_ok_bt.setIcon(new ImageIcon(MesClient.class.getResource("/bg/ok_bg.png")));
- finish_ok_bt.setFont(new Font("微软雅黑", Font.PLAIN, 32));
- // finish_ok_bt.setBounds(185, 291, 240, 80);
- finish_ok_bt.setBounds(185, 371, 240, 80);
- finish_ok_bt.setEnabled(false);
- indexPanelA.add(finish_ok_bt);
- finish_ng_bt = new JButton("NG");
- finish_ng_bt.setEnabled(false);
- finish_ng_bt.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- submitQualityResult("NG");
- }
- });
- finish_ng_bt.setIcon(new ImageIcon(MesClient.class.getResource("/bg/ng_bg.png")));
- finish_ng_bt.setFont(new Font("微软雅黑", Font.PLAIN, 32));
- finish_ng_bt.setBounds(508, 371, 240, 80);
- finish_ng_bt.setEnabled(false);
- indexPanelA.add(finish_ng_bt);
- JLabel lblNewLabel = new JLabel("A\u67AA");
- lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
- lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 20));
- lblNewLabel.setBounds(204, 186, 166, 28);
- indexPanelA.add(lblNewLabel);
- JLabel lblNewLabel_1 = new JLabel("\u9884\u8BBE\u6570\u91CF");
- lblNewLabel_1.setFont(new Font("微软雅黑", Font.PLAIN, 18));
- lblNewLabel_1.setBounds(204, 241, 83, 34);
- indexPanelA.add(lblNewLabel_1);
- param1 = new JTextField();
- param1.setHorizontalAlignment(SwingConstants.CENTER);
- param1.setFont(new Font("微软雅黑", Font.PLAIN, 18));
- param1.setText(String.valueOf(aSetNum));
- param1.setEditable(false);
- param1.setBounds(288, 241, 83, 34);
- indexPanelA.add(param1);
- param1.setColumns(10);
- JLabel lblNewLabel_1_1 = new JLabel("\u5B8C\u6210\u6570\u91CF");
- lblNewLabel_1_1.setFont(new Font("微软雅黑", Font.PLAIN, 18));
- lblNewLabel_1_1.setBounds(204, 285, 83, 34);
- indexPanelA.add(lblNewLabel_1_1);
- param2 = new JTextField();
- param2.setHorizontalAlignment(SwingConstants.CENTER);
- param2.setFont(new Font("微软雅黑", Font.PLAIN, 18));
- param2.setText("0");
- param2.setEditable(false);
- param2.setColumns(10);
- param2.setBounds(288, 285, 83, 34);
- indexPanelA.add(param2);
- JLabel lblB = new JLabel("B\u67AA");
- lblB.setHorizontalAlignment(SwingConstants.CENTER);
- lblB.setFont(new Font("微软雅黑", Font.PLAIN, 20));
- lblB.setBounds(525, 186, 166, 28);
- indexPanelA.add(lblB);
- JLabel lblNewLabel_1_2 = new JLabel("\u9884\u8BBE\u6570\u91CF");
- lblNewLabel_1_2.setFont(new Font("微软雅黑", Font.PLAIN, 18));
- lblNewLabel_1_2.setBounds(525, 241, 83, 34);
- indexPanelA.add(lblNewLabel_1_2);
- param3 = new JTextField();
- param3.setText(String.valueOf(bSetNum));
- param3.setHorizontalAlignment(SwingConstants.CENTER);
- param3.setFont(new Font("微软雅黑", Font.PLAIN, 18));
- param3.setEditable(false);
- param3.setColumns(10);
- param3.setBounds(608, 241, 83, 34);
- indexPanelA.add(param3);
- JLabel lblNewLabel_1_1_1 = new JLabel("\u5B8C\u6210\u6570\u91CF");
- lblNewLabel_1_1_1.setFont(new Font("微软雅黑", Font.PLAIN, 18));
- lblNewLabel_1_1_1.setBounds(525, 285, 83, 34);
- indexPanelA.add(lblNewLabel_1_1_1);
- param4 = new JTextField();
- param4.setText("0");
- param4.setHorizontalAlignment(SwingConstants.CENTER);
- param4.setFont(new Font("微软雅黑", Font.PLAIN, 18));
- param4.setEditable(false);
- param4.setColumns(10);
- param4.setBounds(608, 285, 83, 34);
- indexPanelA.add(param4);
- JLabel lblForceA = new JLabel("\u62c9\u529b");
- lblForceA.setFont(new Font("微软雅黑", Font.PLAIN, 18));
- lblForceA.setBounds(204, 329, 50, 34);
- indexPanelA.add(lblForceA);
- param5 = new JTextField();
- param5.setHorizontalAlignment(SwingConstants.CENTER);
- param5.setFont(new Font("微软雅黑", Font.PLAIN, 18));
- param5.setText("-");
- param5.setEditable(false);
- param5.setColumns(10);
- param5.setBounds(260, 329, 65, 34);
- indexPanelA.add(param5);
- JLabel lblStrokeA = new JLabel("\u884c\u7a0b");
- lblStrokeA.setFont(new Font("微软雅黑", Font.PLAIN, 18));
- lblStrokeA.setBounds(330, 329, 50, 34);
- indexPanelA.add(lblStrokeA);
- param6 = new JTextField();
- param6.setHorizontalAlignment(SwingConstants.CENTER);
- param6.setFont(new Font("微软雅黑", Font.PLAIN, 18));
- param6.setText("-");
- param6.setEditable(false);
- param6.setColumns(10);
- param6.setBounds(380, 329, 65, 34);
- indexPanelA.add(param6);
- JLabel lblForceB = new JLabel("\u62c9\u529b");
- lblForceB.setFont(new Font("微软雅黑", Font.PLAIN, 18));
- lblForceB.setBounds(525, 329, 50, 34);
- indexPanelA.add(lblForceB);
- param7 = new JTextField();
- param7.setHorizontalAlignment(SwingConstants.CENTER);
- param7.setFont(new Font("微软雅黑", Font.PLAIN, 18));
- param7.setText("-");
- param7.setEditable(false);
- param7.setColumns(10);
- param7.setBounds(581, 329, 65, 34);
- indexPanelA.add(param7);
- JLabel lblStrokeB = new JLabel("\u884c\u7a0b");
- lblStrokeB.setFont(new Font("微软雅黑", Font.PLAIN, 18));
- lblStrokeB.setBounds(651, 329, 50, 34);
- indexPanelA.add(lblStrokeB);
- param8 = new JTextField();
- param8.setHorizontalAlignment(SwingConstants.CENTER);
- param8.setFont(new Font("微软雅黑", Font.PLAIN, 18));
- param8.setText("-");
- param8.setEditable(false);
- param8.setColumns(10);
- param8.setBounds(701, 329, 65, 34);
- indexPanelA.add(param8);
- tabbedPane.addTab("工作面板", new ImageIcon(MesClient.class.getResource("/bg/a_side.png")), indexScrollPaneA, null);
- tabbedPane.setEnabledAt(0, true);
- // searchScrollPane = new JScrollPane((Component) null);
- indexPanelC = new JPanel();
- searchScrollPaneDj = new JScrollPane(indexPanelC);
- indexPanelC.setLayout(null);
- tabbedPane.addTab("开班点检", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), searchScrollPaneDj, null);
- indexPanelB = new JPanel();
- searchScrollPane = new JScrollPane(indexPanelB);
- indexPanelB.setLayout(null);
- tabbedPane.addTab("工作记录", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), searchScrollPane, null);
- indexPanelD = new DeviceManagePanel();
- JScrollPane scrollPanelD = new JScrollPane(indexPanelD);
- scrollPanelD.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
- scrollPanelD.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
- tabbedPane.addTab("设备管理", new ImageIcon(MesClient.class.getResource("/bg/menu_setting.png")), scrollPanelD, null);
- tabbedPane.addChangeListener(new ChangeListener() {
- @Override
- public void stateChanged(ChangeEvent e) {
- JTabbedPane tabbedPane = (JTabbedPane) e.getSource();
- int selectedIndex = tabbedPane.getSelectedIndex();
- if (selectedIndex == 3) { // 设备管理
- String input = JOptionPane.showInputDialog(mesClientFrame, "请输入管理员密码以进入设备管理:", "权限验证", JOptionPane.QUESTION_MESSAGE);
- if (input != null && input.equals("503833")) {
- lastSelectedIndex = selectedIndex;
- // 每次进入都从数据库重载,确保看到的是已落库的最新配置
- if (indexPanelD instanceof DeviceManagePanel) {
- ((DeviceManagePanel) indexPanelD).reloadFromDb();
- }
- } else {
- if (input != null) {
- JOptionPane.showMessageDialog(mesClientFrame, "密码错误,请重试", "错误", JOptionPane.ERROR_MESSAGE);
- }
- // 跳回上一个页面
- tabbedPane.setSelectedIndex(lastSelectedIndex);
- }
- } else {
- lastSelectedIndex = selectedIndex;
- }
- }
- });
- }
- public static void setMenuStatus(String msg,int error){
- if(error == 0){
- MesClient.status_menu.setForeground(Color.GREEN);
- }else{
- MesClient.status_menu.setForeground(Color.RED);
- }
- MesClient.status_menu.setText(msg);
- }
- public static void getMaterailData(){
- JSONObject retObj = DataUtil.getBindMaterail();
- if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
- java.util.List<BindMaterialResp> arrs = retObj.getList("data",BindMaterialResp.class);
- int i = 0;
- rowData = new Object[arrs.size()][7];
- for (BindMaterialResp bindMaterialResp:arrs){
- rowData[i][0] = bindMaterialResp.getMaterialTitle();
- rowData[i][1] = bindMaterialResp.getBatchSn();
- rowData[i][2] = bindMaterialResp.getLastTimes();
- rowData[i][3] = "";
- rowData[i][4] = bindMaterialResp.getCraft();
- rowData[i][5] = bindMaterialResp.getMaterialId();
- rowData[i][6] = bindMaterialResp.getType();
- i++;
- }
- bindBatchPanel();
- }
- }
- public static void updateMaterailData(){
- JSONObject retObj = DataUtil.getBindMaterail();
- if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
- List<BindMaterialResp> arrs = retObj.getList("data",BindMaterialResp.class);
- int i = 0;
- for (BindMaterialResp bindMaterialResp:arrs){
- rowData[i][0] = bindMaterialResp.getMaterialTitle();
- rowData[i][1] = bindMaterialResp.getBatchSn();
- rowData[i][2] = bindMaterialResp.getLastTimes();
- rowData[i][3] = "";
- rowData[i][4] = bindMaterialResp.getCraft();
- rowData[i][5] = bindMaterialResp.getMaterialId();
- rowData[i][6] = bindMaterialResp.getType();
- i++;
- }
- MesClient.table.repaint();
- }
- }
- // 绑定物料批次码
- public static void scanBatchSn(BindMaterialResp bindMaterialResp) {
- //弹窗扫工件码
- String scanBarcodeTitle = "请扫物料:"+bindMaterialResp.getMaterialTitle();
- String scanBarcode = JOptionPane.showInputDialog(null, scanBarcodeTitle);
- if(scanBarcode!=null&&!scanBarcode.equalsIgnoreCase("")) {
- JSONObject retObj = DataUtil.saveBindMaterail(scanBarcode,bindMaterialResp.getCraft(),bindMaterialResp.getMaterialId(),bindMaterialResp.getType());
- if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
- MesClient.setMenuStatus("扫物料:"+bindMaterialResp.getMaterialTitle()+"成功",0);
- updateMaterailData();
- }else{
- if(retObj.get("result")==null){
- MesClient.setMenuStatus("请求失败,请重试",-1);
- }else{
- if(retObj.get("result").toString().equalsIgnoreCase("false")){
- MesClient.setMenuStatus(retObj.getString("message"),-1);
- }
- }
- }
- }
- }
- public static void bindBatchPanel(){
- JPanel indexPanelBB = new JPanel();
- JPanel panel = new JPanel();
- panel.setBounds(0, 0, 990, 550);
- // panel.setBounds(81, 50, 810, 479);
- indexPanelBB.add(panel);
- panel.setLayout(new GridLayout(0, 1, 0, 0));
- table = new JTable(rowData, columnNames){
- public boolean isCellEditable(int row, int column) {
- if(column == 3){
- return true;
- }
- return false;
- }
- };
- table.setRowHeight(40);
- table.setEnabled(true);
- table.setFont(new Font("微软雅黑", Font.PLAIN, 14));
- table.getColumnModel().getColumn(3).setCellRenderer(new TableCellRendererButton());
- table.getColumnModel().getColumn(3).setCellEditor(new TableCellEditorButton());
- JScrollPane scrollPane = new JScrollPane(table);
- panel.add(scrollPane);
- JScrollPane indexScrollPaneB = new JScrollPane(indexPanelBB);
- indexPanelBB.setLayout(null);
- tabbedPane.addTab("绑定物料", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), indexScrollPaneB, null);
- }
- }
|