`aws iot-data`命令和AWS保留主题$

问题描述

我是AWS IoT的新手,现在尝试使用现有资源来了解主要概念。 在使用 @RunWith(SpringJUnit4ClassRunner.class) @SpringBoottest(classes = MainClass.class) @WebAppConfiguration public class TestWebApplication { @Autowired private WebApplicationContext webApplicationContext; private mockmvc mockmvc; @Before public void setUp() { mockmvc = mockmvcBuilders.webAppContextSetup(webApplicationContext).build(); } @Test public void testabc() throws Exception{ String[] arr ={"A","B"}; ObjectMapper mapper = new ObjectMapper(); mapper.configure(SerializationFeature.WRAP_ROOT_VALUE,false); ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter(); String requestJson=ow.writeValueAsstring(arr ); mockmvc.perform( post("/1").contentType( MediaType.APPLICATION_JSON).content(requestJson)); } 命令尝试将数据发布到AWS保留主题之一中时,我遇到一个奇怪的行为。 假设我想更新名为aws iot-data的{​​{1}}事物的名为Shadow的影子(我正在stubShadow中使用stub标签):

Test

而且效果很好,我可以观察AWS IoT Dashboard主题进行更新。

现在,我想使用 aws iot-data update-thing-shadow --thing-name stub --shadow-name stubShadow \ --cli-binary-format raw-in-base64-out \ --payload '{"state":{"desired":{"ColorRGB":[0,11,11]}},"clientToken":"21b21b21-bfd2-4279-8c65-e2f697ff4fab"}' /dev/stdout 参数发布消息。这是一个示例:

$aws/things/stub/shadow/name/stubShadow/update/accepted

什么也没有发生... 我想知道将此命令发送直接消息到AWS服务主题有什么问题吗?我想念什么吗?因为对于常规(手动创建)主题,它效果很好。

解决方法

正如document所说,

payload是stringify json消息的base64编码表示形式

{"state":{"reported":{"ColorRGB":[0,11,11]}}}

console.log(btoa(JSON.stringify({"state":{"reported":{"ColorRGB":[0,11]}}})))

有效载荷:eyJzdGF0ZSI6eyJyZXBvcnRlZCI6eyJDb2xvclJHQiI6WzAsMTEsMTFdfX19

aws iot-data publish --topic "$aws/things/stub/shadow/name/stubShadow/update" --payload 'eyJzdGF0ZSI6eyJyZXBvcnRlZCI6eyJDb2xvclJHQiI6WzAsMTEsMTFdfX19'