/
main.js
executable file
·357 lines (331 loc) · 11.6 KB
/
main.js
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
var current_page = 1;
var selected_roof = 0;
var page2_override = true;
var area;
var heritage_zone;
var heritage_listed;
var shadow;
let no_building_detected = false;
var avg_kwh = 35;
function hide(el){
el.attr("hidden", "");
}
function show(el){
el.removeAttr("hidden");
}
function show_error(msg){
show($("#error-msg-container"));
$("#error-msg-contents").text(msg);
}
function hide_error(){
hide($("#error-msg-container"));
}
function resize_error_msg(){
var win_w = $(window).width();
var win_h = $(window).height();
$("#error-msg").css("margin-left", Math.min(win_w, (win_w / 2) - 150));
$("#error-msg").css("margin-top", Math.min(win_h, (win_h / 2) - 125));
}
function generate_roof_output(area){
var watt_per_sqm = 150;
var output = 0;
if(selected_roof == 1){
output = area * watt_per_sqm * 0.4;
} else if(selected_roof == 2){
output = area * watt_per_sqm * 0.25;
} else if(selected_roof == 3){
output = area * watt_per_sqm * 0.6;
} else if(selected_roof == 4){
output = area * watt_per_sqm * 0.45;
}
return output * 0.4 * shadow;
}
function render_results(watts){
//console.log("Watts:" + watts);
var area_contents = "<hr/>";
if(watts == "0" || watts == 0 || watts === NaN || no_building_detected){
console.log("No building detected.");
area_contents += "<h2>Sorry, there was an issue finding your address.</h2>";
$("#area").html(area_contents);
return;
}
//avg_kwh = 35;
var kwh = watts / 1000;
var avg_dollar_cost = 0.21;
var avg_yearly_cost = avg_kwh * 365 * avg_dollar_cost;
var feed_in_cost = 0.15;
console.log("Yearly cost: " + avg_yearly_cost);
console.log("Yearly generation: " + (kwh * 24) * 365 * avg_dollar_cost);
var net_pwr_year_cost = (avg_kwh - (kwh * 24)) * 365 * avg_dollar_cost;
var earn_or_save = "savings";
if(net_pwr_year_cost < 0){
net_pwr_year_cost = (avg_kwh - (kwh * 24)) * 365 * feed_in_cost;
earn_or_save = "earnings";
}
var low_checked = "";
var med_checked = "";
var high_checked = "";
if(avg_kwh == 20){
low_checked = "checked";
}
if(avg_kwh == 35){
med_checked = "checked";
}
if(avg_kwh == 45){
high_checked = "checked";
}
if(heritage_zone == "1" && !(heritage_listed == "1")){
area_contents += "<h5><i class=\"fa fa-exclamation-circle\" aria-hidden=\"true\"></i>\nWarning: This property " +
"is in a heritage zone. There may be restrictions on installing solar panels.</h5>";
}
if(heritage_listed == "1"){
area_contents += "<h5><i class=\"fa fa-exclamation-circle\" aria-hidden=\"true\"></i>\nWarning: This property" +
" is heritage listed. There will be restrictions on installing solar panels.</h5>";
}
if(heritage_listed == "1" && heritage_zone == "1"){
area_contents += "<h5>This property is also in a heritage zone.</h5>";
}
area_contents += "<h3>Select your average power consumption</h3>\n" +
"<div id='power-usage'>" +
"<div class=\"form-check\">\n" +
" <ul>"+
" <li><input class=\"form-check-input\" type=\"radio\" name=\"power-low\" id=\"power-low\" value=\"option1\"" + low_checked+ ">\n" +
" <label class=\"form-check-label\" for=\"power-low\">\n" +
" Low - 20kWh per day\n" +
" </label></li>\n" +
" <li><input class=\"form-check-input\" type=\"radio\" name=\"power-med\" id=\"power-med\" value=\"option2\"" + med_checked + ">\n" +
" <label class=\"form-check-label\" for=\"power-med\">\n" +
" Medium - 35kWh per day\n" +
" </label></li>\n" +
" <li><input class=\"form-check-input\" type=\"radio\" name=\"power-high\" id=\"power-high\" value=\"option3\"" + high_checked + ">\n" +
" <label class=\"form-check-label\" for=\"power-high\">\n" +
" High - 45kWh per day\n" +
" </label></li>\n" +
" </ul>" +
"</div>";
var star_rating_contents = "<h3>Your rating:</h3>";
var star_rating = 0;
var rating_2 = 1000;
var rating_3 = 2500;
var rating_4 = 3500;
var rating_5 = 5000;
net_pwr_year_cost = Math.abs(net_pwr_year_cost);
if(earn_or_save == "savings"){
star_rating = 1;
} else {
if(net_pwr_year_cost >= rating_2 && net_pwr_year_cost < rating_3){
star_rating = 2;
} else if(net_pwr_year_cost >= rating_3 && net_pwr_year_cost < rating_4){
star_rating = 3;
} else if(net_pwr_year_cost >= rating_4 && net_pwr_year_cost < rating_5){
star_rating = 4;
} else if(net_pwr_year_cost >= rating_5){
star_rating = 5;
} else {
star_rating = 1;
}
}
console.log("Star rating: " + star_rating);
for(let i = 0; i < star_rating; i++){
star_rating_contents += "<i class=\"fa fa-star power-rating-star\" aria-hidden=\"true\"></i>\n";
}
area_contents += "<hr/><div class=\"container\">\n" +
" <div class=\"row row-1\">\n" +
" <div class=\"col\">\n" +
" <h2>KiloWatt output:</h2>\n" +
" </div>\n" +
" <div class=\"col\">\n" +
" <h2>" + Math.floor(kwh) +" KW</h2>\n" +
" </div>" +
" </div>" +
" <div class=\"row row-2\">\n" +
" <div class=\"col\">\n" +
" <h2>Yearly output:</h2>\n" +
" </div>\n" +
" <div class=\"col\">\n" +
" <h2>" + Math.floor(((watts * 365) * 24) / 1000000 ) +" MWh</h2>\n" +
" </div>" +
" </div>" +
" <div class=\"row row-3\">\n" +
" <div class=\"col\">\n" +
" <h2>Yearly " + earn_or_save + "*:</h2>\n" +
" </div>\n" +
" <div class=\"col\">\n" +
" <h2>$" + Math.floor(Math.abs(net_pwr_year_cost)) +"</h2>\n" +
" </div>" +
" </div>" +
star_rating_contents +
"<p>* Based on average household power usage.</p>"
$("#area").html(area_contents);
$("#power-low").click(function(){
avg_kwh = 20;
render_results(watts);
});
$("#power-med").click(function(){
avg_kwh = 35;
render_results(watts);
});
$("#power-high").click(function(){
avg_kwh = 45;
render_results(watts);
});
}
function show_results(){
var watts = generate_roof_output(parseFloat(area))
render_results(watts);
}
function load_area(){
var lat = place.geometry.location.lat();
var lng = place.geometry.location.lng();
show($("#loading-bar"));
$("#area").html("");
console.log("Loading....");
$("#results-for").text("For: " + place.formatted_address);
$.ajax("/get_area?lat=" + lat + "&lng=" + lng).done(function(resp){
console.log("Loaded area from latlon");
hide($("#loading-bar"));
area = resp.split(",")[0];
console.log("Area: " + area);
if(area == 0 || area == "0"){
no_building_detected = true
}
heritage_zone = resp.split(",")[1];
heritage_listed = resp.split(",")[2];
shadow = resp.split(",")[3];
console.log(resp);
show_results();
});
}
$(document).ready(function(){
//var roofsel = $("#roof-selector");
//$("#windowwidth").text($(window).width());
resize_error_msg();
$("#search-ok").click(function(){
if(place == ""){
show_error("Please select a location.");
} else {
hide($("#search"));
show($("#roof-selector"));
load_area();
current_page = 2;
}
});
$("#roof-ok").click(function(){
if(place == "" && !page2_override){
show_error("Please select a location");
} else {
if(selected_roof == 0 && !page2_override){
show_error("Please select a roof");
} else {
hide($("#roof-selector"));
show($("#results-page"));
current_page = 3;
}
}
});
$("#error-ok").click(function(){
hide_error();
})
$("#nav-1").click(function(){
if(current_page != 1){
if(current_page == 2){
hide($("#roof-selector"));
show($("#search"));
current_page = 1;
} else if (current_page == 3){
hide($("#results-page"));
show($("#search"));
current_page = 1;
} else if (current_page == 4){
hide($("#heritage-page-page"));
show($("#search"));
current_page = 1;
}
}
});
$("#nav-2").click(function(){
if(current_page == 1) {
if (place == "" && !page2_override) {
show_error("Please select a location.");
} else {
hide($("#search"));
show($("#roof-selector"));
current_page = 2;
}
} else if(current_page == 3){
hide($("#results-page"));
show($("#roof-selector"));
current_page = 2;
}else if(current_page == 4){
hide($("#heritage-page"));
show($("#roof-selector"));
current_page = 2;
}
});
$("#nav-3").click(function(){
if(current_page == 1) {
if (place == "") {
show_error("Please select a location.");
} else {
if(selected_roof == 0){
show_error("Please select a roof type (page 2)");
} else {
hide($("#search"));
show($("#results-page"));
current_page = 3;
}
}
} else if(current_page == 2){
if (place == "") {
show_error("Please select a location.");
}else {
if(selected_roof == 0){
show_error("Please select a roof type (page 2)");
}
else {
hide($("#roof-selector"));
show($("#results-page"));
current_page = 3;
}
}
}else if(current_page == 4){
hide($("#heritage-page"));
show($("#results-page"));
current_page = 3;
}
})
$("#roof-1").click(function(){
selected_roof = 1;
$("#roof-1").addClass("selected-roof");
$("#roof-2").removeClass("selected-roof");
$("#roof-3").removeClass("selected-roof");
$("#roof-4").removeClass("selected-roof");
});
$("#roof-2").click(function(){
selected_roof = 2;
$("#roof-1").removeClass("selected-roof");
$("#roof-2").addClass("selected-roof");
$("#roof-3").removeClass("selected-roof");
$("#roof-4").removeClass("selected-roof");
});
$("#roof-3").click(function(){
selected_roof = 3;
$("#roof-1").removeClass("selected-roof");
$("#roof-2").removeClass("selected-roof");
$("#roof-3").addClass("selected-roof");
$("#roof-4").removeClass("selected-roof");
});
$("#roof-4").click(function(){
selected_roof = 4;
$("#roof-1").removeClass("selected-roof");
$("#roof-2").removeClass("selected-roof");
$("#roof-3").removeClass("selected-roof");
$("#roof-4").addClass("selected-roof");
});
})
$( window ).resize(function() {
//$( "body" ).prepend( "<div>" + $( window ).width() + "</div>" );
//console.log($(window).width());
//$("#windowwidth").text($(window).width());
resize_error_msg();
});