Initial commit
This commit is contained in:
205
backend/src/routes/devices.ts
Normal file
205
backend/src/routes/devices.ts
Normal file
@@ -0,0 +1,205 @@
|
||||
import { Router, Request, Response } from 'express';
|
||||
import { z } from 'zod';
|
||||
import { deviceService } from '../services/deviceService';
|
||||
|
||||
const router = Router();
|
||||
|
||||
const createDeviceSchema = z.object({
|
||||
name: z.string().min(1, 'Name is required'),
|
||||
ipAddress: z.string().min(1, 'IP address is required'),
|
||||
port: z.number().int().positive().optional(),
|
||||
enabled: z.boolean().optional(),
|
||||
});
|
||||
|
||||
const updateDeviceSchema = z.object({
|
||||
name: z.string().min(1).optional(),
|
||||
ipAddress: z.string().min(1).optional(),
|
||||
port: z.number().int().positive().optional(),
|
||||
enabled: z.boolean().optional(),
|
||||
});
|
||||
|
||||
// GET /api/devices
|
||||
router.get('/', async (req: Request, res: Response) => {
|
||||
try {
|
||||
const devices = await deviceService.getAllDevices();
|
||||
res.json(devices);
|
||||
} catch (error) {
|
||||
console.error('Error fetching devices:', error);
|
||||
res.status(500).json({ error: 'InternalError', message: 'Failed to fetch devices' });
|
||||
}
|
||||
});
|
||||
|
||||
// GET /api/devices/:id
|
||||
router.get('/:id', async (req: Request, res: Response) => {
|
||||
try {
|
||||
const device = await deviceService.getDeviceById(req.params.id);
|
||||
if (!device) {
|
||||
return res.status(404).json({ error: 'NotFound', message: 'Device not found' });
|
||||
}
|
||||
res.json(device);
|
||||
} catch (error) {
|
||||
console.error('Error fetching device:', error);
|
||||
res.status(500).json({ error: 'InternalError', message: 'Failed to fetch device' });
|
||||
}
|
||||
});
|
||||
|
||||
// POST /api/devices
|
||||
router.post('/', async (req: Request, res: Response) => {
|
||||
try {
|
||||
const body = createDeviceSchema.parse(req.body);
|
||||
const device = await deviceService.createDevice(body);
|
||||
res.status(201).json(device);
|
||||
} catch (error) {
|
||||
if (error instanceof z.ZodError) {
|
||||
return res.status(400).json({
|
||||
error: 'ValidationError',
|
||||
details: error.errors.map((e) => ({ field: e.path.join('.'), message: e.message })),
|
||||
});
|
||||
}
|
||||
console.error('Error creating device:', error);
|
||||
res.status(500).json({ error: 'InternalError', message: 'Failed to create device' });
|
||||
}
|
||||
});
|
||||
|
||||
// PUT /api/devices/:id
|
||||
router.put('/:id', async (req: Request, res: Response) => {
|
||||
try {
|
||||
const body = updateDeviceSchema.parse(req.body);
|
||||
const device = await deviceService.updateDevice(req.params.id, body);
|
||||
res.json(device);
|
||||
} catch (error) {
|
||||
if (error instanceof z.ZodError) {
|
||||
return res.status(400).json({
|
||||
error: 'ValidationError',
|
||||
details: error.errors.map((e) => ({ field: e.path.join('.'), message: e.message })),
|
||||
});
|
||||
}
|
||||
if (error instanceof Error && error.message.includes('Record to update not found')) {
|
||||
return res.status(404).json({ error: 'NotFound', message: 'Device not found' });
|
||||
}
|
||||
console.error('Error updating device:', error);
|
||||
res.status(500).json({ error: 'InternalError', message: 'Failed to update device' });
|
||||
}
|
||||
});
|
||||
|
||||
// DELETE /api/devices/:id
|
||||
router.delete('/:id', async (req: Request, res: Response) => {
|
||||
try {
|
||||
await deviceService.deleteDevice(req.params.id);
|
||||
res.status(204).send();
|
||||
} catch (error) {
|
||||
if (error instanceof Error && error.message.includes('Record to delete does not exist')) {
|
||||
return res.status(404).json({ error: 'NotFound', message: 'Device not found' });
|
||||
}
|
||||
console.error('Error deleting device:', error);
|
||||
res.status(500).json({ error: 'InternalError', message: 'Failed to delete device' });
|
||||
}
|
||||
});
|
||||
|
||||
// POST /api/devices/:id/ping
|
||||
router.post('/:id/ping', async (req: Request, res: Response) => {
|
||||
try {
|
||||
const result = await deviceService.pingDevice(req.params.id);
|
||||
if (result.status === 'error') {
|
||||
return res.status(502).json(result);
|
||||
}
|
||||
res.json(result);
|
||||
} catch (error) {
|
||||
if (error instanceof Error && error.message === 'Device not found') {
|
||||
return res.status(404).json({ error: 'NotFound', message: 'Device not found' });
|
||||
}
|
||||
console.error('Error pinging device:', error);
|
||||
res.status(500).json({ error: 'InternalError', message: 'Failed to ping device' });
|
||||
}
|
||||
});
|
||||
|
||||
// GET /api/devices/:id/details
|
||||
router.get('/:id/details', async (req: Request, res: Response) => {
|
||||
try {
|
||||
const details = await deviceService.getDeviceDetails(req.params.id);
|
||||
res.json(details);
|
||||
} catch (error) {
|
||||
if (error instanceof Error && error.message === 'Device not found') {
|
||||
return res.status(404).json({ error: 'NotFound', message: 'Device not found' });
|
||||
}
|
||||
console.error('Error fetching device details:', error);
|
||||
res.status(500).json({ error: 'InternalError', message: 'Failed to fetch device details' });
|
||||
}
|
||||
});
|
||||
|
||||
// GET /api/devices/:id/presets
|
||||
router.get('/:id/presets', async (req: Request, res: Response) => {
|
||||
try {
|
||||
const presets = await deviceService.getDevicePresets(req.params.id);
|
||||
res.json(presets);
|
||||
} catch (error) {
|
||||
if (error instanceof Error && error.message === 'Device not found') {
|
||||
return res.status(404).json({ error: 'NotFound', message: 'Device not found' });
|
||||
}
|
||||
console.error('Error fetching device presets:', error);
|
||||
res.status(500).json({ error: 'InternalError', message: 'Failed to fetch presets' });
|
||||
}
|
||||
});
|
||||
|
||||
// GET /api/devices/status/all
|
||||
router.get('/status/all', async (req: Request, res: Response) => {
|
||||
try {
|
||||
const devices = await deviceService.getAllDevicesWithStatus();
|
||||
res.json(devices);
|
||||
} catch (error) {
|
||||
console.error('Error fetching devices with status:', error);
|
||||
res.status(500).json({ error: 'InternalError', message: 'Failed to fetch devices status' });
|
||||
}
|
||||
});
|
||||
|
||||
// POST /api/devices/:id/turn-on
|
||||
router.post('/:id/turn-on', async (req: Request, res: Response) => {
|
||||
try {
|
||||
await deviceService.turnOnDevice(req.params.id);
|
||||
res.json({ status: 'ok', message: 'Device turned on' });
|
||||
} catch (error) {
|
||||
if (error instanceof Error && error.message === 'Device not found') {
|
||||
return res.status(404).json({ error: 'NotFound', message: 'Device not found' });
|
||||
}
|
||||
console.error('Error turning on device:', error);
|
||||
res.status(500).json({ error: 'InternalError', message: 'Failed to turn on device' });
|
||||
}
|
||||
});
|
||||
|
||||
// POST /api/devices/:id/turn-off
|
||||
router.post('/:id/turn-off', async (req: Request, res: Response) => {
|
||||
try {
|
||||
await deviceService.turnOffDevice(req.params.id);
|
||||
res.json({ status: 'ok', message: 'Device turned off' });
|
||||
} catch (error) {
|
||||
if (error instanceof Error && error.message === 'Device not found') {
|
||||
return res.status(404).json({ error: 'NotFound', message: 'Device not found' });
|
||||
}
|
||||
console.error('Error turning off device:', error);
|
||||
res.status(500).json({ error: 'InternalError', message: 'Failed to turn off device' });
|
||||
}
|
||||
});
|
||||
|
||||
// POST /api/devices/all/turn-on
|
||||
router.post('/all/turn-on', async (req: Request, res: Response) => {
|
||||
try {
|
||||
const result = await deviceService.turnOnAllDevices();
|
||||
res.json(result);
|
||||
} catch (error) {
|
||||
console.error('Error turning on all devices:', error);
|
||||
res.status(500).json({ error: 'InternalError', message: 'Failed to turn on all devices' });
|
||||
}
|
||||
});
|
||||
|
||||
// POST /api/devices/all/turn-off
|
||||
router.post('/all/turn-off', async (req: Request, res: Response) => {
|
||||
try {
|
||||
const result = await deviceService.turnOffAllDevices();
|
||||
res.json(result);
|
||||
} catch (error) {
|
||||
console.error('Error turning off all devices:', error);
|
||||
res.status(500).json({ error: 'InternalError', message: 'Failed to turn off all devices' });
|
||||
}
|
||||
});
|
||||
|
||||
export default router;
|
||||
Reference in New Issue
Block a user