-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.html
165 lines (141 loc) · 4.74 KB
/
test.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
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<div class="m-auto max-w-2xl shadow-xl border rounded-lg px-6 py-4 mt-8">
<button class="bg-blue-500 border border-transparent text-white py-2 px-4 rounded transition-all hover:bg-blue-700 active:bg-blue-800" id="trigger">Manually Trigger the alarm</button>
<button class="border border-red-500 text-red-600 py-2 px-4 rounded transition-all hover:bg-red-100 active:bg-red-200" id="stop">Restart</button>
<button class="border border-transparent bg-gray-500 text-white py-2 px-4 rounded transition-all hover:bg-gray-600 active:bg-gray-700" id="loadLogs">Load Logs</button>
<br /><br />
<h1 class="text-2xl font-semibold mb-2">Add Alarm</h1>
<div class="flex items-center space-x-2">
<div>
<label for="HH" class="sr-only">HH</label>
<input type="number" class="border focus:ring border-gray-300 rounded-md outline-none px-4 py-2" id="HH" />
</div>
<div>
<label for="MM" class="sr-only">MM</label>
<input type="number" class="border focus:ring border-gray-300 rounded-md outline-none px-4 py-2" id="MM" />
</div>
<div>
<label for="MM" class="sr-only">Weekends?</label>
<input type="checkbox" class="border focus:ring border-gray-300 rounded-md outline-none px-4 py-2" id="ONWEEKENDS" />
</div>
<div>
<button type="submit" class="bg-blue-500 border border-transparent text-white py-2 px-4 rounded transition-all hover:bg-blue-700 active:bg-blue-800" onclick="addAlarm()">
Add Alarm
</button>
</div>
</div>
<h2 class="text-2xl font-semibold my-2">Current Alarms</h2>
<div id="alarms"></div>
<h2 class="text-2xl font-semibold my-2">Log</h2>
<pre id="LOG"></pre>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.7.0.min.js" integrity="sha256-2Pmvv0kuTBOenSvLm6bvfBSSHrUJ+3A7x6P5Ebd07/g=" crossorigin="anonymous"></script>
<script>
function addLog(...a){
$("#LOG").append(a.join(" ") + "\n");
}
addLog("Started");
$("#trigger").click(function () {
let t = $(this);
$(this).attr("disabled", true);
$.ajax({
url: "/ALARM",
//when the request is done
complete: function () {
setTimeout(function () {
t.attr("disabled", false);
}, 8000);
addLog("Triggered");
},
});
});
$("#stop").click(function () {
let t = $(this);
$(this).attr("disabled", true);
$.ajax({
url: "/STOP",
});
addLog("Stopped");
let i = 1;
const interval = setInterval(function () {
t.text(`Reconnecting (${i++}s)`);
$.ajax({
url: "/",
complete: function (data) {
clearInterval(interval);
t.attr("disabled", false);
t.text("Stop");
},
});
}, 1000);
});
function addAlarm() {
let HH = +$("#HH").val();
let MM = +$("#MM").val();
//add 0 if HH or MM is less than 10
if (HH < 10) {
HH = "0" + HH;
}
if (MM < 10) {
MM = "0" + MM;
}
$.ajax({
url: `/ADDALARM/${HH}/${MM}/${$("#ONWEEKENDS").is(":checked") ? 1 : 0}`,
complete: function () {
window.location.reload();
},
});
}
$("#loadLogs").click(function(){
$.ajax({
url: "/JSON",
complete: function(data){
addLog("Loaded Logs");
const blob = new Blob([data.responseText], {type: "text/json"});
window.open(URL.createObjectURL(blob), "Logs", "width=600,height=400,scrollbars=yes");
}
});
});
const alarms = [];
alarms.forEach((alarm) => {
$("#alarms").append(
`<div class="flex items-center space-x-2 mb-3">
<div>
<label for="staticEmail2" class="sr-only">HH</label>
<input type="number" readonly class="border border-gray-300 rounded-md px-4 py-2" id="staticEmail2" value="${alarm[0]}" />
</div>
<div>
<label for="inputPassword2" class="sr-only">MM</label>
<input type="number" readonly class="border border-gray-300 rounded-md px-4 py-2" id="inputPassword2" value="${alarm[1]}" />
</div>
<div>
${alarm[2] == 1 ? '<span class="text-green-500">Also on weekends</span>' : ""}
<button type="submit" class="bg-red-500 border border-transparent text-white py-2 px-4 rounded transition-all hover:bg-red-700 active:bg-red-800" onclick="deleteAlarm(${alarm[0]}, ${alarm[1]})">
Delete
</button>
</div>
</div>`
);
});
function deleteAlarm(HH, MM) {
//add 0 if HH or MM is less than 10
if (HH < 10) {
HH = "0" + HH;
}
if (MM < 10) {
MM = "0" + MM;
}
$.ajax({
url: `/DELETEALARM/${HH}/${MM}`,
complete: function () {
window.location.reload();
},
});
}
</script>
</html>