Esempio n. 1
0
 def get_sources(self,
                 id_: str = None,
                 query: Dict = None) -> Union[Source, List[Source]]:
     logger.debug("getting sources from server")
     if id_:
         try:
             source_data = self.http.get(f'{self.sources_url}/{id_}')
             return Source(
                 id_=source_data['external_id'],
                 tdmq_id=source_data['tdmq_id'],
                 type_=EntityType(source_data['entity_type'],
                                  source_data['entity_category']),
                 station_model=source_data.get('station_model', ''),
                 geometry=Point(
                     source_data['default_footprint']['coordinates'][1],
                     source_data['default_footprint']['coordinates'][0]))
         except HTTPError as e:
             logger.error('error response from server with status code %s',
                          e.response.status_code)
             raise_exception(e.response.status_code)
     try:
         return [
             Source(
                 id_=s['external_id'],
                 tdmq_id=s['tdmq_id'],
                 type_=EntityType(s['entity_type'], s['entity_category']),
                 station_model=s.get('station_model', ''),
                 geometry=Point(s['default_footprint']['coordinates'][1],
                                s['default_footprint']['coordinates'][0]))
             for s in self.http.get(f'{self.sources_url}', params=query)
         ]
     except HTTPError as e:
         logger.error('error response from server with status code %s',
                      e.response.status_code)
         raise_exception(e.response.status_code)
    def _create_sensor(self, sensor_name: str, sensor_type: EntityType,
                       sensor_model: str, geometry: Geometry,
                       properties: List[str]):

        match = NgsiConverter._edge_id_regex.search(sensor_name)
        if match:
            edge_, station_, sensor_ = match.groups()
            edge_id = edge_
            station_id = "{}.{}".format(edge_, station_)
            sensor_id = sensor_
        else:
            edge_id = None
            station_id = None
            sensor_id = None

        return self.sensors.setdefault(
            sensor_name,
            Source(sensor_name,
                   sensor_type,
                   sensor_model,
                   geometry,
                   properties,
                   edge_id=edge_id,
                   station_id=station_id,
                   sensor_id=sensor_id))
Esempio n. 3
0
    def test_get_source_by_id(self):
        """
        Tests getting a source using the tdmq id
        """
        expected_source = Source(
            id_=REST_SOURCE["external_id"],
            type_=EntityType(REST_SOURCE["entity_type"], REST_SOURCE["entity_category"]),
            geometry=Point(*REST_SOURCE["default_footprint"]["coordinates"][::-1]),  # for some strange reason the points are inverted
            controlled_properties=None,
            tdmq_id=REST_SOURCE["tdmq_id"]
        )

        client = Client(self.url)
        httpretty.register_uri(httpretty.GET, f'{client.sources_url}/{SENSORS[0].tdmq_id}',
                               body=jsons.dumps(REST_SOURCE), match_querystring=False)

        res = client.get_sources(REST_SOURCE["tdmq_id"])
        self.assertEqual(res.to_json(), expected_source.to_json())
Esempio n. 4
0
    def test_get_all_sources(self):
        """
        Tests getting all sources
        """
        expected_sources = [
            Source(
                id_=REST_SOURCE["external_id"],
                type_=EntityType(REST_SOURCE["entity_type"], REST_SOURCE["entity_category"]),
                geometry=Point(*REST_SOURCE["default_footprint"]["coordinates"][::-1]),
                controlled_properties=None,
                tdmq_id=REST_SOURCE["tdmq_id"]
            )
        ]

        client = Client(self.url)
        httpretty.register_uri(httpretty.GET, client.sources_url,
                               body=jsons.dumps([REST_SOURCE]), match_querystring=False)

        res = client.get_sources()
        self.assertEqual([s.to_json() for s in res],
                         [s.to_json() for s in expected_sources])
Esempio n. 5
0
 def _create_sensor(self, sensor_name: str, sensor_type: EntityType,
                    geometry: Geometry, properties: List[str]):
     return self.sensors.setdefault(
         sensor_name, Source(sensor_name, sensor_type, geometry,
                             properties))
Esempio n. 6
0
 def _create_sensor(sensor_name: str, sensor_type: EntityType,
                    geometry: Geometry, properties: List[str]) -> Source:
     return Source(sensor_name, sensor_type, geometry, properties)
Esempio n. 7
0
from datetime import datetime, timezone

from tdm_ingestion.tdmq.models import EntityType, Point, Record, Source

now = datetime.now(timezone.utc)

SENSORS_TYPE = [EntityType("st1", "cat1"), EntityType("st2", "cat2")]

SENSORS = [
    Source("s1", SENSORS_TYPE[0], Point(0, 1), ["temperature"],
           "4d9ae10d-df9b-546c-a586-925e1e9ec049"),
    Source("s2", SENSORS_TYPE[1], Point(2, 3), ["humidity"],
           "6eb57b7e-43a3-5ad7-a4d1-d1ec54bb5520")
]

TIME_SERIES = [
    Record(now, SENSORS[0], Point(0, 1), {"temperature": 14.0}),
    Record(now, SENSORS[1], Point(2, 3), {"humidity": 95.0})
]

# the dictionary returned from the tdmq polystore rest api
REST_SOURCE = {
    "default_footprint": {
        "coordinates":
        [SENSORS[0].geometry.latitude, SENSORS[0].geometry.longitude],
        "type":
        "Point"
    },
    "entity_type": SENSORS_TYPE[0].name,
    "entity_category": SENSORS_TYPE[0].category,
    "external_id": SENSORS[0].id_,
Esempio n. 8
0
from datetime import datetime, timezone

from tdm_ingestion.tdmq.models import EntityType, Point, Record, Source

now = datetime.now(timezone.utc)

SENSORS_TYPE = [
    EntityType("st1", "cat1"),
    EntityType("st2", "cat2")
]

SENSORS = [
    Source("s1", SENSORS_TYPE[0], 'model1', Point(0, 1), ["temperature"],
           "4d9ae10d-df9b-546c-a586-925e1e9ec049", "Edge1", "Station1",
           "Sensor1"),
    Source("s2", SENSORS_TYPE[1], 'model2', Point(2, 3), ["humidity"],
           "6eb57b7e-43a3-5ad7-a4d1-d1ec54bb5520", "Edge2", "Station1",
           "Sensor1"),
]

TIME_SERIES = [
    Record(now, SENSORS[0], Point(0, 1), {"temperature": 14.0}),
    Record(now, SENSORS[1], Point(2, 3), {"humidity": 95.0})
]

# the dictionary returned from the tdmq polystore rest api
REST_SOURCE = {
    "default_footprint": {
        "coordinates": [
            SENSORS[0].geometry.latitude,
            SENSORS[0].geometry.longitude