Commit 725ea77c authored by Federico Sismondi's avatar Federico Sismondi

Fixes for fountain DCA

parent 2735a24b
......@@ -27,28 +27,14 @@ from flask import request
import requests as client_request
app = Flask(__name__, )
print("Starting to collect..")
# get config from environment
COLLECTION_DIR = os.getenv('COLLECTION_DIR', default='.')
URL_BASE = 'http://{}:1026'.format(os.getenv('ORION_HOST', default='localhost'))
LORA_PLATFORM_URL = 'https://{}'.format(os.getenv('LORA_PLATFORM_SERVER', default='lora-ns.sig-ge.ch:443'))
LORA_PLATFORM_URL = os.getenv('LORA_PLATFORM_SERVER')
LORA_PLATFORM_SECRET_FILE = os.getenv('LORA_PLATFORM_SECRET_FILE', 'secret_carouge_lora')
with open(LORA_PLATFORM_SECRET_FILE, 'r') as file:
secret = file.read().replace('\n', '')
assert secret, 'No auth token defined for LoRa platform API'
# AUTH Header for lora platform
h = {'Authorization': secret}
# build header for GET
http_header_get = {
'Fiware-Service': 'carouge',
'Accept': 'application/json',
}
def get_ngsiv2_typed_description(val):
if isinstance(val, Point):
......@@ -126,28 +112,82 @@ def _dump_raw_data_to_filesystem():
return 'ok'
def _dummy_post_handler():
if request.data:
app.logger.info("Request data: %s" % request.data)
if request.form:
app.logger.info("Request form: %s" % request.form)
def _get_reading(message_data):
"""
>>> base64.standard_b64decode("+AACoQEDAAAAAANY").hex()
'f8 00 02 a1 01 03 00 00 00 00 03 58'
decode:
f8 -> reg00-04 OK, reg05-07 NOK/not-used
00 -> reg08-15 NOK/not-used
_
02 |-> 673
a1 _|
01 |-> 259
03 _|
00 |-> 0
00 _|
00 |-> 0
00 _|
03 |-> 856
58 _|
units:
ph *100 ,
T celcius * 10,
DIS1 *100
DIS2 *100
REDOX mV
:param message_data: modbus base64 encoded message
:return: (ph, T , DIS1, DIS2, redox)
"""
resp = []
reading_hex = base64.standard_b64decode(message_data).hex()
status_registry = reading_hex[0:4]
values_registry = reading_hex[4:]
for i in reversed(range(0, 16)): # iterates i from 15 to 0
reg_i_status = int(status_registry, 16) >> i & 1 # checks reg bit
if reg_i_status:
rev_i = 15 - i # reverse index
resp.append(int(values_registry[4 * rev_i: 4 * rev_i + 4], 16))
else:
resp.append(None)
return 'Got POST with json %s' % request.json
app.logger.info("modbus / LoRa registers status : {}".format("{0:b}".format(int(status_registry, 16))))
app.logger.info("modbus / LoRa registers values : {}".format(values_registry))
print("modbus / LoRa registers status : {}".format("{0:b}".format(int(status_registry, 16))))
print("modbus / LoRa registers values : {}".format(values_registry))
return resp
# = = = = = = = = FLASK ROUTES = = = = = = = =
@app.route('/dca-carouge-fountain-sensed-data/rest/callback/payloads/ul', methods=['POST'])
def post_dca_carouge_fountain_sensed_data_payload():
# get values
#soil_humidity, sensor_eui = _get_reading(request.json['dataFrame']), request.json['deveui']
# soil_humidity, sensor_eui = _get_reading(request.json['dataFrame']), request.json['deveui']
# log reading
#_log_level_of_moisture(soil_humidity, sensor_eui)
# _log_level_of_moisture(soil_humidity, sensor_eui)
if request.json['deveui'] == '70b3d5078000066e':
app.logger.info('Got message from probe LoRa device, hence sensors have LoRa coverage')
return {'status': 'ok'}
if not request.json['dataFrame'] or request.json['dataFrame'] == '' :
app.logger.error('Message with empty <data frame> field from sensor {}'.format(request.json['deveui'] ))
raise InvalidUsage(message='Empty data frame field received', payload=request.json)
app.logger("Got message from FOUNTAIN devices: {}".format(request.json))
# push data to context broker
resp = {}
resp.update({"fs_dump": _dump_raw_data_to_filesystem()})
#resp.update({"update_flowerbed_entities": _update_flowerbed_entity(soil_humidity, sensor_eui)})
# resp.update({"update_flowerbed_entities": _update_flowerbed_entity(soil_humidity, sensor_eui)})
# resp.update({"update_device_entities": _update_device_entity(
# deveui=request.json['deveui'],
# comment=None,
......@@ -164,7 +204,13 @@ def post_dca_carouge_fountain_sensed_data_payload():
@app.route('/dca-carouge-fountain-sensed-data/rest/callback/nodeinfo', methods=['PUT'])
def put_dca_carouge_fountain_sensed_node_info():
_dummy_post_handler()
app.logger.info("Got FOUNTAINS message: {}".format(request.json))
if request.data:
app.logger.info("\t Request data: %s" % request.data)
if request.form:
app.logger.info("\t Request form: %s" % request.form)
raise InvalidUsage(message='This is still not implemented', payload=request.json)
......@@ -183,5 +229,21 @@ def main():
if __name__ == "__main__":
#init()
with open(LORA_PLATFORM_SECRET_FILE, 'r') as file:
secret = file.read().replace('\n', '')
assert secret, 'No auth token defined for LoRa platform API'
# AUTH Header for lora platform
h = {'Authorization': secret}
# build header for GET
http_header_get = {
'Fiware-Service': 'carouge',
'Accept': 'application/json',
}
print("Starting to collect..")
# init()
main()
......@@ -29,7 +29,7 @@ print("Starting to collect..")
COLLECTION_DIR = os.getenv('COLLECTION_DIR', default='.')
URL_BASE = 'http://{}:1026'.format(os.getenv('ORION_HOST', default='localhost'))
LORA_PLATFORM_URL = 'https://{}'.format(os.getenv('LORA_PLATFORM_SERVER', default='lora-ns.sig-ge.ch:443'))
LORA_PLATFORM_URL = os.getenv('LORA_PLATFORM_SERVER')
LORA_PLATFORM_SECRET_FILE = os.getenv('LORA_PLATFORM_SECRET_FILE', 'secret_carouge_lora')
with open(LORA_PLATFORM_SECRET_FILE, 'r') as file:
......
......@@ -322,6 +322,7 @@ services:
- COLLECTION_DIR=/code/raw_data
- ORION_HOST=orion
- LORA_PLATFORM_SECRET_FILE=/run/secrets/lora_platform_auth_header
- LORA_PLATFORM_SERVER=https://lora-ns.sig-ge.ch:443
secrets:
- lora_platform_auth_header
expose:
......@@ -354,6 +355,7 @@ services:
- COLLECTION_DIR=/code/raw_data
- ORION_HOST=orion
- LORA_PLATFORM_SECRET_FILE=/run/secrets/lora_platform_auth_header
- LORA_PLATFORM_SERVER=https://eu.saas.orbiwise.com:443
secrets:
- lora_platform_auth_header
expose:
......@@ -369,6 +371,7 @@ services:
interval: 5m00s
timeout: 10s
retries: 3
# - + - + - + - + - + - + - + - + - + - + - + - + - +
# Docker configs #
# - + - + - + - + - + - + - + - + - + - + - + - + - +
......
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