Commit 4a3d4be5 authored by Kristian Tan's avatar Kristian Tan

Display cost per unit

parent 82e86496
...@@ -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)
......
...@@ -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>
......
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