TC7.7 - How to Save resulting data via API from R Studio

×

Warning message

  • You can't delete this newsletter because it has not been sent to all its subscribers.
  • You can't delete this newsletter because it has not been sent to all its subscribers.

Test Case Title

TC7.7 - How to save resulting data via API from R Studio

Goal

TBD

Prerequisites

Using a PC with a web browser. You need credentials to access to the Node-Red application.

The following functionalities are available only for specific Snap4city users with specific privileges.

Expected successful result

TBD

Steps

 

 

  1. Install the “httr” package with the command install.packages(“httr”) and load it with the command library(httr); install the “jsonlite” package with the command install.packages(“jsonlite”) and load it with the command library(jsonlite).

 

  1. Convert the list that contains all new data into a Json format with the command toJSON:


request_body_json <- toJSON (dataList, auto_unbox = TRUE)
 

    1. To save a single value (for example the average value of the last 2 hours for a specific variable):

 

  • It’is necessary to register the attributes for a specific Service Uri, creating a Json in the following format:


{"serviceUris":["http://..."],"attributes":[{"valueName":"…","valueUnit":"#","dataType":"float","value_refresh_rate":"null","message":"string"}]}

Example: 

{"serviceUris":["http://www.disit.org/km4city/resource/TA120-T246189"],"attributes":[{"valueName":"noiseLAeqAvg2h","valueUnit":"#","dataType":"float","value_refresh_rate":"null","message":"string"}]} 
           
To create a Json in R is possible to create a list first, and then using the function toJSON to convert the list in a Json: 



register=list()
register$serviceUris=list()
register$serviceUris=array(suri)
register$attributes= list(4)
register$attributes[[1]]=list("valueName"= "noiseLAeqAvg2h", "valueUnit"="#", "dataType"="float", "value_refresh_rate"="null", "message"="string")

register_body_json <- toJSON (register, auto_unbox = TRUE)

Using the POST function of the httr package is possible to register the attributes:

registerPOST <- POST (url = "https://www.snap4city.org/ServiceMap/api/v1/values/register", body = register_body_json, encode = "json", add_headers ("Content-Type" = "application/json"))

  • To save the data, Json format must be of the following form:
{"serviceUri":"http://...","attributes":[{"valueName":"…","value": …,"valueDate":"…","valueAcqDate":"…","message":"…"}]} 
 
Example:
{"serviceUri":"http://www.disit.org/km4city/resource/TA120-T246189","attributes":[{"valueName":"noiseLAeqAvg2h","value":47.75,"valueDate":"2019-01-11T17:29:48+01:00","valueAcqDate":"2019-01-11T17:29:48+01:00","message":"Ok"}]} 

Using the POST function is possible to save the data for a specific Service Uri:

 resultPOST <- POST (url = "https://www.snap4city.org/ServiceMap/api/v1/values?serviceUri=http://...", body = request_body_json, encode = "json", add_headers ("Content-Type" = "application/json"))

 

    1. To save multiple values as trends (for example the hourly average per day of the measures related to a specific sensor category) is necessary to create the following Json:
{"serviceUri":"http://...","trendType":"…","valueNames":["…","…",…],"trends":{"day1":[["time1","…","…",…],["time2","…","…",…],["time3"…","…",…],["time4","…","…",…],…,"day2":[["time1",…
 
Example:

{"serviceUri":"http://www.disit.org/km4city/resource/TA120-T246189","trendType":"Last30DaysAverage","valueNames":["noiseLAeq","noiseLAmax"],"trends":{"Tuesday":[["00:00","35.35","42.75"],["01:00","30.9","31.2"],["02:00","30.4","32.7"],["03:00","40.6","48.2"],["04:00","50.65","62.85"],["05:00","44.25","49.95"],["06:00","39.25","44.15"],…

result <- POST (url = https://www.snap4city.org/ServiceMap/api/v1/values/trends?serviceUri= http://..., body = request_body_json, encode = "json", add_headers("Content-Type" = "application/json"))

 

Note that to save data to create a heatmap the following url in the POST function has to be use

http://snap4city:disit2019@192.168.0.59:8000/insert

The Json (a single json for each lat and long):

{"mapName": "name",
"description": "description of the map",
"days": 0,
"clustered": 0,
"latitude": 43.1,
"longitude": 11.1,
"value": 3001,
"date": "2018-12-18T00:00:00Z"
}

TC7.7 - How to Save resulting data via API from R Studio | Snap4City

Error

×

Error message

  • Notice: Trying to get property of non-object in user_is_logged_in() (line 1576 of /var/www/html/drupal/modules/user/user.module).
  • Notice: Trying to get property of non-object in user_is_logged_in() (line 1576 of /var/www/html/drupal/modules/user/user.module).
  • Notice: Trying to get property of non-object in og_field_access() (line 578 of /var/www/html/drupal/sites/default/modules/og/og.module).
  • Notice: Trying to get property of non-object in user_access() (line 806 of /var/www/html/drupal/modules/user/user.module).
  • Notice: Trying to get property of non-object in user_access() (line 818 of /var/www/html/drupal/modules/user/user.module).
  • Notice: Trying to get property of non-object in user_access() (line 819 of /var/www/html/drupal/modules/user/user.module).
  • Notice: Trying to get property of non-object in user_access() (line 825 of /var/www/html/drupal/modules/user/user.module).
  • Notice: Trying to get property of non-object in user_access() (line 828 of /var/www/html/drupal/modules/user/user.module).
  • Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/drupal/includes/common.inc:2791) in drupal_send_headers() (line 1499 of /var/www/html/drupal/includes/bootstrap.inc).
  • Error: __clone method called on non-object in og_user_access_entity() (line 2249 of /var/www/html/drupal/sites/default/modules/og/og.module).
The website encountered an unexpected error. Please try again later.