Advertisement
Guest User

Untitled

a guest
Nov 20th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.io.UnsupportedEncodingException;
  4. import java.net.HttpURLConnection;
  5. import java.net.URL;
  6. import java.net.URLEncoder;
  7. import java.util.ArrayList;
  8. import java.util.Arrays;
  9.  
  10. import com.google.gson.Gson;
  11. import com.phidget22.*;
  12.  
  13. public class RFIDServo
  14. {
  15. //Declare RFID and set as phid
  16. RFID phid = new RFID();
  17.  
  18. final String[] tagItemsArray = {"123"};
  19. ArrayList<String> tagItemsList = new ArrayList<String>(Arrays.asList(tagItemsArray));
  20.  
  21. //Declare Publisher and set as pub
  22. Publisher pub = new Publisher();
  23.  
  24. // Declare a default sensor object (no location, name/value set later)
  25. // This will only work when you have SensorData class developed
  26. // and a SensorServer developed that accepts json strings, as in the lab
  27.  
  28. //RFIDServo oneSensor = new RFIDServo();
  29.  
  30. //SensorData oneSensor = new SensorData("unknown", "unknown", "16024003");
  31.  
  32. // Declare GSON utility object
  33. Gson gson = new Gson();
  34. // Declare String to hold JSON representation of sensor object data
  35. //String oneSensorJson = new String();
  36.  
  37. // address of server which will receive sensor data
  38. // Altered to point to server to deal with incoming json parameters
  39. public static String sensorServerURL = "http://localhost:8080/MADServerSide/SensorServerDB";
  40.  
  41.  
  42. public static void main(String[] args) throws PhidgetException
  43. {
  44. new RFIDServo();
  45. }
  46.  
  47.  
  48. public String sendToServer(String oneSensorJson)
  49. {
  50. System.out.println("test");
  51. URL url;
  52. HttpURLConnection conn;
  53. BufferedReader rd;
  54. // Replace invalid URL characters from json string
  55. try {
  56. oneSensorJson = URLEncoder.encode(oneSensorJson, "UTF-8");
  57. } catch (UnsupportedEncodingException e1) {
  58. e1.printStackTrace();
  59. }
  60. String fullURL = sensorServerURL + "?sensordata="+oneSensorJson;
  61. System.out.println("Sending data to: "+fullURL); // DEBUG confirmation message
  62. String line;
  63. String result = "";
  64. try {
  65. url = new URL(fullURL);
  66. conn = (HttpURLConnection) url.openConnection();
  67. conn.setRequestMethod("GET");
  68. rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  69. // Request response from server to enable URL to be opened
  70. while ((line = rd.readLine()) != null) {
  71. result += line;
  72. }
  73. rd.close();
  74. } catch (Exception e) {
  75. e.printStackTrace();
  76. }
  77. return result;
  78. }
  79.  
  80. private void pause(int secs)
  81. {
  82. try {
  83. Thread.sleep(secs*1000);
  84. } catch (InterruptedException e1) {
  85. // TODO Auto-generated catch block
  86. e1.printStackTrace();
  87. }
  88. }
  89.  
  90. public RFIDServo() throws PhidgetException
  91. {
  92.  
  93.  
  94. // Make the RFID Phidget able to detect loss or gain of an rfid card
  95. phid.addTagListener(new RFIDTagListener()
  96. {
  97. public void onTag(RFIDTagEvent e) {
  98. System.out.println("Tag read: " + e.getTag());
  99. if (checkTag(e.getTag()))
  100. try {
  101. pub.publishRfid(e.getTag()); //Publishing my tag
  102. //oneSensor.sendToServer();
  103.  
  104. } catch (Exception ex) {
  105.  
  106. }
  107. }
  108. });
  109.  
  110. phid.addTagLostListener(new RFIDTagLostListener()
  111. {
  112. public void onTagLost(RFIDTagLostEvent e)
  113. {
  114. System.out.println("Tag lost: " + e.getTag());
  115. }
  116. });
  117.  
  118. try {
  119. /*
  120. * Please review the Phidget22 channel matching documentation for details on the device
  121. * and class architecture of Phidget22, and how channels are matched to device features.
  122. */
  123.  
  124. // Open and start detecting rfid cards
  125. phid.open(5000); // wait 5 seconds for device to respond
  126.  
  127. // Display info on currently connected devices
  128. System.out.println("Device Name " + phid.getDeviceName());
  129. System.out.println("Serial Number " + phid.getDeviceSerialNumber());
  130. System.out.println("Device Version " + phid.getDeviceVersion());
  131.  
  132.  
  133. phid.setAntennaEnabled(true);
  134.  
  135.  
  136. System.out.println("\n\nGathering data for 15 seconds\n\n");
  137. pause(15);
  138.  
  139. phid.close();
  140. System.out.println("\nClosed RFID");
  141.  
  142. } catch (PhidgetException ex)
  143. {
  144. System.out.println(ex.getDescription());
  145. }
  146. }
  147.  
  148.  
  149. //Using a boolean to checkTag
  150. private boolean checkTag(String tagStr)
  151. {
  152. if (tagItemsList.contains(tagStr)) {
  153. System.out.println("HELLO AND WELCOME MY FRIEND!");
  154. return true;
  155.  
  156. }
  157. else {
  158. System.out.println("UNKNOWN TAG. PLEASE GO AWAY.");
  159. return false;
  160. }
  161. }
  162.  
  163.  
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement