@7365admin1/layer-common 4.30.0 → 4.32.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 +16 -4
- package/assets/css/robot-hero.css +489 -1
- package/components/Robot/BuildingMap.vue +403 -0
- package/components/Robot/Detail.vue +445 -0
- package/components/Robot/Fleet.vue +3 -1
- package/components/Robot/FloorPlan.vue +43 -0
- package/components/Robot/LiftQueue.vue +247 -0
- package/components/Robot/LivePlan.vue +59 -0
- package/components/Robot/MapEditor.vue +233 -0
- package/components/Robot/Mark.vue +17 -0
- package/components/Robot/MiniMap.vue +120 -0
- package/components/Robot/PatrolRoutes.vue +48 -0
- package/components/Robot/SiteFleet.vue +266 -0
- package/components/Robot/SiteHeader.vue +64 -0
- package/components/Robot/SiteScreen.vue +195 -0
- package/components/Robot/Table.vue +4 -1
- package/composables/useRobotBuilding.ts +66 -0
- package/composables/useRobotLive.ts +18 -0
- package/composables/useRobots.ts +52 -1
- package/package.json +2 -2
- package/utils/robot-building.ts +521 -0
- package/utils/robot-display.ts +520 -9
|
@@ -0,0 +1,521 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HERO phase (d): the rules behind the 3D building map, its editor and the
|
|
3
|
+
* lift queue (design_handoff_robot_fleet/README.md §4, §5). Pure functions -
|
|
4
|
+
* the components only draw what these return, and `robot-building.test.ts`
|
|
5
|
+
* pins every number the handoff states (clamps, presets, zoom bounds, the
|
|
6
|
+
* editor's words, the lift chips).
|
|
7
|
+
*/
|
|
8
|
+
import { layoutProblem, nextFeatureName, popUndo, pushUndo, type TMapPointM } from "./building-map";
|
|
9
|
+
import { floorName } from "./robot-display";
|
|
10
|
+
|
|
11
|
+
/* ================================================================ camera */
|
|
12
|
+
|
|
13
|
+
export type TMapAngle = "iso" | "top" | "side" | "under";
|
|
14
|
+
/** The four presets: [tilt, turn] in degrees, and the button's word. */
|
|
15
|
+
export const MAP_ANGLES: Record<TMapAngle, { tilt: number; turn: number; label: string }> = {
|
|
16
|
+
iso: { tilt: 62, turn: -36, label: "Iso" },
|
|
17
|
+
top: { tilt: 0, turn: 0, label: "Top" },
|
|
18
|
+
side: { tilt: 80, turn: -90, label: "Side" },
|
|
19
|
+
under: { tilt: -62, turn: -36, label: "Under" },
|
|
20
|
+
};
|
|
21
|
+
export const ZOOM_MIN = 0.5;
|
|
22
|
+
export const ZOOM_MAX = 3;
|
|
23
|
+
/** Degrees per pixel dragged: tilt (vertical) and turn (horizontal). */
|
|
24
|
+
export const TILT_PER_PX = 0.4;
|
|
25
|
+
export const TURN_PER_PX = 0.5;
|
|
26
|
+
|
|
27
|
+
export type TMapCamera = {
|
|
28
|
+
zoom: number;
|
|
29
|
+
panX: number;
|
|
30
|
+
panY: number;
|
|
31
|
+
tilt: number;
|
|
32
|
+
turn: number;
|
|
33
|
+
/** The preset in use, or "free" once dragged. */
|
|
34
|
+
angle: TMapAngle | "free";
|
|
35
|
+
/** Auto-orbit (heroOrbit 18s) - only ever runs at Iso. */
|
|
36
|
+
orbit: boolean;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export function defaultCamera(): TMapCamera {
|
|
40
|
+
return { zoom: 1, panX: 0, panY: 0, tilt: 62, turn: -36, angle: "iso", orbit: true };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export const clampZoom = (z: number) => Math.min(ZOOM_MAX, Math.max(ZOOM_MIN, Number.isFinite(z) ? z : 1));
|
|
44
|
+
export const clampTilt = (t: number) => Math.min(90, Math.max(-90, t));
|
|
45
|
+
/** The + / − buttons: a quarter each step, to 2 decimals. */
|
|
46
|
+
export const zoomIn = (z: number) => clampZoom(+(z * 1.25).toFixed(2));
|
|
47
|
+
export const zoomOut = (z: number) => clampZoom(+(z / 1.25).toFixed(2));
|
|
48
|
+
/** The wheel: 10% a notch, up when scrolling up. */
|
|
49
|
+
export const wheelZoom = (z: number, deltaY: number) => clampZoom(+(z * (deltaY < 0 ? 1.1 : 1 / 1.1)).toFixed(3));
|
|
50
|
+
|
|
51
|
+
/** A preset: tilt + turn jump (0.6 s transition in CSS); orbit survives only at Iso. */
|
|
52
|
+
export function presetCamera(cam: TMapCamera, angle: TMapAngle): TMapCamera {
|
|
53
|
+
const a = MAP_ANGLES[angle];
|
|
54
|
+
return { ...cam, tilt: a.tilt, turn: a.turn, angle, orbit: angle === "iso" && cam.orbit };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Reset: zoom 1, centred, Iso. */
|
|
58
|
+
export function resetCamera(cam: TMapCamera): TMapCamera {
|
|
59
|
+
return { ...cam, zoom: 1, panX: 0, panY: 0, tilt: 62, turn: -36, angle: "iso" };
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** The orbit button: toggles, and always returns to Iso (the prototype's). */
|
|
63
|
+
export function toggleOrbit(cam: TMapCamera): TMapCamera {
|
|
64
|
+
return { ...cam, orbit: !cam.orbit, angle: "iso", tilt: 62, turn: -36 };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Pan with shift, the right or the middle button; rotate otherwise.
|
|
69
|
+
* Grabbing to rotate stops the auto-orbit and leaves the preset ("free").
|
|
70
|
+
*/
|
|
71
|
+
export function dragMode(e: { button: number; shiftKey: boolean }): "pan" | "rotate" {
|
|
72
|
+
return e.button === 1 || e.button === 2 || e.shiftKey ? "pan" : "rotate";
|
|
73
|
+
}
|
|
74
|
+
export function startDrag(cam: TMapCamera, mode: "pan" | "rotate"): TMapCamera {
|
|
75
|
+
return mode === "pan" ? cam : { ...cam, orbit: false, angle: "free" };
|
|
76
|
+
}
|
|
77
|
+
export function dragCamera(start: TMapCamera, mode: "pan" | "rotate", dx: number, dy: number): TMapCamera {
|
|
78
|
+
if (mode === "pan") return { ...start, panX: start.panX + dx, panY: start.panY + dy };
|
|
79
|
+
return { ...start, tilt: clampTilt(start.tilt - dy * TILT_PER_PX), turn: start.turn + dx * TURN_PER_PX, orbit: false, angle: "free" };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Turn as a compass reading, 0-359. */
|
|
83
|
+
export const turnDeg = (turn: number) => Math.round(((turn % 360) + 360) % 360) % 360;
|
|
84
|
+
/** "111% · tilt 62° · turn 324°" */
|
|
85
|
+
export function cameraReadout(cam: TMapCamera): string {
|
|
86
|
+
return `${Math.round(cam.zoom * 100)}% · tilt ${Math.round(cam.tilt)}° · turn ${turnDeg(cam.turn)}°`;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Near-flat (Top) draws the planes as a see-through stack: less fill, no pins, faint shaft. */
|
|
90
|
+
export function planeLook(tilt: number) {
|
|
91
|
+
const a = Math.abs(tilt);
|
|
92
|
+
return {
|
|
93
|
+
fill: a < 20 ? "30%" : "88%",
|
|
94
|
+
pinH: a < 20 ? 0 : a < 70 ? 18 : 44,
|
|
95
|
+
pinZ: a < 20 ? 8 : a < 70 ? 20 : 46,
|
|
96
|
+
shaftOpacity: a < 20 ? 0.35 : 1,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** Floor filter: the chosen floor stays, the others fade to 18% (6% seen from the top). */
|
|
101
|
+
export function floorOpacity(tilt: number, focus: number | null, floorNo: number): number {
|
|
102
|
+
if (focus === null || focus === floorNo) return 1;
|
|
103
|
+
return Math.abs(tilt) < 20 ? 0.06 : 0.18;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** Planes 70px apart; the stack is shifted down so its middle sits at the centre. */
|
|
107
|
+
export const FLOOR_GAP_PX = 70;
|
|
108
|
+
export function stackMetrics(floorCount: number) {
|
|
109
|
+
const n = Math.max(1, floorCount);
|
|
110
|
+
return { height: (n - 1) * FLOOR_GAP_PX + 8, shift: -(n - 1) * (FLOOR_GAP_PX / 2) - 40 };
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/* ================================================================ floors */
|
|
114
|
+
|
|
115
|
+
/** Every floor the building knows: robots' floors, the map's levels, lifts, doors. Lowest first; L1 when nothing is known. */
|
|
116
|
+
export function buildingFloors(robotFloors: number[], map?: Record<string, any> | null): number[] {
|
|
117
|
+
const set = new Set<number>(robotFloors.filter((f) => Number.isInteger(f)));
|
|
118
|
+
for (const l of map?.levels ?? []) if (Number.isInteger(l?.floorNo)) set.add(l.floorNo);
|
|
119
|
+
for (const d of map?.doors ?? []) if (Number.isInteger(d?.floorNo)) set.add(d.floorNo);
|
|
120
|
+
for (const lift of map?.lifts ?? []) for (const f of lift?.floors ?? []) if (Number.isInteger(f)) set.add(f);
|
|
121
|
+
return set.size ? [...set].sort((a, b) => a - b) : [1];
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/* ============================================================ plan frame */
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* The HERO plan is drawn in shares of the floor (0-100); the API stores site
|
|
128
|
+
* metres. The plan frame is fixed at 40 x 26 m (the 400 x 260 plane's shape),
|
|
129
|
+
* so a saved shape lands in the same place every time it is opened.
|
|
130
|
+
* ponytail: fixed frame; use the robot's reported plan extent once brands
|
|
131
|
+
* report one (HERO-PENDING_INTEGRATIONS "Plan position").
|
|
132
|
+
*/
|
|
133
|
+
export const PLAN_W_M = 40;
|
|
134
|
+
export const PLAN_H_M = 26;
|
|
135
|
+
const round2 = (n: number) => Math.round(n * 100) / 100;
|
|
136
|
+
export const toMetres = (p: { x: number; y: number }): TMapPointM => ({ x: round2((p.x / 100) * PLAN_W_M), y: round2((p.y / 100) * PLAN_H_M) });
|
|
137
|
+
export const toShare = (p: { x: number; y: number }) => ({ x: round2((p.x / PLAN_W_M) * 100), y: round2((p.y / PLAN_H_M) * 100) });
|
|
138
|
+
|
|
139
|
+
/* ================================================================ editor */
|
|
140
|
+
|
|
141
|
+
export type TEditorTool = "wall" | "zone" | "door" | "lift" | "move" | "delete";
|
|
142
|
+
export type TShapeKind = "wall" | "zone" | "door" | "lift";
|
|
143
|
+
|
|
144
|
+
/** One drawn element, in plan shares. A lift runs through `floors`; the rest sit on `floorNo`. */
|
|
145
|
+
export type TEditorShape = {
|
|
146
|
+
id: string;
|
|
147
|
+
kind: TShapeKind;
|
|
148
|
+
floorNo: number;
|
|
149
|
+
points: Array<{ x: number; y: number }>;
|
|
150
|
+
name?: string;
|
|
151
|
+
floors?: number[];
|
|
152
|
+
/** What the API had that the editor does not draw (door type), kept on save. */
|
|
153
|
+
meta?: Record<string, unknown>;
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
export type TEditorState = {
|
|
157
|
+
shapes: TEditorShape[];
|
|
158
|
+
undo: string[];
|
|
159
|
+
/** The shape Move picked (outlined), waiting for its destination. */
|
|
160
|
+
moving: string | null;
|
|
161
|
+
notice: { text: string; ok: boolean } | null;
|
|
162
|
+
dirty: boolean;
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
export type TEditorAction =
|
|
166
|
+
| { type: "place"; tool: TEditorTool; floorNo: number; x: number; y: number; zoneName?: string; floors?: number[]; id?: string }
|
|
167
|
+
| { type: "pick"; id: string; tool: TEditorTool; zoneName?: string }
|
|
168
|
+
| { type: "rename"; name: string }
|
|
169
|
+
| { type: "tool" }
|
|
170
|
+
| { type: "undo" }
|
|
171
|
+
| { type: "notice"; text: string; ok: boolean }
|
|
172
|
+
| { type: "saved"; text: string }
|
|
173
|
+
| { type: "load"; shapes: TEditorShape[] };
|
|
174
|
+
|
|
175
|
+
/** The handoff's default sizes (shares of the plan): wall 16 x 1.5, zone 22 x 24, door 3, lift 8 x 14. */
|
|
176
|
+
export const SHAPE_SIZE: Record<TShapeKind, { w: number; h: number }> = {
|
|
177
|
+
wall: { w: 16, h: 1.5 },
|
|
178
|
+
zone: { w: 22, h: 24 },
|
|
179
|
+
door: { w: 3, h: 3 },
|
|
180
|
+
lift: { w: 8, h: 14 },
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
export const TOOL_HINTS: Record<TEditorTool, string> = {
|
|
184
|
+
wall: "Click to place a wall segment.",
|
|
185
|
+
zone: "Click to place a zone, then name it.",
|
|
186
|
+
door: "Click a wall position to place a door.",
|
|
187
|
+
lift: "Click to place a lift shaft; it runs through every floor.",
|
|
188
|
+
move: "Click a shape, then click where it should go.",
|
|
189
|
+
delete: "Click a shape to delete it.",
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
const clampShare = (n: number) => Math.max(0, Math.min(98, n));
|
|
193
|
+
const rect = (x: number, y: number, w: number, h: number) => [
|
|
194
|
+
{ x, y },
|
|
195
|
+
{ x: x + w, y },
|
|
196
|
+
{ x: x + w, y: y + h },
|
|
197
|
+
{ x, y: y + h },
|
|
198
|
+
];
|
|
199
|
+
|
|
200
|
+
/** The shape a click drops at (x, y): its top-left there, except a door, which centres on the click. */
|
|
201
|
+
export function newShape(kind: TShapeKind, id: string, floorNo: number, x: number, y: number, extra: { name?: string; floors?: number[] } = {}): TEditorShape {
|
|
202
|
+
const px = clampShare(x);
|
|
203
|
+
const py = clampShare(y);
|
|
204
|
+
const { w, h } = SHAPE_SIZE[kind];
|
|
205
|
+
const points =
|
|
206
|
+
kind === "wall" ? [{ x: px, y: py + h / 2 }, { x: Math.min(100, px + w), y: py + h / 2 }]
|
|
207
|
+
: kind === "door" ? [{ x: Math.max(0, px - w / 2), y: py }, { x: Math.min(100, px + w / 2), y: py }]
|
|
208
|
+
: rect(px, py, Math.min(w, 100 - px), Math.min(h, 100 - py));
|
|
209
|
+
const shape: TEditorShape = { id, kind, floorNo, points };
|
|
210
|
+
if (kind === "zone") shape.name = (extra.name ?? "").trim();
|
|
211
|
+
if (kind === "lift") shape.floors = extra.floors ?? [floorNo];
|
|
212
|
+
return shape;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export function shapeBox(s: TEditorShape) {
|
|
216
|
+
const xs = s.points.map((p) => p.x);
|
|
217
|
+
const ys = s.points.map((p) => p.y);
|
|
218
|
+
const x = Math.min(...xs);
|
|
219
|
+
const y = Math.min(...ys);
|
|
220
|
+
return { x, y, w: Math.max(...xs) - x, h: Math.max(...ys) - y };
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** A shape's anchor: a door's middle, a wall's start (top of its bar), otherwise the top-left corner. */
|
|
224
|
+
function anchorOf(s: TEditorShape) {
|
|
225
|
+
if (s.kind === "door") {
|
|
226
|
+
const [a, b] = s.points;
|
|
227
|
+
return { x: ((a?.x ?? 0) + (b?.x ?? 0)) / 2, y: ((a?.y ?? 0) + (b?.y ?? 0)) / 2 };
|
|
228
|
+
}
|
|
229
|
+
const box = shapeBox(s);
|
|
230
|
+
return { x: box.x, y: s.kind === "wall" ? Math.min(...s.points.map((p) => p.y)) - SHAPE_SIZE.wall.h / 2 : box.y };
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/** Where the shape is listed and named in messages, in whole shares ("34, 51"). */
|
|
234
|
+
export function shapeAnchor(s: TEditorShape) {
|
|
235
|
+
const a = anchorOf(s);
|
|
236
|
+
return { x: Math.round(a.x), y: Math.round(a.y) };
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** "Wall" · "Door" · "Lift shaft (all floors)" · "Zone · Lobby" / "Zone · unnamed" */
|
|
240
|
+
export function shapeTitle(s: TEditorShape): string {
|
|
241
|
+
if (s.kind === "zone") return `Zone · ${s.name || "unnamed"}`;
|
|
242
|
+
if (s.kind === "lift") return "Lift shaft (all floors)";
|
|
243
|
+
return s.kind === "door" ? "Door" : "Wall";
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/** Moved so its anchor lands on (x, y), kept inside the plan. */
|
|
247
|
+
export function moveShape(s: TEditorShape, x: number, y: number): TEditorShape {
|
|
248
|
+
const a = anchorOf(s);
|
|
249
|
+
const b = shapeBox(s);
|
|
250
|
+
let dx = clampShare(x) - a.x;
|
|
251
|
+
let dy = clampShare(y) - a.y;
|
|
252
|
+
dx = Math.max(-b.x, Math.min(100 - (b.x + b.w), dx));
|
|
253
|
+
dy = Math.max(-b.y, Math.min(100 - (b.y + b.h), dy));
|
|
254
|
+
return { ...s, points: s.points.map((p) => ({ x: round2(p.x + dx), y: round2(p.y + dy) })) };
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/** The shapes on one floor: its own, plus every lift shaft (they run through all floors). */
|
|
258
|
+
export const shapesOnFloor = (shapes: TEditorShape[], floorNo: number) =>
|
|
259
|
+
shapes.filter((s) => s.kind === "lift" || s.floorNo === floorNo);
|
|
260
|
+
|
|
261
|
+
export function emptyEditor(shapes: TEditorShape[] = []): TEditorState {
|
|
262
|
+
return { shapes, undo: [], moving: null, notice: null, dirty: false };
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function commit(state: TEditorState, shapes: TEditorShape[]): TEditorState {
|
|
266
|
+
const undo = [...state.undo];
|
|
267
|
+
pushUndo(undo, state.shapes);
|
|
268
|
+
return { ...state, shapes, undo, notice: null, dirty: true };
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
let seq = 0;
|
|
272
|
+
const nextId = () => `sh${Date.now().toString(36)}${(seq++).toString(36)}`;
|
|
273
|
+
|
|
274
|
+
export function editorReduce(state: TEditorState, action: TEditorAction): TEditorState {
|
|
275
|
+
switch (action.type) {
|
|
276
|
+
case "load":
|
|
277
|
+
return emptyEditor(action.shapes);
|
|
278
|
+
case "tool":
|
|
279
|
+
return { ...state, moving: null, notice: null };
|
|
280
|
+
case "notice":
|
|
281
|
+
return { ...state, notice: { text: action.text, ok: action.ok } };
|
|
282
|
+
case "saved":
|
|
283
|
+
return { ...state, undo: [], moving: null, dirty: false, notice: { text: action.text, ok: true } };
|
|
284
|
+
case "undo": {
|
|
285
|
+
if (!state.undo.length) return { ...state, notice: { text: "Nothing to undo.", ok: false } };
|
|
286
|
+
const undo = [...state.undo];
|
|
287
|
+
const shapes = popUndo<TEditorShape[]>(undo) ?? state.shapes;
|
|
288
|
+
return { ...state, shapes, undo, moving: null, notice: null, dirty: true };
|
|
289
|
+
}
|
|
290
|
+
case "rename": {
|
|
291
|
+
// Typing in the Zone field renames the zone Move has picked (no undo step per key).
|
|
292
|
+
const target = state.shapes.find((s) => s.id === state.moving);
|
|
293
|
+
if (!target || target.kind !== "zone") return state;
|
|
294
|
+
return { ...state, dirty: true, shapes: state.shapes.map((s) => (s.id === target.id ? { ...s, name: action.name.trim() } : s)) };
|
|
295
|
+
}
|
|
296
|
+
case "pick": {
|
|
297
|
+
const shape = state.shapes.find((s) => s.id === action.id);
|
|
298
|
+
if (!shape) return state;
|
|
299
|
+
if (action.tool === "delete") return commit(state, state.shapes.filter((s) => s.id !== shape.id));
|
|
300
|
+
if (action.tool === "move") return { ...state, moving: shape.id, notice: null };
|
|
301
|
+
// Zone tool on a zone: apply the typed name ("click the zone again to apply it").
|
|
302
|
+
if (action.tool === "zone" && shape.kind === "zone" && (action.zoneName ?? "").trim()) {
|
|
303
|
+
return commit(state, state.shapes.map((s) => (s.id === shape.id ? { ...s, name: (action.zoneName ?? "").trim() } : s)));
|
|
304
|
+
}
|
|
305
|
+
return state;
|
|
306
|
+
}
|
|
307
|
+
case "place": {
|
|
308
|
+
if (action.tool === "move") {
|
|
309
|
+
const target = state.shapes.find((s) => s.id === state.moving);
|
|
310
|
+
if (!target) return state;
|
|
311
|
+
return { ...commit(state, state.shapes.map((s) => (s.id === target.id ? moveShape(s, action.x, action.y) : s))), moving: null };
|
|
312
|
+
}
|
|
313
|
+
if (action.tool === "delete") return state;
|
|
314
|
+
const shape = newShape(action.tool, action.id ?? nextId(), action.floorNo, action.x, action.y, {
|
|
315
|
+
name: action.zoneName,
|
|
316
|
+
floors: action.floors,
|
|
317
|
+
});
|
|
318
|
+
return commit(state, [...state.shapes, shape]);
|
|
319
|
+
}
|
|
320
|
+
default:
|
|
321
|
+
return state;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Why Save must wait, in plain words - or null. The handoff's sentence for
|
|
327
|
+
* an unnamed zone; then what the API would refuse (a lift on one floor,
|
|
328
|
+
* shapes past its limits).
|
|
329
|
+
*/
|
|
330
|
+
export function editorProblem(state: TEditorState): string | null {
|
|
331
|
+
const unnamed = state.shapes.find((s) => s.kind === "zone" && !s.name);
|
|
332
|
+
if (unnamed) {
|
|
333
|
+
const a = shapeAnchor(unnamed);
|
|
334
|
+
return `Can't save yet: the zone on ${floorName(unnamed.floorNo)} at (${a.x}, ${a.y}) has no name. Type a name in the Zone field, then click the zone again to apply it.`;
|
|
335
|
+
}
|
|
336
|
+
const lonely = state.shapes.find((s) => s.kind === "lift" && (s.floors?.length ?? 0) < 2);
|
|
337
|
+
if (lonely) {
|
|
338
|
+
const a = shapeAnchor(lonely);
|
|
339
|
+
return `Can't save yet: the lift shaft at (${a.x}, ${a.y}) serves only one floor. A lift needs at least two floors - this building's robots report only ${floorName(lonely.floorNo)}.`;
|
|
340
|
+
}
|
|
341
|
+
const long = state.shapes.find((s) => s.kind === "zone" && (s.name ?? "").length > 60);
|
|
342
|
+
if (long) return `Can't save yet: the zone name "${long.name!.slice(0, 20)}…" is too long. Use 60 characters or fewer.`;
|
|
343
|
+
if (!state.dirty) return "Nothing to save — the map hasn't changed.";
|
|
344
|
+
const body = toMapBody(state.shapes, "check");
|
|
345
|
+
const walls = body.levels.flatMap((l) => (l.walls ?? []).map((w) => ({ floorNo: l.floorNo, ...w })));
|
|
346
|
+
const zones = body.levels.flatMap((l) => (l.zones ?? []).map((z) => ({ floorNo: l.floorNo, ...z })));
|
|
347
|
+
const problem = layoutProblem(walls, zones);
|
|
348
|
+
return problem ? `Can't save yet: ${problem}` : null;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/** "Saved at 14:02 · 5 elements on Tower A." */
|
|
352
|
+
export function savedNotice(at: Date, count: number, siteName: string): string {
|
|
353
|
+
const hh = String(at.getHours()).padStart(2, "0");
|
|
354
|
+
const mm = String(at.getMinutes()).padStart(2, "0");
|
|
355
|
+
return `Saved at ${hh}:${mm} · ${count} ${count === 1 ? "element" : "elements"} on ${siteName || "this site"}.`;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/** A failed save, in plain words: the server's own sentence where it gave one. */
|
|
359
|
+
export function saveErrorText(e: any): string {
|
|
360
|
+
const status = e?.status ?? e?.statusCode ?? e?.response?.status;
|
|
361
|
+
const msg = e?.data?.message ?? e?.response?._data?.message;
|
|
362
|
+
if (status === 409) return "Someone else saved this map after you opened it. Your changes are still here - reload the page to see theirs, then make yours again.";
|
|
363
|
+
if (status === 401) return "Your session has ended. Sign in again, then save - your changes stay on this page until you leave it.";
|
|
364
|
+
if (status === 403 || status === 404) return "You can't change this building's map. Ask a manager with robot set-up rights to save it.";
|
|
365
|
+
if (typeof msg === "string" && msg.trim()) return `The map was not saved: ${msg.trim()}`;
|
|
366
|
+
return "The map was not saved - the server could not be reached. Your changes are still here; try Save again.";
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/* ---- API <-> editor ---- */
|
|
370
|
+
|
|
371
|
+
type TMapBody = {
|
|
372
|
+
name: string;
|
|
373
|
+
levels: Array<{ floorNo: number; name?: string; elevationM?: number; walls?: Array<{ start: TMapPointM; end: TMapPointM }>; zones?: Array<{ name: string; points: TMapPointM[] }> }>;
|
|
374
|
+
doors: Array<{ name: string; floorNo: number; type: "hinged" | "sliding"; start: TMapPointM; end: TMapPointM }>;
|
|
375
|
+
lifts: Array<{ name: string; x: number; y: number; widthM: number; depthM: number; floors: number[] }>;
|
|
376
|
+
};
|
|
377
|
+
|
|
378
|
+
/** A saved building map as editor shapes (plan shares). */
|
|
379
|
+
export function shapesFromMap(map: Record<string, any> | null | undefined): TEditorShape[] {
|
|
380
|
+
if (!map) return [];
|
|
381
|
+
const out: TEditorShape[] = [];
|
|
382
|
+
let i = 0;
|
|
383
|
+
const id = (k: string) => `${k}${i++}`;
|
|
384
|
+
for (const l of map.levels ?? []) {
|
|
385
|
+
for (const w of l.walls ?? []) out.push({ id: id("w"), kind: "wall", floorNo: l.floorNo, points: [toShare(w.start), toShare(w.end)] });
|
|
386
|
+
for (const z of l.zones ?? []) out.push({ id: id("z"), kind: "zone", floorNo: l.floorNo, name: z.name ?? "", points: (z.points ?? []).map(toShare) });
|
|
387
|
+
}
|
|
388
|
+
for (const d of map.doors ?? []) {
|
|
389
|
+
out.push({ id: id("d"), kind: "door", floorNo: d.floorNo, points: [toShare(d.start), toShare(d.end)], meta: { name: d.name, type: d.type } });
|
|
390
|
+
}
|
|
391
|
+
for (const lift of map.lifts ?? []) {
|
|
392
|
+
const w = (lift.widthM / PLAN_W_M) * 100;
|
|
393
|
+
const h = (lift.depthM / PLAN_H_M) * 100;
|
|
394
|
+
const c = toShare({ x: lift.x, y: lift.y });
|
|
395
|
+
const floors = [...(lift.floors ?? [])].sort((a: number, b: number) => a - b);
|
|
396
|
+
out.push({ id: id("l"), kind: "lift", floorNo: floors[0] ?? 1, floors, points: rect(round2(c.x - w / 2), round2(c.y - h / 2), round2(w), round2(h)), meta: { name: lift.name } });
|
|
397
|
+
}
|
|
398
|
+
return out;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* The editor's shapes as the body the building-map API validates. Levels the
|
|
403
|
+
* map already had keep their name and height; doors keep their name and type;
|
|
404
|
+
* lifts keep their name (the queue's key). New doors and lifts get the next
|
|
405
|
+
* free name ("Door 3", "Lift B").
|
|
406
|
+
*/
|
|
407
|
+
export function toMapBody(shapes: TEditorShape[], name: string, base?: Record<string, any> | null): TMapBody {
|
|
408
|
+
const levels = new Map<number, TMapBody["levels"][number]>();
|
|
409
|
+
for (const l of base?.levels ?? []) {
|
|
410
|
+
const keep: TMapBody["levels"][number] = { floorNo: l.floorNo };
|
|
411
|
+
if (typeof l.name === "string" && l.name) keep.name = l.name;
|
|
412
|
+
if (typeof l.elevationM === "number") keep.elevationM = l.elevationM;
|
|
413
|
+
levels.set(l.floorNo, keep);
|
|
414
|
+
}
|
|
415
|
+
const level = (f: number) => {
|
|
416
|
+
if (!levels.has(f)) levels.set(f, { floorNo: f });
|
|
417
|
+
return levels.get(f)!;
|
|
418
|
+
};
|
|
419
|
+
const doorNames: string[] = shapes.filter((s) => s.kind === "door" && s.meta?.name).map((s) => String(s.meta!.name));
|
|
420
|
+
const liftNames: string[] = shapes.filter((s) => s.kind === "lift" && s.meta?.name).map((s) => String(s.meta!.name));
|
|
421
|
+
const body: TMapBody = { name, levels: [], doors: [], lifts: [] };
|
|
422
|
+
for (const s of shapes) {
|
|
423
|
+
if (s.kind === "wall") (level(s.floorNo).walls ??= []).push({ start: toMetres(s.points[0]!), end: toMetres(s.points[1]!) });
|
|
424
|
+
else if (s.kind === "zone") (level(s.floorNo).zones ??= []).push({ name: s.name ?? "", points: s.points.map(toMetres) });
|
|
425
|
+
else if (s.kind === "door") {
|
|
426
|
+
let n = s.meta?.name ? String(s.meta.name) : "";
|
|
427
|
+
if (!n) doorNames.push((n = nextFeatureName(doorNames, "door")));
|
|
428
|
+
body.doors.push({ name: n, floorNo: s.floorNo, type: s.meta?.type === "sliding" ? "sliding" : "hinged", start: toMetres(s.points[0]!), end: toMetres(s.points[1]!) });
|
|
429
|
+
level(s.floorNo);
|
|
430
|
+
} else {
|
|
431
|
+
let n = s.meta?.name ? String(s.meta.name) : "";
|
|
432
|
+
if (!n) liftNames.push((n = nextFeatureName(liftNames, "lift")));
|
|
433
|
+
const b = shapeBox(s);
|
|
434
|
+
const c = toMetres({ x: b.x + b.w / 2, y: b.y + b.h / 2 });
|
|
435
|
+
body.lifts.push({
|
|
436
|
+
name: n,
|
|
437
|
+
x: c.x,
|
|
438
|
+
y: c.y,
|
|
439
|
+
widthM: Math.min(10, Math.max(0.5, round2((b.w / 100) * PLAN_W_M))),
|
|
440
|
+
depthM: Math.min(10, Math.max(0.5, round2((b.h / 100) * PLAN_H_M))),
|
|
441
|
+
floors: [...new Set(s.floors ?? [s.floorNo])].sort((a, b2) => a - b2),
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
body.levels = [...levels.values()].sort((a, b) => a.floorNo - b.floorNo);
|
|
446
|
+
return body;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
/* ============================================================ lift queue */
|
|
450
|
+
|
|
451
|
+
export type TLiftRowView = { key: string; n: number; robot: string; robotId: string; from: string; to: string; fromNo: number; toNo: number; wait: string; state: "requested" | "riding" | "released" | "timed out" };
|
|
452
|
+
|
|
453
|
+
const secs = (ms: number) => {
|
|
454
|
+
const s = Math.max(0, Math.round(ms / 1000));
|
|
455
|
+
return s < 180 ? `${s} s` : `${Math.round(s / 60)} min`;
|
|
456
|
+
};
|
|
457
|
+
const ms = (v: unknown) => (v ? new Date(v as string).getTime() : NaN);
|
|
458
|
+
|
|
459
|
+
/** One lift's rows: who rides, who waits (oldest first), then rides that just ended (up to 3). */
|
|
460
|
+
export function liftQueueRows(lift: Record<string, any>, now = Date.now()): TLiftRowView[] {
|
|
461
|
+
const rows: Omit<TLiftRowView, "n">[] = [];
|
|
462
|
+
const row = (s: any, state: TLiftRowView["state"], wait: string) =>
|
|
463
|
+
rows.push({
|
|
464
|
+
key: String(s._id ?? `${state}-${rows.length}`),
|
|
465
|
+
robot: String(s.robotName || "Robot"),
|
|
466
|
+
robotId: String(s.robotId ?? ""),
|
|
467
|
+
from: floorName(s.fromFloor),
|
|
468
|
+
to: floorName(s.toFloor),
|
|
469
|
+
fromNo: Number(s.fromFloor),
|
|
470
|
+
toNo: Number(s.toFloor),
|
|
471
|
+
wait,
|
|
472
|
+
state,
|
|
473
|
+
});
|
|
474
|
+
if (lift?.active) row(lift.active, "riding", "on board");
|
|
475
|
+
for (const q of lift?.queue ?? []) row(q, "requested", secs(now - ms(q.requestedAt)));
|
|
476
|
+
for (const r of (lift?.recent ?? []).slice(0, 3)) {
|
|
477
|
+
if (r.status === "expired") row(r, "timed out", secs(ms(r.closedAt) - ms(r.requestedAt)));
|
|
478
|
+
else row(r, "released", "done");
|
|
479
|
+
}
|
|
480
|
+
return rows.map((r, i) => ({ ...r, n: i + 1 }));
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
/** Rail geometry: 44px per floor, 12px margin; the car is 30px tall. */
|
|
484
|
+
const ROW_H = 44;
|
|
485
|
+
const CAR_H = ROW_H - 14;
|
|
486
|
+
|
|
487
|
+
/** The whole card: header words, car status, shaft geometry, rows (README §5). */
|
|
488
|
+
export function liftCard(lift: Record<string, any>, now = Date.now()) {
|
|
489
|
+
const rows = liftQueueRows(lift, now);
|
|
490
|
+
const floors = [...new Set<number>((lift?.floors ?? []).map(Number).filter(Number.isFinite))].sort((a, b) => a - b);
|
|
491
|
+
const n = Math.max(1, floors.length);
|
|
492
|
+
const topOf = (f: number) => 12 + (n - 1 - Math.max(0, floors.indexOf(f))) * ROW_H;
|
|
493
|
+
const riding = rows.find((r) => r.state === "riding") ?? null;
|
|
494
|
+
const waiting = rows.filter((r) => r.state === "requested");
|
|
495
|
+
const idleFloor = Number(lift?.state?.currentFloor);
|
|
496
|
+
const rest = floors.includes(idleFloor) ? idleFloor : floors[0] ?? 1;
|
|
497
|
+
const from = riding ? riding.fromNo : rest;
|
|
498
|
+
const to = riding ? riding.toNo : rest;
|
|
499
|
+
return {
|
|
500
|
+
name: String(lift?.liftName ?? "Lift"),
|
|
501
|
+
sub: rows.length ? `${riding ? 1 : 0} riding · ${waiting.length} waiting` : "idle",
|
|
502
|
+
carText: riding ? `Car · ${riding.robot} · ${riding.from} → ${riding.to}` : "Car idle",
|
|
503
|
+
carLabel: riding ? riding.robot.replace(/^HERO-/, "") : "",
|
|
504
|
+
carTone: riding ? "ink" : rows.some((r) => r.state === "timed out") ? "accent" : "neutral",
|
|
505
|
+
riding: !!riding,
|
|
506
|
+
empty: !rows.length,
|
|
507
|
+
shaftH: (n - 1) * ROW_H + 24,
|
|
508
|
+
carH: CAR_H,
|
|
509
|
+
fromTop: topOf(from) - CAR_H / 2,
|
|
510
|
+
toTop: topOf(to) - CAR_H / 2,
|
|
511
|
+
rails: [...floors].reverse().map((f) => ({
|
|
512
|
+
floorNo: f,
|
|
513
|
+
name: floorName(f),
|
|
514
|
+
top: topOf(f),
|
|
515
|
+
lit: !!riding && (f === riding.fromNo || f === riding.toNo),
|
|
516
|
+
waiting: waiting.filter((w) => w.fromNo === f).map((w) => w.robot),
|
|
517
|
+
})),
|
|
518
|
+
rows,
|
|
519
|
+
simulated: lift?.simulated === true,
|
|
520
|
+
};
|
|
521
|
+
}
|