package phidgets; import com.phidgets.*; import com.phidgets.event.*; import java.net.*; import java.io.*; import java.util.Date; //public class GPSChimes implements Runnable, AttachListener, DetachListener, ErrorListener, ServoPositionChangeListener { public class GPSChimes implements Runnable { Thread t; ServoPhidget servo; boolean attached; boolean keepGoing; public GPSChimes(){ keepGoing = true; attached = true; t = new Thread(this); t.start(); } /* public void attached(AttachEvent ae) { attached = true; } public void detached(DetachEvent ae) { attached = false; } public void error(ErrorEvent ee) { kill(); } public void servoPositionChanged(ServoPositionChangeEvent oe){ System.out.println(oe); } */ public void run(){ try { while (keepGoing){ boolean ringIt = checkServer(); if (ringIt){ if (servo != null) kill(); try { servo = new ServoPhidget(); // servo.addAttachListener(this); // servo.addDetachListener(this); // servo.addErrorListener(this); // servo.addServoPositionChangeListener(this); servo.openAny(); servo.waitForAttachment(); for (int i = 15; i < 230; i++) { servo.setPosition(0, i); Thread.sleep(10); // System.out.println("Position: " + servo.getPosition(0)); } servo.setPosition(0, -23); } catch (Exception ep){ ep.printStackTrace(System.out); } } kill(); t.sleep(1000 * 60 * 5);//5 minutes System.gc(); } } catch (Exception ep){ ep.printStackTrace(System.out); } } private void kill() { try { if (servo != null) servo.close(); servo = null; } catch (Exception ep){ ep.printStackTrace(System.out); } } private boolean checkServer(){ try { int lineCounter = 0; double distance = 100.0d; long time = 0l; URL u = new URL("http://USE_YOUR_OWN_DATA_FEED"); HttpURLConnection hc = (HttpURLConnection)u.openConnection(); BufferedReader buf = new BufferedReader(new InputStreamReader(hc.getInputStream())); String inputLine; while ((inputLine = buf.readLine()) != null){ String s = inputLine.trim(); if (s.length() > 0){ // System.out.println(inputLine); if (lineCounter == 0){ String[] sx = s.split("//"); distance = Double.parseDouble(sx[0].trim()); lineCounter++; } else if (lineCounter == 1){ String[] sx = s.split("//"); time = Long.parseLong(sx[0].trim()); lineCounter++; } } } long diff = (new Date()).getTime() - (new Date(time)).getTime(); diff = (diff/1000)/60; int minAgo = (int)diff; System.out.println("distance = " + distance); System.out.println("minutes ago = " + minAgo); if (distance <= 1.0 && minAgo < 8){ return true; } } catch (Exception eps){ eps.printStackTrace(System.out); } return false; } public static final void main(String args[]) throws Exception { GPSChimes gpsc = new GPSChimes(); } }