zMoooooritz / stapy

An easy to use SensorThings API Client written in Python
MIT License
6 stars 2 forks source link

Post Datastream error #19

Closed BWibo closed 2 years ago

BWibo commented 2 years ago

Hey there, thx for this library, looks promising. I just did some testing and ran into an error when posting a Datastream:

Python 3.8.10
stapy 0.2.4
import  stapy as sta

sta.set_api_url('https://top-secret-frost-server.de/v1.1')

iotid_thing = sta.Post.thing("rpi-new", "Rpi4")
iotid_sensor = sta.Post.sensor("Temperature", "On-Board temperature sensor", "text/html", "https://abc.de")

iotid_datastream = sta.Post.datastream(
  "Rpi-4 temperature",
  "Rpi4 temperature datastream",
  "DegC",
  "http://www.opengis.net/def/observationType/OGC-OM/2.0/OM_Measurement",
  iotid_thing, 2, iotid_sensor
)

Posting the Datastream fails with this error:

```text
Traceback (most recent call last):   
  File "main.py", line 8, in <module>
    iotid_datastream = sta.Post.datastream(
  File "/home/bruno/.local/lib/python3.8/site-packages/stapy/sta/post.py", line 32, in datastream
    return Post.entity(Entity.Datastream, **params)
  File "/home/bruno/.local/lib/python3.8/site-packages/stapy/sta/post.py", line 195, in entity
    ent.set_param(**params)
  File "/home/bruno/.local/lib/python3.8/site-packages/stapy/sta/abstract_entity.py", line 50, in set_param
    self.json = self._update_json(self.entry_map, self.json, **data)
  File "/home/bruno/.local/lib/python3.8/site-packages/stapy/sta/abstract_entity.py", line 112, in _update_json
    val_is = cast(dict, val_is)
  File "/home/bruno/.local/lib/python3.8/site-packages/stapy/common/util.py", line 28, in cast
    return switch.get(typ)(value)
  File "/usr/lib/python3.8/json/__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.8/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.8/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Any idea what I am doing wrong? I tired with setting a dict as properties as well.

zMoooooritz commented 2 years ago

Hey, thank you for your feedback!

Indeed the implementation is correct but does obviously not provide sufficient feedback... As to why the last POST did fail.

The STA requires an JSON-Object as unitOfMeasurement (containing three specific entries) therefore the following snippet does create the Datastream as you tried to achieve:

import stapy as sta

sta.set_api_url("https://top-secret-frost-server.de/v1.1")

iotid_obprop = sta.Post.observed_property("Temperature", "Temperature", "Temperature")
iotid_thing = sta.Post.thing("rpi-new", "Rpi4")
iotid_sensor = sta.Post.sensor("Temperature", "On-Board temperature sensor", "text/html", "https://abc.de")
unit = {
    "name": "Degree Celsius",
    "symbol": "degC",
    "definition": "http://www.qudt.org/qudt/owl/1.0.0/unit/Instances.html#DegreeCelsius"
}

iotid_datastream = sta.Post.datastream(
  "Rpi-4 temperature",
  "Rpi4 temperature datastream",
  unit,
  "http://www.opengis.net/def/observationType/OGC-OM/2.0/OM_Measurement",
  iotid_thing, iotid_obprop, iotid_sensor
)

For more (advanced) information see the following link

BWibo commented 2 years ago

Ah right, I forgot for a moment that UoM is not just a String in STA. Thx.