AWS IOT の shadow の値を読む方法

AWS IOT の shadow の値を読む方法です。

エンドポイントは次のコマンドで調べます。

```bash
aws iot describe-endpoint --endpoint-type iot:Data-ATS
```

エンドポイント が次の場合のサンプルです。
>https://abcd6goq68zt4o-ats.iot.ap-northeast-1.amazonaws.com

もの
>violet

##Curl##

```bash:curl_get.sh
#
#	curl_get.sh
#
#					Sep/11/2021
#
HOST="https://abcd6goq68zt4o-ats.iot.ap-northeast-1.amazonaws.com:8443"
THINGNAME="violet"
URL=$HOST"/things/"$THINGNAME"/shadow"
#
curl --tlsv1.2 \
	--cert ../certs/device.pem.crt \
	--key ../certs/private.pem.key \
	-X GET $URL
#
```

##HTTPie##

```bash:http_get.sh
#
#	http_get.sh
#
#					Sep/11/2021
#
HOST="https://abcd6goq68zt4o-ats.iot.ap-northeast-1.amazonaws.com:8443"
THINGNAME="violet"
URL=$HOST"/things/"$THINGNAME"/shadow"
#
http --cert ../certs/device.pem.crt \
	--cert-key ../certs/private.pem.key \
	$URL
#
```

##Python3##

```py:get_value.py
#! /usr/bin/python
#
#	get_value.py
#
#					Sep/11/2021
# ------------------------------------------------------------------
import requests
import sys

host = "https://abcd6goq68zt4o-ats.iot.ap-northeast-1.amazonaws.com:8443"
thingname="violet"
url = host + "/things/" + thingname + "/shadow"
cert = "../certs/device.pem.crt"
key = "../certs/private.pem.key"
#
publish = requests.request('GET', url, cert=[cert, key])
#
sys.stderr.write("Response status: " + str(publish.status_code) + "\n")
if publish.status_code == 200:
	print(publish.text)
#
# ------------------------------------------------------------------
```

##mosquitto_sub##

```bash:get_mosquitto.sh
#! /bin/bash
#
#	get_mosquitto.sh
#
#						Sep/11/2021
cafile="../certs/Amazon-root-CA-1.pem"
certfile="../certs/device.pem.crt"
keyfile="../certs/private.pem.key"
#
host="abcd6goq68zt4o-ats.iot.ap-northeast-1.amazonaws.com"

port=8883
device="violet"
topic="\$aws/things/"$device"/shadow/update/documents"
qos=1
#
mosquitto_sub -d --cafile $cafile --cert $certfile \
	--key $keyfile -q $qos -h $host -p $port \
	-t $topic
  ```

##Paho##

```py:get_paho.py
#!/usr/bin/python
#
#	get_paho.py
#
#					Sep/11/2021
# ------------------------------------------------------------------
import paho.mqtt.client as mqtt
import ssl

host = 'abcd6goq68zt4o-ats.iot.ap-northeast-1.amazonaws.com'
port = 8883
cacert = '../certs/Amazon-root-CA-1.pem'
clientCert = '../certs/device.pem.crt'
clientKey = '../certs/private.pem.key'

thing="violet"
topic = "$aws/things/" + thing + "/shadow/update/documents"

# ------------------------------------------------------------------
def on_connect(client, userdata, flags, respons_code):
	print('status {0}'.format(respons_code))
	client.subscribe(topic)

# ------------------------------------------------------------------
def on_message(client, userdata, msg):
	print(msg.topic + ' ' + str(msg.payload))

# ------------------------------------------------------------------
client = mqtt.Client(client_id= "python",protocol=mqtt.MQTTv311)
	### SSL
client.tls_set(cacert,
	certfile = clientCert,
	keyfile = clientKey,
	tls_version = ssl.PROTOCOL_TLSv1_2)
client.tls_insecure_set(True)

client.on_connect = on_connect
client.on_message = on_message

client.connect(host, port=port, keepalive=6)
client.loop_forever()
#
# ------------------------------------------------------------------
```

##Boto3##

```py:get_thing_shadow.py
#! /usr/bin/python
# coding: utf-8
#
#	get_thing_shadow.py
#
#					Sep/28/2021
# ------------------------------------------------------------------
import sys
import json
import boto3

# ------------------------------------------------------------------
url='https://abcd6goq68zt4o-ats.iot.ap-northeast-1.amazonaws.com'
thing='violet'
#
iot_data = boto3.client('iot-data', endpoint_url=url)

res = iot_data.get_thing_shadow(thingName=thing)

bb=res['payload'].read()
print(bb.decode())
# ------------------------------------------------------------------
```

実行結果

```text
$ ./get_thing_shadow.py 
{"state":{"desired":{"power":"on"},"reported":{"power":"off"},"delta":{"power":"on"}},"metadata":{"desired":{"power":{"timestamp":1632782252}},"reported":{"power":{"timestamp":1632782577}}},"version":5,"timestamp":1632786613}
```