Python3: AWS IOT の Device Shadow

こちらのスクリプトのいくつかを、Boto3 で書いてみました。
[AWS IOT の Device Shadow](https://qiita.com/ekzemplaro/items/1d6bbc68703aa646380f)

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

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

エンドポイント が次の場合のサンプルです。

>https://abcd6goq68zt4o-ats.iot.ap-northeast-1.amazonaws.com

もの

>sample_sep26

デバイスの状態が OFF であることを通知

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

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

shadowDoc = {'state':{'reported':{'led':'off'}}}
payload = json.dumps(shadowDoc)

res = iot_data.update_thing_shadow(thingName=thing, payload=payload)
#
# ------------------------------------------------------------------
```

デバイスの状態が ON であることを通知

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

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

shadowDoc = {'state':{'reported':{'led':'on'}}}
payload = json.dumps(shadowDoc)

res = iot_data.update_thing_shadow(thingName=thing, payload=payload)
#
# ------------------------------------------------------------------
```

デバイスを OFF にするように指令

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

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

shadowDoc = {'state':{'desired':{'led':'off'}}}
payload = json.dumps(shadowDoc)

res = iot_data.update_thing_shadow(thingName=thing, payload=payload)
#
# ------------------------------------------------------------------
```

デバイスを ON にするように指令

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

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

shadowDoc = {'state':{'desired':{'led':'on'}}}
payload = json.dumps(shadowDoc)

res = iot_data.update_thing_shadow(thingName=thing, payload=payload)
#
# ------------------------------------------------------------------
```

Shadow の状態を取得

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

# ------------------------------------------------------------------
url='https://abcd6goq68zt4o-ats.iot.ap-northeast-1.amazonaws.com'
thing='sample_sep26'
#
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
$ python
Python 3.9.7 (default, Aug 31 2021, 13:28:12) 
[GCC 11.1.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import boto3
>>> boto3.__version__
'1.18.34'
>>> 
```