@Override protected AsyncTaskResult<String> doInBackground(final Void... voids) { try { GetThingShadowRequest getThingShadowRequest = new GetThingShadowRequest().withThingName(thingName); GetThingShadowResult result = mIotDataClient.getThingShadow(getThingShadowRequest); byte[] bytes = new byte[result.getPayload().remaining()]; result.getPayload().get(bytes); String resultString = new String(bytes); return new AsyncTaskResult<>(resultString); } catch (Exception e) { if (DEBUG) { Log.e(TAG, "Error on GetShadowTask", e); } return new AsyncTaskResult<>(""); } }
@Override @SuppressWarnings("unchecked") public Map<String, Object> readThing(Thing thing) { if (thing == null || StringUtils.isBlank(thing.getId())) { return Collections.emptyMap(); } String id = cloudIDForThing(thing); ByteBuffer bb = getDataClient().getThingShadow(new GetThingShadowRequest().withThingName(id)).getPayload(); if (bb != null) { try (ByteArrayInputStream bais = new ByteArrayInputStream(bb.array())) { Map<String, Object> payload = ParaObjectUtils.getJsonReader(Map.class).readValue(bais); if (payload != null && payload.containsKey("state")) { return (Map<String, Object>) ((Map<String, Object>) payload.get("state")).get("desired"); } } catch (Exception ex) { logger.warn("Failed to connect to IoT device {}: {}", id, ex.getMessage()); } } return Collections.emptyMap(); }
@Override protected AsyncTaskResult<String> doInBackground(Void... voids) { try { GetThingShadowRequest getThingShadowRequest = new GetThingShadowRequest() .withThingName(thingName); GetThingShadowResult result = iotDataClient.getThingShadow(getThingShadowRequest); byte[] bytes = new byte[result.getPayload().remaining()]; result.getPayload().get(bytes); String resultString = new String(bytes); return new AsyncTaskResult<String>(resultString); } catch (Exception e) { Log.e("E", "getShadowTask", e); return new AsyncTaskResult<String>(e); } }