About me
home
Portfolio
home
🪢

4 FIWARE with Python 실습

앞서 3. Custom FIWARE 매뉴얼 마지막 부분에 설명했듯이 결국 현재 터미널에서 curl로 보내는 request를 코드로 프로그램화 해야한다.
이번 매뉴얼에서는 해당 과정을 처리해본다.
모든 요청은 Raspberry Pi 4B 2GB에서 전송하며 코드 역시 해당 플랫폼에서 실행한다.
3. Custom FIWARE 에 이어서 해야 된다. (서비스 그룹 프로비저닝 한 상태로 이어서 해야 됨.)

Curl on Python

먼저 requests 모듈을 설치한다.
pip install requests
Python
복사
결국 기존에 Json type data를 전송하기에 Python 코드도 역시 해당 부분을 보내야 한다.
간단하게 아래 사이트를 보면 curl 명령어를 python 명령어로 변환해줌을 알 수 있다.
예를 들면 디바이스를 등록하는 코드는 아래와 같이 변환된다.
<일단 Curl 코드> curl -L -X POST 'http://172.16.63.209:4041/iot/devices' \ -H 'fiware-service: youngcustomdevice' \ -H 'fiware-servicepath: /escTest' \ -H 'Content-Type: application/json' \ --data-raw '{ "devices": [ { "device_id": "acoustic01", "entity_name": "urn:ngsi-ld:Device:acoustic01", "entity_type": "Device", "timezone": "Asia/Seoul", "attributes": [ { "object_id": "t", "name": "acoustic", "type": "Property", "metadata": { "unitCode": { "type": "Property", "value": "PER" } } } ], "static_attributes": [ {"name": "category", "type":"Property", "value": ["sensor"]}, {"name": "supportedProtocol", "type": "Property", "value": ["ul20"]} ] } ] }'
JavaScript
복사
Python으로 변환된 코드는 아래와 같다.
import requests headers = { 'fiware-service': 'youngcustomdevice', 'fiware-servicepath': '/escTest', # Already added when you pass json= but not when you pass data= # 'Content-Type': 'application/json', } json_data = { 'devices': [ { 'device_id': 'acoustic01', 'entity_name': 'urn:ngsi-ld:Device:acoustic01', 'entity_type': 'Device', 'timezone': 'Asia/Seoul', 'attributes': [ { 'object_id': 't', 'name': 'acoustic', 'type': 'Property', 'metadata': { 'unitCode': { 'type': 'Property', 'value': 'PER', }, }, }, ], 'static_attributes': [ { 'name': 'category', 'type': 'Property', 'value': [ 'sensor', ], }, { 'name': 'supportedProtocol', 'type': 'Property', 'value': [ 'ul20', ], }, ], }, ], } response = requests.post('http://172.16.63.209:4041/iot/devices', headers=headers, json=json_data)
Python
복사
디바이스 등록 시 실질적으로 바꿔줄 부분은 device_id, entity_name 뿐이다. 이를 변수화하여 넣어주자.
import requests custom_device_id = 'acoustic02' headers = { 'fiware-service': 'youngcustomdevice', 'fiware-servicepath': '/escTest', # Already added when you pass json= but not when you pass data= # 'Content-Type': 'application/json', } json_data = { 'devices': [ { 'device_id': custom_device_id, 'entity_name': 'urn:ngsi-ld:Device:'+custom_device_id, 'entity_type': 'Device', 'timezone': 'Asia/Seoul', 'attributes': [ { 'object_id': 't', 'name': 'acoustic', 'type': 'Property', 'metadata': { 'unitCode': { 'type': 'Property', 'value': 'PER', }, }, }, ], 'static_attributes': [ { 'name': 'category', 'type': 'Property', 'value': [ 'sensor', ], }, { 'name': 'supportedProtocol', 'type': 'Property', 'value': [ 'ul20', ], }, ], }, ], } response = requests.post('http://172.16.63.209:4041/iot/devices', headers=headers, json=json_data)
Python
복사
로컬 서버에서 서비스에 등록된 모든 디바이스를 출력하면 아래와 같이 등록한 acoustic02 센서가 등록되어있음을 볼 수 있다.
출력 요청
curl -L -X GET 'http://localhost:1026/ngsi-ld/v1/entities/?type=Device' \ -H 'NGSILD-Tenant: youngcustomdevice' \ -H 'NGSILD-Path: /escTest' \ -H 'Link: <http://context/ngsi-context.jsonld>; rel="http://www.w3.org/ns/json-ld#context"; type="application/ld+json"' | python3 -m json.tool
JavaScript
복사
결과 코드
"@context": "http://context/ngsi-context.jsonld", "id": "urn:ngsi-ld:Device:acoustic02", "type": "Device", "category": { "type": "Property", "value": "sensor" }, "supportedProtocol": { "type": "Property", "value": "ul20" }, "location": { "type": "GeoProperty", "value": { "type": "Point", "coordinates": [ 0, 0 ] } } }
JavaScript
복사
디바이스를 등록했으면 이제 데이터를 전송해보자.
큰 틀은 같다. 기존 curl 명령어를 python으로 전송하기 위해 header, data로 나누고 전송하는 것이다.
import requests headers = { 'Content-Type': 'text/plain', } params = { 'k': 'rtestaeyoungtest0710', 'i': 'acoustic02', ##오타였던듯 잘 확인하기 } # 실제 데이터를 측정하는 코드가 들어가는 부분 # acquired_data list에 클래스 별 예측 확률이 들어간다 가정하자 acquired_data = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0] sending_data = '' for i in range(len(acquired_data)): sending_data += 'c'+str(i+1)+'|'+str(acquired_data[i])+'|' sending_data = sending_data[:-1] response = requests.post('http://172.16.63.209:7896/iot/d', params=params, headers=headers, data=sending_data)
Python
복사
동일하게 결과를 보면 아래와 같이 정확하게 들어감을 볼 수 있다.
curl -L -X GET 'http://localhost:1026/ngsi-ld/v1/entities/?type=Device' \ -H 'NGSILD-Tenant: youngcustomdevice' \ -H 'NGSILD-Path: /escTest' \ -H 'Link: <http://context/ngsi-context.jsonld>; rel="http://www.w3.org/ns/json-ld#context"; type="application/ld+json"' | python3 -m json.tool
JavaScript
복사
{ "@context": "http://context/ngsi-context.jsonld", "id": "urn:ngsi-ld:Device:acoustic02", "type": "Device", "class1": { "type": "Property", "value": 0.1, "observedAt": "2022-07-10T06:51:10.461Z" }, "class10": { "type": "Property", "value": 1, "observedAt": "2022-07-10T06:51:10.461Z" }, "class2": { "type": "Property", "value": 0.2, "observedAt": "2022-07-10T06:51:10.461Z" }, "class3": { "type": "Property", "value": 0.3, "observedAt": "2022-07-10T06:51:10.461Z" }, "class4": { "type": "Property", "value": 0.4, "observedAt": "2022-07-10T06:51:10.461Z" }, "class5": { "type": "Property", "value": 0.5, "observedAt": "2022-07-10T06:51:10.461Z" }, "class6": { "type": "Property", "value": 0.6, "observedAt": "2022-07-10T06:51:10.461Z" }, "class7": { "type": "Property", "value": 0.7, "observedAt": "2022-07-10T06:51:10.461Z" }, "class8": { "type": "Property", "value": 0.8, "observedAt": "2022-07-10T06:51:10.461Z" }, "class9": { "type": "Property", "value": 0.9, "observedAt": "2022-07-10T06:51:10.461Z" }, "category": { "type": "Property", "value": "sensor", "observedAt": "2022-07-10T06:51:10.461Z" }, "supportedProtocol": { "type": "Property", "value": "ul20", "observedAt": "2022-07-10T06:51:10.461Z" }, "location": { "type": "GeoProperty", "value": { "type": "Point", "coordinates": [ 0, 0 ] } } }
JavaScript
복사

전부 정상적으로 출력됨 (23.01.26 김영준)