@7365admin1/layer-common 4.21.4-staging.347 → 4.21.4-staging.349
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/components/Robot/Fleet.vue +360 -0
- package/components/Robot/Form.vue +170 -0
- package/components/Robot/Map3D.vue +382 -0
- package/composables/useRobots.ts +125 -0
- package/package.json +2 -2
- package/utils/building-map.ts +366 -0
- package/utils/robot-display.ts +123 -0
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<PageHeader :title="title">
|
|
3
|
+
<template #actions>
|
|
4
|
+
<AppButton v-if="canAdd" @click="dialog = true">Add Robot</AppButton>
|
|
5
|
+
</template>
|
|
6
|
+
</PageHeader>
|
|
7
|
+
|
|
8
|
+
<v-row no-gutters>
|
|
9
|
+
<v-col cols="12">
|
|
10
|
+
<AppCard class="table-card">
|
|
11
|
+
<CardToolbar :count="pageRange">
|
|
12
|
+
<template #left>
|
|
13
|
+
<AppButton
|
|
14
|
+
variant="icon"
|
|
15
|
+
icon="mdi-refresh"
|
|
16
|
+
aria-label="Refresh"
|
|
17
|
+
:loading="loading"
|
|
18
|
+
@click="load()"
|
|
19
|
+
/>
|
|
20
|
+
<AppField
|
|
21
|
+
v-model="search"
|
|
22
|
+
search
|
|
23
|
+
compact
|
|
24
|
+
placeholder="Search robots"
|
|
25
|
+
klass="app-toolbar__search"
|
|
26
|
+
@update:model-value="onSearch"
|
|
27
|
+
/>
|
|
28
|
+
</template>
|
|
29
|
+
<template #right>
|
|
30
|
+
<LocalPagination v-model="page" :length="pages" @update:model-value="load()" />
|
|
31
|
+
</template>
|
|
32
|
+
</CardToolbar>
|
|
33
|
+
|
|
34
|
+
<!--
|
|
35
|
+
The empty state distinguishes "nothing registered yet" from "we could
|
|
36
|
+
not load" - the two look identical if you only render an empty table,
|
|
37
|
+
and they need opposite actions from the person looking at the screen.
|
|
38
|
+
-->
|
|
39
|
+
<v-alert
|
|
40
|
+
v-if="error"
|
|
41
|
+
type="error"
|
|
42
|
+
variant="tonal"
|
|
43
|
+
class="ma-4"
|
|
44
|
+
:text="error"
|
|
45
|
+
/>
|
|
46
|
+
|
|
47
|
+
<div v-else-if="!loading && !items.length" class="robot-empty">
|
|
48
|
+
<v-icon size="56" class="mb-3">mdi-robot-outline</v-icon>
|
|
49
|
+
<p class="text-subtitle-1 font-weight-medium mb-1">No robots yet</p>
|
|
50
|
+
<p class="text-body-2 text-medium-emphasis mb-4">
|
|
51
|
+
{{
|
|
52
|
+
canAdd
|
|
53
|
+
? "Add a robot to see it here and send it to clean."
|
|
54
|
+
: "No robots have been added to this site yet."
|
|
55
|
+
}}
|
|
56
|
+
</p>
|
|
57
|
+
<AppButton v-if="canAdd" @click="dialog = true">Add Robot</AppButton>
|
|
58
|
+
</div>
|
|
59
|
+
|
|
60
|
+
<div v-else class="app-table-scroll">
|
|
61
|
+
<v-data-table
|
|
62
|
+
:loading="loading"
|
|
63
|
+
:headers="headers"
|
|
64
|
+
:items="items"
|
|
65
|
+
:items-per-page="limit"
|
|
66
|
+
fixed-header
|
|
67
|
+
hide-default-footer
|
|
68
|
+
style="max-height: calc(100vh - 200px)"
|
|
69
|
+
>
|
|
70
|
+
<template #item.name="{ item }">
|
|
71
|
+
<div class="d-flex align-center">
|
|
72
|
+
<v-icon size="20" color="primary" class="mr-3">mdi-robot-outline</v-icon>
|
|
73
|
+
<div>
|
|
74
|
+
<div class="font-weight-medium">{{ item.name }}</div>
|
|
75
|
+
<div class="text-caption text-medium-emphasis">
|
|
76
|
+
{{ item.robotSn || "Serial not yet known" }}
|
|
77
|
+
</div>
|
|
78
|
+
</div>
|
|
79
|
+
</div>
|
|
80
|
+
</template>
|
|
81
|
+
|
|
82
|
+
<template #item.state="{ item }">
|
|
83
|
+
<!--
|
|
84
|
+
Live state is fetched per robot AFTER the list renders. The list
|
|
85
|
+
itself comes from our database and is fast; each state call
|
|
86
|
+
crosses to the vendor's cloud. Blocking the table on the slowest
|
|
87
|
+
robot would make the whole screen feel broken.
|
|
88
|
+
-->
|
|
89
|
+
<v-chip v-if="!states[item._id]" size="small" label variant="tonal">
|
|
90
|
+
<v-progress-circular indeterminate size="12" width="2" class="mr-2" />
|
|
91
|
+
Checking
|
|
92
|
+
</v-chip>
|
|
93
|
+
<v-chip
|
|
94
|
+
v-else
|
|
95
|
+
size="small"
|
|
96
|
+
label
|
|
97
|
+
:color="display(item).color"
|
|
98
|
+
variant="tonal"
|
|
99
|
+
>
|
|
100
|
+
<v-icon start size="16">{{ display(item).icon }}</v-icon>
|
|
101
|
+
{{ display(item).label }}
|
|
102
|
+
</v-chip>
|
|
103
|
+
</template>
|
|
104
|
+
|
|
105
|
+
<template #item.floor="{ item }">
|
|
106
|
+
<span class="text-body-2 text-medium-emphasis">
|
|
107
|
+
{{ floorLabel(states[item._id]) }}
|
|
108
|
+
</span>
|
|
109
|
+
</template>
|
|
110
|
+
|
|
111
|
+
<template #item.actions="{ item }">
|
|
112
|
+
<div class="d-flex justify-end ga-2">
|
|
113
|
+
<AppButton
|
|
114
|
+
v-if="canCommand"
|
|
115
|
+
size="small"
|
|
116
|
+
variant="outlined"
|
|
117
|
+
:disabled="!canDispatch(states[item._id]) || busy === item._id"
|
|
118
|
+
:loading="busy === item._id && lastCommand === 'full'"
|
|
119
|
+
@click="send(item, { kind: 'full' }, 'full')"
|
|
120
|
+
>
|
|
121
|
+
Clean
|
|
122
|
+
</AppButton>
|
|
123
|
+
<AppButton
|
|
124
|
+
v-if="canCommand"
|
|
125
|
+
size="small"
|
|
126
|
+
variant="text"
|
|
127
|
+
:disabled="!canRecall(states[item._id]) || busy === item._id"
|
|
128
|
+
:loading="busy === item._id && lastCommand === 'return'"
|
|
129
|
+
@click="send(item, { kind: 'return' }, 'return')"
|
|
130
|
+
>
|
|
131
|
+
Return to dock
|
|
132
|
+
</AppButton>
|
|
133
|
+
<AppButton size="small" variant="text" @click="openMap(item)">
|
|
134
|
+
View layout
|
|
135
|
+
</AppButton>
|
|
136
|
+
</div>
|
|
137
|
+
</template>
|
|
138
|
+
|
|
139
|
+
<template #bottom />
|
|
140
|
+
</v-data-table>
|
|
141
|
+
</div>
|
|
142
|
+
</AppCard>
|
|
143
|
+
</v-col>
|
|
144
|
+
</v-row>
|
|
145
|
+
|
|
146
|
+
<Snackbar v-model="toast" :text="toastText" :color="toastColor" />
|
|
147
|
+
|
|
148
|
+
<!--
|
|
149
|
+
The building in 3D, generated from this robot's own floor plan. Per robot
|
|
150
|
+
rather than per site because the maps come from the robot - it is the thing
|
|
151
|
+
that surveyed the building.
|
|
152
|
+
-->
|
|
153
|
+
<v-dialog v-model="mapDialog" width="960" scrollable>
|
|
154
|
+
<AppCard class="pa-0">
|
|
155
|
+
<v-toolbar density="comfortable" class="pl-4">
|
|
156
|
+
<span class="font-weight-medium text-h6">{{ mapRobot?.name }} — building layout</span>
|
|
157
|
+
<v-spacer />
|
|
158
|
+
<AppButton variant="icon" icon="mdi-close" aria-label="Close" @click="mapDialog = false" />
|
|
159
|
+
</v-toolbar>
|
|
160
|
+
<RobotMap3D
|
|
161
|
+
v-if="mapDialog && mapRobot"
|
|
162
|
+
:key="mapRobot._id"
|
|
163
|
+
:robot-id="mapRobot._id"
|
|
164
|
+
:robot-state="states[mapRobot._id] ?? null"
|
|
165
|
+
/>
|
|
166
|
+
</AppCard>
|
|
167
|
+
</v-dialog>
|
|
168
|
+
|
|
169
|
+
<v-dialog v-model="dialog" width="460" persistent>
|
|
170
|
+
<RobotForm
|
|
171
|
+
:organization="organization"
|
|
172
|
+
:site="site"
|
|
173
|
+
:type="robotType"
|
|
174
|
+
@cancel="dialog = false"
|
|
175
|
+
@created="onCreated"
|
|
176
|
+
/>
|
|
177
|
+
</v-dialog>
|
|
178
|
+
</template>
|
|
179
|
+
|
|
180
|
+
<script lang="ts" setup>
|
|
181
|
+
/**
|
|
182
|
+
* The fleet screen, shared by every application that shows robots.
|
|
183
|
+
*
|
|
184
|
+
* One component rather than a copy per app, so Property Management, Hygiene,
|
|
185
|
+
* the Seven365 console and the two mobile apps cannot drift into describing the
|
|
186
|
+
* same robot four different ways.
|
|
187
|
+
*
|
|
188
|
+
* What each app changes is only what it passes in: which robot `type` it
|
|
189
|
+
* presents, and which actions its user holds a permission for. The API narrows
|
|
190
|
+
* the result again on its own side, so a cleaning app asking for security robots
|
|
191
|
+
* gets nothing back rather than being trusted.
|
|
192
|
+
*/
|
|
193
|
+
import {
|
|
194
|
+
canDispatch,
|
|
195
|
+
canRecall,
|
|
196
|
+
floorLabel,
|
|
197
|
+
robotDisplay,
|
|
198
|
+
robotErrorMessage,
|
|
199
|
+
} from "../../utils/robot-display";
|
|
200
|
+
|
|
201
|
+
const props = withDefaults(
|
|
202
|
+
defineProps<{
|
|
203
|
+
organization: string;
|
|
204
|
+
site: string;
|
|
205
|
+
/** Which class of robot this application presents. */
|
|
206
|
+
robotType?: "cleaning" | "security";
|
|
207
|
+
title?: string;
|
|
208
|
+
canAdd?: boolean;
|
|
209
|
+
/** Whether this user may dispatch. Reads and writes are separate grants. */
|
|
210
|
+
canCommand?: boolean;
|
|
211
|
+
}>(),
|
|
212
|
+
{ robotType: "cleaning", title: "Robots", canAdd: false, canCommand: false },
|
|
213
|
+
);
|
|
214
|
+
|
|
215
|
+
const { getRobots, getRobotState, sendRobotCommand } = useRobots();
|
|
216
|
+
|
|
217
|
+
// The house pattern is a local Snackbar with local state, as
|
|
218
|
+
// `AccessCardPreviewDialog.vue` does - there is no global toast composable.
|
|
219
|
+
const toast = ref(false);
|
|
220
|
+
const toastText = ref("");
|
|
221
|
+
const toastColor = ref<"success" | "error">("success");
|
|
222
|
+
|
|
223
|
+
function showToast(message: string, color: "success" | "error") {
|
|
224
|
+
toastText.value = message;
|
|
225
|
+
toastColor.value = color;
|
|
226
|
+
toast.value = true;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const items = ref<Array<Record<string, any>>>([]);
|
|
230
|
+
const states = ref<Record<string, any>>({});
|
|
231
|
+
const loading = ref(false);
|
|
232
|
+
const error = ref("");
|
|
233
|
+
const search = ref("");
|
|
234
|
+
const page = ref(1);
|
|
235
|
+
const pages = ref(1);
|
|
236
|
+
const limit = 10;
|
|
237
|
+
const dialog = ref(false);
|
|
238
|
+
const mapDialog = ref(false);
|
|
239
|
+
const mapRobot = ref<Record<string, any> | null>(null);
|
|
240
|
+
|
|
241
|
+
function openMap(robot: Record<string, any>) {
|
|
242
|
+
mapRobot.value = robot;
|
|
243
|
+
mapDialog.value = true;
|
|
244
|
+
}
|
|
245
|
+
const busy = ref("");
|
|
246
|
+
const lastCommand = ref("");
|
|
247
|
+
|
|
248
|
+
const headers = [
|
|
249
|
+
{ title: "Robot", value: "name", sortable: false },
|
|
250
|
+
{ title: "Status", value: "state", sortable: false },
|
|
251
|
+
{ title: "Floor", value: "floor", sortable: false },
|
|
252
|
+
{ title: "", value: "actions", sortable: false, align: "end" as const },
|
|
253
|
+
];
|
|
254
|
+
|
|
255
|
+
const pageRange = computed(() => {
|
|
256
|
+
if (!items.value.length) return "0 of 0";
|
|
257
|
+
const from = (page.value - 1) * limit + 1;
|
|
258
|
+
return `${from} - ${from + items.value.length - 1}`;
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
const display = (item: Record<string, any>) =>
|
|
262
|
+
robotDisplay(states.value[item._id]?.mode);
|
|
263
|
+
|
|
264
|
+
async function load() {
|
|
265
|
+
loading.value = true;
|
|
266
|
+
error.value = "";
|
|
267
|
+
try {
|
|
268
|
+
const res = await getRobots({
|
|
269
|
+
organization: props.organization,
|
|
270
|
+
site: props.site,
|
|
271
|
+
search: search.value,
|
|
272
|
+
page: page.value,
|
|
273
|
+
limit,
|
|
274
|
+
type: props.robotType,
|
|
275
|
+
});
|
|
276
|
+
items.value = res?.items ?? [];
|
|
277
|
+
pages.value = Math.max(1, Number(res?.totalPages ?? 1));
|
|
278
|
+
// Deliberately not awaited: the table renders from our own database
|
|
279
|
+
// immediately, and each robot's chip fills in as its cloud call returns.
|
|
280
|
+
loadStates();
|
|
281
|
+
} catch (e) {
|
|
282
|
+
error.value = robotErrorMessage(e, "Could not load the robots for this site.");
|
|
283
|
+
items.value = [];
|
|
284
|
+
} finally {
|
|
285
|
+
loading.value = false;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* One state call per robot, each failing on its own.
|
|
291
|
+
*
|
|
292
|
+
* `allSettled`, not `all`: one robot that is off must not blank the status of
|
|
293
|
+
* every other robot on the site.
|
|
294
|
+
*/
|
|
295
|
+
async function loadStates() {
|
|
296
|
+
states.value = {};
|
|
297
|
+
await Promise.allSettled(
|
|
298
|
+
items.value.map(async (robot) => {
|
|
299
|
+
try {
|
|
300
|
+
states.value = { ...states.value, [robot._id]: await getRobotState(robot._id) };
|
|
301
|
+
} catch {
|
|
302
|
+
states.value = {
|
|
303
|
+
...states.value,
|
|
304
|
+
[robot._id]: { mode: "UNAVAILABLE", online: false, dispatchable: false },
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
}),
|
|
308
|
+
);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
async function send(robot: Record<string, any>, command: any, kind: string) {
|
|
312
|
+
busy.value = robot._id;
|
|
313
|
+
lastCommand.value = kind;
|
|
314
|
+
try {
|
|
315
|
+
await sendRobotCommand(robot._id, command);
|
|
316
|
+
showToast(
|
|
317
|
+
kind === "return"
|
|
318
|
+
? `${robot.name} is returning to its dock.`
|
|
319
|
+
: `${robot.name} has been sent to clean.`,
|
|
320
|
+
"success",
|
|
321
|
+
);
|
|
322
|
+
// Re-read rather than assume: the API returns no job id, so the robot's own
|
|
323
|
+
// state is the only truthful confirmation that anything happened.
|
|
324
|
+
states.value = { ...states.value, [robot._id]: await getRobotState(robot._id) };
|
|
325
|
+
} catch (e) {
|
|
326
|
+
showToast(robotErrorMessage(e), "error");
|
|
327
|
+
} finally {
|
|
328
|
+
busy.value = "";
|
|
329
|
+
lastCommand.value = "";
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
let searchTimer: ReturnType<typeof setTimeout> | undefined;
|
|
334
|
+
function onSearch() {
|
|
335
|
+
clearTimeout(searchTimer);
|
|
336
|
+
searchTimer = setTimeout(() => {
|
|
337
|
+
page.value = 1;
|
|
338
|
+
load();
|
|
339
|
+
}, 350);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
function onCreated() {
|
|
343
|
+
dialog.value = false;
|
|
344
|
+
page.value = 1;
|
|
345
|
+
load();
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
onMounted(load);
|
|
349
|
+
</script>
|
|
350
|
+
|
|
351
|
+
<style scoped>
|
|
352
|
+
.robot-empty {
|
|
353
|
+
display: flex;
|
|
354
|
+
flex-direction: column;
|
|
355
|
+
align-items: center;
|
|
356
|
+
justify-content: center;
|
|
357
|
+
text-align: center;
|
|
358
|
+
padding: 56px 16px;
|
|
359
|
+
}
|
|
360
|
+
</style>
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<v-card width="100%">
|
|
3
|
+
<v-toolbar density="comfortable" class="pl-4 py-2">
|
|
4
|
+
<span class="font-weight-medium text-h6">Add {{ typeLabel }} robot</span>
|
|
5
|
+
</v-toolbar>
|
|
6
|
+
|
|
7
|
+
<v-card-text class="pa-0" style="max-height: 70vh; overflow-y: auto">
|
|
8
|
+
<v-form ref="form" @submit.prevent="submit">
|
|
9
|
+
<v-row no-gutters class="px-6 pt-4">
|
|
10
|
+
<v-col cols="12" class="mb-2">
|
|
11
|
+
<p class="text-body-2 text-medium-emphasis">
|
|
12
|
+
The app key and token come from your robot supplier. They are used
|
|
13
|
+
to talk to the robot and are never shown again once saved.
|
|
14
|
+
</p>
|
|
15
|
+
</v-col>
|
|
16
|
+
|
|
17
|
+
<v-col cols="12">
|
|
18
|
+
<InputLabel class="font-weight-bold" title="Name" required />
|
|
19
|
+
<v-text-field
|
|
20
|
+
v-model="name"
|
|
21
|
+
density="comfortable"
|
|
22
|
+
placeholder="e.g. Cleaner 02"
|
|
23
|
+
:rules="[requiredRule]"
|
|
24
|
+
:disabled="saving"
|
|
25
|
+
/>
|
|
26
|
+
</v-col>
|
|
27
|
+
|
|
28
|
+
<v-col cols="12">
|
|
29
|
+
<InputLabel class="font-weight-bold" title="App key" required />
|
|
30
|
+
<v-text-field
|
|
31
|
+
v-model="appKey"
|
|
32
|
+
density="comfortable"
|
|
33
|
+
autocomplete="off"
|
|
34
|
+
:rules="[requiredRule]"
|
|
35
|
+
:disabled="saving"
|
|
36
|
+
/>
|
|
37
|
+
</v-col>
|
|
38
|
+
|
|
39
|
+
<v-col cols="12">
|
|
40
|
+
<InputLabel class="font-weight-bold" title="App token" required />
|
|
41
|
+
<!--
|
|
42
|
+
Masked like a password because that is what it is: this token signs
|
|
43
|
+
every command sent to the estate's robots. It is never returned by
|
|
44
|
+
any read, so it cannot be recovered from the screen later either.
|
|
45
|
+
-->
|
|
46
|
+
<v-text-field
|
|
47
|
+
v-model="appToken"
|
|
48
|
+
density="comfortable"
|
|
49
|
+
autocomplete="off"
|
|
50
|
+
:type="reveal ? 'text' : 'password'"
|
|
51
|
+
:append-inner-icon="reveal ? 'mdi-eye-off-outline' : 'mdi-eye-outline'"
|
|
52
|
+
:rules="[requiredRule]"
|
|
53
|
+
:disabled="saving"
|
|
54
|
+
@click:append-inner="reveal = !reveal"
|
|
55
|
+
/>
|
|
56
|
+
</v-col>
|
|
57
|
+
|
|
58
|
+
<v-col cols="12">
|
|
59
|
+
<InputLabel class="font-weight-bold" title="Serial number" />
|
|
60
|
+
<v-text-field
|
|
61
|
+
v-model="robotSn"
|
|
62
|
+
density="comfortable"
|
|
63
|
+
placeholder="Optional — found automatically if left blank"
|
|
64
|
+
:disabled="saving"
|
|
65
|
+
/>
|
|
66
|
+
<p class="text-caption text-medium-emphasis mt-n3 mb-2">
|
|
67
|
+
Leave this blank unless the same key covers more than one robot.
|
|
68
|
+
</p>
|
|
69
|
+
</v-col>
|
|
70
|
+
|
|
71
|
+
<v-col cols="12" v-if="error">
|
|
72
|
+
<v-alert type="error" variant="tonal" class="mb-3" :text="error" />
|
|
73
|
+
</v-col>
|
|
74
|
+
</v-row>
|
|
75
|
+
</v-form>
|
|
76
|
+
</v-card-text>
|
|
77
|
+
|
|
78
|
+
<v-toolbar density="compact">
|
|
79
|
+
<v-row no-gutters>
|
|
80
|
+
<v-col cols="6">
|
|
81
|
+
<v-btn
|
|
82
|
+
tile block variant="flat" size="48"
|
|
83
|
+
class="text-none" color="#EFEFEF"
|
|
84
|
+
:disabled="saving"
|
|
85
|
+
@click="$emit('cancel')"
|
|
86
|
+
>
|
|
87
|
+
Close
|
|
88
|
+
</v-btn>
|
|
89
|
+
</v-col>
|
|
90
|
+
<v-col cols="6">
|
|
91
|
+
<v-btn
|
|
92
|
+
tile block variant="flat" size="48"
|
|
93
|
+
class="text-none" color="primary"
|
|
94
|
+
:loading="saving"
|
|
95
|
+
@click="submit"
|
|
96
|
+
>
|
|
97
|
+
Add robot
|
|
98
|
+
</v-btn>
|
|
99
|
+
</v-col>
|
|
100
|
+
</v-row>
|
|
101
|
+
</v-toolbar>
|
|
102
|
+
</v-card>
|
|
103
|
+
</template>
|
|
104
|
+
|
|
105
|
+
<script lang="ts" setup>
|
|
106
|
+
/**
|
|
107
|
+
* Register a robot against a site.
|
|
108
|
+
*
|
|
109
|
+
* The credential pair travels up once and is never returned by any read, so
|
|
110
|
+
* there is deliberately no edit-credentials path here: a wrong key is fixed by
|
|
111
|
+
* removing the robot and adding it again, which is honest about the fact that
|
|
112
|
+
* the old value is gone.
|
|
113
|
+
*
|
|
114
|
+
* The serial is optional because the supplier's Add-Robot flow has never asked
|
|
115
|
+
* for one. When it is blank the server finds it from the robot cloud — and
|
|
116
|
+
* refuses rather than guessing if the key covers more than one machine, which is
|
|
117
|
+
* exactly when the person filling this in should type it themselves.
|
|
118
|
+
*/
|
|
119
|
+
import { robotErrorMessage } from "../../utils/robot-display";
|
|
120
|
+
|
|
121
|
+
const props = withDefaults(
|
|
122
|
+
defineProps<{
|
|
123
|
+
organization: string;
|
|
124
|
+
site: string;
|
|
125
|
+
type?: "cleaning" | "security";
|
|
126
|
+
}>(),
|
|
127
|
+
{ type: "cleaning" },
|
|
128
|
+
);
|
|
129
|
+
|
|
130
|
+
const emit = defineEmits<{ cancel: []; created: [] }>();
|
|
131
|
+
|
|
132
|
+
const { createRobot } = useRobots();
|
|
133
|
+
const { requiredRule } = useUtils();
|
|
134
|
+
|
|
135
|
+
const form = ref();
|
|
136
|
+
const name = ref("");
|
|
137
|
+
const appKey = ref("");
|
|
138
|
+
const appToken = ref("");
|
|
139
|
+
const robotSn = ref("");
|
|
140
|
+
const reveal = ref(false);
|
|
141
|
+
const saving = ref(false);
|
|
142
|
+
const error = ref("");
|
|
143
|
+
|
|
144
|
+
const typeLabel = computed(() => (props.type === "security" ? "security" : "cleaning"));
|
|
145
|
+
|
|
146
|
+
async function submit() {
|
|
147
|
+
const { valid } = await form.value?.validate?.() ?? { valid: false };
|
|
148
|
+
if (!valid) return;
|
|
149
|
+
|
|
150
|
+
saving.value = true;
|
|
151
|
+
error.value = "";
|
|
152
|
+
try {
|
|
153
|
+
await createRobot({
|
|
154
|
+
name: name.value.trim(),
|
|
155
|
+
appKey: appKey.value.trim(),
|
|
156
|
+
appToken: appToken.value.trim(),
|
|
157
|
+
type: props.type,
|
|
158
|
+
// Sent only when the person typed one; an empty string would otherwise be
|
|
159
|
+
// stored and stop the server ever discovering the real serial.
|
|
160
|
+
...(robotSn.value.trim() ? { robotSn: robotSn.value.trim() } : {}),
|
|
161
|
+
metadata: { orgId: props.organization, siteId: props.site },
|
|
162
|
+
});
|
|
163
|
+
emit("created");
|
|
164
|
+
} catch (e) {
|
|
165
|
+
error.value = robotErrorMessage(e, "Could not add the robot.");
|
|
166
|
+
} finally {
|
|
167
|
+
saving.value = false;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
</script>
|