-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontrollerRoutes.ts
More file actions
177 lines (159 loc) · 4.6 KB
/
Copy pathcontrollerRoutes.ts
File metadata and controls
177 lines (159 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// src/routes/controllerRoutes.ts
import { Router } from 'express';
import { printerController } from '../controller/printerController';
import { asyncHandler } from '../middleware/asyncHandler';
/**
* Express router providing endpoints for controlling the printer.
*
* These routes are mounted under the `/api` prefix in {@link ../server.ts | server.ts}.
*/
const router = Router();
/**
* POST `/prints/parameterized/coin`
*
* Starts a print job for a parameterised coin.
*
* @param req.body.machineConfigID - Identifier of the machine configuration.
* @param req.body.configSetID - Identifier of the configuration set.
*
* @returns 201 - `{ jobId: string }` when the job is started.
* @returns 500 - `{ error: string }` on failure.
*/
router.post(
'/prints/parameterized/coin',
asyncHandler(async (req, res) => {
const { machineConfigID, configSetID } = req.body;
if (!machineConfigID || !configSetID) {
res
.status(400)
.json({ error: 'machineConfigID and configSetID are required' });
return;
}
const jobId = await printerController.startPrint(machineConfigID, configSetID);
res.status(201).location(jobId).json({ jobId });
})
);
/**
* GET `/prints/parameterized/coin/getJobId`
*
* Retrieves the ID of the currently running print job.
*
* @returns 200 - `{ jobId: string }` when a job is active.
* @returns 404 - `{ error: string }` when no job is running.
* @returns 500 - `{ error: string }` on internal errors.
*/
router.get(
'/prints/parameterized/coin/getJobId',
asyncHandler(async (req, res) => {
const jobId = await printerController.getCurrentJobId();
if (!jobId) {
res.status(404).json({ error: 'No active print job found' });
return;
}
res.json({ jobId });
})
);
/**
* GET `/prints/parameterized/coin/:coinJobId/status`
*
* Returns the status of a running print job.
*
* @returns 200 - `JobStatus` object describing the current progress.
* @returns 500 - `{ error: string }` on internal errors.
*/
router.get(
'/prints/parameterized/coin/:coinJobId/status',
asyncHandler(async (req, res) => {
const { coinJobId } = req.params;
const status = await printerController.getPrintStatus(coinJobId);
res.json(status);
})
);
/**
* PUT `/prints/parameterized/coin/:coinJobId/pause`
*
* Pauses the running print job.
*
* @returns 204 - When the job was paused successfully.
* @returns 500 - `{ error: string }` on internal errors.
*/
router.put(
'/prints/parameterized/coin/:coinJobId/pause',
asyncHandler(async (req, res) => {
const { coinJobId } = req.params;
await printerController.pausePrint(coinJobId);
res.sendStatus(204);
})
);
/**
* PUT `/prints/parameterized/coin/:coinJobId/resume`
*
* Resumes a paused print job.
*
* @returns 204 - When the job was resumed successfully.
* @returns 500 - `{ error: string }` on internal errors.
*/
router.put(
'/prints/parameterized/coin/:coinJobId/resume',
asyncHandler(async (req, res) => {
const { coinJobId } = req.params;
await printerController.resumePrint(coinJobId);
res.sendStatus(204);
})
);
/**
* DELETE `/prints/parameterized/coin/:coinJobId/cancel`
*
* Cancels a running print job.
*
* @returns 204 - When the job was cancelled successfully.
* @returns 500 - `{ error: string }` on internal errors.
*/
router.delete(
'/prints/parameterized/coin/:coinJobId/cancel',
asyncHandler(async (req, res) => {
const { coinJobId } = req.params;
await printerController.cancelPrint(coinJobId);
res.sendStatus(204);
})
);
/**
* PUT `/printer/status`
*
* Starts the warm up or initiates the shutdown procedure depending on the
* provided printer status.
*
* @returns 202 - Accepted when the operation was triggered successfully.
* @returns 400 - `{ error: string }` on validation errors.
* @returns 500 - `{ error: string }` on failure.
*/
router.put(
'/printer/status',
asyncHandler(async (req, res) => {
const { status } = req.body;
if (status !== 'start-up' && status !== 'shutting-down') {
res.status(400).json({
error: "Status must be either 'start-up' or 'shutting-down'",
});
return;
}
await printerController.updatePrinterStatus(status);
res.sendStatus(202);
})
);
/**
* GET `/printer/status`
*
* Returns the current printer status fetched from PrusaLink.
*
* @returns 200 - `{ status: string }` with the mapped status.
* @returns 500 - `{ error: string }` on failure.
*/
router.get(
'/printer/status',
asyncHandler(async (req, res) => {
const status = await printerController.getPrinterStatus();
res.json(status);
})
);
export default router;