Commit 1d4ec5fb authored by Alan Mitchell's avatar Alan Mitchell

Added ability to store Radio Bridge data from TTN.

parent 7a866357
......@@ -12,7 +12,8 @@ from bmsapp import views_api_v2
urlpatterns = [
re_path(r'^readingdb/reading/(\w+)/store/$', views.store_reading), # URL to store one reading into database
re_path(r'^readingdb/reading/store/$', views.store_readings), # URL to store multiple readings into database
re_path(r'^readingdb/reading/store-things/$', views.store_readings_things), # URL to store readings from Things Network
re_path(r'^readingdb/reading/store-things/$', views.store_readings_things), # URL to store readings from Things Network
re_path(r'^readingdb/reading/store-rb/$', views.store_readings_radio_bridge),
re_path(r'^st8(\w+)/', views.store_reading_old), # Old URL pattern for storing. Shouldn't be used for new sensors.
re_path(r'^readingdb/reading/(\w+)/$', views.get_readings), # gets all readings for one reading ID.
re_path(r'^$', views.index, name='index'),
......
......@@ -239,10 +239,6 @@ def store_readings(request):
_logger.exception('Error Storing Reading')
return HttpResponse(sys.exc_info()[1])
# Payload Fields from Things Network nodes that do not contain sensor
# readings.
EXCLUDE_THINGS_FIELDS = ('event', )
@csrf_exempt # needed to accept HTTP POST requests from systems other than this one.
def store_readings_things(request):
'''
......@@ -251,6 +247,11 @@ def store_readings_things(request):
Application in the Things Network. The BMON Store Key is in a custom HTTP header.
The readings and other data are in the POST data encoded in JSON.
'''
# Payload Fields from Things Network nodes that do not contain sensor
# readings.
EXCLUDE_THINGS_FIELDS = ('event', )
try:
# The post data is JSON, so decode it.
......@@ -291,6 +292,63 @@ def store_readings_things(request):
_logger.exception('Error Storing Reading')
return HttpResponse(sys.exc_info()[1])
@csrf_exempt # needed to accept HTTP POST requests from systems other than this one.
def store_readings_radio_bridge(request):
'''
Stores a set of sensor readings from a Radio Bridge LoRaWAN sensor in the sensor reading
database. The readings are assumed to originate from an HTTP Integration on an
Application in the Things Network. The BMON Store Key is in a custom HTTP header.
The readings and other data are in the POST data encoded in JSON.
'''
try:
# The post data is JSON, so decode it.
req_data = json.loads(request.body)
# Return if this is a message that does not have any data in it, like an
# activate or join message.
if 'payload_fields' not in req_data:
return HttpResponse('No Data')
# See if the store key is valid. It's stored in the "store-key" header, which
# is found in the "HTTP_STORE_KEY" key in the META dictionary.
storeKey = request.META.get('HTTP_STORE_KEY', 'None_Present')
if store_key_is_valid(storeKey):
readings = []
ts = dateutil.parser.parse(req_data['metadata']['time']).timestamp()
hdw_serial = req_data['hardware_serial']
pf = req_data['payload_fields']
event = pf['EVENT_TYPE']
if event == '01': # Supervisory Event
readings.append( [ts, f'{hdw_serial}_battery', float(pf['BATTERY_LEVEL'])] )
elif event == '07': # Contact event
val = float(pf['SENSOR_STATE'])
# Invert their logic: they have a 0 when the contacts are closed
val = val * -1 + 1
readings.append( [ts, f'{hdw_serial}_state', val] )
elif event == '09': # Temperature Event
readings.append( [ts, f'{hdw_serial}_temp', pf['TEMPERATURE'] * 1.8 + 32.] )
else:
return HttpResponse('No Data')
msg = storereads.store_many({'readings': readings})
return HttpResponse(msg)
else:
_logger.warning('Invalid Storage Key in Reading Post: %s', storeKey)
return HttpResponse('Invalid Key')
except:
_logger.exception('Error Storing Reading')
return HttpResponse(sys.exc_info()[1])
@csrf_exempt
def store_reading_old(request, store_key):
'''
......
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