更新规则方法调用,添加对应类型定义

This commit is contained in:
龚皓 2024-11-22 17:54:24 +08:00
parent 11ea95b902
commit 60f2a7f196
1 changed files with 116 additions and 11 deletions

View File

@ -1,4 +1,6 @@
import axios from 'axios';
import type { CameraData } from '@/types/CameraData'
import type { RuleData } from '@/types/RuleData'
class BoxApi {
@ -14,10 +16,14 @@ class BoxApi {
private readonly apiAlgorithms: string = "/algorithms";
private readonly apiResetUser: string = "/auth/resetuser";
private readonly apiResetPassword: string = "/auth/reset_password";
private readonly getMinCamerasApi: string = "/camera/cameras/get_all";
private readonly getMinCamera: string = "/camera/cameras";
private readonly apiAllCameras: string = "/camera/cameras/get_all";
private readonly superCamera: string = "/camera/cameras";
private readonly superRule: string = "/rules";
private readonly getEventByIdUrl: string = "/event/events/retrieves";
private readonly loginConfig: object = {
headers: {
'Content-Type': 'application/json',
@ -60,6 +66,7 @@ class BoxApi {
this.createAxiosInstance();
}
public setPort(port: number) {
this.port = port === 0 ? this.defaultPort : port;
this.createAxiosInstance();
@ -213,8 +220,8 @@ class BoxApi {
return await this.getCamerasByUrl(url, token);
}
public async getMinCameras(token: string | null = null): Promise<any> {
const url = `${this.getMinCamerasApi}`;
public async getAllCameras(token: string | null = null): Promise<any> {
const url = `${this.apiAllCameras}`;
try {
const res = await this.axios.get(url, this._authHeader(token));
if (res.data.err.ec === 0) {
@ -227,8 +234,106 @@ class BoxApi {
}
}
public async getCameraById(token: string | null = null, cameraId: number): Promise<any> {
const url = `${this.superCamera}/${cameraId}`;
try {
const res = await this.axios.get(url, this._authHeader(token));
if (res.data.err.ec === 0) {
return res.data.ret;
} else {
throw new Error(res.data.err.msg);
}
} catch (error) {
throw error;
}
}
public async updateCamera(token: string | null = null, cameraId: number, jsonData: CameraData): Promise<any> {
const url = `${this.superCamera}/${cameraId}`;
// const { rules, ...cameraData } = jsonData;
console.log("接口接收的摄像设置>>>>>>>>>>>>>>", jsonData);
try {
const newCamera = {
name: jsonData.name,
mode: jsonData.mode,
}
const res = await this.axios.patch(url, newCamera);
if (res.data.err.ec === 0) {
return res.data.ret;
} else {
throw new Error(res.data.err.dm);
}
} catch (error) {
throw error;
}
}
// public async updateCamera(
// token: string | null = null,
// cameraId: number,
// jsonData: CameraData
// ): Promise<any> {
// const url = `${this.superCamera}/${cameraId}`;
// // 确保 mode 为小写
// const cameraData = {
// name: jsonData.name,
// mode: jsonData.mode?.toLowerCase() === "on" || jsonData.mode?.toLowerCase() === "off"
// ? jsonData.mode.toLowerCase()
// : undefined, // 非法值转为 undefined
// };
// console.log("接口接收的摄像头更新数据:", cameraData);
// try {
// const res = await this.axios.patch(url, cameraData, this._authHeader(token));
// if (res.data.err.ec === 0) {
// return res.data.ret; // 返回后端成功结果
// } else {
// throw new Error(res.data.err.dm); // 抛出后端返回的错误消息
// }
// } catch (error) {
// console.error("更新摄像头失败:", error);
// throw error;
// }
// }
public async updateRule(token: string | null = null, rules: RuleData[]): Promise<any[]> {
const results: any[] = [];
for (const rule of rules) {
const url = `${this.superRule}/${rule.id}`;
try {
const cleanedRule = {
...rule,
schedule: rule.schedule || {},
};
console.log("接口接收的规则设置>>>>>>>>>>>>>>", cleanedRule);
const res = await this.axios.patch(url, cleanedRule, this._authHeader(token));
if (res.data.err.ec === 0) {
results.push({ id: rule.id, success: true, data: res.data.ret });
} else {
results.push({ id: rule.id, success: false, message: res.data.err.dm });
}
} catch (error: any) {
console.error(`更新规则失败: rule.id=${rule.id}`, error);
results.push({ id: rule.id, success: false, message: error.message });
}
}
return results; // 返回所有规则更新结果
}
public async startCameraStream(token: string | null = null, cameraId: number): Promise<any> {
const url = `${this.getMinCamera}/${cameraId}/start_stream`;
const url = `${this.superCamera}/${cameraId}/start_stream`;
try {
const res = await this.axios.post(url, this._authHeader(token));
if (res.data.err.ec === 0) {
@ -242,7 +347,7 @@ class BoxApi {
}
public async stopCameraStream(token: string | null = null, cameraId: number): Promise<any> {
const url = `${this.getMinCamera}/${cameraId}/stop_stream`;
const url = `${this.superCamera}/${cameraId}/stop_stream`;
try {
const res = await this.axios.post(url, this._authHeader(token));
if (res.data.err.ec === 0) {
@ -340,7 +445,7 @@ class BoxApi {
if (types) {
url += `&types=${encodeURIComponent(types)}`;
}
if(camera_id){
if (camera_id) {
url += `&camera_id=${camera_id}`;
}
if (status) {
@ -380,7 +485,7 @@ class BoxApi {
if (res.data.err.ec === 0) {
// return res.data.ret.objects[0];
return res.data.ret.objects[0];
return res.data.ret.objects[0];
} else {
throw new Error(res.data.err.dm);
}
@ -406,13 +511,13 @@ class BoxApi {
public async setEventStatus(eventId: number, status: string, remark: string | null = null, token: string | null = null): Promise<any> {
const url = `${this.apiEvents}/${eventId}`;
const data: { status: string; remark?: string } = { status: status };
if (remark && remark.trim() !== "") {
data.remark = remark;
}
try {
const res = await this.axios.patch(url, data, this._authHeader(token));
if (res.data.err.ec === 0) {