You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
RTSPtoWeb/API_USAGE.md

371 lines
8.9 KiB
Markdown

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# 摄像头管理 API 使用说明
本文档说明如何使用摄像头管理相关的 REST API 接口。
## 基础配置
### 服务器地址
```
http://localhost:8083 # 默认地址,根据实际配置调整
```
### 认证
如果启用了 HTTP 认证,需要在请求头中添加 Basic Auth
```
Authorization: Basic <base64(username:password)>
```
## API 接口列表
### 1. 获取所有摄像头列表
**请求方式:** `GET`
**请求路径:** `/cameras`
**描述:** 获取所有未删除的摄像头列表
**响应示例:**
```json
{
"cameras": [
{
"camera_id": 1,
"ip": "192.168.1.100",
"port": 554,
"username": "admin",
"password": "password",
"url": "rtsp://admin:password@192.168.1.100:554/stream1",
"camera_produce": "海康威视",
"camera_name": "前门摄像头",
"device_type": "网络摄像头",
"unit_code": "UNIT001",
"nvr_produce": "海康威视",
"nvr_path": "/stream1",
"play_back": "支持",
"del_flag": "0",
"create_by": "admin",
"create_time": "2024-01-01T10:00:00Z",
"update_by": "admin",
"update_time": "2024-01-01T10:00:00Z",
"user_id": 1,
"dept_id": 1,
"stream_status": "online"
}
],
"total": 1
}
```
### 2. 根据单位代码获取摄像头列表
**请求方式:** `GET`
**请求路径:** `/cameras/unit/{unitcode}`
**描述:** 根据单位代码获取对应的摄像头列表
**路径参数:**
- `unitcode`: 单位代码(必填)
**响应示例:**
```json
{
"cameras": [
{
"camera_id": 1,
"ip": "192.168.1.100",
"port": 554,
"username": "admin",
"password": "password",
"url": "rtsp://admin:password@192.168.1.100:554/stream1",
"camera_produce": "海康威视",
"camera_name": "前门摄像头",
"device_type": "网络摄像头",
"unit_code": "UNIT001",
"nvr_produce": "海康威视",
"nvr_path": "/stream1",
"play_back": "支持",
"del_flag": "0",
"create_by": "admin",
"create_time": "2024-01-01T10:00:00Z",
"update_by": "admin",
"update_time": "2024-01-01T10:00:00Z",
"user_id": 1,
"dept_id": 1,
"stream_status": "online"
}
],
"total": 1,
"unit_code": "UNIT001"
}
```
### 3. 其他摄像头管理接口
- `POST /camera/add` - 添加摄像头
- `GET /camera/{uuid}` - 获取单个摄像头信息
- `PUT /camera/{uuid}` - 更新摄像头信息
- `DELETE /camera/{uuid}` - 删除摄像头
- `POST /cameras/refresh` - 刷新摄像头列表
- `GET /database/status` - 获取数据库状态
## 前端调用示例
### JavaScript (原生)
```javascript
// 获取所有摄像头
async function getAllCameras() {
try {
const response = await fetch('http://localhost:8083/cameras', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
// 如果需要认证,添加以下头部
// 'Authorization': 'Basic ' + btoa('username:password')
}
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
console.log('所有摄像头:', data.cameras);
return data;
} catch (error) {
console.error('获取摄像头列表失败:', error);
}
}
// 根据单位代码获取摄像头
async function getCamerasByUnitCode(unitCode) {
try {
const response = await fetch(`http://localhost:8083/cameras/unit/${unitCode}`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
// 如果需要认证,添加以下头部
// 'Authorization': 'Basic ' + btoa('username:password')
}
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
console.log(`单位 ${unitCode} 的摄像头:`, data.cameras);
return data;
} catch (error) {
console.error('获取摄像头列表失败:', error);
}
}
// 使用示例
getAllCameras();
getCamerasByUnitCode('UNIT001');
```
### jQuery
```javascript
// 获取所有摄像头
function getAllCameras() {
$.ajax({
url: 'http://localhost:8083/cameras',
type: 'GET',
dataType: 'json',
// 如果需要认证
// beforeSend: function(xhr) {
// xhr.setRequestHeader('Authorization', 'Basic ' + btoa('username:password'));
// },
success: function(data) {
console.log('所有摄像头:', data.cameras);
// 处理数据
},
error: function(xhr, status, error) {
console.error('获取摄像头列表失败:', error);
}
});
}
// 根据单位代码获取摄像头
function getCamerasByUnitCode(unitCode) {
$.ajax({
url: `http://localhost:8083/cameras/unit/${unitCode}`,
type: 'GET',
dataType: 'json',
// 如果需要认证
// beforeSend: function(xhr) {
// xhr.setRequestHeader('Authorization', 'Basic ' + btoa('username:password'));
// },
success: function(data) {
console.log(`单位 ${unitCode} 的摄像头:`, data.cameras);
// 处理数据
},
error: function(xhr, status, error) {
console.error('获取摄像头列表失败:', error);
}
});
}
```
### Axios
```javascript
import axios from 'axios';
// 配置基础URL和认证
const api = axios.create({
baseURL: 'http://localhost:8083',
headers: {
'Content-Type': 'application/json',
// 如果需要认证
// 'Authorization': 'Basic ' + btoa('username:password')
}
});
// 获取所有摄像头
export const getAllCameras = async () => {
try {
const response = await api.get('/cameras');
return response.data;
} catch (error) {
console.error('获取摄像头列表失败:', error);
throw error;
}
};
// 根据单位代码获取摄像头
export const getCamerasByUnitCode = async (unitCode) => {
try {
const response = await api.get(`/cameras/unit/${unitCode}`);
return response.data;
} catch (error) {
console.error('获取摄像头列表失败:', error);
throw error;
}
};
```
### Vue.js 组件示例
```vue
<template>
<div>
<h2>摄像头管理</h2>
<!-- 单位代码选择 -->
<div>
<label>选择单位代码:</label>
<select v-model="selectedUnitCode" @change="loadCamerasByUnit">
<option value="">全部</option>
<option value="UNIT001">UNIT001</option>
<option value="UNIT002">UNIT002</option>
</select>
</div>
<!-- 摄像头列表 -->
<div v-if="loading">加载中...</div>
<div v-else>
<h3>摄像头列表 (共 {{ cameras.length }} 个)</h3>
<div v-for="camera in cameras" :key="camera.camera_id" class="camera-item">
<h4>{{ camera.camera_name }}</h4>
<p>IP: {{ camera.ip }}:{{ camera.port }}</p>
<p>单位代码: {{ camera.unit_code }}</p>
<p>状态: <span :class="camera.stream_status">{{ camera.stream_status }}</span></p>
</div>
</div>
</div>
</template>
<script>
import { getAllCameras, getCamerasByUnitCode } from './api';
export default {
name: 'CameraManager',
data() {
return {
cameras: [],
selectedUnitCode: '',
loading: false
};
},
mounted() {
this.loadAllCameras();
},
methods: {
async loadAllCameras() {
this.loading = true;
try {
const data = await getAllCameras();
this.cameras = data.cameras;
} catch (error) {
console.error('加载摄像头失败:', error);
} finally {
this.loading = false;
}
},
async loadCamerasByUnit() {
if (!this.selectedUnitCode) {
this.loadAllCameras();
return;
}
this.loading = true;
try {
const data = await getCamerasByUnitCode(this.selectedUnitCode);
this.cameras = data.cameras;
} catch (error) {
console.error('加载摄像头失败:', error);
} finally {
this.loading = false;
}
}
}
};
</script>
<style scoped>
.camera-item {
border: 1px solid #ddd;
padding: 10px;
margin: 10px 0;
border-radius: 5px;
}
.online {
color: green;
font-weight: bold;
}
.offline {
color: red;
font-weight: bold;
}
</style>
```
## 错误处理
### 常见错误码
- `400 Bad Request` - 请求参数错误
- `401 Unauthorized` - 认证失败
- `500 Internal Server Error` - 服务器内部错误
- `503 Service Unavailable` - 数据库未启用
### 错误响应格式
```json
{
"error": "错误描述信息"
}
```
## 注意事项
1. **数据库配置**:确保在 `config.json` 中正确配置了数据库连接信息
2. **认证**:如果启用了 HTTP 认证,所有请求都需要包含认证头部
3. **CORS**:如果前端和后端不在同一域名下,需要处理跨域问题
4. **流状态**`stream_status` 字段表示摄像头的实时流状态online/offline
5. **单位代码**`unit_code` 字段用于按组织单位过滤摄像头
6. **删除标记**:只返回 `del_flag != '1'` 的摄像头(未删除的摄像头)