2020-09-24

package com.zjw.frame;

import com.zjw.entity.AgvInfo;
import com.zjw.utils.ByteUtils;

import javax.swing.*;
import java.awt.*;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class MainFrame extends JFrame {
    private int width = 900;
    private int height = 550;
    private JLabel lblCurrent;
    private JTextArea firstTextArea;
    private JLabel lblOther;
    private JTextArea secondTextArea;
    private JScrollPane firstTextAreaScrollPane;
    private JScrollPane secondTextAreaScrollPane;
    private JLabel lblPort;
    private JTextField txtPort;
    private JButton btnStartServer;
    private JButton btnGetCurrent;
    private JButton btnSet;
    public boolean isStarted = false;
    public Map<String, AgvInfo> agvInfos;//存储AGV信息

    public MainFrame() {
        agvInfos = new LinkedHashMap<>();
        //获得屏幕分辨率
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension dims = toolkit.getScreenSize();
        int sWidth = (int) dims.getWidth();
        int sHeight = (int) dims.getHeight();
        this.setSize(width, height);
        this.setLayout(null);
        this.setTitle("AGV模拟器");
        this.setResizable(false);//不能放大缩小
        this.setLocation((sWidth - width) / 2, (sHeight - height) / 2);
        initWidget();
        setWidgetBounds();
        addWidget();
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setVisible(true);
        setListener();
        new Thread(() -> {
            while (true) {
                try {
                    SwingUtilities.invokeLater(() -> {
                        String str = "";
                        //循环显示agv信息
                        for (Map.Entry<String, AgvInfo> pair : agvInfos.entrySet()) {
                            str += "AGV" + pair.getKey() + "  当前位置:" + pair.getValue().getCurrentPosition() + "   当前状态:" + pair.getValue().getStatus() + "   当前电量:" + pair.getValue().getElectric() + "   瞬时速度:" + pair.getValue().getSpeed() + "   异常:" + pair.getValue().getException() + "\r\n";
                        }
                        firstTextArea.setText(str);
                    });
                    Thread.sleep(500);
                } catch (Exception ex) {
                }


            }
        }).start();
    }

    private void setListener() {
        btnGetCurrent.addActionListener(e -> {
            String str = "";
            for (Map.Entry<String, AgvInfo> entry : agvInfos.entrySet()) {
                str = str + entry.getValue().getCurrentPosition() + ";" + entry.getValue().getStatus() + ";" + entry.getValue().getElectric() + ";" + entry.getValue().getSpeed() + ";" + entry.getValue().getException() + "\r\n";
            }
            secondTextArea.setText(str);
        });

        btnSet.addActionListener(e -> {
            try {
                List<String> list = getLines(secondTextArea);
                int startPort = Integer.parseInt(txtPort.getText().trim());
                for (int i = 0; i < list.size(); i++) {
                    int port = startPort + i;
                    String[] split = list.get(i).split(";");
                    agvInfos.get(port + "").setCurrentPosition(Integer.parseInt(split[0]));
                    agvInfos.get(port + "").setStatus(Integer.parseInt(split[1]));
                    agvInfos.get(port + "").setElectric(Integer.parseInt(split[2]));
                    agvInfos.get(port + "").setSpeed(Integer.parseInt(split[3]));
                    agvInfos.get(port + "").setException(Integer.parseInt(split[4]));
                }
            } catch (Exception ex) {

            }
        });

        btnStartServer.addActionListener(e -> {
            if (isStarted) {
                return;
            }
            isStarted = true;
            new Thread(() ->
            {
                try {
                    List<String> lines = getLines(secondTextArea);
                    int port = Integer.parseInt(txtPort.getText().trim());
                    //循环开启服务
                    for (int i = 0; i < lines.size(); i++) {
                        int myPort = port + i;
                        String[] split = lines.get(i).split(";");
                        AgvInfo agvInfo = new AgvInfo();
                        agvInfo.setCurrentPosition(Integer.parseInt(split[0]));
                        agvInfo.setStatus(Integer.parseInt(split[1]));
                        agvInfo.setElectric(Integer.parseInt(split[2]));
                        agvInfo.setSpeed(Integer.parseInt(split[3]));
                        agvInfo.setException(Integer.parseInt(split[4]));
                        agvInfos.put(myPort + "", agvInfo);
                        new StartServerThread(myPort).start();
                    }
                } catch (Exception ex) {
                    isStarted = false;
                }
            }).start();
        });
    }

    class StartServerThread extends Thread {
        private int port;
        ServerSocket serverSocket;

        public StartServerThread(int port) {
            this.port = port;
        }

        @Override
        public void run() {
            try {
                serverSocket = new ServerSocket(port);
                while (true) {
                    //建立连接,获取socket对象
                    Socket socket = serverSocket.accept();
                    new ClientHandler(port, socket).start();
                }
            } catch (Exception ex) {
                System.out.println(ex.getMessage());
            }
        }
    }

    class ClientHandler extends Thread {
        private int port;
        private Socket socket;

        public ClientHandler(int port, Socket socket) {
            this.port = port;
            this.socket = socket;
        }

        @Override
        public void run() {
            try {
                socket.setSoTimeout(1000);
                InputStream in = socket.getInputStream();
                byte[] retBytes = new byte[4096];
                int recCount = in.read(retBytes);
                OutputStream out = socket.getOutputStream();
                if (recCount != 8) {
                    out.write(getErrorBytes());
                } else if (ByteUtils.byteToUnsignedInt(retBytes[0]) == 'a') {
                    out.write(getStatusBytes(port));
                } else if (ByteUtils.byteToUnsignedInt(retBytes[0]) == 'b') {
                    int destPosition = ByteUtils.byteToUnsignedInt(retBytes[1]) << 8 | ByteUtils.byteToUnsignedInt(retBytes[2]);
                    doExecute(port, destPosition);
                    out.write(getSuccessByte());
                } else {
                    out.write(getErrorBytes());
                }
                out.close();
                in.close();
            } catch (Exception e) {
            }
        }
    }

    private void doExecute(int port, int position) {
        new ExecuteThread(port, position).start();
    }

    class ExecuteThread extends Thread {
        private int port;
        private int position;

        public ExecuteThread(int port, int position) {
            this.port = port;
            this.position = position;
        }

        @Override
        public void run() {
            try {
                if (agvInfos.get(port + "").getStatus() == 1) {
                    return;
                }
                agvInfos.get(port + "").setStatus(1);
                Thread.sleep(5000);
                agvInfos.get(port + "").setCurrentPosition(position);
                agvInfos.get(port + "").setStatus(0);
            } catch (Exception ex) {

            }
        }

    }

    private byte[] getSuccessByte() {
        byte[] bytes = new byte[8];
        bytes[0] = (byte) 'b';
        bytes[1] = (byte) 'o';
        bytes[2] = 0;
        bytes[3] = 0;
        bytes[4] = 0;
        bytes[5] = 0;
        bytes[6] = 0;
        bytes[7] = 0;
        return bytes;
    }

    private byte[] getStatusBytes(int port) {
        AgvInfo agvInfo = agvInfos.get(port + "");
        byte[] bytes = new byte[8];
        bytes[0] = (byte) 'a';
        bytes[1] = (byte) (agvInfo.getCurrentPosition() >> 8);
        bytes[2] = (byte) agvInfo.getCurrentPosition();
        bytes[3] = (byte) agvInfo.getSpeed();
        bytes[4] = (byte) agvInfo.getElectric();
        bytes[5] = (byte) agvInfo.getException();
        bytes[6] = (byte) agvInfo.getStatus();
        bytes[7] = 0;
        return bytes;
    }

    private byte[] getErrorBytes() {
        byte[] bytes = new byte[8];
        bytes[0] = (byte) 'c';
        bytes[1] = 0;
        bytes[2] = 0;
        bytes[3] = 0;
        bytes[4] = 0;
        bytes[5] = 0;
        bytes[6] = 0;
        bytes[7] = 0;
        return bytes;
    }


    private List<String> getLines(JTextArea area) {
        String str = area.getText().replace("\r", "");
        String[] strArray = str.split("\n");
        List<String> list = new ArrayList<>();
        for (String str1 : strArray) {
            if (str1.trim().length() != 0) {
                list.add(str1.trim());
            }
        }
        return list;
    }

    private void initWidget() {
        lblCurrent = new JLabel("当前信息");
        firstTextArea = new JTextArea();
        firstTextAreaScrollPane = new JScrollPane(firstTextArea);
        lblOther = new JLabel("其他信息");

        secondTextArea = new JTextArea("0;0;80;3;0");
        secondTextAreaScrollPane = new JScrollPane(secondTextArea);

        lblPort = new JLabel("起始端口号:");
        txtPort = new JTextField("4001");
        btnStartServer = new JButton("开启服务");
        btnGetCurrent = new JButton("获取当前信息");
        btnSet = new JButton("设置");
    }

    private void setWidgetBounds() {
        lblCurrent.setBounds(10, 5, 200, 30);
        firstTextAreaScrollPane.setBounds(10, 35, 880, 200);
        lblOther.setBounds(10, 236, 200, 30);
        secondTextAreaScrollPane.setBounds(10, 267, 880, 200);
        lblPort.setBounds(10, 470, 160, 30);
        txtPort.setBounds(100, 470, 100, 30);
        btnStartServer.setBounds(210, 470, 100, 30);
        btnGetCurrent.setBounds(320, 470, 160, 30);
        btnSet.setBounds(490, 470, 100, 30);
    }

    private void addWidget() {
        this.add(lblCurrent);
        this.add(firstTextAreaScrollPane);
        this.add(lblOther);
        this.add(secondTextAreaScrollPane);
        this.add(lblPort);
        this.add(txtPort);
        this.add(btnStartServer);
        this.add(btnGetCurrent);
        this.add(btnSet);
    }
}

