public static void newSensorData(String deviceName, String data) { Device found = null; for (Device device : Kernel.getInstance().getDevices()) { if (device.getName().equals(deviceName)) { found = device; } } if (found == null) { return; } JSONObject jsonObject = (JSONObject) JSONValue.parse(data); if (jsonObject == null || jsonObject.keySet() == null || jsonObject.keySet().iterator() == null) { // System.out.println("Erro json " + data); return; } JSONArray components = (JSONArray) jsonObject.get("components"); Iterator i = components.iterator(); while (i.hasNext()) { Object oo = i.next(); JSONObject joo = (JSONObject) oo; String thing = joo.get("name").toString(); String value = joo.get("value").toString(); found.getThings().get(thing).setLastValue(value); } }
@GET @Produces("text/html") @Path("/data/{device}/{sensor}") public String execute( @PathParam("device") String deviceName, @PathParam("sensor") String sensor) { for (Device device : Kernel.getInstance().getDevices()) { if (device.getName().equals(deviceName)) { Thing thing = device.getThings().get(sensor); return thing.getLastValue(); } else { return "Sensor not found"; } } return "Device not found"; }
@GET @Produces("text/html") @Path("/{device}/{sensor}") public String read(@PathParam("device") String deviceName, @PathParam("sensor") String sensor) { if (deviceName != null && sensor != null) { for (Device d : Kernel.getInstance().devices) { if (d.getName().equals(deviceName)) { try { return d.getThings().get(sensor).execute(sensor); } catch (Exception ex) { Logger.getLogger(Manager.class.getName()) .log( Level.SEVERE, "Error sending Serial Message to {0}. {1}", new Object[] {d.getName(), ex.getMessage()}); } } } } return "not found"; }