Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.io.UnsupportedEncodingException;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.net.URLEncoder;
- import java.util.ArrayList;
- import java.util.Arrays;
- import com.google.gson.Gson;
- import com.phidget22.*;
- public class RFIDServo
- {
- //Declare RFID and set as phid
- RFID phid = new RFID();
- final String[] tagItemsArray = {"123"};
- ArrayList<String> tagItemsList = new ArrayList<String>(Arrays.asList(tagItemsArray));
- //Declare Publisher and set as pub
- Publisher pub = new Publisher();
- // Declare a default sensor object (no location, name/value set later)
- // This will only work when you have SensorData class developed
- // and a SensorServer developed that accepts json strings, as in the lab
- //RFIDServo oneSensor = new RFIDServo();
- //SensorData oneSensor = new SensorData("unknown", "unknown", "16024003");
- // Declare GSON utility object
- Gson gson = new Gson();
- // Declare String to hold JSON representation of sensor object data
- //String oneSensorJson = new String();
- // address of server which will receive sensor data
- // Altered to point to server to deal with incoming json parameters
- public static String sensorServerURL = "http://localhost:8080/MADServerSide/SensorServerDB";
- public static void main(String[] args) throws PhidgetException
- {
- new RFIDServo();
- }
- public String sendToServer(String oneSensorJson)
- {
- System.out.println("test");
- URL url;
- HttpURLConnection conn;
- BufferedReader rd;
- // Replace invalid URL characters from json string
- try {
- oneSensorJson = URLEncoder.encode(oneSensorJson, "UTF-8");
- } catch (UnsupportedEncodingException e1) {
- e1.printStackTrace();
- }
- String fullURL = sensorServerURL + "?sensordata="+oneSensorJson;
- System.out.println("Sending data to: "+fullURL); // DEBUG confirmation message
- String line;
- String result = "";
- try {
- url = new URL(fullURL);
- conn = (HttpURLConnection) url.openConnection();
- conn.setRequestMethod("GET");
- rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
- // Request response from server to enable URL to be opened
- while ((line = rd.readLine()) != null) {
- result += line;
- }
- rd.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return result;
- }
- private void pause(int secs)
- {
- try {
- Thread.sleep(secs*1000);
- } catch (InterruptedException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- }
- public RFIDServo() throws PhidgetException
- {
- // Make the RFID Phidget able to detect loss or gain of an rfid card
- phid.addTagListener(new RFIDTagListener()
- {
- public void onTag(RFIDTagEvent e) {
- System.out.println("Tag read: " + e.getTag());
- if (checkTag(e.getTag()))
- try {
- pub.publishRfid(e.getTag()); //Publishing my tag
- //oneSensor.sendToServer();
- } catch (Exception ex) {
- }
- }
- });
- phid.addTagLostListener(new RFIDTagLostListener()
- {
- public void onTagLost(RFIDTagLostEvent e)
- {
- System.out.println("Tag lost: " + e.getTag());
- }
- });
- try {
- /*
- * Please review the Phidget22 channel matching documentation for details on the device
- * and class architecture of Phidget22, and how channels are matched to device features.
- */
- // Open and start detecting rfid cards
- phid.open(5000); // wait 5 seconds for device to respond
- // Display info on currently connected devices
- System.out.println("Device Name " + phid.getDeviceName());
- System.out.println("Serial Number " + phid.getDeviceSerialNumber());
- System.out.println("Device Version " + phid.getDeviceVersion());
- phid.setAntennaEnabled(true);
- System.out.println("\n\nGathering data for 15 seconds\n\n");
- pause(15);
- phid.close();
- System.out.println("\nClosed RFID");
- } catch (PhidgetException ex)
- {
- System.out.println(ex.getDescription());
- }
- }
- //Using a boolean to checkTag
- private boolean checkTag(String tagStr)
- {
- if (tagItemsList.contains(tagStr)) {
- System.out.println("HELLO AND WELCOME MY FRIEND!");
- return true;
- }
- else {
- System.out.println("UNKNOWN TAG. PLEASE GO AWAY.");
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement