#!/usr/bin/env python3 ''' Creates all entities needed for the weather use case for /Carouge NOTES: - id and type of message sent should follow convention defined for URN in spreadsheet https://telecombretagneeu-my.sharepoint.com/:x:/g/personal/federico_sismondi_telecom-bretagne_eu/Ebw_b9iHempGoSPEz__sYCcB_qPu0pwxDAgAeKXFoMu6cA?e=6uuUGD ''' import os import json import datetime import requests import logging import warlock import requests from pprint import pprint logging.basicConfig(level=logging.INFO) logger = logging.getLogger() # get latest data model version SCHEMA_WEATHER_OBSERVED = requests.get( 'https://raw.githubusercontent.com/smart-data-models/dataModel.Weather/master/WeatherObserved/schema.json').json() SCHEMA_WEATHER_FORECAST = requests.get( 'https://raw.githubusercontent.com/smart-data-models/dataModel.Weather/master/WeatherForecast/schema.json').json() # ToDo should we care if the entry point is Orion or Wilma? maybe we should use IOT_PLATFORM_HOST,IOT_PLATFORM_PORT ? URL_BASE = "http://{}:1026".format(os.getenv("ORION_HOST")) # tuples as : (id, type, save data as historic) weather_entities = [ ('urn:ngsi-ld:WeatherForecast:carouge_weather_prediction_day_morning', SCHEMA_WEATHER_FORECAST, True), ('urn:ngsi-ld:WeatherForecast:carouge_weather_prediction_day_noon', SCHEMA_WEATHER_FORECAST, True), ('urn:ngsi-ld:WeatherForecast:carouge_weather_prediction_day_afternoon', SCHEMA_WEATHER_FORECAST, True), ('urn:ngsi-ld:WeatherForecast:carouge_weather_prediction_day_midnight', SCHEMA_WEATHER_FORECAST, True), ('urn:ngsi-ld:WeatherForecast:carouge_weather_prediction_day_plus_1_morning', SCHEMA_WEATHER_FORECAST, True), ('urn:ngsi-ld:WeatherForecast:carouge_weather_prediction_day_plus_1_noon', SCHEMA_WEATHER_FORECAST, True), ('urn:ngsi-ld:WeatherForecast:carouge_weather_prediction_day_plus_1_afternoon', SCHEMA_WEATHER_FORECAST, True), ('urn:ngsi-ld:WeatherForecast:carouge_weather_prediction_day_plus_1_midnight', SCHEMA_WEATHER_FORECAST, True), ('urn:ngsi-ld:WeatherForecast:carouge_weather_prediction_day_plus_2_morning', SCHEMA_WEATHER_FORECAST, True), ('urn:ngsi-ld:WeatherForecast:carouge_weather_prediction_day_plus_2_noon', SCHEMA_WEATHER_FORECAST, True), ('urn:ngsi-ld:WeatherForecast:carouge_weather_prediction_day_plus_2_afternoon', SCHEMA_WEATHER_FORECAST, True), ('urn:ngsi-ld:WeatherForecast:carouge_weather_prediction_day_plus_2_midnight', SCHEMA_WEATHER_FORECAST, True), ('urn:ngsi-ld:WeatherObserved:carouge_weather_observed', SCHEMA_WEATHER_OBSERVED, True), ] # include all those 'required' args for building the instances extra_args = { 'WeatherObserved': { 'dateObserved': datetime.datetime.utcnow().isoformat(), # mandatory 'location': {'type': 'Point', 'coordinates': [6.1385878, 46.1838613]}, # mandatory 'source': 'misc.', 'illuminance': 0, 'temperature': 0.0, 'precipitation': 0, 'atmosphericPressure': 0.0, 'windSpeed': 0, 'pressureTendency': 0.0, 'relativeHumidity': 0, }, 'WeatherForecast': { 'dateIssued': datetime.datetime.utcnow().isoformat(), # mandatory 'address': { 'streetAddress': 'Place de Sardaigne', 'addressLocality': 'Carouge', 'addressCountry': 'Switzerland' }, # mandatory "dayMinimum": { "feelsLikeTemperature": 11, "temperature": 11, "relativeHumidity": 0.7 }, "dayMaximum": { "feelsLikeTemperature": 15, "temperature": 15, "relativeHumidity": 0.9 }, "precipitationProbability": 0.15, 'windSpeed': 0, } } # http headers h = { "Fiware-Service": "carouge", "Content-Type": "application/json" } entities = [] # build instances from schemas for urn, schema, keep_history in weather_entities: ModelClass = warlock.model_factory(schema) data_type = str(urn).split(':')[2] # this validates and builds data from model :) instance = ModelClass( id=urn, type=data_type, **extra_args[data_type] ) entities.append(instance) # create entities in IoT platform print("Updating entities") # log created entities print('Entities list (len {}) ready to be pushed to platform '.format(len(entities))) for i in entities: pprint(i, indent=4) operation = { "actionType": "append", "entities": entities } try: r = requests.post(url=URL_BASE + "/v2/op/update?options=keyValues", headers=h, json=operation) if not (200 <= r.status_code < 300): print("HTTP status code not 2xx, something went wrong..") print(r.status_code) print(r.reason) print(r.json()) else: print("Entities updated (HTTP response is 2xx)") except StopIteration: pass