Files
nxxmdata/backend/routes/devices.js
2025-09-12 20:08:42 +08:00

369 lines
10 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 设备路由
* @file devices.js
* @description 定义设备相关的API路由
*/
const express = require('express');
const router = express.Router();
const deviceController = require('../controllers/deviceController');
const { verifyToken } = require('../middleware/auth');
// 公开API路由不需要验证token
const publicRoutes = express.Router();
router.use('/public', publicRoutes);
// 公开创建设备接口
publicRoutes.post('/', deviceController.createDevice);
// 公开获取单个设备接口
publicRoutes.get('/:id', deviceController.getDeviceById);
// 公开更新设备接口
publicRoutes.put('/:id', deviceController.updateDevice);
// 公开删除设备接口
publicRoutes.delete('/:id', deviceController.deleteDevice);
// 公开获取设备状态统计接口
publicRoutes.get('/stats/status', deviceController.getDeviceStatsByStatus);
// 公开获取设备类型统计接口
publicRoutes.get('/stats/type', deviceController.getDeviceStatsByType);
// 公开获取所有设备数据
publicRoutes.get('/', deviceController.getAllDevices);
/**
* @swagger
* tags:
* name: Devices
* description: 设备管理API
*/
/**
* @swagger
* /api/devices:
* get:
* summary: 获取所有设备
* tags: [Devices]
* security:
* - bearerAuth: []
* responses:
* 200:
* description: 成功获取设备列表
* content:
* application/json:
* schema:
* type: object
* properties:
* success:
* type: boolean
* example: true
* data:
* type: array
* items:
* $ref: '#/components/schemas/Device'
* 401:
* description: 未授权
* 500:
* description: 服务器错误
*/
router.get('/', verifyToken, deviceController.getAllDevices);
// 根据设备名称搜索设备
router.get('/search', verifyToken, deviceController.searchDevicesByName);
/**
* @swagger
* /api/devices/{id}:
* get:
* summary: 获取单个设备
* tags: [Devices]
* security:
* - bearerAuth: []
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: 设备ID
* responses:
* 200:
* description: 成功获取设备详情
* content:
* application/json:
* schema:
* type: object
* properties:
* success:
* type: boolean
* example: true
* data:
* $ref: '#/components/schemas/Device'
* 401:
* description: 未授权
* 404:
* description: 设备不存在
* 500:
* description: 服务器错误
*/
router.get('/:id', verifyToken, deviceController.getDeviceById);
/**
* @swagger
* /api/devices:
* post:
* summary: 创建设备
* tags: [Devices]
* security:
* - bearerAuth: []
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* required:
* - name
* - type
* - farmId
* properties:
* name:
* type: string
* description: 设备名称
* type:
* type: string
* description: 设备类型
* status:
* type: string
* enum: [online, offline, maintenance]
* description: 设备状态
* farmId:
* type: integer
* description: 所属养殖场ID
* last_maintenance:
* type: string
* format: date-time
* description: 最近维护时间
* installation_date:
* type: string
* format: date-time
* description: 安装日期
* metrics:
* type: object
* description: 设备指标数据
* responses:
* 201:
* description: 设备创建成功
* content:
* application/json:
* schema:
* type: object
* properties:
* success:
* type: boolean
* example: true
* message:
* type: string
* example: 设备创建成功
* data:
* $ref: '#/components/schemas/Device'
* 400:
* description: 请求参数错误
* 401:
* description: 未授权
* 404:
* description: 养殖场不存在
* 500:
* description: 服务器错误
*/
router.post('/', verifyToken, deviceController.createDevice);
/**
* @swagger
* /api/devices/{id}:
* put:
* summary: 更新设备
* tags: [Devices]
* security:
* - bearerAuth: []
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: 设备ID
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* properties:
* name:
* type: string
* description: 设备名称
* type:
* type: string
* description: 设备类型
* status:
* type: string
* enum: [online, offline, maintenance]
* description: 设备状态
* farmId:
* type: integer
* description: 所属养殖场ID
* last_maintenance:
* type: string
* format: date-time
* description: 最近维护时间
* installation_date:
* type: string
* format: date-time
* description: 安装日期
* metrics:
* type: object
* description: 设备指标数据
* responses:
* 200:
* description: 设备更新成功
* content:
* application/json:
* schema:
* type: object
* properties:
* success:
* type: boolean
* example: true
* message:
* type: string
* example: 设备更新成功
* data:
* $ref: '#/components/schemas/Device'
* 400:
* description: 请求参数错误
* 401:
* description: 未授权
* 404:
* description: 设备不存在或养殖场不存在
* 500:
* description: 服务器错误
*/
router.put('/:id', verifyToken, deviceController.updateDevice);
/**
* @swagger
* /api/devices/{id}:
* delete:
* summary: 删除设备
* tags: [Devices]
* security:
* - bearerAuth: []
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: 设备ID
* responses:
* 200:
* description: 设备删除成功
* content:
* application/json:
* schema:
* type: object
* properties:
* success:
* type: boolean
* example: true
* message:
* type: string
* example: 设备删除成功
* 401:
* description: 未授权
* 404:
* description: 设备不存在
* 500:
* description: 服务器错误
*/
router.delete('/:id', verifyToken, deviceController.deleteDevice);
/**
* @swagger
* /api/devices/stats/status:
* get:
* summary: 按状态统计设备数量
* tags: [Devices]
* security:
* - bearerAuth: []
* responses:
* 200:
* description: 成功获取设备状态统计
* content:
* application/json:
* schema:
* type: object
* properties:
* success:
* type: boolean
* example: true
* data:
* type: array
* items:
* type: object
* properties:
* status:
* type: string
* example: online
* count:
* type: integer
* example: 25
* 401:
* description: 未授权
* 500:
* description: 服务器错误
*/
router.get('/stats/status', verifyToken, deviceController.getDeviceStatsByStatus);
/**
* @swagger
* /api/devices/stats/type:
* get:
* summary: 按类型统计设备数量
* tags: [Devices]
* security:
* - bearerAuth: []
* responses:
* 200:
* description: 成功获取设备类型统计
* content:
* application/json:
* schema:
* type: object
* properties:
* success:
* type: boolean
* example: true
* data:
* type: array
* items:
* type: object
* properties:
* type:
* type: string
* example: 温度传感器
* count:
* type: integer
* example: 15
* 401:
* description: 未授权
* 500:
* description: 服务器错误
*/
router.get('/stats/type', verifyToken, deviceController.getDeviceStatsByType);
module.exports = router;