Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
My task from stretch:

Retrieve sensor data from a specific REST endpoint. This would be achieved by using a HTTP GET issued against the following URL: <a href="https://ws1.chic.ulster.ac.uk/SensorCentral/REST/SensorDataRangeNanos/flgSh9oHnutzvZVOnnixFNjXIda2zula_101_101?startTs=1594297110372000000&endTs=1594297110372000000"></a>

The format of the data provided by the REST ENDPOINT as below:


blobJson	String	A string containing a JSON-based representation of the sensor data payload. This is typically used when sensor data is non-binary. This is to be processed on a Schema on Read basis. In this case, this captures data from an accelerometer. This payload describes some information which you are expected to import into your solution. In this instance, there are over 6900 records within this payload.


deviceMfg	64-bit Integer	Metadata. A value indicating the associated manufacturer, this is a pointer to the ID a of record within the manufacturers roster. In this case, this corresponds to BTIIC and has the value 101.


eventCode	64-bit Integer	Metadata. An enumeration indicating the state of the sensor. For simple binary sensors, this may be 0 or 1, indicating off or on. For more complex sensors this may be 101, indicating that the blobJson string should be referred to. 


sensorClass	64-bit Integer	Metadata. A value indicating the associated sensor class, this is a pointer to the ID of a record within the sensor class roster. In this case, this corresponds to a TestDevice and has the value 101.



sensorUUID	String	Metadata. The manufacturer provided sensor ID. In this case, this corresponds to this specific test device and has the id flgSh9oHnutzvZVOnnixFNjXIda2zula. 



timeStamp	Float	The "UNIX-time" based timestamp of the sensor reading/payload submission. This is a high-resolution value in nanoseconds. This is the point in time where a sensor/device sent data to the database. Payloads may encode alternative timestamps, this to be considered in the design of your solution. 



uID	String	Metadata. The globally unique UUID of the sensor, this is the sensorUUID with class and manufacturer data appended. In this case this value is flgSh9oHnutzvZVOnnixFNjXIda2zula_100_100


What I have tried:

I am new to this project, but have no idea where should I start.
Posted
Updated 16-Jul-20 20:06pm

1 solution

There are plenty of libraries etc you can use API Integration in Python – Part 1 – Real Python[^] & Writing a REST client in Python[^] for example

DigitalOcean (as usual) also have a nice article How To Use Web APIs in Python 3 | DigitalOcean[^]

- thats two votes for the 'requests' library

BTW, there's a great tool you can use to test REST API's (ie, as a client), called 'Postman' - well worth downloading and using to check the call, data etc https://www.postman.com/downloads/[^]

