Commit deebcb26 authored by Kristian Tan's avatar Kristian Tan

Added field to pin dict to record on time

parent f33c56bb
from flask import Flask, render_template 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
from sqlalchemy import create_engine
import os import os
import datetime
# from daily_usage import DailyUsage # from daily_usage import DailyUsage
app = Flask(__name__) app = Flask(__name__)
...@@ -22,7 +21,7 @@ db = SQLAlchemy(app) ...@@ -22,7 +21,7 @@ db = SQLAlchemy(app)
class DailyUsage(db.Model): class DailyUsage(db.Model):
__tablename__ = 'daily_usage' __tablename__ = 'daily_usage'
id = db.Column(db.Integer, primary_key=True, autoincrement=True) id = db.Column(db.Integer, primary_key=True, autoincrement=True)
date = db.Column(db.String(80), unique=True, nullable=False) date = db.Column(db.DateTime, unique=True, nullable=False)
hours = db.Column(db.Integer, unique=False) hours = db.Column(db.Integer, unique=False)
def __init__(self, date, hours): def __init__(self, date, hours):
...@@ -34,14 +33,12 @@ class DailyUsage(db.Model): ...@@ -34,14 +33,12 @@ class DailyUsage(db.Model):
db.create_all() db.create_all()
test = DailyUsage(date="12/11/2019", hours=5)
db.session.add(test) DailyUsage.query.filter_by(id=1).delete()
db.session.commit()
print(DailyUsage.query.all())
# Create dictionary to store pin info # Create dictionary to store pin info
pins = { pins = {
25: {'name': 'Light', 'state': GPIO.LOW} 25: {'name': 'Light', 'state': GPIO.LOW, 'on_time': None}
} }
# Setup each pin # Setup each pin
...@@ -81,6 +78,8 @@ def toggle_pin(change_pin): ...@@ -81,6 +78,8 @@ def toggle_pin(change_pin):
message += " off." message += " off."
else: else:
message += " on." message += " on."
pins[change_pin]['on_time'] = datetime.datetime.now()
print(pins[change_pin])
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