Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
- import org.eclipse.paho.client.mqttv3.MqttClient;
- import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
- import org.eclipse.paho.client.mqttv3.MqttException;
- import org.eclipse.paho.client.mqttv3.MqttMessage;
- import org.eclipse.paho.client.mqttv3.MqttCallback;
- import java.awt.event.ActionListener;
- import java.util.ArrayList;
- import java.util.List;
- import java.awt.event.ActionEvent;
- public class AnalyticApplication {
- String broker;
- String username;
- String password;
- MqttClient mqttClient;
- List<Device> devices;
- private JTextField txtBroker;
- private JTextField txtUsername;
- private JTextField txtPort;
- private JTextField txtPassword;
- private JTextArea noticeBoard;
- AnalyticApplication() {
- devices = new ArrayList<Device>();
- }
- public void initializeWindow() {
- JFrame frmAnalyticApplication = new JFrame();
- frmAnalyticApplication.setTitle("Analytic Application");
- frmAnalyticApplication.setResizable(false);
- frmAnalyticApplication.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frmAnalyticApplication.setSize(600, 400);
- frmAnalyticApplication.getContentPane().setLayout(null);
- JLabel lblBroker = new JLabel("Broker:");
- lblBroker.setBounds(40, 40, 80, 15);
- frmAnalyticApplication.getContentPane().add(lblBroker);
- txtBroker = new JTextField();
- txtBroker.setBounds(127, 38, 150, 19);
- frmAnalyticApplication.getContentPane().add(txtBroker);
- txtBroker.setColumns(10);
- JLabel lblUsername = new JLabel("Username:");
- lblUsername.setBounds(40, 95, 80, 15);
- frmAnalyticApplication.getContentPane().add(lblUsername);
- txtUsername = new JTextField();
- txtUsername.setBounds(127, 93, 150, 19);
- frmAnalyticApplication.getContentPane().add(txtUsername);
- txtUsername.setColumns(10);
- JLabel lblPassword = new JLabel("Password:");
- lblPassword.setBounds(40, 122, 80, 15);
- frmAnalyticApplication.getContentPane().add(lblPassword);
- JButton btnConnect = new JButton("Connect ");
- btnConnect.addActionListener(new ConnectListener());
- btnConnect.setBounds(436, 35, 114, 25);
- frmAnalyticApplication.getContentPane().add(btnConnect);
- JButton btnExit = new JButton("Exit");
- btnExit.addActionListener(new ExitListener());
- btnExit.setBounds(436, 72, 114, 25);
- frmAnalyticApplication.getContentPane().add(btnExit);
- JScrollPane scrollPane = new JScrollPane();
- scrollPane.setBounds(40, 167, 510, 174);
- frmAnalyticApplication.getContentPane().add(scrollPane);
- noticeBoard = new JTextArea();
- scrollPane.setViewportView(noticeBoard);
- noticeBoard.setWrapStyleWord(true);
- noticeBoard.setRows(10);
- noticeBoard.setEditable(false);
- JLabel lblPort = new JLabel("Port:");
- lblPort.setBounds(40, 67, 66, 15);
- frmAnalyticApplication.getContentPane().add(lblPort);
- txtPort = new JTextField();
- txtPort.setBounds(127, 65, 60, 19);
- frmAnalyticApplication.getContentPane().add(txtPort);
- txtPort.setColumns(10);
- txtPassword = new JTextField();
- txtPassword.setBounds(127, 124, 150, 19);
- frmAnalyticApplication.getContentPane().add(txtPassword);
- txtPassword.setColumns(10);
- JButton btnClear = new JButton("Clear Board");
- btnClear.addActionListener(new ClearListener());
- btnClear.setBounds(436, 109, 114, 25);
- frmAnalyticApplication.getContentPane().add(btnClear);
- frmAnalyticApplication.setVisible(true);
- }
- public void run() {
- try {
- mqttClient = new MqttClient(broker, "AnalyticApp");
- MqttConnectOptions connOpts = new MqttConnectOptions();
- connOpts.setCleanSession(false);
- connOpts.setUserName(username);
- connOpts.setPassword(password.toCharArray());
- noticeBoard.append("Connecting to " + broker + "n");
- String topic = "Assignment";
- mqttClient.setCallback(new MqttCallback() {
- public void messageArrived(String topic, MqttMessage message) throws Exception {
- String data = new String(message.getPayload());
- String[] field = data.split(",");
- boolean isDuplicate = false;
- Device newDevice = new Device(field);
- for (int index = 0; index < devices.size(); index++) {
- if (devices.get(index).id.equals(field[0])) {
- isDuplicate = true;
- devices.remove(index);
- devices.add(index, newDevice);
- }
- }
- if (!isDuplicate || devices.isEmpty()) {
- devices.add(newDevice);
- }
- noticeBoard.setText("");
- for (int index = 0; index < devices.size(); index++) {
- noticeBoard.append(devices.get(index).id + "t"
- + devices.get(index).longitude + "t"
- + devices.get(index).latitude + "t"
- + devices.get(index).time + "n");
- }
- }
- public void connectionLost(Throwable cause) {
- noticeBoard.append("Connection to broker lost!" + cause.getMessage() + "n");
- }
- public void deliveryComplete(IMqttDeliveryToken token) {
- }
- });
- while (true) {
- mqttClient.connect(connOpts);
- mqttClient.subscribe(topic, 1);
- }
- } catch (MqttException me) {
- noticeBoard.append(me.getMessage() + "n");
- }
- }
- public static void main(String[] args) {
- AnalyticApplication app = new AnalyticApplication();
- app.initializeWindow();
- }
- class ConnectListener implements ActionListener {
- public void actionPerformed(ActionEvent event) {
- broker = "tcp://" + txtBroker.getText();
- if (!txtPort.getText().isEmpty()) {
- broker = broker + ":" + Integer.parseInt(txtPort.getText());
- }
- username = txtUsername.getText();
- password = txtPassword.getText();
- run();
- }
- }
- class ExitListener implements ActionListener {
- public void actionPerformed(ActionEvent event) {
- if(mqttClient != null) {
- if(mqttClient.isConnected()) {
- try {
- mqttClient.disconnect();
- noticeBoard.append("Disconnected!");
- } catch (MqttException e) {
- e.printStackTrace();
- }
- }
- }
- System.exit(0);
- }
- }
- class ClearListener implements ActionListener {
- public void actionPerformed(ActionEvent event) {
- noticeBoard.setText("");
- }
- }
- }
Add Comment
Please, Sign In to add comment