Interesting that there's no authentication required, but using that URL directly (which postman then identifies the two query parameters startTs & endTs) gives (truncated)
[
    {
        "blobJson": "[{'deviceTimestamp': 166150, 'x': 3.8919, 'y': -8.6865, 'z': -3.3077}, {'deviceTimestamp': 166170, 'x': 4.2998, 'y': -9.8624, 'z': -2.0769}, {'deviceTimestamp': 166190, 'x': 0.74341, 'y': -9.4483, 'z': 0.57692}, {'deviceTimestamp': 166210, 'x': 0.73178, 'y': -9.5004, 'z': 3.0769}, {'deviceTimestamp': 166230, 'x': 3.5538, 'y': -9.7006, 'z': 3.3077}, {'deviceTimestamp': 166250, 'x': -1.4629, 'y': -7.0692, 'z': 2.2308}, {'deviceTimestamp': 166260, 'x': -6.7571, 'y': -5.0299, 'z': -0.076923}, {'deviceTimestamp': 166280, 'x': -5.0319, 'y': -6.5896, 'z': -0.076923}, {'deviceTimestamp': 166300, 'x': -0.21122, 'y': -8.9002, 'z': 2.5}, {'deviceTimestamp': 166320, 'x': 6.3477, 'y': -9.3142, 'z': 4.5385}, {'deviceTimestamp': 166340, 'x': 10.904, 'y': -9.9498, 'z': 5.6538}, {'deviceTimestamp': 166360, 'x': 9.5501, 'y': -8.5014, 'z': 5.6923}, {'deviceTimestamp': 166380, 'x': 7.7312, 'y': -8.5973, 'z': 5.6154}, {'deviceTimestamp': 166400, 'x': 8.9783, 'y': -10.132, 'z': 4.7692}, {'deviceTimestamp': 166420, 'x': 9.8307, 'y': -11.159, 'z': 5.0385}, {'deviceTimestamp': 166440, 'x': 7.7934, 'y': -10.556, 'z': 5.8077}, {'deviceTimestamp': 166460, 'x': 5.8261, 'y': -10.259, 'z': 2.8462}, {'deviceTimestamp': 166480, 'x': 4.4837, 'y': -10.092, 'z': 1.6538}, {'deviceTimestamp': 166500, 'x': 5.5556, 'y': -11.08, 'z': 1.8462}, {'deviceTimestamp': 166520, 'x': 8.4009, 'y': -12.06, 'z': 1.0}, {'deviceTimestamp': 166540, 'x': 8.9573, 'y': -12.456, 'z': -0.73077}, {'deviceTimestamp': 166560, 'x': 8.2957, 'y': -14.59, 'z': -1.9231}, {'deviceTimestamp': 166580, 'x': 6.9804, 'y': -14.953, 'z': -2.3077}, {'deviceTimestamp': 166600, 'x': 6.6967, 'y': -13.515, 'z': -0.5}, {'deviceTimestamp': 166620, 'x': 9.6361, 'y': -14.48, 'z': 0.076923}, {'deviceTimestamp': 166640, 'x': 10.407, 'y': -15.51, 'z': 0.15385}, {'deviceTimestamp': 166660, 'x': 10.14, 'y': -15.447, 'z': -0.96154}, {'deviceTimestamp': 166670, 'x': 10.684, 'y': -14.821, 'z': -0.5}, {'deviceTimestamp': 166690, 'x': 11.048, 'y': -15.117, 'z': 0.34615}, {'deviceTimestamp': 166710, 'x': 8.4361, 'y': -13.489, 'z': 0.38462}, {'deviceTimestamp': 166730, 'x': 5.8655, 'y': -12.051, 'z': 0.57692}, {'deviceTimestamp': 166750, 'x': 2.3597, 'y': -10.318, 'z': 0.46154}, {'deviceTimestamp': 166770, 'x': -0.62984, 'y': -7.8898, 'z': 0.11538}, {'deviceTimestamp': 166790, 'x': -2.7007, 'y': -5.8762, 'z': -0.30769}, {'deviceTimestamp': 166810, 'x': -3.7714, 'y': -4.7735, 'z': -0.5}, {'deviceTimestamp': 166830, 'x': -4.9101, 'y': -5.2045, 'z': -0.15385}, {'deviceTimestamp': 166850, 'x': -4.8295, 'y': -4.2244, 'z': 1.6154}, {'deviceTimestamp': 166870, 'x': -5.7695, 'y': -2.9912, 'z': 2.8077}, {'deviceTimestamp': 166890, 'x': -5.2339, 'y': -3.1535, 'z': 1.4231}, {'deviceTimestamp': 166910, 'x': -4.6184, 'y': -4.11, 'z': 1.3077}, {'deviceTimestamp': 166930, 'x': -4.0096, 'y': -4.637, 'z': 1.8462}, {'deviceTimestamp': 166950, 'x': -1.5587, 'y': -6.1456, 'z': 2.3846}, {'deviceTimestamp': 166970, 'x': 1.9769, 'y': -9.0787, 'z': 1.6923}, {'deviceTimestamp': 166990, 'x': 3.6063, 'y': -11.394, 'z': 3.1923}, {'deviceTimestamp': 167010, 'x': 8.8673, 'y': -12.291, 'z': 4.3846}, {'deviceTimestamp': 167030, 'x': 17.873, 'y': -15.767, 'z': 5.3846}, 
that you can then parse / convert to objects / otherwise use
 
Share this answer
 
v4
Comments
Member 14891535 18-Jul-20 5:06am    
Thanks. Is it possible to retrieve only 2 data from the blobJson?

I had tried this:
import requests
import json

url = "https://ws1.chic.ulster.ac.uk/SensorCentral/REST/SensorDataRangeNanos/flgSh9oHnutzvZVOnnixFNjXIda2zula_101_101?startTs=1594297110372000000&endTs=1594297110372000000"

response = requests.get(url)
data = response.text
parsed = json.loads(data)

print(json.dumps(parsed, indent=1))
Garth J Lancaster 19-Jul-20 2:33am    
yes, its possible, but I dont work with Python all the time, so, at a guess, and noting the blobJson has single quotes in the JSON, try this
blobJson = parsed[0]["blobJson"]
blobJson = blobJson.replace("'", '"')
parsedbj = json.loads(blobJson)
print("[0].x = ", parsedbj[0]["x"])
print("[0].y = ", parsedbj[0]["y"])
print("[0].z = ", parsedbj[0]["z"])
print("[1].x = ", parsedbj[1]["x"])
print("[1].y = ", parsedbj[1]["y"])
print("[1].z = ", parsedbj[1]["z"])
instead of "print(json.dumps(parsed, indent=1))"

My way probably isnt very efficient, there's likely a python library that can do the same, I just 'followed' the data structure after the first parse, pulled out and corrected the required piece (blobJson), than parsed that from json to get a list of dictionary objects

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900