Commit be7f3218 authored by Federico Sismondi's avatar Federico Sismondi

Add() python script for creating weather observed and weather prediction entities (empty instances)

parent 24ffefa8
requests
\ No newline at end of file
requests
warlock
#!/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(),
'location': {'type': 'Point', 'coordinates': [6.1385878, 46.1838613]}
},
'WeatherForecast':
{
'dateIssued': datetime.datetime.utcnow().isoformat(),
'address': {
'streetAddress': 'Place de Sardaigne',
'adressLocality': 'Carouge',
'addressCountry': 'Switzerland'
},
}
}
# 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)
# log created entities
print('Entities list (len {}) ready to be pushed to platform '.format(len(entities)))
for i in entities:
pprint(i, indent=4)
# create entities in IoT platform
print("%s - updating entities" % datetime.datetime.now().replace(microsecond=0).isoformat())
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(r.status_code)
print(r.reason)
print(r.json())
except StopIteration:
pass
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment