Guest User

Untitled

a guest
Oct 21st, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.13 KB | None | 0 0
  1. import javax.swing.*;
  2. import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
  3. import org.eclipse.paho.client.mqttv3.MqttClient;
  4. import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
  5. import org.eclipse.paho.client.mqttv3.MqttException;
  6. import org.eclipse.paho.client.mqttv3.MqttMessage;
  7. import org.eclipse.paho.client.mqttv3.MqttCallback;
  8. import java.awt.event.ActionListener;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. import java.awt.event.ActionEvent;
  12.  
  13. public class AnalyticApplication {
  14.  
  15. String broker;
  16. String username;
  17. String password;
  18. MqttClient mqttClient;
  19. List<Device> devices;
  20.  
  21. private JTextField txtBroker;
  22. private JTextField txtUsername;
  23. private JTextField txtPort;
  24. private JTextField txtPassword;
  25. private JTextArea noticeBoard;
  26.  
  27. AnalyticApplication() {
  28. devices = new ArrayList<Device>();
  29. }
  30.  
  31. public void initializeWindow() {
  32. JFrame frmAnalyticApplication = new JFrame();
  33. frmAnalyticApplication.setTitle("Analytic Application");
  34. frmAnalyticApplication.setResizable(false);
  35. frmAnalyticApplication.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  36. frmAnalyticApplication.setSize(600, 400);
  37. frmAnalyticApplication.getContentPane().setLayout(null);
  38.  
  39. JLabel lblBroker = new JLabel("Broker:");
  40. lblBroker.setBounds(40, 40, 80, 15);
  41. frmAnalyticApplication.getContentPane().add(lblBroker);
  42.  
  43. txtBroker = new JTextField();
  44. txtBroker.setBounds(127, 38, 150, 19);
  45. frmAnalyticApplication.getContentPane().add(txtBroker);
  46. txtBroker.setColumns(10);
  47.  
  48. JLabel lblUsername = new JLabel("Username:");
  49. lblUsername.setBounds(40, 95, 80, 15);
  50. frmAnalyticApplication.getContentPane().add(lblUsername);
  51.  
  52. txtUsername = new JTextField();
  53. txtUsername.setBounds(127, 93, 150, 19);
  54. frmAnalyticApplication.getContentPane().add(txtUsername);
  55. txtUsername.setColumns(10);
  56.  
  57. JLabel lblPassword = new JLabel("Password:");
  58. lblPassword.setBounds(40, 122, 80, 15);
  59. frmAnalyticApplication.getContentPane().add(lblPassword);
  60.  
  61. JButton btnConnect = new JButton("Connect ");
  62. btnConnect.addActionListener(new ConnectListener());
  63. btnConnect.setBounds(436, 35, 114, 25);
  64.  
  65. frmAnalyticApplication.getContentPane().add(btnConnect);
  66.  
  67. JButton btnExit = new JButton("Exit");
  68. btnExit.addActionListener(new ExitListener());
  69. btnExit.setBounds(436, 72, 114, 25);
  70. frmAnalyticApplication.getContentPane().add(btnExit);
  71.  
  72. JScrollPane scrollPane = new JScrollPane();
  73. scrollPane.setBounds(40, 167, 510, 174);
  74. frmAnalyticApplication.getContentPane().add(scrollPane);
  75.  
  76. noticeBoard = new JTextArea();
  77. scrollPane.setViewportView(noticeBoard);
  78. noticeBoard.setWrapStyleWord(true);
  79. noticeBoard.setRows(10);
  80. noticeBoard.setEditable(false);
  81.  
  82. JLabel lblPort = new JLabel("Port:");
  83. lblPort.setBounds(40, 67, 66, 15);
  84. frmAnalyticApplication.getContentPane().add(lblPort);
  85.  
  86. txtPort = new JTextField();
  87. txtPort.setBounds(127, 65, 60, 19);
  88. frmAnalyticApplication.getContentPane().add(txtPort);
  89. txtPort.setColumns(10);
  90.  
  91. txtPassword = new JTextField();
  92. txtPassword.setBounds(127, 124, 150, 19);
  93. frmAnalyticApplication.getContentPane().add(txtPassword);
  94. txtPassword.setColumns(10);
  95.  
  96. JButton btnClear = new JButton("Clear Board");
  97. btnClear.addActionListener(new ClearListener());
  98. btnClear.setBounds(436, 109, 114, 25);
  99. frmAnalyticApplication.getContentPane().add(btnClear);
  100.  
  101. frmAnalyticApplication.setVisible(true);
  102. }
  103.  
  104. public void run() {
  105. try {
  106. mqttClient = new MqttClient(broker, "AnalyticApp");
  107. MqttConnectOptions connOpts = new MqttConnectOptions();
  108. connOpts.setCleanSession(false);
  109. connOpts.setUserName(username);
  110. connOpts.setPassword(password.toCharArray());
  111.  
  112. noticeBoard.append("Connecting to " + broker + "n");
  113.  
  114. String topic = "Assignment";
  115.  
  116. mqttClient.setCallback(new MqttCallback() {
  117.  
  118. public void messageArrived(String topic, MqttMessage message) throws Exception {
  119. String data = new String(message.getPayload());
  120. String[] field = data.split(",");
  121. boolean isDuplicate = false;
  122. Device newDevice = new Device(field);
  123. for (int index = 0; index < devices.size(); index++) {
  124. if (devices.get(index).id.equals(field[0])) {
  125. isDuplicate = true;
  126. devices.remove(index);
  127. devices.add(index, newDevice);
  128. }
  129. }
  130.  
  131. if (!isDuplicate || devices.isEmpty()) {
  132. devices.add(newDevice);
  133. }
  134.  
  135. noticeBoard.setText("");
  136.  
  137. for (int index = 0; index < devices.size(); index++) {
  138. noticeBoard.append(devices.get(index).id + "t"
  139. + devices.get(index).longitude + "t"
  140. + devices.get(index).latitude + "t"
  141. + devices.get(index).time + "n");
  142. }
  143.  
  144. }
  145.  
  146. public void connectionLost(Throwable cause) {
  147. noticeBoard.append("Connection to broker lost!" + cause.getMessage() + "n");
  148. }
  149.  
  150. public void deliveryComplete(IMqttDeliveryToken token) {
  151. }
  152.  
  153. });
  154.  
  155. while (true) {
  156. mqttClient.connect(connOpts);
  157. mqttClient.subscribe(topic, 1);
  158. }
  159. } catch (MqttException me) {
  160. noticeBoard.append(me.getMessage() + "n");
  161. }
  162. }
  163.  
  164. public static void main(String[] args) {
  165. AnalyticApplication app = new AnalyticApplication();
  166. app.initializeWindow();
  167.  
  168. }
  169.  
  170. class ConnectListener implements ActionListener {
  171. public void actionPerformed(ActionEvent event) {
  172. broker = "tcp://" + txtBroker.getText();
  173. if (!txtPort.getText().isEmpty()) {
  174. broker = broker + ":" + Integer.parseInt(txtPort.getText());
  175. }
  176.  
  177. username = txtUsername.getText();
  178. password = txtPassword.getText();
  179.  
  180. run();
  181. }
  182. }
  183.  
  184. class ExitListener implements ActionListener {
  185. public void actionPerformed(ActionEvent event) {
  186. if(mqttClient != null) {
  187. if(mqttClient.isConnected()) {
  188. try {
  189. mqttClient.disconnect();
  190. noticeBoard.append("Disconnected!");
  191. } catch (MqttException e) {
  192. e.printStackTrace();
  193. }
  194. }
  195. }
  196. System.exit(0);
  197. }
  198. }
  199.  
  200. class ClearListener implements ActionListener {
  201. public void actionPerformed(ActionEvent event) {
  202. noticeBoard.setText("");
  203. }
  204. }
  205. }
Add Comment
Please, Sign In to add comment