-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
163 lines (131 loc) · 5.08 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="style.css" />
<link rel="manifest" href="manifest.json"/>
<meta name="theme-color" content="#2e7d32"/>
<link rel="apple-touch-icon" href="icons/logo192.png" />
<link rel="icon" href="icons/logo192.png" />
<title>SC Gate Opener</title>
<meta name="description" content="A PWA implementing the S&C APIs to more efficiently open the gates" />
<script type="module">
import { getUsername, postRequest, getRequest, login, hasValidToken } from './js/utils.js';
// Make XHR tools available in the devtools
window.postRequest = postRequest;
window.getRequest = getRequest;
async function showLogin() {
const username = await getUsername();
if(username) document.getElementById("username").value = username;
document.getElementById("login-dialog").style.visibility = "visible";
}
function hideLogin() {
document.getElementById("login-dialog").style.visibility = "hidden";
}
function createNotification(message, success) {
const text = document.createTextNode(message);
const content = document.createElement("div");
content.classList.add("paper-content");
content.appendChild(text);
const notification = document.createElement("div");
notification.classList.add("paper-root");
notification.classList.add(`notification-${success ? "success" : "fail"}`);
notification.appendChild(content);
document.getElementById("notifications").appendChild(notification);
setTimeout(() => notification.remove(), 5000);
}
async function openGate(gateCode) {
const response = await postRequest("tasks", {
type: "AccessControlRequestEntryByCode",
isAsync: false,
params: {
gateCode
},
});
if(response.result === "error") throw new Error(`Failed to open: ${response.message}`);
return `Opened gate: ${response.message}`;
}
async function wrapNotification(fn, ...args) {
try {
const message = await fn(...args);
createNotification(message, true);
console.log(message);
return true;
} catch(e) {
createNotification(e.message ?? "Unknown error", false);
console.error(e);
return false;
}
}
function createGatePanel({name, gateCode}) {
const root = document.createElement("div");
root.classList.add("paper-root");
const title = document.createElement("h2");
title.classList.add("paper-title");
title.innerText = name;
root.appendChild(title);
const content = document.createElement("div");
content.classList.add("paper-content");
root.appendChild(content);
const button = document.createElement("button");
button.addEventListener("click", () => wrapNotification(openGate, gateCode));
button.innerText = `Open ${name}`;
content.appendChild(button);
const actions = document.createElement("div");
actions.classList.add("paper-actions");
root.appendChild(actions);
document.querySelector("main").appendChild(root);
}
const gates = [
{name: "Left Gate", tag: "left", gateCode: "AC7D9A9F38CD99B7F9BCA84E920ACABBD34D3C9D1A3677CC75885B02B36560FDB16B826456A619E6B9528B7ECA33B0A824E20FFB3056784D06AC478FC1FF377AB90AF5029B6010954B4B7FABF2B3EE68C2D1D344"},
{name: "Right Gate", tag: "right", gateCode: "A1B68D31185180FC172B7C3B7BC7C16A108CEF84E9D324D5D8EC42514B0C1E873375962AB9AFAAFD79224046D3213F9F6E28042BD3BA890225D5CAE6E591C45D1E2617290258EE67F41DE9DD5401D94D6D27F36D"},
];
addEventListener("load", async () => {
var previousHash = "";
async function processHash() {
const hash = window.location.hash.replace(/#/, "");
if(hash === previousHash) return;
previousHash = hash;
const gate = gates.find(gate => gate.tag === hash);
if(gate) await wrapNotification(openGate, gate.gateCode);
}
document.querySelector("form").addEventListener("submit", async (e) => {
e.preventDefault();
const username = document.getElementById("username").value;
const password = document.getElementById("password").value;
if(await wrapNotification(login, username, password)) {
hideLogin();
await processHash();
}
document.getElementById("password").value = "";
});
for(const gate of gates) {
createGatePanel(gate);
}
// Make sure we have a valid token on start of the application
if(!await hasValidToken()) await showLogin();
else await processHash();
});
// Register service worker, necessary for PWA
const isLocalhost = location.hostname === "localhost" || location.hostname === "127.0.0.1" || location.protocol === "file:";
if(!isLocalhost && 'serviceWorker' in navigator) navigator.serviceWorker.register('./sw.js');
</script>
</head>
<body>
<main></main>
<div class="dialog-root" id="login-dialog" style="visibility: hidden">
<div class="backdrop"></div>
<form class="paper-root">
<h2 class="paper-title">Login</h2>
<div class="paper-content">
<input type="text" placeholder="Username" id="username" />
<input type="password" placeholder="Password" id="password" />
</div>
<div class="paper-actions">
<button type="submit">LOGIN</button>
</div>
</form>
</div>
<div class="notifications" id="notifications"></div>
</body>
</html>