@GET @Path("/entities/{id}/readings") @Produces(MediaType.APPLICATION_JSON) public Response getReadings(@Context UriInfo uriInfo, @PathParam("id") long id) throws Exception { try { Map<String, List<String>> params = uriInfo.getQueryParameters(); /* * from, to, function, rollup, all_interva, attribute_id, offset, limit */ int attribute_id = toInt(getLatestParam(params, "attribute_id"), -1); SimpleCache.Element cache = SimpleCache.getCache().getElement(DATA_SOURCE_CACHE_KEY); if (!cache.isValid() && SimpleCache.getCache().doUpdate(cache)) { DataSourceListUpdater.runUpdater(); } AakDataService service = AakDataService.createService(); @SuppressWarnings("unchecked") List<Device> devices = (List<Device>) cache.value; Device d = service.getDevice(devices, id); if (d == null) return Response.status(Status.NOT_FOUND) .entity( "[{ \"success\":\"false\", \"message\":\"Not data source with id: " + id + " could be found\"}]") .build(); DeviceSensor attribute = service.getDeviceAttribute(d, attribute_id); if (attribute == null) return Response.status(Status.NOT_FOUND) .entity( "[{ \"success\":\"false\", \"message\":\"Found not find attribute with id " + attribute_id + "\"}]") .build(); DeviceReadings readings = service.getReadings(d, attribute); ObjectMapper m = new ObjectMapper(); String res = m.writeValueAsString(readings); return Response.ok(res).build(); } catch (Exception e) { System.out.println("Readings failed: " + e.getMessage()); e.printStackTrace(); throw e; } }
@GET @Path("/entities") @Produces(MediaType.APPLICATION_JSON) public String getDataSources() throws JsonGenerationException, JsonMappingException, IOException { try { SimpleCache.Element cache = SimpleCache.getCache().getElement(DATA_SOURCE_CACHE_KEY); if (!cache.isValid() && SimpleCache.getCache().doUpdate(cache)) { DataSourceListUpdater.runUpdater(); } @SuppressWarnings("unchecked") List<Device> devices = (List<Device>) cache.value; ObjectMapper m = new ObjectMapper(); String res = m.writeValueAsString(devices); return res; } catch (Exception e) { System.out.println("GET entities failed: " + e.getMessage()); e.printStackTrace(); throw e; } }