edge-proxy-app/src/api/alarms.ts

114 lines
3.2 KiB
TypeScript
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.

import axios from "axios";
const API_BASE_URL = "http://192.168.0.33:8080";
const apiClient = axios.create({
baseURL: API_BASE_URL,
timeout: 5000,
});
export interface AlarmRule {
rule_id: string;
device_id: string;
data_point_name: string;
compare_type: "GT" | "LT" | "EQ"; // 限制为已知类型
threshold: number;
level: "INFO" | "WARNING" | "CRITICAL"; // 限制为已知类型
debounce_seconds: number;
alarm_mqtt_topic?: string; // 可选
message_template: string;
clear_message_template?: string; // 可选
// [重要] 用于前端的额外状态
_isNew?: boolean; // 标记这是不是一条新添加的规则
}
export interface AlarmEvent {
event_id?: number;
rule_id: string;
device_id: string;
status: "ACTIVE" | "CLEARED";
level: "INFO" | "WARNING" | "CRITICAL"; // 假设的级别
message: string;
trigger_value: number;
timestamp_ms: number;
}
export const getActiveAlarms = async (): Promise<AlarmEvent[]> => {
try {
const response = await apiClient.get<AlarmEvent[]>("/api/alarms/active");
return response.data;
} catch (error) {
console.error("API 请求失败 (getActiveAlarms):", error);
throw new Error("无法获取活动告警");
}
};
export const getAlarmHistory = async (
limit: number = 200
): Promise<AlarmEvent[]> => {
try {
const response = await apiClient.get<AlarmEvent[]>(
`/api/alarms/history?limit=${limit}`
);
return response.data;
} catch (error) {
console.error("API 请求失败 (getAlarmHistory):", error);
throw new Error("无法获取告警历史");
}
};
export const postClearAlarm = async (
rule_id: string,
device_id: string
): Promise<any> => {
try {
const response = await apiClient.post("/api/alarms/clear", {
rule_id,
device_id,
});
return response.data;
} catch (error) {
console.error("API 请求失败 (postClearAlarm):", error);
throw new Error("无法清除告警");
}
};
export const postReloadRules = async (): Promise<any> => {
try {
const response = await apiClient.post("/api/alarms/reload");
return response.data;
} catch (error) {
console.error("API 请求失败 (postReloadRules):", error);
throw new Error("无法重载规则");
}
};
export const getAlarmConfig = async (): Promise<string> => {
try {
const response = await apiClient.get<string>("/api/alarms/config");
if (typeof response.data === 'object') {
return JSON.stringify(response.data, null, 2);
}
return response.data;
} catch (error) {
console.error("API 请求失败 (getAlarmConfig):", error);
throw new Error("无法获取告警配置");
}
};
export const postAlarmConfig = async (jsonContent: string): Promise<any> => {
try {
const response = await apiClient.post("/api/alarms/config", jsonContent, {
headers: { 'Content-Type': 'application/json' }
});
return response.data;
} catch (error) {
console.error("API 请求失败 (postAlarmConfig):", error);
if (axios.isAxiosError(error) && error.response?.status === 400) {
throw new Error("保存失败JSON 格式或规则 schema 无效。请检查 C++ 服务日志。");
}
throw new Error("无法保存告警配置");
}
};