Commit 5d5d9ed3 authored by Federico Sismondi's avatar Federico Sismondi
Browse files

Fix for watering use case app, handle differently a patch from a create for Device entity

parent fcdee32c
......@@ -512,10 +512,22 @@ def _update_historical_db_from_local_raw_files():
print('History has been rewritten for FLOWERBED entities! (HTTP response is 2xx)')
def _update_device_entity(deveui, comment, longitude, latitude, last_reception, dataFrame, dataFrame_rssi,
def _create_device_entity(deveui, comment, longitude, latitude, last_reception, dataFrame, dataFrame_rssi,
dataFrame_timestamp):
""" Creates/Updates LoRa devs into ctx broker
"""
Creates LoRa devs into ctx broker
:param deveui:
:param comment:
:param longitude:
:param latitude:
:param last_reception:
:param dataFrame:
:param dataFrame_rssi:
:param dataFrame_timestamp:
:return:
"""
assert deveui
body = get_model_device(deveui,
......@@ -529,12 +541,46 @@ def _update_device_entity(deveui, comment, longitude, latitude, last_reception,
app.logger.debug('Sending data to CTX broker: {}'.format(body))
url = "{}/v2/entities/".format(URL_BASE)
r = client_request.post(
url=url,
headers=http_header_post,
json=body
)
if not (200 <= r.status_code < 300):
app.logger.error('HTTP status code not 2xx, something went wrong..')
app.logger.error(r.status_code)
app.logger.error(r.reason)
app.logger.error(r.json())
return "Got error {}".format(r.reason)
else:
app.logger.info('CREATED entity (code {}), url {}'.format(r.status_code, url))
return "ok"
def _patch_device_entity(deveui, comment, longitude, latitude, last_reception, dataFrame, dataFrame_rssi,
dataFrame_timestamp):
"""Updates LoRa devs into ctx broker
"""
assert deveui
body = get_model_device(deveui,
comment,
longitude,
latitude,
last_reception,
dataFrame,
dataFrame_rssi,
dataFrame_timestamp)
entity_id = body['id']
entity_type = body['type']
body.pop('id') # we don't need this for a PATCH
body.pop('type') # we don't need this for a PATCH
app.logger.debug('Sending data to CTX broker: {}'.format(body))
url = "{}/v2/entities/{}/attrs".format(URL_BASE, entity_id)
r = client_request.patch(
url=url,
......@@ -549,7 +595,7 @@ def _update_device_entity(deveui, comment, longitude, latitude, last_reception,
app.logger.error(r.json())
return "Got error {}".format(r.reason)
else:
app.logger.info('Entity updated (code {}), url {}'.format(r.status_code, url))
app.logger.info('PATCHED entity (code {}), url {}'.format(r.status_code, url))
return "ok"
......@@ -611,7 +657,7 @@ def post_dca_carouge_watering_sensed_data_payload():
soil_humidity,
sensor_eui
)})
resp.update({"update_device_entities": _update_device_entity(
resp.update({"update_device_entities": _patch_device_entity(
deveui=request.json['deveui'],
comment=None,
longitude=None,
......@@ -650,7 +696,7 @@ def init():
# push data to CTX broker
for i in lora_nodes_info:
_update_device_entity(
_create_device_entity(
deveui=i['deveui'],
comment=i['comment'],
longitude=i['longitude'],
......
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