Commit 7dcecf25 authored by Kristian Tan's avatar Kristian Tan

Displaying daily total on html

parent 920acd3e
...@@ -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)
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