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

Display cost per unit

parent 82e86496
......@@ -18,6 +18,7 @@ GPIO.setwarnings(False)
db = SQLAlchemy(app)
os.environ['cost_per_kWh'] = '0.1622'
# TODO: Move this into daily_usage class file
class DailyUsage(db.Model):
__tablename__ = 'daily_usage'
......@@ -34,9 +35,6 @@ class DailyUsage(db.Model):
return '<DailyUsage %r, %r, %r>' % (self.id, self.date, self.kwhUsed)
db.create_all()
def get_todays_usage():
latest_entry = db.session.query(DailyUsage).order_by(DailyUsage.id.desc()).first()
if latest_entry:
......@@ -50,16 +48,34 @@ def get_todays_cost():
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()
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
pins = {
......@@ -83,7 +99,9 @@ def main():
template_data = {
'pins': pins,
'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)
......@@ -110,37 +128,13 @@ def toggle_pin(change_pin):
template_data = {
'pins': pins,
'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)
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'])
def handle_data():
new_price = request.form['kWhprice']
......@@ -148,7 +142,8 @@ def handle_data():
template_data = {
'pins': pins,
'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)
......@@ -158,7 +153,8 @@ def add_new_device():
template_data = {
'pins': pins,
'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)
......
......@@ -31,6 +31,11 @@
<div class="smart_meter">
<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>
<ul>
{% for pin in pins %}
......@@ -39,10 +44,6 @@
{% endif %}
{% endfor %}
</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>
......
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