简体   繁体   English

如何使用 AWS CLI 为 IoT 设备发送十六进制字符串?

[英]How do I send a hex string with AWS CLI for IoT devices?

I'm trying to send a stream of data to an IoT device.我正在尝试将 stream 数据发送到 IoT 设备。 The general data flow is:一般数据流为:

  • Send payload via AWS CLI or custom tool written by coworkers.通过 AWS CLI 或同事编写的自定义工具发送有效负载。
  • Lambda function receives base64 encoded payload, converts to hex string, and forwards data to device. Lambda function 接收 base64 编码的有效载荷,转换为十六进制字符串,并将数据转发到设备。
  • Device unescapes the data and then parses it to run a command.设备对数据进行转义,然后对其进行解析以运行命令。

The issue I'm running into is this: we would like to move away from using a very fragile custom tool since we're a small team and the devs who wrote it have moved on.我遇到的问题是:我们希望摆脱使用非常脆弱的自定义工具,因为我们是一个小团队,并且编写它的开发人员已经继续前进。 The AWS CLI only accepts raw or base64 encodings for data, and my current input is a hex string like "C00020000000ff64". AWS CLI 只接受原始或 base64 数据编码,我当前的输入是一个十六进制字符串,如“C00020000000ff64”。 I am able to send data with the following commands in a powershell script:我可以在 powershell 脚本中使用以下命令发送数据:

$tmp="<SOME_HEX_STRING>"
$tmp -split '(.{2})' | %{ if ($_ -ne "") { $payload += [CHAR]([CONVERT]::toint16("$_",16)) }}
aws --region <SOME_AWS_REGION> iot-data publish --topic "<TOPIC_ID_HERE>" --cli-binary-format raw-in-base64-out --payload "$payload"

However, the base64 parsing by the lambda function seems to barf at this approach and I get mangled data on the IoT device.但是,lambda function 解析的 base64 似乎对这种方法感到厌烦,我在物联网设备上得到了错误的数据。 I do not currently have debug access to our server to check the logs, but the actual parsing is done in node as a simple Buffer.from(event.payload, 'base64') , which is then parsed into an object containing a hex string.我目前没有对我们服务器的调试访问权限来检查日志,但实际解析是在节点中作为简单的Buffer.from(event.payload, 'base64')完成的,然后将其解析为包含十六进制字符串的 object . My impression is that the encoding method I'm using for input with the CLI is likely wrong (I strongly suspect that [CHAR] cast, to be honest), but I don't know enough in this situation to be able to identify where the problem is in my code, and Python (the code base for AWS CLI) is not my strong suit, so looking at their repo has only led to dead ends, moreso since there are no examples for using aws iot-data publish .我的印象是,我使用 CLI 进行输入的编码方法可能是错误的(老实说,我强烈怀疑 [CHAR] 转换),但在这种情况下我知道的不够多,无法确定在哪里问题出在我的代码中,并且 Python(AWS CLI 的代码库)不是我的强项,因此查看他们的 repo 只会导致死胡同,而且因为没有使用aws iot-data publish示例。

So my question is this:所以我的问题是:

Does anyone have a reliable method of converting a hex string to base64 or raw binary for input with AWS CLI?有没有人有可靠的方法将十六进制字符串转换为 base64 或 AWS CLI 输入的原始二进制文件?

If the hex string is being passed to Python, I believe it is expecting a UTF8 Base64 Enconded string.如果十六进制字符串被传递给 Python,我相信它期待一个 UTF8 Base64 编码字符串。 Your current $payload is definitely not Base64.您当前的$payload绝对不是 Base64。

This is how you can encode your payload:这是您可以对有效负载进行编码的方式:

$tmp = 'C00020000000ff64'

$payload = [convert]::ToBase64String(
    [System.Text.Encoding]::UTF8.GetBytes($tmp)
)

python3 ./decode.py $payload # => Decoded from Python: b'C00020000000ff64'
  • decode.py解码.py
import base64, sys

print(
    'Decoded from Python: {0}'.format(base64.b64decode(sys.argv[1]))
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM