Commit ed17e334 authored by Kristian Tan's avatar Kristian Tan

Record the usage in db entry

parent aa0aad97
...@@ -2,7 +2,7 @@ from flask import Flask, render_template ...@@ -2,7 +2,7 @@ from flask import Flask, render_template
import RPi.GPIO as GPIO import RPi.GPIO as GPIO
from flask_sqlalchemy import SQLAlchemy from flask_sqlalchemy import SQLAlchemy
import os import os
import datetime from datetime import datetime
# from daily_usage import DailyUsage # from daily_usage import DailyUsage
app = Flask(__name__) app = Flask(__name__)
...@@ -74,13 +74,17 @@ def toggle_pin(change_pin): ...@@ -74,13 +74,17 @@ def toggle_pin(change_pin):
message = "Turned " + device_name message = "Turned " + device_name
if GPIO.input(change_pin) == 0: if GPIO.input(change_pin) == 0:
message += " off." message += " off."
uptime = datetime.now() - pins[change_pin]['on_time']
d = DailyUsage(date=pins[change_pin]['on_time'], hours=uptime)
db.session.add(d)
db.session.commit()
pins[change_pin]['on_time'] = None
print("==========")
print(DailyUsage.query.all())
print("==========")
else: else:
message += " on." message += " on."
pins[change_pin]['on_time'] = datetime.datetime.now() pins[change_pin]['on_time'] = datetime.now()
print("==================")
print("HERE IS THE PIN DATA: ")
print(pins[change_pin])
print("==================")
for pin in pins: for pin in pins:
pins[pin]['state'] = GPIO.input(pin) pins[pin]['state'] = GPIO.input(pin)
......
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