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/Stream.tsx
1091 views
1
import type React from "react";
2
import { 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
export default function Stream() {
13
const STREAM_IMAGE = "platerecognizer/alpr-stream";
14
const [command, setCommand] = useState<string>("");
15
const [license, setLicense] = useState<string>("");
16
const [token, setToken] = useState<string>("");
17
const [streamPath, setStreamPath] = useState<string>("");
18
const [tokenValidated, setTokenValidated] = useState(false);
19
const [isLoading, setLoading] = useState(false);
20
const [restartPolicy, setRestartPolicy] = useState("no");
21
22
const ddClient = useDockerDesktopClient();
23
24
const handleLinkClick = (e: React.MouseEvent<HTMLAnchorElement>) => {
25
e.preventDefault();
26
openBrowserUrl(ddClient, e.currentTarget.href);
27
};
28
29
const handleConfigureClick = () => {
30
if (license) {
31
const url = `https://app.platerecognizer.com/stream-config/${license}`;
32
openBrowserUrl(ddClient, url);
33
} else {
34
ddClient.desktopUI.toast.error("License Key is required");
35
}
36
};
37
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
38
const { name, value } = e.target;
39
if (name === "license") {
40
setLicense(value);
41
} else if (name === "restart-policy") {
42
setRestartPolicy(value);
43
} else if (name === "token") {
44
setToken(value);
45
} else if (name === "streamPath") {
46
setStreamPath(value);
47
}
48
setTokenValidated(false);
49
};
50
51
const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
52
event.preventDefault();
53
const form: HTMLFormElement = event.currentTarget;
54
const formData = new FormData(form);
55
56
const data: any = Object.fromEntries(formData.entries());
57
// console.log(data);
58
setLoading(true);
59
ddClient.extension.vm?.service?.post("/verify-token", data).then((res: any) => {
60
console.debug(res);
61
const valid = res.valid;
62
const message = res.message;
63
if (valid) {
64
localStorage.setItem(
65
"stream",
66
JSON.stringify({
67
token: token,
68
streamPath: streamPath,
69
restartPolicy: restartPolicy,
70
license: license,
71
}),
72
);
73
// Pull image and update
74
ddClient.docker.cli.exec("pull", [STREAM_IMAGE]).then((result) => {
75
console.debug(result);
76
const autoBoot =
77
restartPolicy !== "no" ? ` --restart ${restartPolicy}` : "--rm";
78
const command = `docker run ${autoBoot} -t -v ${data.streamPath}:/user-data/ -e LICENSE_KEY=${data.license} -e TOKEN=${data.token} ${STREAM_IMAGE}`;
79
setCommand(command);
80
setTokenValidated(valid);
81
setLoading(false);
82
});
83
} else {
84
setLoading(false);
85
ddClient.desktopUI.toast.error(`Verify Token: ${message}`);
86
}
87
});
88
};
89
90
// Load any existing data from local storage on component mount
91
useEffect(() => {
92
const storedData = localStorage.getItem("stream");
93
if (storedData) {
94
const streamData = JSON.parse(storedData);
95
setToken(streamData?.token);
96
setStreamPath(streamData?.streamPath);
97
setRestartPolicy(streamData?.restartPolicy);
98
setLicense(streamData?.license);
99
}
100
}, []);
101
102
return (
103
<Form onSubmit={handleSubmit}>
104
<Form.Group as={Row} className="mb-3" controlId="streamToken">
105
<Form.Label column sm={4}>
106
Please enter your Plate Recognizer{" "}
107
<a
108
href="https://app.platerecognizer.com/service/stream/"
109
onClick={handleLinkClick}
110
>
111
API Token
112
</a>
113
:
114
</Form.Label>
115
<Col sm={8}>
116
<Form.Control
117
type="text"
118
placeholder="Token"
119
required
120
name="token"
121
value={token}
122
onChange={handleInputChange}
123
/>
124
</Col>
125
</Form.Group>
126
127
<Form.Group as={Row} className="mb-3" controlId="streamLicense">
128
<Form.Label column sm={4}>
129
Please enter your{" "}
130
<a
131
href="https://app.platerecognizer.com/service/stream/"
132
onClick={handleLinkClick}
133
>
134
Stream License Key
135
</a>
136
:
137
</Form.Label>
138
<Col sm={8}>
139
<Form.Control
140
type="text"
141
placeholder="License Key"
142
required
143
name="license"
144
value={license}
145
onChange={handleInputChange}
146
/>
147
</Col>
148
</Form.Group>
149
150
<Form.Group as={Row} className="mb-3" controlId="streamPath">
151
<Form.Label column sm={4}>
152
Path to your Stream installation directory:
153
</Form.Label>
154
<Col sm={8}>
155
<Form.Control
156
type="text"
157
placeholder="Path to directory"
158
required
159
name="streamPath"
160
value={streamPath}
161
onChange={handleInputChange}
162
/>
163
</Col>
164
</Form.Group>
165
166
<Form.Group as={Row} className="mb-3">
167
<Form.Label column sm={4}>
168
Restart policy
169
</Form.Label>
170
<Col sm={8} className="mt-2 d-flex justify-content-between">
171
<Form.Check
172
type="radio"
173
name="restart-policy"
174
label="No (Docker Default)"
175
id="rp1"
176
value="no"
177
checked={restartPolicy === "no"}
178
onChange={handleInputChange}
179
/>
180
<Form.Check
181
type="radio"
182
name="restart-policy"
183
label="Unless Stopped"
184
id="rp2"
185
value="unless-stopped"
186
checked={restartPolicy === "unless-stopped"}
187
onChange={handleInputChange}
188
/>
189
<Form.Check
190
type="radio"
191
name="restart-policy"
192
label="Always"
193
id="rp3"
194
value="always"
195
checked={restartPolicy === "always"}
196
onChange={handleInputChange}
197
/>
198
<Form.Check
199
type="radio"
200
name="restart-policy"
201
label="On Failure"
202
id="rp4"
203
value="on-failure"
204
checked={restartPolicy === "on-failure"}
205
onChange={handleInputChange}
206
/>
207
</Col>
208
</Form.Group>
209
210
<ShowCommand curlPort={""} command={command} validated={tokenValidated} />
211
212
<Form.Group as={Row} className="mb-3">
213
<div className="col-2">
214
<Button
215
onClick={handleConfigureClick}
216
className="btn btn-success"
217
type="button"
218
>
219
Configure
220
</Button>
221
</div>
222
<label
223
className="col-auto align-self-center form-label"
224
htmlFor="button-submit-snapshot"
225
>
226
Edit configurations for your Stream license
227
</label>
228
</Form.Group>
229
230
<Form.Group as={Row} className="mb-3">
231
<div className="col-2">
232
<Button
233
className="btn btn-primary"
234
type="submit"
235
id="button-submit-docker-command"
236
>
237
<Loader isLoading={isLoading} />
238
Show Docker Command
239
</Button>
240
</div>
241
<label
242
className="col-auto align-self-center form-label"
243
htmlFor="button-submit-docker-command"
244
>
245
Confirm settings and show docker command.
246
</label>
247
</Form.Group>
248
249
<Update isEnabled={true} image={STREAM_IMAGE} />
250
<Uninstall isEnabled={true} image={STREAM_IMAGE} />
251
</Form>
252
);
253
}
254
255