weather_create_entities.py 4.89 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/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':
        {
54
55
56
57
58
59
60
61
62
63
            '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,
64
65
66
67
        },

    'WeatherForecast':
        {
68
            'dateIssued': datetime.datetime.utcnow().isoformat(),  # mandatory
69
70
            'address': {
                'streetAddress': 'Place de Sardaigne',
71
                'addressLocality': 'Carouge',
72
                'addressCountry': 'Switzerland'
73
74
75
76
77
78
79
80
81
82
83
            },  # mandatory

            "dayMinimum": {
                "feelsLikeTemperature": 11,
                "temperature": 11,
                "relativeHumidity": 0.7
            },
            "dayMaximum": {
                "feelsLikeTemperature": 15,
                "temperature": 15,
                "relativeHumidity": 0.9
84
            },
85
86
            "precipitationProbability": 0.15,
            'windSpeed': 0,
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
        }

}

# 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)

112
113
114
115
# create entities in IoT platform
print("Updating entities")


116
117
118
119
120
121
122
123
124
125
126
127
128
129
# 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):
130
        print("HTTP status code not 2xx, something went wrong..")
131
132
133
134
        print(r.status_code)
        print(r.reason)
        print(r.json())

135
136
    else:
        print("Entities updated (HTTP response is 2xx)")
137
138
except StopIteration:
    pass