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
7dcecf25
Commit
7dcecf25
authored
Nov 13, 2019
by
Kristian Tan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Displaying daily total on html
parent
920acd3e
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
42 additions
and
21 deletions
+42
-21
app.py
app.py
+42
-21
No files found.
app.py
View file @
7dcecf25
...
@@ -41,12 +41,11 @@ latest_entry = db.session.query(DailyUsage).order_by(DailyUsage.id.desc()).first
...
@@ -41,12 +41,11 @@ latest_entry = db.session.query(DailyUsage).order_by(DailyUsage.id.desc()).first
if
latest_entry
:
if
latest_entry
:
latest_entry_date
=
date
(
latest_entry
.
date
.
year
,
latest_entry
.
date
.
month
,
latest_entry
.
date
.
day
)
latest_entry_date
=
date
(
latest_entry
.
date
.
year
,
latest_entry
.
date
.
month
,
latest_entry
.
date
.
day
)
if
latest_entry_date
==
datetime
.
today
()
.
date
():
if
latest_entry_date
==
datetime
.
today
()
.
date
():
daily_total
+=
latest_entry
.
on_time_seconds
daily_total
=
timedelta
(
seconds
=
latest_entry
.
on_time_seconds
)
# Create dictionary to store pin info
# Create dictionary to store pin info
pins
=
{
pins
=
{
25
:
{
'name'
:
'Light'
,
'state'
:
GPIO
.
LOW
,
'on_time'
:
None
,
'on_date'
:
None
}
25
:
{
'name'
:
'Light'
,
'state'
:
GPIO
.
LOW
,
'on_time'
:
None
,
'on_date'
:
None
,
'Wattage'
:
5.4
}
}
}
# Setup each pin
# Setup each pin
...
@@ -82,25 +81,26 @@ def toggle_pin(change_pin):
...
@@ -82,25 +81,26 @@ def toggle_pin(change_pin):
if
GPIO
.
input
(
change_pin
)
==
0
:
if
GPIO
.
input
(
change_pin
)
==
0
:
message
+=
" off."
message
+=
" off."
if
pins
[
change_pin
][
'on_time'
]
is
not
None
:
if
pins
[
change_pin
][
'on_time'
]
is
not
None
:
latest_entry
=
db
.
session
.
query
(
DailyUsage
)
.
order_by
(
DailyUsage
.
id
.
desc
())
.
first
()
create_entry
(
change_pin
)
start_time
=
pins
[
change_pin
][
'on_time'
]
# latest_entry = db.session.query(DailyUsage).order_by(DailyUsage.id.desc()).first()
# Get the elapsed time and strip away milliseconds
# start_time = pins[change_pin]['on_time']
elapsed
=
int
((
datetime
.
now
()
-
start_time
)
.
total_seconds
())
# # Get the elapsed time and strip away milliseconds
start_date
=
pins
[
change_pin
][
'on_date'
]
# elapsed = int((datetime.now() - start_time).total_seconds())
# start_date = pins[change_pin]['on_date']
# If there is already an entry for today, update on time
#
# # If there is already an entry for today, update on time
# # if latest_entry:
# if latest_entry:
# if latest_entry:
if
latest_entry
:
# latest_entry_date = date(latest_entry.date.year, latest_entry.date.month, latest_entry.date.day)
latest_entry_date
=
date
(
latest_entry
.
date
.
year
,
latest_entry
.
date
.
month
,
latest_entry
.
date
.
day
)
# if latest_entry_date == start_date:
if
latest_entry_date
==
start_date
:
# latest_entry.on_time_seconds += elapsed
latest_entry
.
on_time_seconds
+=
elapsed
# else:
else
:
# # If no entry for today, make one
# If no entry for today, make one
# entry = DailyUsage(date=start_date, on_time_seconds=elapsed)
entry
=
DailyUsage
(
date
=
start_date
,
on_time_seconds
=
elapsed
)
# db.session.add(entry)
db
.
session
.
add
(
entry
)
# db.session.commit()
db
.
session
.
commit
()
# pins[change_pin]['on_time'] = None
pins
[
change_pin
][
'on_time'
]
=
None
# pins[change_pin]['on_date'] = None
pins
[
change_pin
][
'on_date'
]
=
None
else
:
else
:
message
+=
" on."
message
+=
" on."
pins
[
change_pin
][
'on_time'
]
=
datetime
.
now
()
pins
[
change_pin
][
'on_time'
]
=
datetime
.
now
()
...
@@ -126,6 +126,27 @@ def toggle_pin(change_pin):
...
@@ -126,6 +126,27 @@ def toggle_pin(change_pin):
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'
]
# 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
.
on_time_seconds
+=
elapsed
else
:
# If no entry for today, make one
entry
=
DailyUsage
(
date
=
start_date
,
on_time_seconds
=
elapsed
)
db
.
session
.
add
(
entry
)
db
.
session
.
commit
()
pins
[
change_pin
][
'on_time'
]
=
None
pins
[
change_pin
][
'on_date'
]
=
None
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
app
.
run
(
host
=
'0.0.0.0'
,
port
=
8090
)
app
.
run
(
host
=
'0.0.0.0'
,
port
=
8090
)
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