TOP
SSV Software Systems Register  Register
Log in to check your private messages  Log in to check your private messages
Startseite FAQ Search Mitglieder Profile  Log in 
SSV Support-Forum
RTDC mit Raspberry Pi …

 
Post new topic   Reply to topic    SSV-Forum Forum Index >>> Real Time Data Channels (RTDC)
<<< Previous topic - Next topic >>>  
Display posts from previous:   
Author Message
kdw



Joined: 05 May 2006
Posts: 1460

PostPosted: 13.12.2014, 17:38    Post subject: RTDC mit Raspberry Pi … Reply with quote

Hallo Forum.

Auf RTDC-Datenprojekte lässt sich auch problemlos per Raspberry Pi zugreifen. Nehmen wir einfach einmal das folgende Datenprojekt mit dem Namen TEMP und den beiden Daten-Items T_SSV und T_SC als Beispiel:

Code:
01: {
02:     "do": [
03:         {
04:             "id": 10,
05:             "name": "TEMP",
06:             "desc": "Temperaturfuehler",
07:             "property": {},
08:             "item": [
09:                {
10:                     "id": 49,
11:                     "name": "T_SSV",
12:                     "data": [
13:                         1416655631,
14:                         "18.2"
15:                     ],
16:                     "type": "GAUGE",
17:                     "desc": "Temperatur Demo SSV",
18:                     "property": {
19:                         "unit": "C"
20:                     }
21:                 },
22:                 {
23:                     "id": 50,
24:                     "name": "T_SC",
25:                     "data": [
26:                         1416583523,
27:                         "23.6"
28:                     ],
29:                     "type": "GAUGE",
30:                     "desc": "Temperatur Demo SC",
31:                     "property": {
32:                         "unit": "C"
33:                     }
34:                 }
35:             ]
36:         }
37:     ]
38: }


Durch dieses Datenprojekt ergeben sich die folgenden RTDC-REST-API-Links für HTTP-Zugriffe:

Code:
01: http://<SERVER:PORT>/rtdc/v0/?get=do
02: http://<SERVER:PORT>/rtdc/v0/?get=do&do=TEMP
03: http://<SERVER:PORT>/rtdc/v0/?get=data&do=TEMP&item=T_SC
04: http://<SERVER:PORT>/rtdc/v0/?get=data&do=TEMP&item=T_SSV


Mit den ersten beiden Links (Zeilen 01 und 02) lässt sich per REST-API-READ mittels einer HTTP-GET-Abfrage (HTTP GET Request) das komplette Datenprojekt TEMP abfragen – die HTTP Response liefert in diesem Fall die vollständigen JSON-Daten zum RTDC-Datenprojekt. Der Link in Zeile 03 dient zur Abfrage des Daten-Items T_SC, der Link in Zeile 04 bezieht sich auf T_SSV. In beiden Fällen liefert die jeweilige HTTP Response JSON-Daten mit dem betreffenden Item. Hier ein Python-Beispiel-Code für Raspberry Pi:

Code:
import httplib
import socket

try:
    conn = httplib.HTTPConnection('<SERVER>', <PORT>)
    conn.connect()
    request = conn.putrequest('GET', '/rtdc/v0/?get=data&do=TEMP&item=T_SSV')
    headers = {}
    headers['X-RTDC-Auth-Key'] = '<API KEY>'
    headers['Content-Type'] = 'application/json; charset=utf-8'
    for item in headers:
        conn.putheader(item, headers[item])
    conn.endheaders()
    response = conn.getresponse()
    print response.status, response.reason
    print response.read()
    conn.close()
except (httplib.HTTPException, socket.error) as ex:
    print 'RTDC service not ready ...'


Speichert man diesen Code auf dem Raspberry Pi in einer Datei mit dem Namen rtdc_rest_get_td.py und ersetzt die Platzhalter für <SERVER:PORT>, <SERVER>, <PORT> und <API KEY> durch gültige Zeichenfolgen, ist über die folgende Raspberry Pi-Kommandozeile die Abfrage des Items T_SSV möglich:

Code:
python rtdc_rest_get_td.py


Mit Hilfe einer HTTP-PUT-Anfrage (HTTP PUT Request) lassen sich die Daten-Items T_SSV und T_SC des Datenobjekt TEMP mit neuen Werten versehen. Hier der Python-Beispiel-Code für Raspberry Pi:

Code:
import httplib
import socket

# body_js = '<JSON DATA FOR T_SC>'
body_js = '<JSON DATA FOR T_SSV>'

try:
    conn = httplib.HTTPConnection('<SERVER>', <PORT>)
    conn.connect()
    request = conn.putrequest('PUT', '/rtdc/v0/')
    headers = {}
    headers['X-RTDC-Auth-Key'] = '<API KEY1>'
    headers['X-RTDC-Access-Key'] = '<API KEY2>'
    headers['Content-Type'] = 'application/json; charset=utf-8'
    headers['Content-Length'] = "%d"%(len(body_js))
    for item in headers:
        conn.putheader(item, headers[item])
    conn.endheaders()
    conn.send(body_js)
    response = conn.getresponse()
    print response.status, response.reason
    print response.read()
    conn.close()
except (httplib.HTTPException, socket.error) as ex:
    print 'RTDC service not ready ...'


Speichert man diesen Code auf dem Raspberry Pi in einer Datei mit dem Namen rtdc_rest_put_td.py und ersetzt die Platzhalter für <SERVER>, <PORT>, <API KEY1>, <API KEY2> und <JSON DATA FOR …> durch gültige Zeichenfolgen, ist über die folgende Raspberry Pi-Kommandozeile ein Daten-Update des gewünschten Items möglich:

Code:
python rtdc_rest_put_td.py   


Beispiel für <JSON DATA FOR T_SC>
Code:
{"do":[{"name":"TEMP","item":[{"name":"T_SC","data": 12.3}]}]}


Beispiel für <JSON DATA FOR T_SSV>
Code:
{"do":[{"name":"TEMP","item":[{"name":"T_SSV","data": 12.3}]}]}


Beide Beispiele setzen selbstverständlich voraus, dass der Raspberry Pi eine Verbindung zum Internet besitzt.

Gruß KDW
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    SSV-Forum Forum Index >>> Real Time Data Channels (RTDC) All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

SSV Software Systems GmbH

Dünenweg 5
30419 Hannover

Fon: +49(0)511  ·  40 000-0
Fax: +49(0)511  ·  40 000-40

sales@ssv-embedded.de


Impressum    ·    Datenschutz    ·    AGB

© 2023 SSV SOFTWARE SYSTEMS GmbH. Alle Rechte vorbehalten.

ISO 9001:2015