top of page

施設園芸のための環境制御システムUECSをOpenBlocks IoTで受信する (② CLOUD SHIP API編)



はじめに


前回は OpenBlocks IoT VX2 で UECS(ユビキタス環境制御システム)データを受信する方法について紹介しました。今回は受信した UECS データをパースし、時系列データベース CLOUDSHIP に送信する方法について Python を使って説明します。


UECS のパースと CLOUDSHIP への送信



UECS のパース


UECS データは下記のような XML 形式です。


<?xml version="1.0"?>
<UECS ver="1.00-E10">
<DATA type="InAirTemp" room="1" region="1" order="1" priority="29">23.0</DATA>
<IP>192.168.1.64</IP>
</UECS>


Python で XML をパースするには、xml.etree.ElementTree モジュールを使用します。以下は、UECS データをパースするコードの例です。


import xml.etree.ElementTree as ET

xml_data = '''
<?xml version="1.0"?>
<UECS ver="1.00-E10">
<DATA type="InAirTemp" room="1" region="1" order="1" priority="29">23.0</DATA>
<IP>192.168.1.64</IP>
</UECS>
'''

root = ET.fromstring(xml_data)
data = root.find('DATA')

uecs_data = {
    'type': data.get('type'),
    'room': int(data.get('room')),
    'region': int(data.get('region')),
    'order': int(data.get('order')),
    'priority': int(data.get('priority')),
    'value': float(data.text),
    'ip': root.find('IP').text
}

print(uecs_data)

このコードでは、`xml.etree.ElementTree` モジュールを使用して XML 文字列をパースし、`DATA` 要素と `IP` 要素の値を取得しています。取得した値は `uecs_data` 辞書に格納されます。


CLOUDSHIP への送信


UECS データと CLOUDSHIP のデータ形式をマッピングするために、下記のような JSON 形式のマッピングデータを用意します。pointIdはCLOUDSHIPがパラメータの識別のために用いるもので、一意の文字列であれば使用可能です。ここではURI形式で指定しています。pointIdをUECSのipAddress, room, region...といったデータ属性と対応づけています。


[
  {
    "pointId": "promptk.jp/promotion/uecs/device0001/cnd.cMC",
    "ipAddress": "192.168.1.70",
    "room": 1,
    "region": 11,
    "order": 1,
    "type": "cnd.cMC",
    "label": "機器動作状態"
  },
  {
    "pointId": "promptk.jp/promotion/uecs/device0001/IntgRadiation.cMC",
    "ipAddress": "192.168.1.70",
    "room": 1,
    "region": 11,
    "order": 1,
    "type": "IntgRadiation.cMC",
    "label": "温室内日射量"
  },
  ...
]


以下は、パースした UECS データを CLOUDSHIP のデータ形式に変換し、送信するコードの例です。


import json
import requests
from datetime import datetime

def convert_to_cs_data(uecs_data, mappings):
    cs_data = {'Data': []}
    for mapping in mappings:
        if (uecs_data['type'] == mapping['type'] and
            uecs_data['room'] == mapping['room'] and
            uecs_data['region'] == mapping['region'] and
            uecs_data['order'] == mapping['order'] and
            uecs_data['ip'] == mapping['ipAddress']):

            cs_data['Data'].append({
                'PointID': mapping['pointId'],
                'Values': [
                    {
                        'TimeStamp': datetime.now().isoformat(),
                        'Value': uecs_data['value']
                    }
                ]
            })
            break
    return cs_data

def send_to_cloudship(data):
    url = 'https://your-api-gateway-url'
    headers = {
        'Content-Type': 'application/json',
        'X-API-Key': 'your-api-key'
    }

    response = requests.put(url, data=json.dumps(data), headers=headers)

    
    if response.status_code != 200:
        raise Exception(f'Failed to send data to CLOUDSHIP: {response.status_code}')

# マッピングデータを読み込む
with open('mapping.json') as f:
    mappings = json.load(f)

# UECS データを変換して CLOUDSHIP に送信
cs_data = convert_to_cs_data(uecs_data, mappings)
send_to_cloudship(cs_data)

このコードでは、convert_to_cs_data 関数を使用して、パースした UECS データを CLOUDSHIP のデータ形式に変換しています。変換には、mapping.json ファイルに定義されたマッピングデータを使用します。


変換したデータは send_to_cloudship 関数で CLOUDSHIP に送信されます。この関数では、requests モジュールを使用して HTTP PUT リクエストを送信します。のX-API-Key ヘッダーには、Amazon API Gatewayであらかじめ発行した API キーを設定します。


まとめ


今回は OpenBlocks IoT VX2 で受信した UECS データをパースし、CLOUDSHIP に送信する方法を Python を使って紹介しました。CLOUDSHIP に送信された時系列データは Realboard で可視化することで見える化や分析が可能です。こちらをぜひご覧ください

閲覧数:15回
bottom of page