Personal tools

Difference between revisions of "Data storage example"

From PhotoVoltaic Logger new generation

Jump to: navigation, search
m (Insert data)
m (Default)
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
== Data ==
+
[[Category:Example]]
 +
[[Category:API]]
 +
== Default ==
  
 
As example we will store a temperature.
 
As example we will store a temperature.
Line 14: Line 16:
 
|}
 
|}
  
== Insert data ==
+
* We have to make a PUT request with JSON encoded body content: <tt>{"data":"<value>"}</tt> from a console with [[Wikipedia:curl|curl]].
 
+
* The API key must be send as a Header field named <tt>X-PVLng-key</tt>
To store data we send a <tt>PUT</tt> request from a console with [[Wikipedia:curl|curl]].
+
* Here only the measuring data is send, the timestamp will be used from server.
 
 
The API key must be send as a Header field named <tt>X-PVLng-key</tt>
 
  
 
Interesting is the HTTP code returned and the response body for error analysis in case of HTTP code is not 200 or 201.
 
Interesting is the HTTP code returned and the response body for error analysis in case of HTTP code is not 200 or 201.
Line 27: Line 27:
 
         --header "X-PVLng-key: a10d3cca-370c-121e-b79c-2f0a58030a01" \
 
         --header "X-PVLng-key: a10d3cca-370c-121e-b79c-2f0a58030a01" \
 
         --data-binary {"data":"23.45"} \
 
         --data-binary {"data":"23.45"} \
         <nowiki>http://your.domain.here/api/r3/data/87fd-cc15-cb33-71a8-6b78-3855-4c11-a79e.txt</nowiki>)
+
         <nowiki>http://your.domain.here/api/r4/data/87fd-cc15-cb33-71a8-6b78-3855-4c11-a79e.txt</nowiki>)
  
 
To get readable error messages back, request content type text/plain (triggered by file extension <tt>.txt</tt>).
 
To get readable error messages back, request content type text/plain (triggered by file extension <tt>.txt</tt>).
  
== Simple check ==
+
Check the return code like this:
  
 
  $ test $1 -eq 200 -o $1 -eq 201 || echo "Error ($1): $(</tmp/pvlng.curl)" && exit 1
 
  $ test $1 -eq 200 -o $1 -eq 201 || echo "Error ($1): $(</tmp/pvlng.curl)" && exit 1
  
[[Category:Example]]
+
== With timestamp ==
 +
 
 +
If you run the command from a remote equipment and want to use its time, you can send it as well.
 +
 
 +
{| class="wikitable"
 +
|-
 +
! Parameter !! Value !! since
 +
|-
 +
| Timestamp || Sat, 30 Aug 2014 22:11:11 GMT == 1409436671 || <tt>r1</tt>
 +
|}
 +
 
 +
* We have to make a PUT request with JSON encoded body content: <tt>{"data":"<value>","timestamp":"<timestamp>"}</tt>
 +
 
 +
$ set $(curl --request PUT \
 +
        –-write-out %{http_code} \
 +
        --output /tmp/pvlng.curl \
 +
        --header "X-PVLng-key: a10d3cca-370c-121e-b79c-2f0a58030a01" \
 +
        --data-binary {"data":"23.45","timestamp":"1409436671"} \
 +
        <nowiki>http://your.domain.here/api/r4/data/87fd-cc15-cb33-71a8-6b78-3855-4c11-a79e.txt</nowiki>)

Latest revision as of 16:30, 28 September 2014

Default

As example we will store a temperature.

Parameter Value since
API key a10d3cca-370c-121e-b79c-2f0a58030a01 r1
Channel GUID 87fd-cc15-cb33-71a8-6b78-3855-4c11-a79e r1
Temperature 23.45 °C r1
  • We have to make a PUT request with JSON encoded body content: {"data":"<value>"} from a console with curl.
  • The API key must be send as a Header field named X-PVLng-key
  • Here only the measuring data is send, the timestamp will be used from server.

Interesting is the HTTP code returned and the response body for error analysis in case of HTTP code is not 200 or 201.

$ set $(curl --request PUT \
        –-write-out %{http_code} \
        --output /tmp/pvlng.curl \
        --header "X-PVLng-key: a10d3cca-370c-121e-b79c-2f0a58030a01" \
        --data-binary {"data":"23.45"} \
        http://your.domain.here/api/r4/data/87fd-cc15-cb33-71a8-6b78-3855-4c11-a79e.txt)

To get readable error messages back, request content type text/plain (triggered by file extension .txt).

Check the return code like this:

$ test $1 -eq 200 -o $1 -eq 201 || echo "Error ($1): $(</tmp/pvlng.curl)" && exit 1

With timestamp

If you run the command from a remote equipment and want to use its time, you can send it as well.

Parameter Value since
Timestamp Sat, 30 Aug 2014 22:11:11 GMT == 1409436671 r1
  • We have to make a PUT request with JSON encoded body content: {"data":"<value>","timestamp":"<timestamp>"}
$ set $(curl --request PUT \
        –-write-out %{http_code} \
        --output /tmp/pvlng.curl \
        --header "X-PVLng-key: a10d3cca-370c-121e-b79c-2f0a58030a01" \
        --data-binary {"data":"23.45","timestamp":"1409436671"} \
        http://your.domain.here/api/r4/data/87fd-cc15-cb33-71a8-6b78-3855-4c11-a79e.txt)