- Store lastPeriodicInformAt on device when PERIODIC Inform received - Display 'Prochaine connexion' in DeviceDetail identity card - Fix tsconfig exclude (tests, inertia, coverage) for Docker build - Fix TS errors in tests (CommandResult type, unused imports) - Fix distinctCriteriaValues typing in controller
95 lines
3.4 KiB
TypeScript
95 lines
3.4 KiB
TypeScript
import type { HttpContext } from '@adonisjs/core/http'
|
|
import DeviceService from '#services/device_service'
|
|
import { PaginationData } from '#services/device_service'
|
|
import { CommandType } from '../domain/commands/command.js'
|
|
import DeviceModel from '#models/device'
|
|
|
|
export default class DevicesController {
|
|
private deviceService = new DeviceService()
|
|
|
|
async criteriaValues({ response }: HttpContext) {
|
|
const values = await (DeviceModel as any).distinctCriteriaValues()
|
|
return response.json(values)
|
|
}
|
|
|
|
async index({ request, response }: HttpContext) {
|
|
const offset = Number(request.qs().offset) || 0
|
|
const limit = Number(request.qs().limit) || 100
|
|
|
|
const { data, nbDocuments, pager } = await this.deviceService.getDevices(offset, limit)
|
|
|
|
response.header('x-total-count', nbDocuments.toString())
|
|
response.header('link', this.generateLinkHeader(request, pager))
|
|
return response.json(data)
|
|
}
|
|
|
|
async show({ params, response }: HttpContext) {
|
|
const device = await this.deviceService.getDeviceByIdentifier(params.identifier)
|
|
if (!device) {
|
|
return response.status(404).json({ message: 'Device not found' })
|
|
}
|
|
return response.json(device)
|
|
}
|
|
|
|
async destroy({ params, response }: HttpContext) {
|
|
const deleted = await this.deviceService.deleteByIdentifier(params.identifier)
|
|
if (!deleted) {
|
|
return response.status(404).json({ message: 'Device not found' })
|
|
}
|
|
return response.json({ message: 'Device deleted successfully' })
|
|
}
|
|
|
|
async executeCommand({ params, request, response }: HttpContext) {
|
|
try {
|
|
const body = request.body() as {
|
|
action: CommandType
|
|
parameters?: Record<string, any>
|
|
mode?: string
|
|
priority?: number
|
|
}
|
|
await this.deviceService.executeCommand(params.identifier, body)
|
|
return response.status(201).json({ message: 'Command created' })
|
|
} catch (error: any) {
|
|
if (error.message?.includes('not found')) {
|
|
return response.status(404).json({ message: error.message })
|
|
}
|
|
if (error.message?.includes('Duplicate')) {
|
|
return response.status(409).json({ message: error.message })
|
|
}
|
|
return response.status(400).json({ message: error.message })
|
|
}
|
|
}
|
|
|
|
async refresh({ params, response }: HttpContext) {
|
|
try {
|
|
await this.deviceService.refreshDevice(params.identifier)
|
|
return response.status(200).json({ message: 'Refresh command sent' })
|
|
} catch (error: any) {
|
|
if (error.message?.includes('not found')) {
|
|
return response.status(404).json({ message: error.message })
|
|
}
|
|
return response.status(500).json({ message: error.message })
|
|
}
|
|
}
|
|
|
|
private generateLinkHeader(request: any, pager: PaginationData): string {
|
|
const baseUrl = `${request.protocol()}://${request.host()}${request.url().split('?')[0]}`
|
|
const parts: string[] = []
|
|
|
|
if (pager.first) {
|
|
parts.push(`<${baseUrl}?offset=${pager.first.offset}&limit=${pager.first.limit}>; rel="first"`)
|
|
}
|
|
if (pager.previous) {
|
|
parts.push(`<${baseUrl}?offset=${pager.previous.offset}&limit=${pager.previous.limit}>; rel="prev"`)
|
|
}
|
|
if (pager.next) {
|
|
parts.push(`<${baseUrl}?offset=${pager.next.offset}&limit=${pager.next.limit}>; rel="next"`)
|
|
}
|
|
if (pager.last) {
|
|
parts.push(`<${baseUrl}?offset=${pager.last.offset}&limit=${pager.last.limit}>; rel="last"`)
|
|
}
|
|
|
|
return parts.join(',')
|
|
}
|
|
}
|