Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
I
IoTSmartMeter
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
kristian.tan
IoTSmartMeter
Commits
4a3d4be5
Commit
4a3d4be5
authored
Nov 13, 2019
by
Kristian Tan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Display cost per unit
parent
82e86496
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
41 additions
and
44 deletions
+41
-44
app.py
app.py
+36
-40
main.html
templates/main.html
+5
-4
No files found.
app.py
View file @
4a3d4be5
...
@@ -18,6 +18,7 @@ GPIO.setwarnings(False)
...
@@ -18,6 +18,7 @@ GPIO.setwarnings(False)
db
=
SQLAlchemy
(
app
)
db
=
SQLAlchemy
(
app
)
os
.
environ
[
'cost_per_kWh'
]
=
'0.1622'
os
.
environ
[
'cost_per_kWh'
]
=
'0.1622'
# TODO: Move this into daily_usage class file
# TODO: Move this into daily_usage class file
class
DailyUsage
(
db
.
Model
):
class
DailyUsage
(
db
.
Model
):
__tablename__
=
'daily_usage'
__tablename__
=
'daily_usage'
...
@@ -34,9 +35,6 @@ class DailyUsage(db.Model):
...
@@ -34,9 +35,6 @@ class DailyUsage(db.Model):
return
'<DailyUsage
%
r,
%
r,
%
r>'
%
(
self
.
id
,
self
.
date
,
self
.
kwhUsed
)
return
'<DailyUsage
%
r,
%
r,
%
r>'
%
(
self
.
id
,
self
.
date
,
self
.
kwhUsed
)
db
.
create_all
()
def
get_todays_usage
():
def
get_todays_usage
():
latest_entry
=
db
.
session
.
query
(
DailyUsage
)
.
order_by
(
DailyUsage
.
id
.
desc
())
.
first
()
latest_entry
=
db
.
session
.
query
(
DailyUsage
)
.
order_by
(
DailyUsage
.
id
.
desc
())
.
first
()
if
latest_entry
:
if
latest_entry
:
...
@@ -50,16 +48,34 @@ def get_todays_cost():
...
@@ -50,16 +48,34 @@ def get_todays_cost():
return
format
(
float
(
get_todays_usage
())
*
float
(
os
.
environ
[
'cost_per_kWh'
]),
'0.5f'
)
return
format
(
float
(
get_todays_usage
())
*
float
(
os
.
environ
[
'cost_per_kWh'
]),
'0.5f'
)
# TODO: be able to query db by date
def
create_entry
(
change_pin
):
latest_entry
=
db
.
session
.
query
(
DailyUsage
)
.
order_by
(
DailyUsage
.
id
.
desc
())
.
first
()
start_time
=
pins
[
change_pin
][
'on_time'
]
# Get the elapsed time and strip away milliseconds
elapsed
=
int
((
datetime
.
now
()
-
start_time
)
.
total_seconds
())
start_date
=
pins
[
change_pin
][
'on_date'
]
# Formula to calculate kWh based on time and wattage
kwh
=
pins
[
change_pin
][
'Wattage'
]
*
(
elapsed
/
3600
)
/
1000
# If there is already an entry for today, update on time
# if latest_entry:
if
latest_entry
:
latest_entry_date
=
date
(
latest_entry
.
date
.
year
,
latest_entry
.
date
.
month
,
latest_entry
.
date
.
day
)
if
latest_entry_date
==
start_date
:
latest_entry
.
kwhUsed
+=
kwh
else
:
# If no entry for today, make one
entry
=
DailyUsage
(
date
=
start_date
,
kwhUsed
=
kwh
)
db
.
session
.
add
(
entry
)
db
.
session
.
commit
()
pins
[
change_pin
][
'on_time'
]
=
None
pins
[
change_pin
][
'on_date'
]
=
None
db
.
create_all
()
daily_total
=
get_todays_usage
()
daily_total
=
get_todays_usage
()
todays_cost
=
get_todays_cost
()
todays_cost
=
get_todays_cost
()
# latest_entry = db.session.query(DailyUsage).order_by(DailyUsage.id.desc()).first()
# if latest_entry:
# latest_entry_date = date(latest_entry.date.year, latest_entry.date.month, latest_entry.date.day)
# if latest_entry_date == datetime.today().date():
# daily_total = format(latest_entry.kwhUsed, '.7f')
#
# todays_cost = format(float(daily_total) * float(os.environ['cost_per_kWh']), '0.5f')
# Create dictionary to store pin info
# Create dictionary to store pin info
pins
=
{
pins
=
{
...
@@ -83,7 +99,9 @@ def main():
...
@@ -83,7 +99,9 @@ def main():
template_data
=
{
template_data
=
{
'pins'
:
pins
,
'pins'
:
pins
,
'daily_total'
:
daily_total
,
'daily_total'
:
daily_total
,
'todays_cost'
:
todays_cost
'todays_cost'
:
todays_cost
,
'cost_per_kWh'
:
os
.
environ
[
'cost_per_kWh'
]
}
}
return
render_template
(
'main.html'
,
**
template_data
)
return
render_template
(
'main.html'
,
**
template_data
)
...
@@ -110,37 +128,13 @@ def toggle_pin(change_pin):
...
@@ -110,37 +128,13 @@ def toggle_pin(change_pin):
template_data
=
{
template_data
=
{
'pins'
:
pins
,
'pins'
:
pins
,
'daily_total'
:
get_todays_usage
(),
'daily_total'
:
get_todays_usage
(),
'todays_cost'
:
get_todays_cost
()
'todays_cost'
:
get_todays_cost
(),
'cost_per_kWh'
:
os
.
environ
[
'cost_per_kWh'
]
}
}
return
render_template
(
'main.html'
,
**
template_data
)
return
render_template
(
'main.html'
,
**
template_data
)
def
create_entry
(
change_pin
):
latest_entry
=
db
.
session
.
query
(
DailyUsage
)
.
order_by
(
DailyUsage
.
id
.
desc
())
.
first
()
start_time
=
pins
[
change_pin
][
'on_time'
]
# Get the elapsed time and strip away milliseconds
elapsed
=
int
((
datetime
.
now
()
-
start_time
)
.
total_seconds
())
start_date
=
pins
[
change_pin
][
'on_date'
]
# Formula to calculate kWh based on time and wattage
kwh
=
pins
[
change_pin
][
'Wattage'
]
*
(
elapsed
/
3600
)
/
1000
# If there is already an entry for today, update on time
# if latest_entry:
if
latest_entry
:
latest_entry_date
=
date
(
latest_entry
.
date
.
year
,
latest_entry
.
date
.
month
,
latest_entry
.
date
.
day
)
if
latest_entry_date
==
start_date
:
latest_entry
.
kwhUsed
+=
kwh
else
:
# If no entry for today, make one
entry
=
DailyUsage
(
date
=
start_date
,
kwhUsed
=
kwh
)
db
.
session
.
add
(
entry
)
db
.
session
.
commit
()
pins
[
change_pin
][
'on_time'
]
=
None
pins
[
change_pin
][
'on_date'
]
=
None
@
app
.
route
(
'/handle_data'
,
methods
=
[
'POST'
])
@
app
.
route
(
'/handle_data'
,
methods
=
[
'POST'
])
def
handle_data
():
def
handle_data
():
new_price
=
request
.
form
[
'kWhprice'
]
new_price
=
request
.
form
[
'kWhprice'
]
...
@@ -148,7 +142,8 @@ def handle_data():
...
@@ -148,7 +142,8 @@ def handle_data():
template_data
=
{
template_data
=
{
'pins'
:
pins
,
'pins'
:
pins
,
'daily_total'
:
daily_total
,
'daily_total'
:
daily_total
,
'todays_cost'
:
todays_cost
'todays_cost'
:
todays_cost
,
'cost_per_kWh'
:
os
.
environ
[
'cost_per_kWh'
]
}
}
return
render_template
(
'main.html'
,
**
template_data
)
return
render_template
(
'main.html'
,
**
template_data
)
...
@@ -158,7 +153,8 @@ def add_new_device():
...
@@ -158,7 +153,8 @@ def add_new_device():
template_data
=
{
template_data
=
{
'pins'
:
pins
,
'pins'
:
pins
,
'daily_total'
:
daily_total
,
'daily_total'
:
daily_total
,
'todays_cost'
:
todays_cost
'todays_cost'
:
todays_cost
,
'cost_per_kWh'
:
os
.
environ
[
'cost_per_kWh'
]
}
}
return
render_template
(
'main.html'
,
**
template_data
)
return
render_template
(
'main.html'
,
**
template_data
)
...
...
templates/main.html
View file @
4a3d4be5
...
@@ -31,6 +31,11 @@
...
@@ -31,6 +31,11 @@
<div
class=
"smart_meter"
>
<div
class=
"smart_meter"
>
<div
class=
"smart_meter_info"
>
<div
class=
"smart_meter_info"
>
{# Implement some way to display "No devices" message if none are in use#}
<p>
Cosr per kWh: {{ cost_per_kWh }}
</p>
<p>
Total energy used today: {{ daily_total }} kWh.
</p>
<p>
Total cost of today's energy usage: £{{ todays_cost }}
</p>
<p>
Devices currently in use:
</p>
<p>
Devices currently in use:
</p>
<ul>
<ul>
{% for pin in pins %}
{% for pin in pins %}
...
@@ -39,10 +44,6 @@
...
@@ -39,10 +44,6 @@
{% endif %}
{% endif %}
{% endfor %}
{% endfor %}
</ul>
</ul>
{# Implement some way to display "No devices" message if none are in use#}
<p>
Total energy used today: {{ daily_total }} kWh.
</p>
<p>
Total cost of today's energy usage: £{{ todays_cost }}
</p>
</div>
</div>
</div>
</div>
</div>
</div>
...
...
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