chore: initial commit — AdonisJS v7 TR-069 ACS
- Docker infrastructure (Dockerfile, docker-compose.yml, genieacs-sim) - AdonisJS v7 app with Inertia.js + Vue 3 - MongoDB integration - CWMP/TR-069 state machine engine - Configuration engine with compliance tracking - Branding: ADAM ACS (favicon, navbar, locales) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
commit
d1c65dbf1e
164 files changed
+29127
No files matched your search
@@ -0,0 +1,94 @@
|
||||
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.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(',')
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user