package com.zjw.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class ByteUtils {
    public static final InputStream byte2Input(byte[] buf) {
        return new ByteArrayInputStream(buf);
    }

    public static final byte[] input2byte(InputStream inStream) throws IOException {
        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
        byte[] buff = new byte[100];
        int rc = 0;
        while ((rc = inStream.read(buff, 0, 100)) > 0) {
            swapStream.write(buff, 0, rc);
        }
        byte[] in2b = swapStream.toByteArray();
        return in2b;
    }

    public static String byteToHexString(byte bt) {
        byte[] bytes = new byte[1];
        bytes[0] = bt;
        return bytesToHexString(bytes);
    }

    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }

    /**
     * Convert hex string to byte[]
     *
     * @param hexString
     *            the hex string
     * @return byte[]
     */
    public static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }

    /**
     * Convert char to byte
     *
     * @param c
     *            char
     * @return byte
     */
    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }

    public static char byteToChar(byte bt) {
        String str = "0123456789ABCDEF";
        char[] charArray =str.toCharArray();
        for(char ch : charArray) {
            if(bt==charToByte(ch)) {
                return ch;
            }
        }
        return ' ';
    }

    public static int getUnsignedByte(byte data) { // 将data字节型数据转换为0~255 (0xFF 即BYTE)。
        return data & 0x0FF;
    }

    public static int getUnsignedShort(short data) {
        return data & 0xFFFF;
    }

    public static long getUnsignedInt(int data) {
        return data & 0xFFFFFF;
    }

    /**
     * byte转无符号int
     * @param data
     * @return
     */
    public static Integer byteToUnsignedInt(byte data) {
        return data & 0xff;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值