更新规则方法调用,添加对应类型定义
This commit is contained in:
parent
11ea95b902
commit
60f2a7f196
|
@ -1,4 +1,6 @@
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
|
import type { CameraData } from '@/types/CameraData'
|
||||||
|
import type { RuleData } from '@/types/RuleData'
|
||||||
|
|
||||||
class BoxApi {
|
class BoxApi {
|
||||||
|
|
||||||
|
@ -14,10 +16,14 @@ class BoxApi {
|
||||||
private readonly apiAlgorithms: string = "/algorithms";
|
private readonly apiAlgorithms: string = "/algorithms";
|
||||||
private readonly apiResetUser: string = "/auth/resetuser";
|
private readonly apiResetUser: string = "/auth/resetuser";
|
||||||
private readonly apiResetPassword: string = "/auth/reset_password";
|
private readonly apiResetPassword: string = "/auth/reset_password";
|
||||||
private readonly getMinCamerasApi: string = "/camera/cameras/get_all";
|
private readonly apiAllCameras: string = "/camera/cameras/get_all";
|
||||||
private readonly getMinCamera: string = "/camera/cameras";
|
private readonly superCamera: string = "/camera/cameras";
|
||||||
|
private readonly superRule: string = "/rules";
|
||||||
private readonly getEventByIdUrl: string = "/event/events/retrieves";
|
private readonly getEventByIdUrl: string = "/event/events/retrieves";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private readonly loginConfig: object = {
|
private readonly loginConfig: object = {
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
|
@ -60,6 +66,7 @@ class BoxApi {
|
||||||
this.createAxiosInstance();
|
this.createAxiosInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public setPort(port: number) {
|
public setPort(port: number) {
|
||||||
this.port = port === 0 ? this.defaultPort : port;
|
this.port = port === 0 ? this.defaultPort : port;
|
||||||
this.createAxiosInstance();
|
this.createAxiosInstance();
|
||||||
|
@ -213,8 +220,8 @@ class BoxApi {
|
||||||
return await this.getCamerasByUrl(url, token);
|
return await this.getCamerasByUrl(url, token);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getMinCameras(token: string | null = null): Promise<any> {
|
public async getAllCameras(token: string | null = null): Promise<any> {
|
||||||
const url = `${this.getMinCamerasApi}`;
|
const url = `${this.apiAllCameras}`;
|
||||||
try {
|
try {
|
||||||
const res = await this.axios.get(url, this._authHeader(token));
|
const res = await this.axios.get(url, this._authHeader(token));
|
||||||
if (res.data.err.ec === 0) {
|
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> {
|
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 {
|
try {
|
||||||
const res = await this.axios.post(url, this._authHeader(token));
|
const res = await this.axios.post(url, this._authHeader(token));
|
||||||
if (res.data.err.ec === 0) {
|
if (res.data.err.ec === 0) {
|
||||||
|
@ -242,7 +347,7 @@ class BoxApi {
|
||||||
}
|
}
|
||||||
|
|
||||||
public async stopCameraStream(token: string | null = null, cameraId: number): Promise<any> {
|
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 {
|
try {
|
||||||
const res = await this.axios.post(url, this._authHeader(token));
|
const res = await this.axios.post(url, this._authHeader(token));
|
||||||
if (res.data.err.ec === 0) {
|
if (res.data.err.ec === 0) {
|
||||||
|
@ -340,7 +445,7 @@ class BoxApi {
|
||||||
if (types) {
|
if (types) {
|
||||||
url += `&types=${encodeURIComponent(types)}`;
|
url += `&types=${encodeURIComponent(types)}`;
|
||||||
}
|
}
|
||||||
if(camera_id){
|
if (camera_id) {
|
||||||
url += `&camera_id=${camera_id}`;
|
url += `&camera_id=${camera_id}`;
|
||||||
}
|
}
|
||||||
if (status) {
|
if (status) {
|
||||||
|
@ -380,7 +485,7 @@ class BoxApi {
|
||||||
|
|
||||||
if (res.data.err.ec === 0) {
|
if (res.data.err.ec === 0) {
|
||||||
// return res.data.ret.objects[0];
|
// return res.data.ret.objects[0];
|
||||||
return res.data.ret.objects[0];
|
return res.data.ret.objects[0];
|
||||||
} else {
|
} else {
|
||||||
throw new Error(res.data.err.dm);
|
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> {
|
public async setEventStatus(eventId: number, status: string, remark: string | null = null, token: string | null = null): Promise<any> {
|
||||||
const url = `${this.apiEvents}/${eventId}`;
|
const url = `${this.apiEvents}/${eventId}`;
|
||||||
|
|
||||||
|
|
||||||
const data: { status: string; remark?: string } = { status: status };
|
const data: { status: string; remark?: string } = { status: status };
|
||||||
if (remark && remark.trim() !== "") {
|
if (remark && remark.trim() !== "") {
|
||||||
data.remark = remark;
|
data.remark = remark;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const res = await this.axios.patch(url, data, this._authHeader(token));
|
const res = await this.axios.patch(url, data, this._authHeader(token));
|
||||||
if (res.data.err.ec === 0) {
|
if (res.data.err.ec === 0) {
|
||||||
|
|
Loading…
Reference in New Issue