forked from CMyers79/solarApp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
151 lines (120 loc) · 5.49 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# This program sets up Flask view functions for 4 html templates, with variables passed between pages with the
# session cookie. The program calls nrel.py, which calls the NREL NSRDB API and returns the solar irradiation data
# for the GPS coordinates and time period that are passed in as parameters. The program then calls calcs.py with
# the solar data, a set of load, solar panel, and battery data input by the user on one of the web pages, and generates
# a graph of the battery charge level over the time period specified.
from flask import Flask, render_template, request, flash, redirect, url_for, session
from nrel import get_GHI
from calcs import track_battery_changes
app = Flask(__name__)
app.config['SECRET_KEY'] = '1ED964D2E87CF13129B56A5F4B86A'
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
latitude = request.form['latitude']
longitude = request.form['longitude']
startdate = request.form['startdate']
enddate = request.form['enddate']
if not latitude or not longitude:
flash('Latitude and Longitude are required')
elif not startdate or not enddate:
flash('Start and end dates are required')
else:
startdate = [int(numString) for numString in startdate.split("/")]
enddate = [int(numString) for numString in enddate.split("/")]
coords = [float(latitude), float(longitude)]
session['startdate'] = startdate
session['enddate'] = enddate
session['coords'] = coords
return redirect(url_for('result1', startdate=startdate, enddate=enddate, coords=coords), code=307)
return render_template('index.html')
@app.route('/result1', methods=['GET', 'POST'])
def result1():
startdate = session['startdate']
enddate = session['enddate']
coords = session['coords']
GHI_list = get_GHI(coords, startdate, enddate)
start_hour = startdate[2]
session['start_hour'] = start_hour
session['GHI_list'] = GHI_list
if request.method == 'POST':
if "continue" in request.form:
return redirect(url_for('index2', start_hour=start_hour, GHI_list=GHI_list))
return render_template('result1.html', list1=GHI_list)
@app.route('/index2', methods=['GET', 'POST'])
def index2():
if request.method == 'POST':
water = request.form.get('water', False)
fan = request.form.get('fan', False)
lighting = request.form.get('lighting', False)
plug_load = request.form.get('plug_load', False)
refrig = request.form.get('refrig', False)
batt_type = request.form.get('batt_type', False)
batt_cap = request.form.get('batt_cap', False)
panel_size = request.form.get('panel_size', False)
num_panels = request.form.get('num_panels', False)
start_hour = session['start_hour']
GHI_list = session['GHI_list']
if not water or not fan or not lighting or not plug_load or not refrig or not batt_type or not batt_cap or not panel_size or not num_panels:
flash('All inputs are required, enter 0 for no consumption')
else:
water = int(water) / 2
fan = int(fan)
lighting = int(lighting)
plug_load = int(plug_load)
batt_cap = int(batt_cap)
panel_size = int(panel_size)
num_panels = int(num_panels)
session['water'] = water
session['fan'] = fan
session['lighting'] = lighting
session['plug_load'] = plug_load
session['refrig'] = refrig
session['batt_type'] = batt_type
session['batt_cap'] = batt_cap
session['start_hour'] = start_hour
session['GHI_list'] = GHI_list
session['panel_size'] = panel_size
session['num_panels'] = num_panels
return redirect(url_for('result2', water=water, fan=fan, lighting=lighting, plug_load=plug_load,
refrig=refrig, batt_type=batt_type, batt_cap=batt_cap, start_hour=start_hour,
GHI_list=GHI_list), code=307)
return render_template('index2.html')
@app.route('/result2', methods=['GET', 'POST'])
def result2():
water = session['water']
fan = session['fan']
lighting=session['lighting']
plug_load = session['plug_load']
refrig = session['refrig']
batt_type = session['batt_type']
batt_cap = session['batt_cap']
start_hour = session['start_hour']
GHI_list = session['GHI_list']
panel_size = session['panel_size']
num_panels = session['num_panels']
GHI_list = [int(numString) for numString in GHI_list]
if batt_type == "LI":
batt_charge = batt_cap * 0.8
elif batt_type == "PB":
batt_charge = batt_cap * 0.5
else:
batt_charge = 0
if refrig == "LP":
refrig = 15
elif refrig == "DC":
refrig = 220
elif refrig == "AC":
refrig = 275
else:
refrig = 0
load_dict = {'water': water, 'fan': fan, 'lighting': lighting, 'plug load': plug_load, 'refrigerator': refrig}
batt_result = track_battery_changes(batt_charge, load_dict, GHI_list, start_hour, panel_size, num_panels)
charge_list = batt_result[0]
percent_remaining = batt_result[1]
if request.method == 'POST':
if "continue" in request.form:
return redirect(url_for('/'))
return render_template('result2.html', percent_remaining=percent_remaining)
if __name__ == '__main__':
app.run(debug=True)