@Override protected AsyncTaskResult<String> doInBackground(final Void... voids) { try { UpdateThingShadowRequest request = new UpdateThingShadowRequest(); request.setThingName(thingName); ByteBuffer payloadBuffer = ByteBuffer.wrap(updateState.getBytes()); request.setPayload(payloadBuffer); UpdateThingShadowResult result = mIotDataClient.updateThingShadow(request); byte[] bytes = new byte[result.getPayload().remaining()]; result.getPayload().get(bytes); String resultString = new String(bytes); mLatch.countDown(); return new AsyncTaskResult<>(resultString); } catch (Exception e) { if (DEBUG) { Log.e(TAG, "Error on UpdateShadowTask", e); } mLatch.countDown(); return new AsyncTaskResult<>(e); } }
@Override protected AsyncTaskResult<String> doInBackground(Void... voids) { try { UpdateThingShadowRequest request = new UpdateThingShadowRequest(); request.setThingName(thingName); ByteBuffer payloadBuffer = ByteBuffer.wrap(updateState.getBytes()); request.setPayload(payloadBuffer); UpdateThingShadowResult result = iotDataClient.updateThingShadow(request); 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(UpdateShadowTask.class.getCanonicalName(), "updateShadowTask", e); return new AsyncTaskResult<String>(e); } }
private void publishState(final String thingName, final String json) throws AlexaStateException { final ByteBuffer buffer; try { buffer = ByteBuffer.wrap(json.getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { final String error = format("Could not prepare JSON for model state publication to thing shadow '%1$s'", thingName); log.error(error, e); throw AlexaStateException.create(error).withCause(e).withHandler(this).build(); } final UpdateThingShadowRequest iotRequest = new UpdateThingShadowRequest().withThingName(thingName).withPayload(buffer); awsDataClient.updateThingShadow(iotRequest); }
@Override public void updateThing(Thing thing) { if (thing == null || StringUtils.isBlank(thing.getId())) { return; } String id = cloudIDForThing(thing); try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { Object data = Collections.singletonMap("state", Collections.singletonMap("desired", thing.getDeviceState())); ParaObjectUtils.getJsonWriterNoIdent().writeValue(baos, data); getDataClient().updateThingShadow(new UpdateThingShadowRequest(). withThingName(id).withPayload(ByteBuffer.wrap(baos.toByteArray()))); } catch (Exception ex) { logger.warn("Failed to connect to IoT device {}: {}", id, ex.getMessage()); } }