|
2 | 2 | * @fileoverview Spoolman integration routes (config, search, active spool management). |
3 | 3 | */ |
4 | 4 |
|
| 5 | +import { FiveMClient } from '@ghosttypes/ff-api'; |
5 | 6 | import type { Response, Router } from 'express'; |
6 | 7 | import { toAppError } from '../../../utils/error.utils'; |
7 | 8 | import { |
8 | 9 | createValidationError, |
| 10 | + SlotConfigRequestSchema, |
9 | 11 | SpoolClearRequestSchema, |
10 | 12 | SpoolSelectRequestSchema, |
11 | 13 | } from '../../schemas/web-api.schemas'; |
12 | 14 | import type { |
13 | 15 | ActiveSpoolResponse, |
| 16 | + SlotConfigResponse, |
14 | 17 | SpoolmanConfigResponse, |
15 | 18 | SpoolSearchResponse, |
16 | 19 | SpoolSelectResponse, |
@@ -88,6 +91,8 @@ export function registerSpoolmanRoutes(router: Router, deps: RouteDependencies): |
88 | 91 | vendor: spool.filament.vendor?.name || null, |
89 | 92 | material: spool.filament.material || null, |
90 | 93 | colorHex: spool.filament.color_hex || '#808080', |
| 94 | + rawColorHex: spool.filament.color_hex || null, |
| 95 | + multiColorHexes: spool.filament.multi_color_hexes || null, |
91 | 96 | remainingWeight: spool.remaining_weight || 0, |
92 | 97 | remainingLength: spool.remaining_length || 0, |
93 | 98 | archived: spool.archived, |
@@ -215,4 +220,77 @@ export function registerSpoolmanRoutes(router: Router, deps: RouteDependencies): |
215 | 220 | return sendErrorResponse<StandardAPIResponse>(res, 500, appError.message); |
216 | 221 | } |
217 | 222 | }); |
| 223 | + |
| 224 | + router.post('/spoolman/slot-config', async (req: AuthenticatedRequest, res: Response) => { |
| 225 | + try { |
| 226 | + const validation = SlotConfigRequestSchema.safeParse(req.body); |
| 227 | + if (!validation.success) { |
| 228 | + const validationError = createValidationError(validation.error); |
| 229 | + return sendErrorResponse<SlotConfigResponse>(res, 400, validationError.error); |
| 230 | + } |
| 231 | + |
| 232 | + const { contextId, slot, materialName, colorHex, currentMaterial } = validation.data; |
| 233 | + const overrideContextId = contextId || null; |
| 234 | + |
| 235 | + const contextResult = resolveContext(req, deps, { |
| 236 | + overrideContextId, |
| 237 | + requireBackendReady: true, |
| 238 | + requireBackendInstance: true, |
| 239 | + }); |
| 240 | + if (!contextResult.success) { |
| 241 | + return sendErrorResponse<SlotConfigResponse>( |
| 242 | + res, |
| 243 | + contextResult.statusCode, |
| 244 | + contextResult.error |
| 245 | + ); |
| 246 | + } |
| 247 | + |
| 248 | + const { contextId: resolvedContextId, backend } = contextResult; |
| 249 | + if (!backend) { |
| 250 | + return sendErrorResponse<SlotConfigResponse>(res, 503, 'Backend not available'); |
| 251 | + } |
| 252 | + |
| 253 | + if (!deps.backendManager.isFeatureAvailable(resolvedContextId, 'material-station')) { |
| 254 | + return sendErrorResponse<SlotConfigResponse>( |
| 255 | + res, |
| 256 | + 400, |
| 257 | + 'Material station not available on this printer' |
| 258 | + ); |
| 259 | + } |
| 260 | + |
| 261 | + // Resolve the material to write: the client snaps the spool material to the |
| 262 | + // fixed palette; when it does not resolve, keep the slot's current material. |
| 263 | + const materialToWrite = materialName ?? currentMaterial ?? null; |
| 264 | + if (!materialToWrite) { |
| 265 | + return sendErrorResponse<SlotConfigResponse>( |
| 266 | + res, |
| 267 | + 400, |
| 268 | + 'Spool material did not match a known material and the slot has no current material to keep' |
| 269 | + ); |
| 270 | + } |
| 271 | + |
| 272 | + const primaryClient = backend.getPrimaryClient(); |
| 273 | + if (!(primaryClient instanceof FiveMClient)) { |
| 274 | + return sendErrorResponse<SlotConfigResponse>( |
| 275 | + res, |
| 276 | + 400, |
| 277 | + 'Material station control requires new API client' |
| 278 | + ); |
| 279 | + } |
| 280 | + |
| 281 | + const result = await primaryClient.control.configureSlot(slot, materialToWrite, colorHex); |
| 282 | + const response: SlotConfigResponse = { |
| 283 | + success: result, |
| 284 | + slot, |
| 285 | + material: materialToWrite, |
| 286 | + colorHex, |
| 287 | + message: result ? `Slot ${slot} updated` : undefined, |
| 288 | + error: result ? undefined : 'Failed to configure slot', |
| 289 | + }; |
| 290 | + return res.status(result ? 200 : 500).json(response); |
| 291 | + } catch (error) { |
| 292 | + const appError = toAppError(error); |
| 293 | + return sendErrorResponse<SlotConfigResponse>(res, 500, appError.message); |
| 294 | + } |
| 295 | + }); |
218 | 296 | } |
0 commit comments