@Override public void discovery() throws Exception { // Those times are totally dependent with the kind of communication... for (int x = 0; x < DISCOVERY_RETRY; x++) { System.out.println("Delaying 2500 - try no." + x); Things.delay(2500); send("discovery"); resources = receive(); Things.delay(50); if (resources != null) { Logger.getLogger(SerialDevice.class.getName()) .log(Level.INFO, "Things API Compatible Device found! Resource String: {0}", resources); things = new Hashtable<String, Thing>(); thingsList = new ArrayList<Thing>(); connected = true; try { StringTokenizer tokenizer = new StringTokenizer(resources, "|"); this.name = tokenizer.nextToken(); int numberOfComponents = Integer.parseInt(tokenizer.nextToken()); for (int y = 0; y < numberOfComponents; y++) { String name = tokenizer.nextToken(); String type = tokenizer.nextToken(); String port = tokenizer.nextToken(); String value = tokenizer.nextToken(); Thing component = new Thing(this, name, name, port, type, value); this.things.put(name, component); this.thingsList.add(component); } break; } catch (Exception e) { Logger.getLogger(SerialDevice.class.getName()) .log(Level.INFO, "Wrong resource String. Parse error!", e); } } else { Logger.getLogger(SerialDevice.class.getName()) .log(Level.INFO, "Empty Resource String - Nor a Thigns API device", resources); } } }
public static void main(String[] args) { System.out.print("Sensor Luz"); System.out.println(things.execute("/dev/ttyUSB0", "sl", "4")); System.out.print("Sensor Temperatura"); System.out.println(things.execute("/dev/ttyUSB0", "st", "4")); System.out.print("Sensor Distancia 1"); System.out.println(things.execute("/dev/ttyUSB0", "sd1", "4")); System.out.print("Sensor Distancia 2"); System.out.println(things.execute("/dev/ttyUSB0", "sd2", "4")); things.execute("/dev/ttyUSB0", "frente", "4"); delay(1000); Things.things.execute("/dev/ttyUSB0", "parar", "4"); delay(1000); Things.things.execute("/dev/ttyUSB0", "servo", "90"); delay(1000); things.execute("/dev/ttyUSB0", "servo", "110"); Things.delay(1000); things.execute("/dev/ttyUSB0", "servo", "130"); delay(1000); things.close(); }