-
Notifications
You must be signed in to change notification settings - Fork 0
/
laundry.js
211 lines (174 loc) · 5.97 KB
/
laundry.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
base_url = "http://169.234.81.18:8000"
// Find Machine ID
// This method pulls the machine id from the url
//
// pre: url must contain a query string with the first parameter being the id
// ex: www.laundryRoom.com/?id=10
// post: returns str of id num
function findMachineId(){
m = location.href.split('?');
f = m[1].split('=');
return f[1];
}
// Get Machines
// Sends a request to the server for the machines
// Recieves a json object containing information about the machines
//
// onload: calls the addMachineButtons func to add machines to the page
function getMachines(){
request = new XMLHttpRequest();
url = base_url + "/api/all_machine/Nieblalaundryroom/";
console.log(url)
request.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var machines = JSON.parse(this.responseText)["machines"];
console.log(machines)
addMachineButtons(machines);
}
};
request.open("GET", url, true);
request.send();
}
// Add Machine Buttons
// Adds the machines to the index page
//
// pre: machines is a json object containing info about machines
// post: buttons for each machine are added to the page
function addMachineButtons(machines){
// Get the divs for washer and dryer
washer_div = document.getElementById("Washers");
dryer_div = document.getElementById("Dryers");
// Loop through the machines
for(var i = 0; i < machines.length; i++){
// Get information about machine from json
id = machines[i]["id"];
action = "machineSelected(" + id + ")";
name = machines[i]["name"];
type = machines[i]["type"];
// Generate the html for the machine
p = document.createElement("p");
p.style = "text-align: center";
link = document.createElement("a");
button = document.createElement("button");
button.addEventListener('click', function(){
machineSelected(this.value);
});
button.className = "btn";
button.value = id;
button.type = "button";
button.innerHTML = name;
link.appendChild(button);
p.appendChild(link);
// put machine under either washer or dryer
if(type == "washer"){
washer_div.appendChild(p);
} else if(type == "dryer"){
dryer_div.appendChild(p);
}
}
}
// Add Machine Name
// Adds a machines name to an element based off of the given id
//
// post: element's inner html is set to the machine's name
function addMachineName(id, element){
request = new XMLHttpRequest();
url = base_url + "/api/machine_info/" + id;
request.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var machine_info = JSON.parse(this.responseText);
element.innerHTML = machine_info["machine_name"];
}
};
request.open("GET", url, true);
request.send();
}
// Machine Selected
// Determines whether to redirect the page to machinebusy or UserForm
// Checks current time against the machine's end time
// if now is before the end time, machinebusy is displayed
// if now is after the end time, UserForm is displayed
//
// post: redirected to a new page
function machineSelected(id){
request = new XMLHttpRequest();
url = base_url + "/api/machine_info/" + id;
request.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var machine_info = JSON.parse(this.responseText);
// Check current time against now
end_time = machine_info["start_time"] + machine_info["duration"]*60*1000;
current_time = + new Date();
// If now is less than end time, display machine busy
// else display UserForm
if (current_time < end_time){
window.location.href = "machinebusy.html?machine=" + id;
} else {
window.location.href = "UserForm.html?machine=" + id;
}
}
};
request.open("GET", url, true);
request.send();
}
// Start Timer
// Creates the timer on machinebusy
function startTimer(duration) {
var timer = duration, minutes, seconds;
setInterval(function () {
minutes = parseInt(timer / 60, 10)
seconds = parseInt(timer % 60, 10);
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
document.getElementById("minutes").innerHTML = minutes;
document.getElementById("seconds").innerHTML = seconds;
if (--timer < 0) {
timer = duration;
}
}, 1000);
}
// ACTIVITY LOG CODE
// Generates a table row
function generate_row(data, rowNum){
var row = document.createElement("tr");
machine_info = data[rowNum]
th = document.createElement("th");
th.scope = "row";
th_text = document.createTextNode(machine_info["name"]);
th.appendChild(th_text);
row.appendChild(th);
type = document.createElement("td");
type_txt = document.createTextNode(machine_info["type"]);
type.appendChild(type_txt);
row.appendChild(type);
user = document.createElement("td");
user_txt = document.createTextNode(machine_info["last_user"]["name"]);
user.appendChild(user_txt);
row.appendChild(user);
user_email = document.createElement("td");
user_email_txt = document.createTextNode(machine_info["last_user"]["email"]);
user_email.appendChild(user_email_txt);
row.appendChild(user_email);
return row;
}
// Generate all rows
// Calls generate_row on all rows
function generate_all_rows(data){
var rows = [];
for (var i = 0; i < data.length; i++){
rows.push(generate_row(data, i));
}
return rows;
}
// Updates the table
function update_table(tbl, rows){
// add all of the rows to the body
for (var r = 0; r < rows.length; r++){
tbl.appendChild(rows[r]);
}
}
// Loads activity log
function loadActivityLog(table, data){
rows = generate_all_rows(data)
update_table(table, rows);
}