Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
B
bmon
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
energy
bmon
Commits
384b4d35
Commit
384b4d35
authored
Jul 11, 2019
by
Alan Mitchell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added method to store HTTP Integration data from Things Network.
parent
fcf0c632
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
50 additions
and
1 deletion
+50
-1
bmsapp/urls.py
bmsapp/urls.py
+2
-1
bmsapp/views.py
bmsapp/views.py
+48
-0
No files found.
bmsapp/urls.py
View file @
384b4d35
...
...
@@ -7,10 +7,11 @@ from django.urls import re_path
from
.
import
views
from
.
import
views_api_v1
# Could work on simplifying many of these by usin the new "path" function
# Could work on simplifying many of these by usin
g
the new "path" function
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'^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'
),
...
...
bmsapp/views.py
View file @
384b4d35
# Create your views here.
import
sys
,
logging
,
json
,
random
,
time
import
dateutil.parser
from
django.http
import
HttpResponse
from
django.shortcuts
import
render_to_response
,
redirect
,
render
from
django.contrib.auth.decorators
import
login_required
...
...
@@ -237,6 +239,52 @@ 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
):
'''
Stores a set of sensor readings from the Things Network in the sensor reading
database. The readings are assumed to originate from an HTTP Integration on an
Application in the Things Network. The Authorization header in the request contains
the BMON Store Key. 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. The Authorization header is of the format:
# BMON <store key>
try
:
_
,
storeKey
=
request
.
META
[
'HTTP_AUTHORIZATION'
].
split
()
except
:
storeKey
=
'None_Present'
if
store_key_is_valid
(
storeKey
):
readings
=
[]
ts
=
dateutil
.
parser
.
parse
(
req_data
[
'metadata'
][
'time'
]).
timestamp
()
hdw_serial
=
req_data
[
'hardware_serial'
]
for
fld
,
val
in
req_data
[
'payload_fields'
].
items
():
if
fld
not
in
EXCLUDE_THINGS_FIELDS
:
readings
.
append
(
[
ts
,
f'
{
hdw_serial
}
_
{
fld
}
'
,
val
]
)
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
):
'''
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment