@7365admin1/layer-common 4.23.0 → 4.25.0
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/CHANGELOG.md +47 -4
- package/components/Robot/Fleet.vue +208 -5
- package/components/Robot/Form.vue +62 -1
- package/components/Robot/Map3D.vue +382 -0
- package/composables/useRobots.ts +88 -0
- package/package.json +2 -2
- package/utils/building-map.ts +366 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,48 @@
|
|
|
1
1
|
# @iservice365/layer-common
|
|
2
2
|
|
|
3
|
+
## 4.25.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- a227cdf: Robot screens: brand picker, live positions, and cleaning by area
|
|
8
|
+
|
|
9
|
+
The Add Robot form now asks which brand a robot is, offering each with what its
|
|
10
|
+
protocol actually allows in plain words. There is no safe default — sending a
|
|
11
|
+
robot a command in another brand's protocol is the one mistake the adapter layer
|
|
12
|
+
exists to prevent — so a brand is preselected only when there is no choice.
|
|
13
|
+
|
|
14
|
+
The fleet screen follows each robot's position while it is open, via the
|
|
15
|
+
`/track` stream, and stops every stream on unmount: the server holds one
|
|
16
|
+
connection to the robot cloud for each open one.
|
|
17
|
+
|
|
18
|
+
Losing a stream marks a robot `UNAVAILABLE` rather than leaving its last known
|
|
19
|
+
mode, which would advertise it as ready for work forever.
|
|
20
|
+
|
|
21
|
+
Adds "Clean area", so zones can be chosen from the robot's own maps rather than
|
|
22
|
+
cleaning everything. Battery is shown only when the brand reports one — a brand
|
|
23
|
+
that cannot measure it must not render as 0%, which reads as flat.
|
|
24
|
+
|
|
25
|
+
## 4.24.0
|
|
26
|
+
|
|
27
|
+
### Minor Changes
|
|
28
|
+
|
|
29
|
+
- 96cc47d: The building in 3D, generated from the robot's own floor plan
|
|
30
|
+
|
|
31
|
+
Adds `RobotMap3D` and `utils/building-map.ts`. The robot's map image is a SLAM
|
|
32
|
+
occupancy grid — walls are already dark pixels — so it is fetched into a canvas,
|
|
33
|
+
thresholded, traced into wall segments and extruded. Nobody draws walls.
|
|
34
|
+
|
|
35
|
+
Reachable from the fleet list's "View layout" action, so every application that
|
|
36
|
+
shows robots gets it without a change of its own.
|
|
37
|
+
|
|
38
|
+
No 3D engine: the scene is extruded boxes on flat floors, which a parallel
|
|
39
|
+
projection and a back-to-front draw order render correctly — and this layer is
|
|
40
|
+
consumed by eleven web apps.
|
|
41
|
+
|
|
42
|
+
The robot is drawn only when its position AND its floor are both known. The
|
|
43
|
+
vendor's tracking stream carries no floor, so drawing it on whichever level
|
|
44
|
+
happens to be selected would put it on a storey nobody confirmed.
|
|
45
|
+
|
|
3
46
|
## 4.23.0
|
|
4
47
|
|
|
5
48
|
### Minor Changes
|
|
@@ -1280,11 +1323,11 @@
|
|
|
1280
1323
|
`utils/console-tier.ts` + `composables/useConsoleTier.ts` mirror the server's
|
|
1281
1324
|
own rule from two endpoints the console already calls, unprojected:
|
|
1282
1325
|
|
|
1283
|
-
|
|
1284
|
-
|
|
1326
|
+
GET /api/members/user/:user/app/admin the Seven365 staff membership
|
|
1327
|
+
GET /api/roles/id/:role that membership's role document
|
|
1285
1328
|
|
|
1286
|
-
|
|
1287
|
-
|
|
1329
|
+
owner = member.type === "admin" && role.type === "admin" && role.default === true
|
|
1330
|
+
staff = member.type === "admin" && role.type === "admin"
|
|
1288
1331
|
|
|
1289
1332
|
`role.default` is the marker because it is the only property of a platform
|
|
1290
1333
|
staff role no API caller can set - `role.controller.ts` validates create and
|
|
@@ -103,9 +103,21 @@
|
|
|
103
103
|
</template>
|
|
104
104
|
|
|
105
105
|
<template #item.floor="{ item }">
|
|
106
|
-
<
|
|
107
|
-
|
|
108
|
-
|
|
106
|
+
<div class="d-flex flex-column">
|
|
107
|
+
<span class="text-body-2 text-medium-emphasis">
|
|
108
|
+
{{ floorLabel(states[item._id]) }}
|
|
109
|
+
</span>
|
|
110
|
+
<!--
|
|
111
|
+
Battery is shown only when the brand reports one. A brand that
|
|
112
|
+
cannot measure it must not render as 0% - which reads as flat.
|
|
113
|
+
-->
|
|
114
|
+
<span
|
|
115
|
+
v-if="typeof states[item._id]?.batteryPercent === 'number'"
|
|
116
|
+
class="text-caption text-medium-emphasis"
|
|
117
|
+
>
|
|
118
|
+
{{ states[item._id].batteryPercent }}% battery
|
|
119
|
+
</span>
|
|
120
|
+
</div>
|
|
109
121
|
</template>
|
|
110
122
|
|
|
111
123
|
<template #item.actions="{ item }">
|
|
@@ -130,6 +142,18 @@
|
|
|
130
142
|
>
|
|
131
143
|
Return to dock
|
|
132
144
|
</AppButton>
|
|
145
|
+
<AppButton
|
|
146
|
+
v-if="canCommand"
|
|
147
|
+
size="small"
|
|
148
|
+
variant="text"
|
|
149
|
+
:disabled="!canDispatch(states[item._id]) || busy === item._id"
|
|
150
|
+
@click="openAreas(item)"
|
|
151
|
+
>
|
|
152
|
+
Clean area
|
|
153
|
+
</AppButton>
|
|
154
|
+
<AppButton size="small" variant="text" @click="openMap(item)">
|
|
155
|
+
View layout
|
|
156
|
+
</AppButton>
|
|
133
157
|
</div>
|
|
134
158
|
</template>
|
|
135
159
|
|
|
@@ -142,6 +166,76 @@
|
|
|
142
166
|
|
|
143
167
|
<Snackbar v-model="toast" :text="toastText" :color="toastColor" />
|
|
144
168
|
|
|
169
|
+
<!--
|
|
170
|
+
The building in 3D, generated from this robot's own floor plan. Per robot
|
|
171
|
+
rather than per site because the maps come from the robot - it is the thing
|
|
172
|
+
that surveyed the building.
|
|
173
|
+
-->
|
|
174
|
+
<v-dialog v-model="mapDialog" width="960" scrollable>
|
|
175
|
+
<AppCard class="pa-0">
|
|
176
|
+
<v-toolbar density="comfortable" class="pl-4">
|
|
177
|
+
<span class="font-weight-medium text-h6">{{ mapRobot?.name }} — building layout</span>
|
|
178
|
+
<v-spacer />
|
|
179
|
+
<AppButton variant="icon" icon="mdi-close" aria-label="Close" @click="mapDialog = false" />
|
|
180
|
+
</v-toolbar>
|
|
181
|
+
<RobotMap3D
|
|
182
|
+
v-if="mapDialog && mapRobot"
|
|
183
|
+
:key="mapRobot._id"
|
|
184
|
+
:robot-id="mapRobot._id"
|
|
185
|
+
:robot-state="states[mapRobot._id] ?? null"
|
|
186
|
+
/>
|
|
187
|
+
</AppCard>
|
|
188
|
+
</v-dialog>
|
|
189
|
+
|
|
190
|
+
<!--
|
|
191
|
+
Choosing zones rather than cleaning everything. The areas come from the
|
|
192
|
+
robot's own maps, so the names are the ones its operator already knows.
|
|
193
|
+
-->
|
|
194
|
+
<v-dialog v-model="areaDialog" width="520" scrollable>
|
|
195
|
+
<AppCard class="pa-0">
|
|
196
|
+
<v-toolbar density="comfortable" class="pl-4">
|
|
197
|
+
<span class="font-weight-medium text-h6">Clean areas — {{ areaRobot?.name }}</span>
|
|
198
|
+
</v-toolbar>
|
|
199
|
+
<v-card-text style="max-height: 55vh">
|
|
200
|
+
<div v-if="loadingAreas" class="py-6 text-center">
|
|
201
|
+
<v-progress-circular indeterminate size="28" />
|
|
202
|
+
</div>
|
|
203
|
+
<v-alert v-else-if="areaError" type="error" variant="tonal" :text="areaError" />
|
|
204
|
+
<div v-else-if="!areaOptions.length" class="py-6 text-center text-body-2 text-medium-emphasis">
|
|
205
|
+
This robot's maps define no cleaning areas.
|
|
206
|
+
</div>
|
|
207
|
+
<template v-else>
|
|
208
|
+
<p class="text-body-2 text-medium-emphasis mb-3">
|
|
209
|
+
Pick the areas to clean. The robot cleans them in the order shown.
|
|
210
|
+
</p>
|
|
211
|
+
<v-checkbox
|
|
212
|
+
v-for="area in areaOptions"
|
|
213
|
+
:key="area.value"
|
|
214
|
+
v-model="chosenAreas"
|
|
215
|
+
:value="area.value"
|
|
216
|
+
:label="area.title"
|
|
217
|
+
density="compact"
|
|
218
|
+
hide-details
|
|
219
|
+
/>
|
|
220
|
+
</template>
|
|
221
|
+
</v-card-text>
|
|
222
|
+
<v-toolbar density="compact">
|
|
223
|
+
<v-row no-gutters>
|
|
224
|
+
<v-col cols="6">
|
|
225
|
+
<v-btn tile block variant="flat" size="48" class="text-none" color="#EFEFEF"
|
|
226
|
+
:disabled="sendingAreas" @click="areaDialog = false">Close</v-btn>
|
|
227
|
+
</v-col>
|
|
228
|
+
<v-col cols="6">
|
|
229
|
+
<v-btn tile block variant="flat" size="48" class="text-none" color="primary"
|
|
230
|
+
:loading="sendingAreas" :disabled="!chosenAreas.length" @click="sendAreas">
|
|
231
|
+
Clean {{ chosenAreas.length || "" }}
|
|
232
|
+
</v-btn>
|
|
233
|
+
</v-col>
|
|
234
|
+
</v-row>
|
|
235
|
+
</v-toolbar>
|
|
236
|
+
</AppCard>
|
|
237
|
+
</v-dialog>
|
|
238
|
+
|
|
145
239
|
<v-dialog v-model="dialog" width="460" persistent>
|
|
146
240
|
<RobotForm
|
|
147
241
|
:organization="organization"
|
|
@@ -188,7 +282,7 @@ const props = withDefaults(
|
|
|
188
282
|
{ robotType: "cleaning", title: "Robots", canAdd: false, canCommand: false },
|
|
189
283
|
);
|
|
190
284
|
|
|
191
|
-
const { getRobots, getRobotState, sendRobotCommand } = useRobots();
|
|
285
|
+
const { getRobots, getRobotState, sendRobotCommand, getRobotMaps, trackRobot } = useRobots();
|
|
192
286
|
|
|
193
287
|
// The house pattern is a local Snackbar with local state, as
|
|
194
288
|
// `AccessCardPreviewDialog.vue` does - there is no global toast composable.
|
|
@@ -211,6 +305,114 @@ const page = ref(1);
|
|
|
211
305
|
const pages = ref(1);
|
|
212
306
|
const limit = 10;
|
|
213
307
|
const dialog = ref(false);
|
|
308
|
+
const mapDialog = ref(false);
|
|
309
|
+
const mapRobot = ref<Record<string, any> | null>(null);
|
|
310
|
+
|
|
311
|
+
const areaDialog = ref(false);
|
|
312
|
+
const areaRobot = ref<Record<string, any> | null>(null);
|
|
313
|
+
const areaOptions = ref<Array<{ title: string; value: string }>>([]);
|
|
314
|
+
const chosenAreas = ref<string[]>([]);
|
|
315
|
+
const loadingAreas = ref(false);
|
|
316
|
+
const sendingAreas = ref(false);
|
|
317
|
+
const areaError = ref("");
|
|
318
|
+
|
|
319
|
+
/** One open stream per robot, so they can all be stopped on unmount. */
|
|
320
|
+
const streams = new Map<string, { stop: () => void }>();
|
|
321
|
+
|
|
322
|
+
async function openAreas(robot: Record<string, any>) {
|
|
323
|
+
areaRobot.value = robot;
|
|
324
|
+
chosenAreas.value = [];
|
|
325
|
+
areaOptions.value = [];
|
|
326
|
+
areaError.value = "";
|
|
327
|
+
areaDialog.value = true;
|
|
328
|
+
loadingAreas.value = true;
|
|
329
|
+
try {
|
|
330
|
+
const res = await getRobotMaps(robot._id);
|
|
331
|
+
// Areas are per floor, and the same name can appear on more than one, so
|
|
332
|
+
// they are labelled with their floor rather than silently de-duplicated.
|
|
333
|
+
const options: Array<{ title: string; value: string }> = [];
|
|
334
|
+
for (const map of res?.items ?? []) {
|
|
335
|
+
for (const area of map.areaList ?? []) {
|
|
336
|
+
options.push({
|
|
337
|
+
title: `${area.areaName} — ${map.mapName?.trim() || `Floor ${map.floorNo}`}`,
|
|
338
|
+
value: area.areaName,
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
areaOptions.value = options;
|
|
343
|
+
} catch (e) {
|
|
344
|
+
areaError.value = robotErrorMessage(e, "Could not load this robot's areas.");
|
|
345
|
+
} finally {
|
|
346
|
+
loadingAreas.value = false;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
async function sendAreas() {
|
|
351
|
+
const robot = areaRobot.value;
|
|
352
|
+
if (!robot || !chosenAreas.value.length) return;
|
|
353
|
+
sendingAreas.value = true;
|
|
354
|
+
try {
|
|
355
|
+
await sendRobotCommand(robot._id, { kind: "area", areas: [...chosenAreas.value] });
|
|
356
|
+
showToast(`${robot.name} has been sent to clean ${chosenAreas.value.length} area(s).`, "success");
|
|
357
|
+
areaDialog.value = false;
|
|
358
|
+
// Re-read rather than assume: the API returns no job id, so the robot's own
|
|
359
|
+
// state is the only truthful confirmation anything happened.
|
|
360
|
+
states.value = { ...states.value, [robot._id]: await getRobotState(robot._id) };
|
|
361
|
+
} catch (e) {
|
|
362
|
+
showToast(robotErrorMessage(e), "error");
|
|
363
|
+
} finally {
|
|
364
|
+
sendingAreas.value = false;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/**
|
|
369
|
+
* Follow each robot's position while this screen is open.
|
|
370
|
+
*
|
|
371
|
+
* The server holds one connection to the robot cloud for as long as each of
|
|
372
|
+
* these is open, so they are all stopped on unmount - leaving them running
|
|
373
|
+
* would consume the vendor's stream budget for a screen nobody is looking at.
|
|
374
|
+
*/
|
|
375
|
+
function watchRobots() {
|
|
376
|
+
stopWatching();
|
|
377
|
+
for (const robot of items.value) {
|
|
378
|
+
streams.set(
|
|
379
|
+
robot._id,
|
|
380
|
+
trackRobot(robot._id, (event) => {
|
|
381
|
+
if (event?.type === "pose" && event.pose) {
|
|
382
|
+
const current = states.value[robot._id] ?? {};
|
|
383
|
+
states.value = {
|
|
384
|
+
...states.value,
|
|
385
|
+
[robot._id]: {
|
|
386
|
+
...current,
|
|
387
|
+
mode: event.pose.mode ?? current.mode,
|
|
388
|
+
position: { x: event.pose.x, y: event.pose.y, theta: event.pose.theta },
|
|
389
|
+
},
|
|
390
|
+
};
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
if (event?.type === "lost") {
|
|
394
|
+
// Losing the stream is not the same as the robot being idle: the last
|
|
395
|
+
// known mode would keep it advertised as ready for work forever.
|
|
396
|
+
const current = states.value[robot._id] ?? {};
|
|
397
|
+
states.value = {
|
|
398
|
+
...states.value,
|
|
399
|
+
[robot._id]: { ...current, mode: "UNAVAILABLE", dispatchable: false },
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
}),
|
|
403
|
+
);
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
function stopWatching() {
|
|
408
|
+
for (const stream of streams.values()) stream.stop();
|
|
409
|
+
streams.clear();
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
function openMap(robot: Record<string, any>) {
|
|
413
|
+
mapRobot.value = robot;
|
|
414
|
+
mapDialog.value = true;
|
|
415
|
+
}
|
|
214
416
|
const busy = ref("");
|
|
215
417
|
const lastCommand = ref("");
|
|
216
418
|
|
|
@@ -246,7 +448,7 @@ async function load() {
|
|
|
246
448
|
pages.value = Math.max(1, Number(res?.totalPages ?? 1));
|
|
247
449
|
// Deliberately not awaited: the table renders from our own database
|
|
248
450
|
// immediately, and each robot's chip fills in as its cloud call returns.
|
|
249
|
-
loadStates();
|
|
451
|
+
loadStates().then(watchRobots);
|
|
250
452
|
} catch (e) {
|
|
251
453
|
error.value = robotErrorMessage(e, "Could not load the robots for this site.");
|
|
252
454
|
items.value = [];
|
|
@@ -315,6 +517,7 @@ function onCreated() {
|
|
|
315
517
|
}
|
|
316
518
|
|
|
317
519
|
onMounted(load);
|
|
520
|
+
onBeforeUnmount(stopWatching);
|
|
318
521
|
</script>
|
|
319
522
|
|
|
320
523
|
<style scoped>
|
|
@@ -25,6 +25,23 @@
|
|
|
25
25
|
/>
|
|
26
26
|
</v-col>
|
|
27
27
|
|
|
28
|
+
<v-col cols="12">
|
|
29
|
+
<InputLabel class="font-weight-bold" title="Brand" required />
|
|
30
|
+
<AppSelect
|
|
31
|
+
v-model="brand"
|
|
32
|
+
:items="brands"
|
|
33
|
+
item-title="label"
|
|
34
|
+
item-value="brand"
|
|
35
|
+
density="comfortable"
|
|
36
|
+
:loading="loadingBrands"
|
|
37
|
+
:rules="[requiredRule]"
|
|
38
|
+
:disabled="saving"
|
|
39
|
+
/>
|
|
40
|
+
<p class="text-caption text-medium-emphasis mt-n3 mb-2">
|
|
41
|
+
{{ brandHint }}
|
|
42
|
+
</p>
|
|
43
|
+
</v-col>
|
|
44
|
+
|
|
28
45
|
<v-col cols="12">
|
|
29
46
|
<InputLabel class="font-weight-bold" title="App key" required />
|
|
30
47
|
<v-text-field
|
|
@@ -129,11 +146,14 @@ const props = withDefaults(
|
|
|
129
146
|
|
|
130
147
|
const emit = defineEmits<{ cancel: []; created: [] }>();
|
|
131
148
|
|
|
132
|
-
const { createRobot } = useRobots();
|
|
149
|
+
const { createRobot, getRobotBrands } = useRobots();
|
|
133
150
|
const { requiredRule } = useUtils();
|
|
134
151
|
|
|
135
152
|
const form = ref();
|
|
136
153
|
const name = ref("");
|
|
154
|
+
const brand = ref("");
|
|
155
|
+
const brands = ref<Array<Record<string, any>>>([]);
|
|
156
|
+
const loadingBrands = ref(false);
|
|
137
157
|
const appKey = ref("");
|
|
138
158
|
const appToken = ref("");
|
|
139
159
|
const robotSn = ref("");
|
|
@@ -143,6 +163,46 @@ const error = ref("");
|
|
|
143
163
|
|
|
144
164
|
const typeLabel = computed(() => (props.type === "security" ? "security" : "cleaning"));
|
|
145
165
|
|
|
166
|
+
/**
|
|
167
|
+
* What each brand's protocol will actually allow, in plain words.
|
|
168
|
+
*
|
|
169
|
+
* The tier comes from Open-RMF's published categories, so this is not a Seven365
|
|
170
|
+
* opinion - and somebody choosing a brand should know before they commit that
|
|
171
|
+
* one of them cannot be paused or told where to go.
|
|
172
|
+
*/
|
|
173
|
+
const TIER_HINT: Record<string, string> = {
|
|
174
|
+
"full-control": "Full control — position, battery, navigation, pause and resume.",
|
|
175
|
+
"traffic-light": "Position, battery, and pause/resume. No navigation to a point.",
|
|
176
|
+
"read-only": "Position only. This brand cannot be given jobs through its API.",
|
|
177
|
+
"no-interface":
|
|
178
|
+
"Limited: the published API does not yet report battery or progress. Cleaning and return-to-dock work.",
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
const brandHint = computed(() => {
|
|
182
|
+
const chosen = brands.value.find((b) => b.brand === brand.value);
|
|
183
|
+
return chosen ? TIER_HINT[chosen.tier] ?? "" : "Which robot this is. Ask your supplier if unsure.";
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
async function loadBrands() {
|
|
187
|
+
loadingBrands.value = true;
|
|
188
|
+
try {
|
|
189
|
+
const res = await getRobotBrands();
|
|
190
|
+
brands.value = (res?.items ?? []).map((b: any) => ({
|
|
191
|
+
...b,
|
|
192
|
+
label: b.displayName ?? b.brand,
|
|
193
|
+
}));
|
|
194
|
+
// Only preselect when there is no choice to make. Guessing a brand is how a
|
|
195
|
+
// robot ends up being sent commands in a protocol it does not speak.
|
|
196
|
+
if (brands.value.length === 1) brand.value = brands.value[0].brand;
|
|
197
|
+
} catch {
|
|
198
|
+
brands.value = [];
|
|
199
|
+
} finally {
|
|
200
|
+
loadingBrands.value = false;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
onMounted(loadBrands);
|
|
205
|
+
|
|
146
206
|
async function submit() {
|
|
147
207
|
const { valid } = await form.value?.validate?.() ?? { valid: false };
|
|
148
208
|
if (!valid) return;
|
|
@@ -152,6 +212,7 @@ async function submit() {
|
|
|
152
212
|
try {
|
|
153
213
|
await createRobot({
|
|
154
214
|
name: name.value.trim(),
|
|
215
|
+
brand: brand.value,
|
|
155
216
|
appKey: appKey.value.trim(),
|
|
156
217
|
appToken: appToken.value.trim(),
|
|
157
218
|
type: props.type,
|
|
@@ -0,0 +1,382 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<AppCard class="pa-0">
|
|
3
|
+
<CardToolbar>
|
|
4
|
+
<template #left>
|
|
5
|
+
<AppSelect
|
|
6
|
+
v-model="selectedFloor"
|
|
7
|
+
:items="floorOptions"
|
|
8
|
+
item-title="title"
|
|
9
|
+
item-value="value"
|
|
10
|
+
label="Level"
|
|
11
|
+
density="compact"
|
|
12
|
+
hide-details
|
|
13
|
+
style="max-width: 180px"
|
|
14
|
+
:disabled="loading || !floorOptions.length"
|
|
15
|
+
/>
|
|
16
|
+
<span v-if="wallCount" class="text-caption text-medium-emphasis ml-4">
|
|
17
|
+
{{ wallCount }} walls found
|
|
18
|
+
</span>
|
|
19
|
+
</template>
|
|
20
|
+
<template #right>
|
|
21
|
+
<AppButton
|
|
22
|
+
variant="icon"
|
|
23
|
+
icon="mdi-rotate-left"
|
|
24
|
+
aria-label="Rotate left"
|
|
25
|
+
:disabled="loading"
|
|
26
|
+
@click="rotate(-1)"
|
|
27
|
+
/>
|
|
28
|
+
<AppButton
|
|
29
|
+
variant="icon"
|
|
30
|
+
icon="mdi-rotate-right"
|
|
31
|
+
aria-label="Rotate right"
|
|
32
|
+
:disabled="loading"
|
|
33
|
+
@click="rotate(1)"
|
|
34
|
+
/>
|
|
35
|
+
<AppButton
|
|
36
|
+
variant="icon"
|
|
37
|
+
:icon="tilted ? 'mdi-cube-outline' : 'mdi-floor-plan'"
|
|
38
|
+
:aria-label="tilted ? 'Show as a flat plan' : 'Show in 3D'"
|
|
39
|
+
:disabled="loading"
|
|
40
|
+
@click="toggleTilt"
|
|
41
|
+
/>
|
|
42
|
+
</template>
|
|
43
|
+
</CardToolbar>
|
|
44
|
+
|
|
45
|
+
<div ref="stage" class="robot-map3d">
|
|
46
|
+
<canvas ref="canvasEl" class="robot-map3d__canvas" />
|
|
47
|
+
|
|
48
|
+
<div v-if="loading" class="robot-map3d__overlay">
|
|
49
|
+
<v-progress-circular indeterminate size="28" />
|
|
50
|
+
<p class="text-body-2 mt-3 mb-0">Building the layout…</p>
|
|
51
|
+
</div>
|
|
52
|
+
|
|
53
|
+
<!--
|
|
54
|
+
A failure is named rather than left as an empty canvas. An empty canvas
|
|
55
|
+
and a failed load look identical, and they need opposite actions from
|
|
56
|
+
the person looking at the screen.
|
|
57
|
+
-->
|
|
58
|
+
<div v-else-if="error" class="robot-map3d__overlay">
|
|
59
|
+
<v-icon size="36" class="mb-2">mdi-map-marker-off-outline</v-icon>
|
|
60
|
+
<p class="text-body-2 mb-3">{{ error }}</p>
|
|
61
|
+
<AppButton size="small" variant="outlined" @click="load">Try again</AppButton>
|
|
62
|
+
</div>
|
|
63
|
+
|
|
64
|
+
<div v-else-if="!wallCount" class="robot-map3d__overlay">
|
|
65
|
+
<v-icon size="36" class="mb-2">mdi-floor-plan</v-icon>
|
|
66
|
+
<p class="text-body-2 mb-0">
|
|
67
|
+
No walls could be found in this floor plan.
|
|
68
|
+
</p>
|
|
69
|
+
</div>
|
|
70
|
+
</div>
|
|
71
|
+
</AppCard>
|
|
72
|
+
</template>
|
|
73
|
+
|
|
74
|
+
<script lang="ts" setup>
|
|
75
|
+
/**
|
|
76
|
+
* The building, in 3D, generated from the robot's own floor plan.
|
|
77
|
+
*
|
|
78
|
+
* Nobody draws walls. The robot's map image is a SLAM occupancy grid — walls
|
|
79
|
+
* are already dark pixels — so it is fetched into a canvas, thresholded, traced
|
|
80
|
+
* into wall segments and extruded. Open-RMF does the same thing from an
|
|
81
|
+
* annotated plan; this skips the annotation because the robot has surveyed the
|
|
82
|
+
* building already.
|
|
83
|
+
*
|
|
84
|
+
* ## Why the image is fetched every time
|
|
85
|
+
*
|
|
86
|
+
* The vendor signs its map URLs for about an hour. A URL kept in a record is a
|
|
87
|
+
* URL that stops working, so it is asked for at the moment of display — the
|
|
88
|
+
* same trap the Virtual Patrol camera addresses hit.
|
|
89
|
+
*
|
|
90
|
+
* ## Why there is no 3D engine
|
|
91
|
+
*
|
|
92
|
+
* The scene is extruded boxes on flat floors. A parallel projection and a
|
|
93
|
+
* back-to-front draw order render that correctly, and `layer-common` is
|
|
94
|
+
* consumed by eleven web apps — an engine is a large thing to put in all of
|
|
95
|
+
* them for a shape this simple.
|
|
96
|
+
*
|
|
97
|
+
* ponytail: painter's algorithm, fixed camera, flat shading by face angle. No
|
|
98
|
+
* lighting model, no textures, no orbit. Swap Three.js in behind `levelSolids`
|
|
99
|
+
* and `projectPoint` if those ever matter; the geometry does not change.
|
|
100
|
+
*/
|
|
101
|
+
import {
|
|
102
|
+
depthOf,
|
|
103
|
+
fitView,
|
|
104
|
+
gridFromImageData,
|
|
105
|
+
levelSolids,
|
|
106
|
+
projectPoint,
|
|
107
|
+
wallCorners,
|
|
108
|
+
wallsFromOccupancyGrid,
|
|
109
|
+
type TView,
|
|
110
|
+
type TWallSolid,
|
|
111
|
+
} from "../../utils/building-map";
|
|
112
|
+
import { robotErrorMessage } from "../../utils/robot-display";
|
|
113
|
+
|
|
114
|
+
const props = defineProps<{
|
|
115
|
+
/** The robot whose maps describe this building. */
|
|
116
|
+
robotId: string;
|
|
117
|
+
/** Drawn on the plan when its position and floor are both known. */
|
|
118
|
+
robotState?: Record<string, any> | null;
|
|
119
|
+
}>();
|
|
120
|
+
|
|
121
|
+
const { getRobotMaps, getRobotMapImages } = useRobots();
|
|
122
|
+
|
|
123
|
+
const stage = ref<HTMLElement | null>(null);
|
|
124
|
+
const canvasEl = ref<HTMLCanvasElement | null>(null);
|
|
125
|
+
|
|
126
|
+
const maps = ref<Array<Record<string, any>>>([]);
|
|
127
|
+
const selectedFloor = ref<number | null>(null);
|
|
128
|
+
const solids = ref<TWallSolid[]>([]);
|
|
129
|
+
const imageHeight = ref(0);
|
|
130
|
+
const loading = ref(true);
|
|
131
|
+
const error = ref("");
|
|
132
|
+
const rotation = ref(Math.PI / 5);
|
|
133
|
+
const tilted = ref(true);
|
|
134
|
+
|
|
135
|
+
const wallCount = computed(() => solids.value.length);
|
|
136
|
+
|
|
137
|
+
const floorOptions = computed(() =>
|
|
138
|
+
maps.value
|
|
139
|
+
.map((m) => ({ title: m.mapName?.trim() || `Floor ${m.floorNo}`, value: m.floorNo }))
|
|
140
|
+
.sort((a, b) => a.value - b.value),
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
const currentMap = computed(() =>
|
|
144
|
+
maps.value.find((m) => m.floorNo === selectedFloor.value) ?? null,
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
/* ------------------------------------------------------------------ load */
|
|
148
|
+
|
|
149
|
+
async function load() {
|
|
150
|
+
loading.value = true;
|
|
151
|
+
error.value = "";
|
|
152
|
+
try {
|
|
153
|
+
if (!maps.value.length) {
|
|
154
|
+
const res = await getRobotMaps(props.robotId);
|
|
155
|
+
maps.value = res?.items ?? [];
|
|
156
|
+
if (!maps.value.length) {
|
|
157
|
+
error.value = "This robot has no floor maps yet.";
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
selectedFloor.value = floorOptions.value[0]?.value ?? null;
|
|
161
|
+
}
|
|
162
|
+
await buildLevel();
|
|
163
|
+
} catch (e) {
|
|
164
|
+
error.value = robotErrorMessage(e, "Could not load this building's floor plan.");
|
|
165
|
+
} finally {
|
|
166
|
+
loading.value = false;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** Fetch this floor's plan, trace its walls, and extrude them. */
|
|
171
|
+
async function buildLevel() {
|
|
172
|
+
const map = currentMap.value;
|
|
173
|
+
solids.value = [];
|
|
174
|
+
if (!map?.backgroundFileKey) {
|
|
175
|
+
error.value = "This floor has no plan image.";
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const files = await getRobotMapImages(props.robotId, [map.backgroundFileKey]);
|
|
180
|
+
const url = files?.items?.find((f: any) => f.fileKey === map.backgroundFileKey)?.url;
|
|
181
|
+
if (!url) {
|
|
182
|
+
error.value = "The floor plan image could not be fetched.";
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const image = await loadImage(url);
|
|
187
|
+
imageHeight.value = image.height;
|
|
188
|
+
|
|
189
|
+
const work = document.createElement("canvas");
|
|
190
|
+
work.width = image.width;
|
|
191
|
+
work.height = image.height;
|
|
192
|
+
const wctx = work.getContext("2d", { willReadFrequently: true });
|
|
193
|
+
if (!wctx) {
|
|
194
|
+
error.value = "This browser could not read the floor plan image.";
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
wctx.drawImage(image, 0, 0);
|
|
198
|
+
|
|
199
|
+
const { data } = wctx.getImageData(0, 0, image.width, image.height);
|
|
200
|
+
const grid = gridFromImageData(data, image.width, image.height);
|
|
201
|
+
const { vertices, walls } = wallsFromOccupancyGrid(grid, { minRunPx: 6 });
|
|
202
|
+
|
|
203
|
+
solids.value = levelSolids(
|
|
204
|
+
{
|
|
205
|
+
name: map.mapName ?? `L${map.floorNo}`,
|
|
206
|
+
elevationM: 0,
|
|
207
|
+
scaleMPerPx: Number(map.resolution) || 0.05,
|
|
208
|
+
originXM: Number(map.originX) || 0,
|
|
209
|
+
originYM: Number(map.originY) || 0,
|
|
210
|
+
vertices,
|
|
211
|
+
walls,
|
|
212
|
+
},
|
|
213
|
+
image.height,
|
|
214
|
+
);
|
|
215
|
+
|
|
216
|
+
draw();
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* `crossOrigin` is set because the pixels are read back, and a canvas tainted
|
|
221
|
+
* by a cross-origin image throws on `getImageData` — which would look like a
|
|
222
|
+
* broken floor plan rather than a CORS rule.
|
|
223
|
+
*/
|
|
224
|
+
function loadImage(url: string): Promise<HTMLImageElement> {
|
|
225
|
+
return new Promise((resolve, reject) => {
|
|
226
|
+
const img = new Image();
|
|
227
|
+
img.crossOrigin = "anonymous";
|
|
228
|
+
img.onload = () => resolve(img);
|
|
229
|
+
img.onerror = () => reject(new Error("The floor plan image could not be loaded."));
|
|
230
|
+
img.src = url;
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/* ------------------------------------------------------------------ draw */
|
|
235
|
+
|
|
236
|
+
function draw() {
|
|
237
|
+
const canvas = canvasEl.value;
|
|
238
|
+
const host = stage.value;
|
|
239
|
+
if (!canvas || !host) return;
|
|
240
|
+
|
|
241
|
+
const dpr = Math.min(window.devicePixelRatio || 1, 2);
|
|
242
|
+
const width = host.clientWidth;
|
|
243
|
+
const height = host.clientHeight;
|
|
244
|
+
canvas.width = width * dpr;
|
|
245
|
+
canvas.height = height * dpr;
|
|
246
|
+
canvas.style.width = `${width}px`;
|
|
247
|
+
canvas.style.height = `${height}px`;
|
|
248
|
+
|
|
249
|
+
const ctx = canvas.getContext("2d");
|
|
250
|
+
if (!ctx) return;
|
|
251
|
+
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
|
|
252
|
+
ctx.clearRect(0, 0, width, height);
|
|
253
|
+
|
|
254
|
+
const view: TView = fitView(solids.value, { width, height }, {
|
|
255
|
+
rotation: rotation.value,
|
|
256
|
+
tilt: tilted.value ? 0.95 : 0,
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
// Painter's algorithm: with no depth buffer, draw order IS the depth test.
|
|
260
|
+
const ordered = [...solids.value].sort(
|
|
261
|
+
(a, b) => depthOf(a.centre, view) - depthOf(b.centre, view),
|
|
262
|
+
);
|
|
263
|
+
|
|
264
|
+
for (const solid of ordered) drawBox(ctx, solid, view);
|
|
265
|
+
drawRobot(ctx, view);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/** The three visible faces of a box: top, and two sides. */
|
|
269
|
+
function drawBox(ctx: CanvasRenderingContext2D, solid: TWallSolid, view: TView) {
|
|
270
|
+
const c = wallCorners(solid).map((p) => projectPoint(p.x, p.y, p.z, view));
|
|
271
|
+
// wallCorners emits the bottom four then the top four, in the same order.
|
|
272
|
+
const faces: Array<{ idx: number[]; shade: number }> = [
|
|
273
|
+
{ idx: [4, 5, 6, 7], shade: 1 }, // top, lit
|
|
274
|
+
{ idx: [0, 1, 5, 4], shade: 0.78 },
|
|
275
|
+
{ idx: [1, 2, 6, 5], shade: 0.62 },
|
|
276
|
+
{ idx: [2, 3, 7, 6], shade: 0.78 },
|
|
277
|
+
{ idx: [3, 0, 4, 7], shade: 0.62 },
|
|
278
|
+
];
|
|
279
|
+
|
|
280
|
+
for (const face of faces) {
|
|
281
|
+
ctx.beginPath();
|
|
282
|
+
face.idx.forEach((i, n) => (n === 0 ? ctx.moveTo(c[i].x, c[i].y) : ctx.lineTo(c[i].x, c[i].y)));
|
|
283
|
+
ctx.closePath();
|
|
284
|
+
const v = Math.round(150 * face.shade + 60);
|
|
285
|
+
ctx.fillStyle = `rgb(${v},${v + 4},${v + 8})`;
|
|
286
|
+
ctx.fill();
|
|
287
|
+
ctx.strokeStyle = "rgba(20,28,38,0.22)";
|
|
288
|
+
ctx.lineWidth = 0.6;
|
|
289
|
+
ctx.stroke();
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* The robot, only when its position AND its floor are both known.
|
|
295
|
+
*
|
|
296
|
+
* The vendor's tracking stream carries no floor, so drawing it on whichever
|
|
297
|
+
* level happens to be selected would put it on a storey nobody confirmed — and
|
|
298
|
+
* a robot shown in the wrong place is worse than one not shown.
|
|
299
|
+
*/
|
|
300
|
+
function drawRobot(ctx: CanvasRenderingContext2D, view: TView) {
|
|
301
|
+
const state = props.robotState;
|
|
302
|
+
const pos = state?.position;
|
|
303
|
+
if (!pos || state?.floorKnown !== true) return;
|
|
304
|
+
if (state.floorNo !== selectedFloor.value) return;
|
|
305
|
+
|
|
306
|
+
const base = projectPoint(pos.x, pos.y, 0, view);
|
|
307
|
+
const top = projectPoint(pos.x, pos.y, 0.9, view);
|
|
308
|
+
|
|
309
|
+
ctx.beginPath();
|
|
310
|
+
ctx.moveTo(base.x, base.y);
|
|
311
|
+
ctx.lineTo(top.x, top.y);
|
|
312
|
+
ctx.strokeStyle = "#FF5A1F";
|
|
313
|
+
ctx.lineWidth = 2;
|
|
314
|
+
ctx.stroke();
|
|
315
|
+
|
|
316
|
+
ctx.beginPath();
|
|
317
|
+
ctx.arc(top.x, top.y, 6, 0, Math.PI * 2);
|
|
318
|
+
ctx.fillStyle = "#FF5A1F";
|
|
319
|
+
ctx.fill();
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/* --------------------------------------------------------------- controls */
|
|
323
|
+
|
|
324
|
+
function rotate(direction: number) {
|
|
325
|
+
rotation.value += (direction * Math.PI) / 12;
|
|
326
|
+
draw();
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
function toggleTilt() {
|
|
330
|
+
tilted.value = !tilted.value;
|
|
331
|
+
draw();
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
watch(selectedFloor, async () => {
|
|
335
|
+
if (!maps.value.length) return;
|
|
336
|
+
loading.value = true;
|
|
337
|
+
error.value = "";
|
|
338
|
+
try {
|
|
339
|
+
await buildLevel();
|
|
340
|
+
} catch (e) {
|
|
341
|
+
error.value = robotErrorMessage(e, "Could not load this floor.");
|
|
342
|
+
} finally {
|
|
343
|
+
loading.value = false;
|
|
344
|
+
}
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
watch(() => props.robotState, draw, { deep: true });
|
|
348
|
+
|
|
349
|
+
let onResize: (() => void) | undefined;
|
|
350
|
+
onMounted(() => {
|
|
351
|
+
load();
|
|
352
|
+
onResize = () => draw();
|
|
353
|
+
window.addEventListener("resize", onResize);
|
|
354
|
+
});
|
|
355
|
+
onBeforeUnmount(() => {
|
|
356
|
+
if (onResize) window.removeEventListener("resize", onResize);
|
|
357
|
+
});
|
|
358
|
+
</script>
|
|
359
|
+
|
|
360
|
+
<style scoped>
|
|
361
|
+
.robot-map3d {
|
|
362
|
+
position: relative;
|
|
363
|
+
width: 100%;
|
|
364
|
+
height: 460px;
|
|
365
|
+
background: linear-gradient(180deg, rgba(0, 0, 0, 0.02), rgba(0, 0, 0, 0.06));
|
|
366
|
+
}
|
|
367
|
+
.robot-map3d__canvas {
|
|
368
|
+
display: block;
|
|
369
|
+
width: 100%;
|
|
370
|
+
height: 100%;
|
|
371
|
+
}
|
|
372
|
+
.robot-map3d__overlay {
|
|
373
|
+
position: absolute;
|
|
374
|
+
inset: 0;
|
|
375
|
+
display: flex;
|
|
376
|
+
flex-direction: column;
|
|
377
|
+
align-items: center;
|
|
378
|
+
justify-content: center;
|
|
379
|
+
text-align: center;
|
|
380
|
+
padding: 24px;
|
|
381
|
+
}
|
|
382
|
+
</style>
|
package/composables/useRobots.ts
CHANGED
|
@@ -83,6 +83,92 @@ export default function useRobots() {
|
|
|
83
83
|
);
|
|
84
84
|
}
|
|
85
85
|
|
|
86
|
+
/**
|
|
87
|
+
* The robot brands this platform can talk to.
|
|
88
|
+
*
|
|
89
|
+
* A robot record has to name its brand and there is no safe default - sending
|
|
90
|
+
* a robot a command in another brand's protocol is the one mistake the
|
|
91
|
+
* adapter layer exists to prevent. Each brand reports its Open-RMF tier, so
|
|
92
|
+
* somebody choosing one can see what that protocol allows before committing.
|
|
93
|
+
*/
|
|
94
|
+
function getRobotBrands() {
|
|
95
|
+
return useNuxtApp().$api<Record<string, any>>("/api/robots/brands", {
|
|
96
|
+
method: "GET",
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Watch one robot's position.
|
|
102
|
+
*
|
|
103
|
+
* `fetch` with a stream reader rather than `EventSource`, because EventSource
|
|
104
|
+
* cannot carry the headers `$api` adds - it would be an unauthenticated
|
|
105
|
+
* request that simply 401s.
|
|
106
|
+
*
|
|
107
|
+
* Returns a `stop`. Calling it aborts the request, which closes the upstream
|
|
108
|
+
* connection to the robot cloud - the server holds one for as long as this
|
|
109
|
+
* one is open, so leaving it running is not free.
|
|
110
|
+
*/
|
|
111
|
+
function trackRobot(
|
|
112
|
+
id: string,
|
|
113
|
+
onEvent: (event: Record<string, any>) => void,
|
|
114
|
+
): { stop: () => void } {
|
|
115
|
+
const controller = new AbortController();
|
|
116
|
+
const base = useRuntimeConfig().public?.apiBase ?? "";
|
|
117
|
+
const query = appSlug ? `?appSlug=${encodeURIComponent(appSlug)}` : "";
|
|
118
|
+
|
|
119
|
+
(async () => {
|
|
120
|
+
try {
|
|
121
|
+
const response = await fetch(`${base}/api/robots/${id}/track${query}`, {
|
|
122
|
+
method: "GET",
|
|
123
|
+
headers: { Accept: "text/event-stream" },
|
|
124
|
+
credentials: "include",
|
|
125
|
+
signal: controller.signal,
|
|
126
|
+
});
|
|
127
|
+
if (!response.ok || !response.body) {
|
|
128
|
+
onEvent({ type: "lost", reason: `The robot stream refused (${response.status}).` });
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const reader = response.body.getReader();
|
|
133
|
+
const decoder = new TextDecoder();
|
|
134
|
+
let buffer = "";
|
|
135
|
+
|
|
136
|
+
// Events do not respect chunk boundaries: whatever follows the last
|
|
137
|
+
// blank line stays buffered for the next read, or a pose arriving in
|
|
138
|
+
// two pieces is dropped.
|
|
139
|
+
for (;;) {
|
|
140
|
+
const { done, value } = await reader.read();
|
|
141
|
+
if (done) break;
|
|
142
|
+
|
|
143
|
+
buffer += decoder.decode(value, { stream: true });
|
|
144
|
+
buffer = buffer.replace(/\r\n|\r/g, "\n");
|
|
145
|
+
|
|
146
|
+
const frames = buffer.split("\n\n");
|
|
147
|
+
buffer = frames.pop() ?? "";
|
|
148
|
+
|
|
149
|
+
for (const frame of frames) {
|
|
150
|
+
for (const line of frame.split("\n")) {
|
|
151
|
+
// A line starting ":" is a keep-alive comment, not an event.
|
|
152
|
+
if (!line.startsWith("data:")) continue;
|
|
153
|
+
try {
|
|
154
|
+
onEvent(JSON.parse(line.slice(5).trim()));
|
|
155
|
+
} catch {
|
|
156
|
+
// One malformed frame must not tear down a stream that is
|
|
157
|
+
// otherwise delivering good poses.
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
onEvent({ type: "lost", reason: "The robot stream ended." });
|
|
163
|
+
} catch (error: any) {
|
|
164
|
+
if (error?.name === "AbortError") return;
|
|
165
|
+
onEvent({ type: "lost", reason: error?.message ?? "The robot stream failed." });
|
|
166
|
+
}
|
|
167
|
+
})();
|
|
168
|
+
|
|
169
|
+
return { stop: () => controller.abort() };
|
|
170
|
+
}
|
|
171
|
+
|
|
86
172
|
/** The cleaning schedule the vendor cloud holds for this robot. */
|
|
87
173
|
function getRobotPlans(id: string) {
|
|
88
174
|
return useNuxtApp().$api<Record<string, any>>(
|
|
@@ -115,6 +201,8 @@ export default function useRobots() {
|
|
|
115
201
|
|
|
116
202
|
return {
|
|
117
203
|
getRobots,
|
|
204
|
+
getRobotBrands,
|
|
205
|
+
trackRobot,
|
|
118
206
|
createRobot,
|
|
119
207
|
getRobotState,
|
|
120
208
|
getRobotMaps,
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@7365admin1/layer-common",
|
|
3
3
|
"license": "MIT",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"version": "4.
|
|
5
|
+
"version": "4.25.0",
|
|
6
6
|
"author": "7365admin1",
|
|
7
7
|
"main": "./nuxt.config.ts",
|
|
8
8
|
"//files": "What a consumer extending this layer actually loads. Without this npm ships the whole working tree - the changesets, the CI workflows, the render harness in tools/ and any scratch directory that happened to exist at publish time. Nuxt resolves a layer by directory, so every runtime directory below has to stay listed; adding a new top-level runtime directory means adding it here too.",
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"build": "nuxt build .playground",
|
|
37
37
|
"generate": "nuxt generate .playground",
|
|
38
38
|
"preview": "nuxt preview .playground",
|
|
39
|
-
"test": "esbuild composables/useVisitorSocket.ts --format=esm --outfile=test/.build/useVisitorSocket.mjs --log-level=error && esbuild composables/usePlateToastWindow.ts --format=esm --outfile=test/.build/usePlateToastWindow.mjs --log-level=error && esbuild utils/client-invite-modules.ts --bundle --platform=node --format=esm --outfile=test/.build/client-invite-modules.mjs --log-level=error && esbuild utils/robot-display.ts --bundle --platform=node --format=esm --outfile=test/.build/robot-display.mjs --log-level=error && node --experimental-strip-types --test \"test/*.test.mjs\" && yarn test:units",
|
|
39
|
+
"test": "esbuild composables/useVisitorSocket.ts --format=esm --outfile=test/.build/useVisitorSocket.mjs --log-level=error && esbuild composables/usePlateToastWindow.ts --format=esm --outfile=test/.build/usePlateToastWindow.mjs --log-level=error && esbuild utils/client-invite-modules.ts --bundle --platform=node --format=esm --outfile=test/.build/client-invite-modules.mjs --log-level=error && esbuild utils/robot-display.ts --bundle --platform=node --format=esm --outfile=test/.build/robot-display.mjs --log-level=error && esbuild utils/building-map.ts --bundle --platform=node --format=esm --outfile=test/.build/building-map.mjs --log-level=error && node --experimental-strip-types --test \"test/*.test.mjs\" && yarn test:units",
|
|
40
40
|
"test:units": "node --experimental-strip-types --test \"utils/*.test.ts\"",
|
|
41
41
|
"release": "yarn run build && changeset publish"
|
|
42
42
|
},
|
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Floor plan to 3D layout, in the browser.
|
|
3
|
+
*
|
|
4
|
+
* This is a deliberate copy of the same logic in `@7365admin1/core`, for the
|
|
5
|
+
* same reason `robot-display.ts` is: core is a backend package and a browser
|
|
6
|
+
* cannot import it. The wall extraction has to run where the floor plan image
|
|
7
|
+
* is — the vendor signs its map URLs for about an hour, so the picture is
|
|
8
|
+
* fetched into a canvas at the moment of display and never stored, and the
|
|
9
|
+
* pixels only exist client-side.
|
|
10
|
+
*
|
|
11
|
+
* The two copies must agree, and the agreement is held by the same tests on
|
|
12
|
+
* both sides.
|
|
13
|
+
*
|
|
14
|
+
* ## Why there is no 3D library here
|
|
15
|
+
*
|
|
16
|
+
* The scene is extruded boxes standing on flat floors. A projection and a
|
|
17
|
+
* back-to-front draw order render that correctly in a few hundred lines, and
|
|
18
|
+
* `layer-common` is consumed by eleven web apps — a 3D engine is a large thing
|
|
19
|
+
* to put in all of them for a shape this simple.
|
|
20
|
+
*
|
|
21
|
+
* ponytail: painter's algorithm with a fixed camera. No lighting model, no
|
|
22
|
+
* textures, no orbit. If any of those matter later, swap Three.js in behind
|
|
23
|
+
* `levelSolids` / `projectPoint` — the geometry above them does not change.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
/** `traffic_editor`'s defaults, matched so our layout and RMF's agree. */
|
|
27
|
+
export const WALL_HEIGHT_M = 2.5;
|
|
28
|
+
export const WALL_THICKNESS_M = 0.1;
|
|
29
|
+
export const DEFAULT_SCALE_M_PER_PX = 0.05;
|
|
30
|
+
|
|
31
|
+
export type TMapVertex = { x: number; y: number; name?: string };
|
|
32
|
+
export type TMapWall = { start: number; end: number; heightM?: number; thicknessM?: number };
|
|
33
|
+
|
|
34
|
+
export type TPlanLevel = {
|
|
35
|
+
name: string;
|
|
36
|
+
elevationM: number;
|
|
37
|
+
scaleMPerPx: number;
|
|
38
|
+
originXM: number;
|
|
39
|
+
originYM: number;
|
|
40
|
+
vertices: TMapVertex[];
|
|
41
|
+
walls: TMapWall[];
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export type TWallSolid = {
|
|
45
|
+
centre: { x: number; y: number; z: number };
|
|
46
|
+
lengthM: number;
|
|
47
|
+
thicknessM: number;
|
|
48
|
+
heightM: number;
|
|
49
|
+
yaw: number;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/* ------------------------------------------------- image to occupancy grid */
|
|
53
|
+
|
|
54
|
+
export type TOccupancyGrid = { data: Uint8Array; width: number; height: number };
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Turn canvas RGBA into one luminance value per pixel.
|
|
58
|
+
*
|
|
59
|
+
* A fully transparent pixel is treated as FREE space, not dark. Map PNGs are
|
|
60
|
+
* commonly transparent outside the surveyed area, and reading alpha 0 as black
|
|
61
|
+
* would wrap the whole plan in a wall.
|
|
62
|
+
*/
|
|
63
|
+
export function gridFromImageData(
|
|
64
|
+
rgba: ArrayLike<number>,
|
|
65
|
+
width: number,
|
|
66
|
+
height: number,
|
|
67
|
+
): TOccupancyGrid {
|
|
68
|
+
const data = new Uint8Array(width * height);
|
|
69
|
+
for (let i = 0, p = 0; p < width * height; i += 4, p++) {
|
|
70
|
+
const alpha = Number(rgba[i + 3]);
|
|
71
|
+
if (alpha === 0) {
|
|
72
|
+
data[p] = 255;
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
// Rec. 601 luma — close enough for "is this pixel dark", and cheap.
|
|
76
|
+
data[p] =
|
|
77
|
+
(0.299 * Number(rgba[i]) + 0.587 * Number(rgba[i + 1]) + 0.114 * Number(rgba[i + 2])) | 0;
|
|
78
|
+
}
|
|
79
|
+
return { data, width, height };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Trace walls out of a floor plan, so nobody has to draw them.
|
|
84
|
+
*
|
|
85
|
+
* Horizontal runs first, then vertical runs from what those did not cover, so a
|
|
86
|
+
* long wall is one segment rather than one per pixel. Corners are de-duplicated
|
|
87
|
+
* so the result stays editable afterwards.
|
|
88
|
+
*/
|
|
89
|
+
export function wallsFromOccupancyGrid(
|
|
90
|
+
grid: TOccupancyGrid,
|
|
91
|
+
options?: { threshold?: number; minRunPx?: number },
|
|
92
|
+
): { vertices: TMapVertex[]; walls: TMapWall[] } {
|
|
93
|
+
const threshold = options?.threshold ?? 100;
|
|
94
|
+
const minRun = Math.max(2, options?.minRunPx ?? 6);
|
|
95
|
+
const { data, width, height } = grid;
|
|
96
|
+
|
|
97
|
+
if (!width || !height || !data || data.length < width * height) {
|
|
98
|
+
return { vertices: [], walls: [] };
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const occupied = (x: number, y: number) => Number(data[y * width + x]) <= threshold;
|
|
102
|
+
const consumed = new Uint8Array(width * height);
|
|
103
|
+
const vertices: TMapVertex[] = [];
|
|
104
|
+
const walls: TMapWall[] = [];
|
|
105
|
+
const index = new Map<string, number>();
|
|
106
|
+
|
|
107
|
+
const vertexAt = (x: number, y: number) => {
|
|
108
|
+
const key = `${x}:${y}`;
|
|
109
|
+
const found = index.get(key);
|
|
110
|
+
if (found !== undefined) return found;
|
|
111
|
+
vertices.push({ x, y });
|
|
112
|
+
index.set(key, vertices.length - 1);
|
|
113
|
+
return vertices.length - 1;
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
for (let y = 0; y < height; y++) {
|
|
117
|
+
let run = 0;
|
|
118
|
+
for (let x = 0; x <= width; x++) {
|
|
119
|
+
if (x < width && occupied(x, y)) {
|
|
120
|
+
run++;
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
if (run >= minRun) {
|
|
124
|
+
const startX = x - run;
|
|
125
|
+
walls.push({ start: vertexAt(startX, y), end: vertexAt(x - 1, y) });
|
|
126
|
+
for (let i = startX; i < x; i++) consumed[y * width + i] = 1;
|
|
127
|
+
}
|
|
128
|
+
run = 0;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
for (let x = 0; x < width; x++) {
|
|
133
|
+
let run = 0;
|
|
134
|
+
for (let y = 0; y <= height; y++) {
|
|
135
|
+
if (y < height && occupied(x, y) && !consumed[y * width + x]) {
|
|
136
|
+
run++;
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
if (run >= minRun) {
|
|
140
|
+
walls.push({ start: vertexAt(x, y - run), end: vertexAt(x, y - 1) });
|
|
141
|
+
}
|
|
142
|
+
run = 0;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return { vertices, walls };
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/* ------------------------------------------------------------- geometry */
|
|
150
|
+
|
|
151
|
+
export function levelScale(level: TPlanLevel): number {
|
|
152
|
+
return level.scaleMPerPx > 0 ? level.scaleMPerPx : DEFAULT_SCALE_M_PER_PX;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* A drawing pixel in site metres.
|
|
157
|
+
*
|
|
158
|
+
* The vertical flip is the occupancy-grid convention: image origin top-left,
|
|
159
|
+
* map origin bottom-left. Without it the building renders mirrored, which reads
|
|
160
|
+
* as "the robot is in the wrong room" rather than as a transform bug.
|
|
161
|
+
*/
|
|
162
|
+
export function levelPixelToMetres(
|
|
163
|
+
level: TPlanLevel,
|
|
164
|
+
px: number,
|
|
165
|
+
py: number,
|
|
166
|
+
imageHeightPx: number,
|
|
167
|
+
): { x: number; y: number } {
|
|
168
|
+
const scale = levelScale(level);
|
|
169
|
+
return {
|
|
170
|
+
x: level.originXM + px * scale,
|
|
171
|
+
y: level.originYM + (imageHeightPx - py) * scale,
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** Extrude a level's walls into boxes a renderer can place. */
|
|
176
|
+
export function levelSolids(level: TPlanLevel, imageHeightPx: number): TWallSolid[] {
|
|
177
|
+
const solids: TWallSolid[] = [];
|
|
178
|
+
for (const wall of level.walls ?? []) {
|
|
179
|
+
const a = level.vertices[wall.start];
|
|
180
|
+
const b = level.vertices[wall.end];
|
|
181
|
+
if (!a || !b) continue;
|
|
182
|
+
|
|
183
|
+
const pa = levelPixelToMetres(level, a.x, a.y, imageHeightPx);
|
|
184
|
+
const pb = levelPixelToMetres(level, b.x, b.y, imageHeightPx);
|
|
185
|
+
const lengthM = Math.hypot(pb.x - pa.x, pb.y - pa.y);
|
|
186
|
+
// A zero-length wall renders as a flickering sliver that looks like a
|
|
187
|
+
// graphics fault rather than a map mistake.
|
|
188
|
+
if (lengthM <= 0) continue;
|
|
189
|
+
|
|
190
|
+
const heightM = wall.heightM ?? WALL_HEIGHT_M;
|
|
191
|
+
solids.push({
|
|
192
|
+
centre: {
|
|
193
|
+
x: (pa.x + pb.x) / 2,
|
|
194
|
+
y: (pa.y + pb.y) / 2,
|
|
195
|
+
// Boxes are centred, so a wall standing ON the floor sits at half its
|
|
196
|
+
// own height above the level's elevation.
|
|
197
|
+
z: level.elevationM + heightM / 2,
|
|
198
|
+
},
|
|
199
|
+
lengthM,
|
|
200
|
+
thicknessM: wall.thicknessM ?? WALL_THICKNESS_M,
|
|
201
|
+
heightM,
|
|
202
|
+
yaw: Math.atan2(pb.y - pa.y, pb.x - pa.x),
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
return solids;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/* ----------------------------------------------------------- projection */
|
|
209
|
+
|
|
210
|
+
export type TView = {
|
|
211
|
+
/** Radians. How far the scene is turned about the vertical axis. */
|
|
212
|
+
rotation: number;
|
|
213
|
+
/** Radians. 0 is a plan view looking straight down; ~0.9 reads as 3D. */
|
|
214
|
+
tilt: number;
|
|
215
|
+
/** Screen pixels per metre. */
|
|
216
|
+
zoom: number;
|
|
217
|
+
/** Where the site origin lands on the canvas. */
|
|
218
|
+
originPx: { x: number; y: number };
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* A point in site metres, on the canvas.
|
|
223
|
+
*
|
|
224
|
+
* An axonometric projection: rotate about the vertical axis, then foreshorten
|
|
225
|
+
* the depth axis by the tilt. Parallel, so there is no perspective distortion
|
|
226
|
+
* and a wall is the same size wherever it sits — which is what you want for a
|
|
227
|
+
* floor plan, and is why a full camera is not needed.
|
|
228
|
+
*
|
|
229
|
+
* Screen y grows downward, so height is SUBTRACTED: a taller wall draws further
|
|
230
|
+
* up the canvas.
|
|
231
|
+
*/
|
|
232
|
+
export function projectPoint(
|
|
233
|
+
x: number,
|
|
234
|
+
y: number,
|
|
235
|
+
z: number,
|
|
236
|
+
view: TView,
|
|
237
|
+
): { x: number; y: number } {
|
|
238
|
+
const cos = Math.cos(view.rotation);
|
|
239
|
+
const sin = Math.sin(view.rotation);
|
|
240
|
+
|
|
241
|
+
const rx = x * cos - y * sin;
|
|
242
|
+
const ry = x * sin + y * cos;
|
|
243
|
+
|
|
244
|
+
return {
|
|
245
|
+
x: view.originPx.x + rx * view.zoom,
|
|
246
|
+
y: view.originPx.y + ry * view.zoom * Math.cos(view.tilt) - z * view.zoom * Math.sin(view.tilt),
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Back-to-front order, so nearer walls are drawn over farther ones.
|
|
252
|
+
*
|
|
253
|
+
* Painter's algorithm: with no depth buffer, draw order IS the depth test. Sort
|
|
254
|
+
* by the rotated depth axis, lowest first — get this backwards and the building
|
|
255
|
+
* renders inside out, which looks like a rendering bug and is really a sort.
|
|
256
|
+
*/
|
|
257
|
+
export function depthOf(point: { x: number; y: number }, view: TView): number {
|
|
258
|
+
return point.x * Math.sin(view.rotation) + point.y * Math.cos(view.rotation);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/** The eight corners of a wall box, in site metres. */
|
|
262
|
+
export function wallCorners(solid: TWallSolid): Array<{ x: number; y: number; z: number }> {
|
|
263
|
+
const hl = solid.lengthM / 2;
|
|
264
|
+
const ht = solid.thicknessM / 2;
|
|
265
|
+
const hh = solid.heightM / 2;
|
|
266
|
+
const cos = Math.cos(solid.yaw);
|
|
267
|
+
const sin = Math.sin(solid.yaw);
|
|
268
|
+
|
|
269
|
+
const corners: Array<{ x: number; y: number; z: number }> = [];
|
|
270
|
+
for (const dz of [-hh, hh]) {
|
|
271
|
+
for (const [dl, dt] of [
|
|
272
|
+
[-hl, -ht],
|
|
273
|
+
[hl, -ht],
|
|
274
|
+
[hl, ht],
|
|
275
|
+
[-hl, ht],
|
|
276
|
+
] as const) {
|
|
277
|
+
corners.push({
|
|
278
|
+
x: solid.centre.x + dl * cos - dt * sin,
|
|
279
|
+
y: solid.centre.y + dl * sin + dt * cos,
|
|
280
|
+
z: solid.centre.z + dz,
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
return corners;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* How big the site is, so a view can be framed without anyone choosing numbers.
|
|
289
|
+
*
|
|
290
|
+
* Returns null for an empty scene rather than a zero-size box: a caller must be
|
|
291
|
+
* able to tell "nothing to show" from "everything is at the origin".
|
|
292
|
+
*/
|
|
293
|
+
export function solidsBounds(solids: TWallSolid[]): {
|
|
294
|
+
minX: number; maxX: number; minY: number; maxY: number;
|
|
295
|
+
} | null {
|
|
296
|
+
if (!solids.length) return null;
|
|
297
|
+
|
|
298
|
+
let minX = Infinity, maxX = -Infinity, minY = Infinity, maxY = -Infinity;
|
|
299
|
+
for (const s of solids) {
|
|
300
|
+
for (const c of wallCorners(s)) {
|
|
301
|
+
if (c.x < minX) minX = c.x;
|
|
302
|
+
if (c.x > maxX) maxX = c.x;
|
|
303
|
+
if (c.y < minY) minY = c.y;
|
|
304
|
+
if (c.y > maxY) maxY = c.y;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
return { minX, maxX, minY, maxY };
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* A view that fits the whole site on the canvas.
|
|
312
|
+
*
|
|
313
|
+
* Falls back to a sane zoom for a degenerate scene — a single wall has zero
|
|
314
|
+
* extent on one axis, and dividing by it would give an infinite zoom and a
|
|
315
|
+
* blank canvas.
|
|
316
|
+
*/
|
|
317
|
+
export function fitView(
|
|
318
|
+
solids: TWallSolid[],
|
|
319
|
+
canvas: { width: number; height: number },
|
|
320
|
+
options?: { rotation?: number; tilt?: number; paddingPx?: number },
|
|
321
|
+
): TView {
|
|
322
|
+
const rotation = options?.rotation ?? Math.PI / 5;
|
|
323
|
+
const tilt = options?.tilt ?? 0.95;
|
|
324
|
+
const padding = options?.paddingPx ?? 32;
|
|
325
|
+
|
|
326
|
+
const base: TView = {
|
|
327
|
+
rotation,
|
|
328
|
+
tilt,
|
|
329
|
+
zoom: 20,
|
|
330
|
+
originPx: { x: canvas.width / 2, y: canvas.height / 2 },
|
|
331
|
+
};
|
|
332
|
+
|
|
333
|
+
const bounds = solidsBounds(solids);
|
|
334
|
+
if (!bounds) return base;
|
|
335
|
+
|
|
336
|
+
const projected = [
|
|
337
|
+
{ x: bounds.minX, y: bounds.minY },
|
|
338
|
+
{ x: bounds.maxX, y: bounds.minY },
|
|
339
|
+
{ x: bounds.maxX, y: bounds.maxY },
|
|
340
|
+
{ x: bounds.minX, y: bounds.maxY },
|
|
341
|
+
].map((p) => projectPoint(p.x, p.y, 0, { ...base, zoom: 1, originPx: { x: 0, y: 0 } }));
|
|
342
|
+
|
|
343
|
+
const spanX = Math.max(...projected.map((p) => p.x)) - Math.min(...projected.map((p) => p.x));
|
|
344
|
+
const spanY = Math.max(...projected.map((p) => p.y)) - Math.min(...projected.map((p) => p.y));
|
|
345
|
+
|
|
346
|
+
const usableW = Math.max(1, canvas.width - padding * 2);
|
|
347
|
+
const usableH = Math.max(1, canvas.height - padding * 2);
|
|
348
|
+
const zoom =
|
|
349
|
+
spanX > 0 && spanY > 0
|
|
350
|
+
? Math.min(usableW / spanX, usableH / spanY)
|
|
351
|
+
: base.zoom;
|
|
352
|
+
|
|
353
|
+
const centre = projectPoint(
|
|
354
|
+
(bounds.minX + bounds.maxX) / 2,
|
|
355
|
+
(bounds.minY + bounds.maxY) / 2,
|
|
356
|
+
0,
|
|
357
|
+
{ ...base, zoom, originPx: { x: 0, y: 0 } },
|
|
358
|
+
);
|
|
359
|
+
|
|
360
|
+
return {
|
|
361
|
+
rotation,
|
|
362
|
+
tilt,
|
|
363
|
+
zoom,
|
|
364
|
+
originPx: { x: canvas.width / 2 - centre.x, y: canvas.height / 2 - centre.y },
|
|
365
|
+
};
|
|
366
|
+
}
|