-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
394 lines (278 loc) · 10.9 KB
/
index.html
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
<link rel="stylesheet" href="style.css" type="text/css">
<script src="all_sensors_cache.js"></script>
<script src="fetch.js"></script>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<script>
async function meter() {
var sensor_id = document.getElementById('gasInput').value
//var dis = document.getElementById('display')
try {
let done1 = fetchDataAndPlotDaily(sensor_id)
let done2 = fetchDataAndPlotWeekly(sensor_id)
// wait until these are done so we can show your individual data
// while we wait for the whole neighborhood to load
let dailyData = await done1
await done2
let first48 = getSelectReadings(dailyData, 300)
//this toggles the visibility of the text overview when fucntion fires
var overview = document.getElementById('overview');
overview.style.display = "inline";
//calculates data to compare
var dustHood = await getDataForNeighborhood(sensor_id);
//console.log("#&#&#&#&hood next:");
//console.dir(dustHood);
entireHood = wholeNeighborhood(dustHood);
console.log("the 2ns from hood");
console.log(entireHood[3].time);
console.log(entireHood[3].reading);
// neighborhoodPlot(entireHood);
leaderboardPlot(dustHood)
var hoodAverage = compareNeighbors(entireHood);
var userinputAverage = userAverage(first48);
console.log("this is the user invputaverage" + userinputAverage);
console.log("hoodave " + hoodAverage);
var usageRelative = ((userinputAverage / (userinputAverage - hoodAverage)) * 100).toFixed(1);
function determineSign(val) {
sign = val > 0 ? "above" : "below";
return sign;
}
var usageRelativeSign = determineSign(usageRelative);
//http://www.enstarnaturalgas.com/wp-content/uploads/2014/09/Attachment-E-Tariff-Sheets-262-4-.pdf
var averageInsert = document.getElementById('ban3');
averageInsert.innerHTML = usageRelative + " % " + usageRelativeSign;
var lastReadingValue = returnLastReading(first48);
var costValue = calculateCost(lastReadingValue);
document.getElementById('ban1').innerHTML = lastReadingValue.toLocaleString()+" BTUs";
document.getElementById('ban2').innerHTML = "$"+costValue;
}
catch (err) {
console.log(err)
}
}
function getSelectReadings(data, numReadings) {
//
let selectTime = [];
let selectReadings = [];
let selectObj = {};
//grab most recent numReadings readings (* 2 for hours)
const selectData = data.readings.slice(0, numReadings);
selectData.forEach(function (val) {
selectTime.push(val[0]);
selectReadings.push(val[1]);
})
//create object
selectObj.time = selectTime;
selectObj.reading = selectReadings;
//console.log("the object"+JSON.stringify(selectObj));
return selectObj;
}
function compareNeighbors(arr) {
var averageArr = [];
var dailyAverage;
//loop through the neighborhood array, removing those w/ fewer than 50 readings
for (i = 0; i < arr.length; i++) {
if (arr[i].reading.length > 6) {
//console.log ("this should be a readings array "+arr[i].reading)
//push the readings to avarge into a new array
var averageArr = averageArr.concat(arr[i].reading);
}
}
//console.log("the average big arr" + averageArr);
//console.dir(averageArr);
//average the new array
var dailySum = averageArr.reduce(function (a, b) { return a + b; });
var dailyAverage = dailySum / averageArr.length;
//console.log("the dailiy ageravge"+dailyAverage);
return dailyAverage;
}
function userAverage(arr) {
var useraverageArr = [];
var userdailyAverage;
// if (arr.reading.length >50) {
//console.log ("this should be a readings array "+arr[i].reading)
//push the readings to avarge into a new array
var useraverageArr = useraverageArr.concat(arr.reading);
// }
//console.log("the average big arr" + useraverageArr);
//console.dir(useraverageArr);
//console.log("the length of the input reading" + arr.reading.length);
//console.log("the length fo the arret is " + useraverageArr.length);
// var arrTest = (arr.reading === useraverageArr);
// console.log("the arr test" + arrTest);
//average the new array
var userdailySum = useraverageArr.reduce(function (a, b) { return a + b; });
var userdailyAverage = userdailySum / useraverageArr.length;
//console.log("the dailiy ageravge"+dailyAverage);
return userdailyAverage;
}
function calculateCost(sensor_id) {
//$0.13361 per hundred cubic feet (Ccf)
}
//take neighborhood data convert for the plotting.
function wholeNeighborhood(hood) {
//
let neighborTime = [];
let neighborReading = [];
let neighborObj = {};
let aggregatedneighborArray = [];
//grab most recent numReadings readings (* 2 for hours)
hood.forEach(
function (val) {
val.readings.forEach(
function (num) {
neighborTime.push(num[0]);
neighborReading.push(num[1]);
})
neighborObj.time = neighborTime;
neighborObj.reading = neighborReading;
neighborReading = [];
neighborTime = [];
{
aggregatedneighborArray.push(neighborObj);
neighborObj = {};
}
})
//console.log("the object"+JSON.stringify(selectObj));
return aggregatedneighborArray;
}
function returnLastReading (obj) {
var userLength = obj.reading.length;
var lastReading = (obj.reading[userLength -1]).toFixed(2);
console.log("the last reading"+ lastReading);
return lastReading;
}
function calculateCost (val) {
// BTU first to CCF
//
var valDaily = (val*24)/103000;
var cost = (valDaily * .13).toFixed(2);
console.log("the cost"+ cost);
return cost;
}
// plotting function in which an object is passed of time series and readings
function plotTime(timeSeries) {
var timesTrace = {
x: timeSeries.time,
y: timeSeries.reading,
mode: 'lines'
};
var timeTraceData = [timesTrace];
var timeLayout = {
title: 'Your gas usage over past day',
font: {
family: 'Open Sans',
color: '#000000',
size: 18
}
};
Plotly.newPlot('dailyUsage', timeTraceData, timeLayout);
}
///Neighborhood large plot
function neighborhoodPlot(hoodSet) {
hoodData = []
hoodSet.forEach(neighbor => {
hoodData.push({
x: neighbor.time,
y: neighbor.reading,
mode: 'lines',
})
})
var hoodLayout = {
title: 'Gas Usage Among Your Neighbors',
font: {
family: "Open-Sans",
color: '#000000',
size: 18
}
};
Plotly.newPlot('neighborhoodTime', hoodData, hoodLayout);
}
async function fetchDataAndPlotWeekly(sensor_id) {
let weeklyData = await getDataForSensorId(sensor_id)
let timeSeries = getSelectReadings(weeklyData, 999);
var timesTrace = {
x: timeSeries.time,
y: timeSeries.reading,
mode: 'lines'
};
var timeTraceData = [timesTrace];
var timeLayout = {
title: 'Your gas usage over past week',
font: {
family: 'Open Sans',
color: '#000000',
size: 18
}
};
Plotly.newPlot('weeklyUsage', timeTraceData, timeLayout);
return weeklyData
}
async function fetchDataAndPlotDaily(sensor_id) {
let dailyData = await getDailyDataForSensorId(sensor_id)
let timeSeries = getSelectReadings(dailyData, 999);
var timesTrace = {
x: timeSeries.time,
y: timeSeries.reading,
mode: 'lines'
};
var timeTraceData = [timesTrace];
var timeLayout = {
title: 'Your gas usage over past day',
font: {
family: 'Open Sans',
color: '#000000',
size: 18
}
};
Plotly.newPlot('dailyUsage', timeTraceData, timeLayout);
return dailyData
}
function leaderboardPlot(hoodSet) {
hoodData = {
x: [],
y: [],
type: 'bar',
}
hoodSet.forEach((neighbor, index) => {
if (!neighbor.readings.length) return
let total = neighbor.readings.reduce((x,y) => x?x[1]:0 + y?(y[1]*24):0)
hoodData.y.push(total)
hoodData.x.push('anon ' + index)
})
var hoodLayout = {
title: 'Weekly Gas Usage Among Your Neighbors',
font: {
family: "Open-Sans",
color: '#000000',
size: 18
}
};
Plotly.newPlot('neighborhoodTime', [hoodData], hoodLayout);
}
</script>
<div id="googleMap" style="width:100%;height:300px;"></div>
<script src="energyMapper.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?callback=myMap"></script>
<div id="main">
<img src="flame.png">
<h1 id='gas'>
Keep it cool</h1>
<h1>
Enter your gas meter ID number to see<br /> how you match up with your neighbors:
</h1>
<input id="gasInput" type="text" value="44634440">
<button onclick="meter()">Go</button>
<pre id="display"></pre>
<div id="overview">
<div id="dailyUsage"></div>
<div id="weeklyUsage"></div>
<div class="container">
<div id="div1"><p id="ban1"><p id="banexplain">Latest hourly reading</div>
<div id="div2"><p id="ban2"><p id="banexplain">Gas cost over 24-hour period</div>
<div id="div3"><p id="ban3"><p id="banexplain">Compared to your neighborhood</div>
</div>
<p id="things"><strong>Three things you can do to cut your gas usage:</strong><br /><br />
*Close your shades at night<br />*Lower your thermostat <br /> *Let in bright sun during the day. <br /></p>
</div>
<div id="neighborhoodTime"></div>
</div>