Advertisement
Guest User

Untitled

a guest
Jun 4th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.23 KB | None | 0 0
  1. /**
  2. * Class responsible for splitting MQTT messages and inserting raw data into database
  3. * <p>
  4. * Version 1.0
  5. * Brynjar Blomvik
  6. * <p>
  7. * v1.0 functionality
  8. * Receive MQTT message from MQTT subscriber client
  9. * Message must consist of max one line of sensor data
  10. * Split single line message into raw data and construct predefined SQL statement
  11. * Pass statement to SQL database
  12. * <p>
  13. * TODO: Convert insertSensorData to preparedStatement.
  14. */
  15.  
  16. package com.FleetControl;
  17.  
  18. import com.mysql.jdbc.Connection;
  19.  
  20. import java.sql.DriverManager;
  21. import java.sql.SQLException;
  22. import java.sql.Statement;
  23. import java.util.Arrays;
  24. import java.util.List;
  25.  
  26.  
  27. public class DatabaseHandler {
  28.  
  29. static String url = "jdbc:mysql://localhost:3306/DatabaseWebserver";
  30. static String username = "java";
  31. static String password = "password";
  32.  
  33.  
  34. public static void main(String[] args) {
  35. // Test calls for development
  36. /*
  37. DatabaseHandler dbHandler = new DatabaseHandler();
  38. String splitTest = "vehicle/001/sensor!time/11!temp/23.09!hum/50.96!Z/z";
  39. dbHandler.insertTestData("hello test");
  40. dbHandler.insertSensorDataIntoDatabase(splitTest);
  41. */
  42. }
  43.  
  44. /**
  45. * Connects and passes data to the SQL database
  46. *
  47. * @param s MQTT message to be added to database
  48. */
  49. public void insertStatementIntoDatabase(String s) throws SQLException {
  50. // Connect to the database with username and password
  51. System.out.println("Statement to be inserted: " + s);
  52. Statement st = null;
  53.  
  54. System.out.println("Connecting to database...");
  55. try (Connection connection = (Connection) DriverManager.getConnection(url, username, password)) {
  56. System.out.println("[ OK ] Database connected");
  57. st = connection.createStatement();
  58. st.executeUpdate(s);
  59. // System.out.println("Data for insertion::\n" + s);
  60. System.out.println("[ OK ] Data inserted to database");
  61. } catch (SQLException e) {
  62. throw new IllegalStateException("Cannot connect the database!", e);
  63. } finally {
  64. // Disconnect from database
  65. if (st != null) {
  66. st.close();
  67. System.out.println("[ OK ] Disconnected from database.");
  68. }
  69. }
  70. }
  71.  
  72. /**
  73. * Function to add new vehicle to the database
  74. * <p>
  75. * v1.0 Adds static vehicle. Improvement take input from user
  76. *
  77. * @return
  78. */
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86. /**
  87. * If MQTT message does not match predefined conditions for message content insert into pure string table
  88. *
  89. * @param s message from MQTT client
  90. */
  91. public void insertGenericMessageIntoDatabase(String s) throws SQLException {
  92. String sql = "INSERT INTO Misc (Message) VALUES (\"" + s + "\")";
  93. // System.out.println("COMPLETE SQL STATEMENT:\n" + sql);
  94. insertStatementIntoDatabase(sql);
  95. }
  96.  
  97. /**
  98. * Split sensordata message and create SQL INSERT statement
  99. *
  100. * @param message The MQTT message to be splitted
  101. * @throws SQLException
  102. */
  103. public void splitMessage(String message) throws SQLException {
  104. // Defining variables for incoming data
  105. String vehicleId = null;
  106. //String timeStamp = null;
  107. String gpsLat = null;
  108. String gpsLon = null;
  109. /* String temp = null;
  110. String humid = null;*/
  111.  
  112. // Defining SQL statement for database insertion
  113. String sqlDataEntry = "INSERT INTO Sensordata (vehicleId, Timestamp, GpsLat, GpsLon, TempSens, HumidSens) " + "VALUES(";
  114. String sqlCurLocUpdate = null;
  115. System.out.println();
  116. // Splitting MQTT message on different data types
  117. String[] sensorType = message.split("!");
  118. //System.out.println("souting \"sensorType\"");
  119. //System.out.println(sensorType + "\n");
  120. List<String> dataList = Arrays.asList(sensorType);
  121. //System.out.println("souting \"dataList\"");
  122. //System.out.println(dataList);
  123.  
  124. int j = 0;
  125. while (j < 5) {
  126. for (String tmp : dataList) {
  127. // check for end of line trigger
  128. if (tmp.contains("y")) {
  129. // prepare for next line of sensordata
  130. sqlDataEntry += "), (" + vehicleId + ", ";
  131. j = 1;
  132. }
  133. // check for end of message trigger
  134. if (tmp.contains("z")) {
  135. // Close SQL statement
  136. sqlDataEntry += ")";
  137. // Returning SQL statement for database insertion
  138. System.out.println("[ OK ] message dissection");
  139. // forward sql statement to database insertion
  140. insertStatementIntoDatabase(sqlDataEntry);
  141. sqlCurLocUpdate = "UPDATE DatabaseWebserver.vehicle SET CurrentLocLat = " + gpsLat + ", CurrentLocLon = " + gpsLon + " WHERE vehicleId = " + vehicleId + ";";
  142. System.out.println(sqlCurLocUpdate);
  143. insertStatementIntoDatabase(sqlCurLocUpdate);
  144. vehicleId = "IDparReset";
  145. break;
  146.  
  147. }
  148. // System.out.println(j);
  149. // System.out.println("tmp: " + tmp);
  150. String[] rawData = tmp.split("/");
  151. // System.out.println("split on data type " + rawData[0].toString());
  152. //System.out.println("Rawdata[0]: " + rawData[0]);
  153. //System.out.println("Rawdata[1]: " + rawData[1]);
  154.  
  155. if (rawData[0].contains("vehicle")) {
  156. vehicleId = "\"" + rawData[1] + "\"";
  157. }
  158. if (rawData[0].contains("Lat")) {
  159. gpsLat = "\'" + rawData[1] + "\'";
  160. }
  161. if (rawData[0].contains("Lon")) {
  162. gpsLon = "\'" + rawData[1] + "\'";
  163. }
  164. if (j < 6) {
  165. sqlDataEntry += "\"" + rawData[1] + "\"";
  166. }
  167. if (j < 5) {
  168. sqlDataEntry += ", ";
  169. }
  170. j++;
  171. }
  172. }
  173. }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement