Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
parkpow
GitHub Repository: parkpow/deep-license-plate-recognition
Path: blob/master/docker/dd-extension/ui/src/components/Snapshot.tsx
1091 views
1
import type React from "react";
2
import { useCallback, useEffect, useState } from "react";
3
import { Button, Col, Row } from "react-bootstrap";
4
import Form from "react-bootstrap/Form";
5
import { openBrowserUrl } from "../helpers";
6
import { useDockerDesktopClient } from "../hooks/useDockerDesktopClient";
7
import Loader from "./Loader";
8
import ShowCommand from "./ShowCommand";
9
import Uninstall from "./Uninstall";
10
import Update from "./Update";
11
12
const countryOptions = [
13
{ value: "", label: "Select country" },
14
{ value: "Global", label: "Global" },
15
{ value: "egypt", label: "Egypt" },
16
{ value: "germany", label: "Germany" },
17
{ value: "japan", label: "Japan" },
18
{ value: "korea", label: "Korea" },
19
{ value: "thailand", label: "Thailand" },
20
{ value: "uae", label: "United Arab Emirates" },
21
];
22
23
const architectureOptionsSnapshot = [
24
{ value: "alpr", label: "Intel x86 or amd64(x64)" },
25
{ value: "alpr-no-avx", label: "Intel x86 or amd64(x64) no-avx" },
26
{ value: "alpr-gpu", label: "Intel x86 or amd64(x64) with Nvidia GPU" },
27
{ value: "alpr-arm", label: "ARM based CPUs, Raspberry Pi or Apple M1" },
28
{ value: "alpr-jetson", label: "Nvidia Jetson (with GPU) for Jetpack 4.6 (r32)" },
29
{ value: "alpr-jetson:r35", label: "Nvidia Jetson (with GPU) for Jetpack 5.x (r35)" },
30
{ value: "alpr-zcu104", label: "ZCU" },
31
];
32
33
export default function Snapshot() {
34
const [licenseKey, setLicenseKey] = useState("");
35
const [token, setToken] = useState("");
36
const [tokenValidated, setTokenValidated] = useState(false);
37
const [isLoading, setLoading] = useState(false);
38
39
const [command, setCommand] = useState<string>("");
40
const [curlPort, setCurlPort] = useState("8080");
41
const [dockerimage, setDockerimage] = useState("");
42
const [country, setCountry] = useState("Global");
43
const [architecture, setArchitecture] = useState("alpr");
44
const [restartPolicy, setRestartPolicy] = useState("no");
45
46
const ddClient = useDockerDesktopClient();
47
48
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
49
setTokenValidated(false);
50
const { name, value } = e.target;
51
if (name === "license") {
52
setLicenseKey(value);
53
} else if (name === "token") {
54
setToken(value);
55
} else if (name === "port") {
56
setCurlPort(value);
57
} else if (name === "restart-policy") {
58
setRestartPolicy(value);
59
}
60
};
61
62
const handleArchitectureChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
63
setTokenValidated(false);
64
setArchitecture(e.target.value);
65
};
66
67
const generateDockerImage = useCallback(() => {
68
let dockerImage = "platerecognizer/";
69
if (
70
country === "Global" ||
71
architecture === "alpr-jetson:r35" ||
72
architecture === "alpr-no-avx"
73
) {
74
dockerImage += `${architecture}`;
75
} else {
76
dockerImage += `${architecture}:${country}`;
77
}
78
setDockerimage(dockerImage);
79
return dockerImage;
80
}, [country, architecture]);
81
82
const generateDockerRunCommand = useCallback(
83
(dockerImage: string) => {
84
let restartOption: string;
85
switch (restartPolicy) {
86
case "no":
87
restartOption = "";
88
break;
89
default:
90
restartOption = `--restart=${restartPolicy} `;
91
break;
92
}
93
const baseCommand = `docker run ${restartOption}-t -p ${curlPort}:8080 -v license:/license`;
94
let platformSpecificCommand = "";
95
96
switch (architecture) {
97
case "alpr-jetson":
98
case "alpr-jetson:r35":
99
platformSpecificCommand = ` --runtime nvidia -e LICENSE_KEY=${licenseKey} -e TOKEN=${token} ${dockerImage}`;
100
break;
101
case "alpr-gpu":
102
platformSpecificCommand = ` --gpus all -e LICENSE_KEY=${licenseKey} -e TOKEN=${token} ${dockerImage}`;
103
break;
104
case "alpr":
105
case "alpr-no-avx":
106
case "alpr-arm":
107
case "alpr-zcu104":
108
platformSpecificCommand = ` -e LICENSE_KEY=${licenseKey} -e TOKEN=${token} ${dockerImage}`;
109
break;
110
default:
111
break;
112
}
113
114
setCommand(`${baseCommand} ${platformSpecificCommand}`);
115
},
116
[restartPolicy, curlPort, architecture, licenseKey, token],
117
);
118
119
useEffect(() => {
120
const imagem = generateDockerImage();
121
generateDockerRunCommand(imagem);
122
}, [generateDockerImage, generateDockerRunCommand]);
123
124
// Load any existing data from local storage on component mount
125
useEffect(() => {
126
const storedData = localStorage.getItem("snapshot");
127
if (storedData) {
128
const snapshotData = JSON.parse(storedData);
129
setToken(snapshotData?.token);
130
setLicenseKey(snapshotData?.license);
131
setRestartPolicy(snapshotData?.restartPolicy);
132
setCurlPort(snapshotData?.curlPort);
133
setCountry(snapshotData?.country);
134
setArchitecture(snapshotData?.architecture);
135
}
136
}, []);
137
138
const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
139
event.preventDefault();
140
const form: HTMLFormElement = event.currentTarget;
141
const formData = new FormData(form);
142
143
const data: any = Object.fromEntries(formData.entries());
144
// console.log(data);
145
setLoading(true);
146
147
ddClient.extension.vm?.service?.post("/verify-token", data).then((res: any) => {
148
console.debug(res);
149
const valid = res.valid;
150
const message = res.message;
151
if (valid) {
152
localStorage.setItem(
153
"snapshot",
154
JSON.stringify({
155
token: token,
156
license: licenseKey,
157
restartPolicy: restartPolicy,
158
curlPort: curlPort,
159
country: country,
160
architecture: architecture,
161
}),
162
);
163
// Pull image and update
164
ddClient.docker.cli.exec("pull", [dockerimage]).then((result) => {
165
console.debug(result);
166
setTokenValidated(valid);
167
setLoading(false);
168
});
169
} else {
170
setLoading(false);
171
ddClient.desktopUI.toast.error(`Verify Token: ${message}`);
172
}
173
});
174
};
175
const handleCountryChange = (e: any) => {
176
setTokenValidated(false);
177
setCountry(e.target.value);
178
};
179
const handleLinkClick = (e: React.MouseEvent<HTMLAnchorElement>) => {
180
e.preventDefault();
181
openBrowserUrl(ddClient, e.currentTarget.href);
182
};
183
return (
184
<Form onSubmit={handleSubmit}>
185
<Form.Group as={Row} className="mb-3" controlId="snapshotToken">
186
<Form.Label column sm={4}>
187
Please enter your Plate Recognizer{" "}
188
<a
189
href="https://app.platerecognizer.com/service/snapshot-sdk/"
190
onClick={handleLinkClick}
191
>
192
API Token
193
</a>
194
:
195
</Form.Label>
196
<Col sm={8}>
197
<Form.Control
198
type="text"
199
placeholder="Token"
200
required
201
name="token"
202
value={token}
203
onChange={handleInputChange}
204
/>
205
</Col>
206
</Form.Group>
207
208
<Form.Group as={Row} className="mb-3" controlId="snapshotLicense">
209
<Form.Label column sm={4}>
210
Please enter your{" "}
211
<a
212
href="https://app.platerecognizer.com/service/snapshot-sdk/"
213
onClick={handleLinkClick}
214
>
215
Snapshot License Key
216
</a>
217
:
218
</Form.Label>
219
<Col sm={8}>
220
<Form.Control
221
type="text"
222
placeholder="License Key"
223
required
224
value={licenseKey}
225
name="license"
226
onChange={handleInputChange}
227
/>
228
</Col>
229
</Form.Group>
230
231
<Form.Group as={Row} className="mb-3">
232
<Form.Label column sm={4}>
233
Restart policy
234
</Form.Label>
235
<Col sm={8} className="mt-2 d-flex justify-content-between">
236
<Form.Check
237
type="radio"
238
name="restart-policy"
239
label="No (Docker Default)"
240
id="rps1"
241
value="no"
242
checked={restartPolicy === "no"}
243
onChange={handleInputChange}
244
/>
245
<Form.Check
246
type="radio"
247
name="restart-policy"
248
label="Unless Stopped"
249
id="rps2"
250
value="unless-stopped"
251
checked={restartPolicy === "unless-stopped"}
252
onChange={handleInputChange}
253
/>
254
<Form.Check
255
type="radio"
256
name="restart-policy"
257
label="Always"
258
id="rps3"
259
value="always"
260
checked={restartPolicy === "always"}
261
onChange={handleInputChange}
262
/>
263
<Form.Check
264
type="radio"
265
name="restart-policy"
266
label="On Failure"
267
id="rps4"
268
value="on-failure"
269
checked={restartPolicy === "on-failure"}
270
onChange={handleInputChange}
271
/>
272
</Col>
273
</Form.Group>
274
275
<Form.Group as={Row} className="mb-3" controlId="snapshotPort">
276
<Form.Label column sm={4}>
277
Set the container port (default is 8080):
278
</Form.Label>
279
<Col sm={8}>
280
<Form.Control
281
type="number"
282
min="0"
283
max="65535"
284
placeholder="Port"
285
name="port"
286
required
287
value={curlPort}
288
onChange={handleInputChange}
289
/>
290
</Col>
291
</Form.Group>
292
293
<Form.Group as={Row} className="mb-3" controlId="snapshotDockerImage">
294
<Form.Label column sm={4}>
295
Docker image to use:
296
</Form.Label>
297
<Col sm={3}>
298
<Form.Select
299
aria-label="Snapshot Docker Image Country"
300
onChange={handleCountryChange}
301
name="country"
302
defaultValue={country}
303
disabled={
304
architecture === "alpr-jetson:r35" || architecture === "alpr-no-avx"
305
}
306
>
307
{countryOptions.map(({ value, label }) => (
308
<option key={label} value={value}>
309
{label}
310
</option>
311
))}
312
</Form.Select>
313
</Col>
314
<Col sm={5}>
315
<Form.Select
316
aria-label="Snapshot Docker Image"
317
onChange={handleArchitectureChange}
318
name="image"
319
defaultValue={architecture}
320
>
321
{architectureOptionsSnapshot.map(({ value, label }) => (
322
<option key={value} value={value}>
323
{label}
324
</option>
325
))}
326
</Form.Select>
327
</Col>
328
</Form.Group>
329
330
<ShowCommand curlPort={curlPort} command={command} validated={tokenValidated} />
331
332
<Form.Group as={Row} className="mb-3">
333
<div className="col-2">
334
<Button className="btn btn-primary" type="submit" id="validateSnapshotBtn">
335
<Loader isLoading={isLoading} />
336
Show Docker Command
337
</Button>
338
</div>
339
<label
340
className="col-auto align-self-center form-label"
341
htmlFor="validateSnapshotBtn"
342
>
343
Confirm settings and show docker command.
344
</label>
345
</Form.Group>
346
347
<Update isEnabled={true} image={dockerimage} />
348
<Uninstall isEnabled={true} image={dockerimage} />
349
</Form>
350
);
351
}
352
353