-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwaterheater.js
executable file
·499 lines (432 loc) · 12.1 KB
/
waterheater.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
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/* Code by Prajun Trital and An Nguyen
Data generation for both water and heater appliances of smart house
Calculating the water uses
water.cost = (gallons/748) * 2.52
Add up all of them at the end to get the total water cost
Heater calcs
2 Baths and 2 Showers: 0.65 * gallons of bath and shower water
Dishwasher: : 1.0 * gallons
Clothes washer: 0.85 * gallons
Time = 4 min * hot gallons
Usage = (time/3600) hour * 4500 watt
Cost = 0.12 kWh * (usage/1000)
*/
const client = require('./config/db'); //Exporting the db module from config folder
client.connect();
const fs = require("fs");
const tf1 = "water.txt"
const tf2 = "heater.txt"
fs.writeFile(tf1, '', function(err) {
if (err) {
return console.error(err);
}
});
fs.writeFile(tf2, '', function(err) {
if (err) {
return console.error(err);
}
});
function writ1(strng) {
fs.appendFile(tf1, strng+="\n", function(err) {
if (err) {
return console.error(err);
}
});
}
function writ2(strng) {
fs.appendFile(tf2, strng+="\n", function(err) {
if (err) {
return console.error(err);
}
});
}
const t = 59; //Desired number of days
function bath1(e,i) {
ta = "water";
id = 1;
gal = 30;
calcw(id,ta,gal,e,i,);
}
function shower1(e,i) {
ta = "water";
id = 2;
gal = 25;
calcw(id,ta,gal,e,i);
}
function bath2(e,i) {
ta = "water";
id = 3;
gal = 30;
calcw(id,ta,gal,e,i);
}
function shower2(e,i) {
ta = "water";
id = 4;
gal = 25;
calcw(id,ta,gal,e,i);
}
function dishwasher(e,i) {
ta = "water";
id = 5;
gal = 6;
calcw(id,ta,gal,e,i);
}
function clothwasher(e,i) {
ta = "water";
id = 6;
gal = 20;
calcw(id,ta,gal,e,i);
}
/*
This function caluclate the total cost and usage.
Param: id : id of the databse table
ta : name of the table from the database
gal : hot water gallons that is being used
e : object that is being passed
i : number of days passed
It calls the update function to update the cost and gallons
*/
function calcw(id,ta,gal,e,i){
var waterCost = ((gal/748) * 2.52).toFixed(2);
e.cost += Number(waterCost);
e.gallons += Number(gal);
e.dailycos += Number(waterCost);
e.dailygal += Number(gal);
updatw(id,ta,gal,e,i,waterCost);
}
/*
This function update the total cost and usage into the databse
Param: id : id of the databse table
ta : name of the table from the database
e : object that is being passed
i : number of days
*/
function updatw(id,ta,gal,e,i,wc) {
const statement = `UPDATE ${ta} SET gallons = ${e.gallons}, cost = ${e.cost.toFixed(2)} WHERE id = ${id}`;
client.query(statement, (err,res) => {
if (err) {
console.log(err.message);
return;
}
var day = days[i%7];
//writ1(`${i}, ${day}, ${e.object}: ${gal}, ${wc}`); //Uncomment this to see the gallon usage and cost of each individual item
});
}
function totwatr(watArr) {
var totc = 0;
var totg = 0;
for(let i = 0; i < 6; i++) {
totg += Number(watArr[i].gallons);
totc += Number(watArr[i].cost);
}
const statement = `UPDATE water SET gallons = ${totg}, cost = ${totc.toFixed(2)} WHERE id = 7`;
client.query(statement, (err,res) => {
if (err) {
console.log(err.message);
return;
}
//console.log(`\nAll ${t} days: water update completed :)`);
//writ1(`Water - Total gallons used: ${totg} | Total water cost: ${totc.toFixed(2)}`);
//writ1(`${totg} ${totc.toFixed(2)}`);
});
}
function batheat1(e,i) {
ta = "hwheater";
id = 1;
gal = 30 * 0.65;
calch(id,ta,gal,e,i);
}
function showerheat1(e,i) {
ta = "hwheater";
id = 2;
gal = 25 * 0.65;
calch(id,ta,gal,e,i);
}
function batheat2(e,i) {
ta = "hwheater";
id = 3;
gal = 30 * 0.65;
calch(id,ta,gal,e,i);
}
function showerheat2(e,i) {
ta = "hwheater";
id = 4;
gal = 25 * 0.65;
calch(id,ta,gal,e,i);
}
function disheater(e,i) {
ta = "hwheater";
id = 5;
gal = 6;
calch(id,ta,gal,e,i);
}
function clotheater(e,i) {
ta = "hwheater";
id = 6;
gal = 20 * 0.85;
calch(id,ta,gal,e,i);
}
/*
This function caluclate the total cost and usage.
Param: id : id of the databse table
ta : name of the table from the database
gal : hot water gallons that is being used
e : object that is being passed
i : number of days passed
Watt: hot water heater always uses 4500 watt per hour
Time: 4 minutes to heat a gallon of water
Usage: hours of use * kilowatt
Cost: $0.12 * usage
It calls the update function to update the usage, cost, gallons and minuteson
*/
function calch(id,ta,gal,e,i){
var time = (4 * gal)/60;
var usage = time * (4500/1000);
var electricityCost = (0.12 * usage);
e.usage += Number(usage);
e.cost += Number(electricityCost);
e.gallons += Number(gal);
e.hourson += Number(time);
e.dailycos += Number(electricityCost);
e.dailygal += Number(gal);
e.dailyuse += Number(time);
updath(id,ta,gal,time,e,i,electricityCost);
}
function updath(id,ta,gal,time,e,i,ec){
const statement = `UPDATE ${ta} SET gallons = ${e.gallons}, hourson = ${e.hourson.toFixed(2)}, usage = ${e.usage.toFixed(3)}, cost = ${e.cost.toFixed(2)} WHERE id = ${id}`;
client.query(statement, (err,res) => {
if (err) {
console.log('An error occured! while updating');
return;
}
var day = days[i%7];
//writ2(`${i}, ${day}, ${e.object}: ${time.toFixed(1)}, ${gal}, ${ec.toFixed(2)}`); //Uncomment this to see the usage and cost of each individual item
});
}
function totheatr(heatArr) {
var totc = 0;
var totg = 0;
var totu = 0;
var toth = 0;
for(let i = 0; i < 6; i++) {
totg += Number(heatArr[i].gallons);
totc += Number(heatArr[i].cost);
totu += Number(heatArr[i].usage);
toth += Number(heatArr[i].hourson);
}
const statement = `UPDATE hwheater SET hourson = ${toth.toFixed(2)}, gallons = ${totg}, usage = ${totu.toFixed(3)}, cost = ${totc.toFixed(2)} WHERE id = 7`;
client.query(statement, (err,res) => {
if (err) {
console.log(err.message);
return;
}
//console.log(`\nAll ${t} days: hot water heater update completed :)`);
//writ2(`Heater - Total time on: ${toth.toFixed(2)} hours | Total gallons heated: ${totg} | Total heater cost: ${totc.toFixed(2)}`);
//writ2(`${toth.toFixed(2)} ${totg} ${totc.toFixed(2)}`);
client.end();
});
}
function hwdailytot(watarr, heatarr, i) {
var wtotc = 0;
var wtotg = 0;
var htotc = 0;
var htotg = 0;
var htotu = 0;
for(let j = 0; j < 6; j++) {
wtotg += Number(watarr[j].dailygal);
wtotc += Number(watarr[j].dailycos);
htotu += Number(heatarr[j].dailyuse);
htotg += Number(heatarr[j].dailygal);
htotc += Number(heatarr[j].dailycos);
watarr[j].dailygal = 0;
watarr[j].dailycos = 0;
heatarr[j].dailyuse = 0;
heatarr[j].dailygal = 0;
heatarr[j].dailycos = 0;
}
writ1(`${i} ${wtotg} ${wtotc.toFixed(2)}`);
writ2(`${i} ${htotu.toFixed(2)} ${htotg} ${htotc.toFixed(2)}`);
}
function weekend(i) { //Checks if the day id is on a weekend
let day = i % 7; //Data generation starts on a Friday, so every 2nd and 3rd step in the week cycle will be Saturday and Sunday
if(day === 2 || day === 3) {
return true;
} else {
return false;
}
}
function washday(i) { //Determines if it's one of the 4 days of the week to run the dishwasher, clothes washer, and clothes dryer
let day = i % 7; //Data generation starts on a Friday; 1 -> 6 -> 0 = Friday -> Wednesday -> Thursday
if(day === 2 || day === 3 || day === 5 || day === 0) { //Cleaning days: Saturday, Sunday, Tuesday, Thursday
return true;
} else {
return false;
}
}
function rangedom(n1, n2) { //n1 = min random number range; n2 = max random number range
return ((Math.floor(Math.random() * (n2 - n1 + 1))) + n1);
}
//Array of objects that uses water
let waterArr = [
{
object: "1st Bath",
cost: 0,
gallons: 0,
dailycos: 0,
dailygal: 0
},
{
object: "2nd Bath",
cost: 0,
gallons: 0,
dailycos: 0,
dailygal: 0
},
{
object: "1st Shower",
cost: 0,
gallons: 0,
dailycos: 0,
dailygal: 0
},
{
object: "2nd Shower",
cost: 0,
gallons: 0,
dailycos: 0,
dailygal: 0
},
{
object: "Dish Washer",
cost: 0,
gallons: 0,
dailycos: 0,
dailygal: 0
},
{
object: "Clothes Washer",
cost: 0,
gallons: 0,
dailycos: 0,
dailygal: 0
}
];
//Array of water appliances that heater needs to be used on
let heaterArr = [
{
object: "1st Bath Heater",
hourson: 0,
cost: 0,
gallons: 0,
usage : 0,
dailyuse: 0,
dailycos: 0,
dailygal: 0
},
{
object: "2nd Bath Heater",
hourson: 0,
cost: 0,
gallons: 0,
usage : 0,
dailyuse: 0,
dailycos: 0,
dailygal: 0
},
{
object: "1st Shower Heater",
hourson: 0,
cost: 0,
gallons: 0,
usage : 0,
dailyuse: 0,
dailycos: 0,
dailygal: 0
},
{
object: "2nd Shower Heater",
hourson: 0,
cost: 0,
gallons: 0,
usage : 0,
dailyuse: 0,
dailycos: 0,
dailygal: 0
},
{
object: "Dish Washer Heater",
hourson: 0,
cost: 0,
gallons: 0,
usage : 0,
dailyuse: 0,
dailycos: 0,
dailygal: 0
},
{
object: "Clothes Washer Heater",
hourson: 0,
cost: 0,
gallons: 0,
usage : 0,
dailyuse: 0,
dailycos: 0,
dailygal: 0
}
];
const days = ["Thursday", "Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday"];
const funw = [bath1, bath2, shower1, shower2, dishwasher, clothwasher]; //Array of functions corresponding to the water objects
const funh = [batheat1, batheat2, showerheat1, showerheat2, disheater, clotheater]; //Array of functions corresponding to the heater objects
for(let i = 1; i <= t; i++) { //Day cycles (Day 1-59; Jan 1 - Feb 28)
let n = 0; //Variable for water and heater objects' address locations
let c = 0; //Variable for amount of times baths/showers are taken per day
if(weekend(i)) { //3 baths on weekends
c = 3;
} else { //2 baths on weekdays
c = 2;
}
for(let r = 0; r < c; r++) {
var y = rangedom(1, 2); //Randomly chooses bath1 or bath2
if (y === 1) {
n = 0; //bath1
funw[n](waterArr[n], i);
funh[n](heaterArr[n], i);
} else if (y === 2) {
n = 1; //bath2
funw[n](waterArr[n], i);
funh[n](heaterArr[n], i);
}
}
if(weekend(i)) { //3 showers on weekends
c = 3;
} else { //2 showers on weekdays
c = 2;
}
for(let r = 0; r < c; r++) {
var y = rangedom(1, 2); //Randomly chooses shower1 or shower2
if (y === 1) {
n = 2; //shower1
funw[n](waterArr[n], i);
funh[n](heaterArr[n], i);
} else if (y === 2) {
n = 3; //shower2
funw[n](waterArr[n], i);
funh[n](heaterArr[n], i);
}
}
n = 4; //dishwasher
if(washday(i)) {
funw[n](waterArr[n], i);
funh[n](heaterArr[n], i);
}
n = 5; //clothwasher
if(washday(i)) {
funw[n](waterArr[n], i);
funh[n](heaterArr[n], i);
}
hwdailytot(waterArr, heaterArr, i);
if(i === t) {
totwatr(waterArr);
totheatr(heaterArr);
}
}