Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Open sidebar
energy
bmon
Commits
31ccf5ec
Commit
31ccf5ec
authored
Dec 15, 2020
by
Alan Mitchell
Browse files
Merge branch 'master' into bare-server
parents
241ced59
8a162e7c
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
43 additions
and
3 deletions
+43
-3
bmsapp/periodic_scripts/ambientweather.py
bmsapp/periodic_scripts/ambientweather.py
+40
-0
bmsapp/views.py
bmsapp/views.py
+3
-3
No files found.
bmsapp/periodic_scripts/ambientweather.py
0 → 100644
View file @
31ccf5ec
"""Periodic script to collect readings from AmbientWeather weather stations.
AmbientWeather provides API access to the data.
"""
import
requests
def
run
(
application_key
:
str
,
api_key
:
str
,
**
kwargs
)
->
dict
:
# collecting the data from the ambient weather site for a sensor
# gathered into a json file format
api_url
=
'https://api.ambientweather.net/v1/devices?'
payload
=
dict
(
applicationKey
=
application_key
,
apiKey
=
api_key
)
response
=
requests
.
get
(
api_url
,
params
=
payload
)
response_body
=
response
.
json
()
# Empty list created to hold all reading data from all sensors.
sensors_reading_list
=
[]
for
sensor
in
response_body
:
# dicts with the neccesary information from api repsonse
mac_address
=
sensor
[
'macAddress'
].
replace
(
':'
,
''
)
sensor_data
=
sensor
[
'lastData'
]
# desired sensor data values for extraction
# hard wire values here for extraction
desired_data_keys
=
[
"windgustmph"
,
"winddir_avg10m"
,
"windspdmph_avg10m"
,
"tempf"
,
"humidity"
,
"baromrelin"
,
"baromabsin"
,
"tempinf"
,
"humidityin"
,
"hourlyrainin"
,
"solarradiation"
]
data_log_timestamp
=
int
(
sensor_data
[
'dateutc'
]
/
1000
)
# extracting data from api response and adding proper key names for value
for
key
in
desired_data_keys
:
if
key
in
sensor_data
.
keys
():
sensor_id
=
mac_address
+
'_'
+
key
sensor_data_value
=
sensor_data
[
key
]
sensor_data_tuple
=
(
data_log_timestamp
,
sensor_id
,
sensor_data_value
)
# appending timestamp sensor data values to readings list
sensors_reading_list
.
append
(
sensor_data_tuple
)
# return of function in dict form for bmon data
return
{
'readings'
:
sensors_reading_list
}
bmsapp/views.py
View file @
31ccf5ec
...
...
@@ -694,7 +694,7 @@ def delete_sensor_values(request):
elif
delete_where
==
'values_lt'
:
where_clause
=
f
'WHERE val <
{
where_value
}
'
elif
delete_where
==
'dates_between'
:
where_clause
=
f
'WHERE ts >
{
bmsapp
.
data_util
.
datestr_to_ts
(
start_date
)
}
and ts <
{
bmsapp
.
data_util
.
datestr_to_ts
(
end_date
)
}
'
where_clause
=
f
'WHERE ts >
{
bmsapp
.
data_util
.
datestr_to_ts
(
where_
start_date
)
}
and ts <
{
bmsapp
.
data_util
.
datestr_to_ts
(
where_
end_date
)
}
'
else
:
return
HttpResponse
(
f
'Invalid parameter:
{
delete_where
}
'
,
status
=
406
)
...
...
@@ -716,12 +716,12 @@ def delete_sensor_values(request):
if
delete_where
==
'all_values'
:
db
.
cursor
.
execute
(
f
'DROP TABLE [
{
sensor_id
}
]'
)
qs
=
models
.
Sensor
.
objects
.
filter
(
sensor_id
=
params
[
'
sensor_id
'
]
)
sensor_id
=
sensor_id
)
if
len
(
qs
)
>
0
:
qs
[
0
].
delete
()
db
.
conn
.
commit
()
except
Exception
as
e
:
return
HttpResponse
(
e
,
status
=
500
)
return
HttpResponse
(
repr
(
e
)
,
status
=
500
)
return
HttpResponse
(
'Records Deleted'
)
...
...
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