设备巡检到期预警规则
适用于矿山、电力、水利、建筑等场景的设备巡检计划管理与到期提醒。
规则文件内容
请复制以下内容,保存为 .cursor/rules/inspection-overdue-alert.mdc:
markdown
---
description: 设备巡检计划到期预警与补检管理规则
globs: src/modules/inspection/**, src/modules/equipment/**, src/modules/alert/**
---
# 设备巡检到期预警规则
## 1. 核心原则
- **计划驱动**:所有设备巡检必须按计划执行,不允许遗漏。
- **提前预警**:计划到期前提醒,到期后立即报警。
- **分级管理**:重点设备(通风机、水泵)到期立即报警,普通设备允许30分钟缓冲。
- **闭环管理**:所有超期巡检必须补检并记录原因。
## 2. 到期判定逻辑
### 2.1 超期时长计算
$$ 超期时长 = 当前时间 - 计划巡检时间 $$
### 2.2 判定规则
| 时长范围 | 严重程度 | 处理动作 |
| :--- | :--- | :--- |
| **提前30分钟** | 提醒 (reminder) | 推送提醒,准备巡检 |
| **0-30分钟** | 预警 (warning) | 推送预警,尽快执行 |
| **30分钟-2小时** | 报警 (alert) | 推送报警,必须补检 |
| **> 2小时** | 严重 (critical) | 推送到管理层,追责 |
### 2.3 重点设备特殊规则
**重点设备(通风机、水泵、提升机等):**
- 到期立即报警(0分钟缓冲)
- 推送到调度室、设备科、值班矿长
- 必须在30分钟内完成补检
**普通设备:**
- 到期30分钟后报警
- 推送到设备科、巡检人员
- 必须在2小时内完成补检
### 2.4 自动响应规则
**提前提醒(提前30分钟):**
1. 推送提醒到巡检人员手机
2. 显示巡检计划、设备位置、巡检内容
3. 提供一键开始巡检功能
**预警响应(0-30分钟):**
1. 推送预警到巡检人员、设备科
2. 显示超期时长、设备信息
3. 提醒尽快执行
**报警响应(30分钟-2小时):**
1. 推送报警到设备科、值班领导
2. 标记为"超期未检"
3. 要求说明原因
**严重响应(> 2小时):**
1. 推送到管理层
2. 启动追责流程
3. 记录到安全台账
## 3. 代码实现规范 (TypeScript)
### 3.1 到期检测函数
```typescript
/**
* 检测巡检计划是否到期
* @param plan 巡检计划
* @param currentTime 当前时间
* @returns 到期检测结果
*/
export function checkInspectionOverdue(
plan: InspectionPlan,
currentTime: Date
): OverdueResult {
const scheduledTime = plan.scheduledTime;
const overdueSeconds = (currentTime.getTime() - scheduledTime.getTime()) / 1000;
// 未到期
if (overdueSeconds < 0) {
// 提前30分钟提醒
if (overdueSeconds >= -1800) {
return {
isOverdue: false,
isReminder: true,
reminderTime: Math.abs(overdueSeconds)
};
}
return { isOverdue: false };
}
// 重点设备:到期立即报警
if (plan.equipment.isCritical) {
return {
isOverdue: true,
overdueSeconds,
severity: overdueSeconds > 7200 ? 'critical' : 'alert',
isCriticalEquipment: true
};
}
// 普通设备:分级判断
if (overdueSeconds > 7200) {
return { isOverdue: true, overdueSeconds, severity: 'critical' };
}
if (overdueSeconds > 1800) {
return { isOverdue: true, overdueSeconds, severity: 'alert' };
}
if (overdueSeconds > 0) {
return { isOverdue: true, overdueSeconds, severity: 'warning' };
}
return { isOverdue: false };
}3.2 自动报警函数
typescript
/**
* 触发巡检到期报警
* @param plan 巡检计划
* @param overdueResult 到期检测结果
*/
export async function triggerInspectionAlert(
plan: InspectionPlan,
overdueResult: OverdueResult
): Promise<void> {
const { equipment, inspector } = plan;
const overdueMinutes = Math.floor(overdueResult.overdueSeconds / 60);
// 提前提醒
if (overdueResult.isReminder) {
await alertService.push({
channels: ['巡检人员手机'],
priority: 'LOW',
message: `巡检提醒:${equipment.name}将在30分钟后到期`,
data: {
plan,
quickStart: true // 提供一键开始巡检
}
});
return;
}
// 预警
if (overdueResult.severity === 'warning') {
await alertService.push({
channels: ['巡检人员', '设备科'],
priority: 'MEDIUM',
message: `巡检预警:${equipment.name}已超期${overdueMinutes}分钟`,
data: { plan, overdueMinutes }
});
}
// 报警
if (overdueResult.severity === 'alert') {
await alertService.push({
channels: ['设备科', '值班领导'],
priority: 'HIGH',
message: `巡检报警:${equipment.name}已超期${overdueMinutes}分钟,必须补检`,
data: { plan, overdueMinutes, requireReason: true }
});
}
// 严重
if (overdueResult.severity === 'critical') {
await alertService.push({
channels: ['管理层', '安全科'],
priority: 'CRITICAL',
message: `巡检严重超期:${equipment.name}已超期${overdueMinutes}分钟,启动追责`,
data: { plan, overdueMinutes }
});
// 记录到安全台账
await safetyArchive.add({
type: 'INSPECTION_OVERDUE',
planId: plan.id,
overdueMinutes
});
}
}3.3 补检记录函数
typescript
/**
* 记录补检信息
* @param planId 巡检计划ID
* @param makeupInfo 补检信息
*/
export async function recordMakeupInspection(
planId: string,
makeupInfo: MakeupInspectionInfo
): Promise<void> {
// 必须记录的字段
const requiredFields = ['actualTime', 'inspector', 'overdueReason', 'result'];
for (const field of requiredFields) {
if (!makeupInfo[field]) {
throw new Error(`补检记录缺少必填字段: ${field}`);
}
}
// 更新巡检计划状态
await db.inspectionPlans.update(planId, {
status: 'COMPLETED',
actualTime: makeupInfo.actualTime,
isOverdue: true,
makeup: {
...makeupInfo,
recordedAt: new Date()
}
});
// 自动归档到设备台账
await equipmentArchive.add({
type: 'MAKEUP_INSPECTION',
planId,
makeupInfo
});
}3.4 批量检测函数
typescript
/**
* 批量检测所有待巡检计划
* @param currentTime 当前时间
*/
export async function checkAllInspectionPlans(
currentTime: Date
): Promise<void> {
// 查询所有未完成的巡检计划
const pendingPlans = await db.inspectionPlans.findMany({
where: {
status: 'PENDING',
scheduledTime: { lte: currentTime }
},
include: { equipment: true, inspector: true }
});
// 批量检测
for (const plan of pendingPlans) {
const overdueResult = checkInspectionOverdue(plan, currentTime);
if (overdueResult.isOverdue || overdueResult.isReminder) {
await triggerInspectionAlert(plan, overdueResult);
}
}
}4. 数据模型约束
4.1 巡检计划
typescript
interface InspectionPlan {
id: string;
equipmentId: string; // 设备ID
equipment: Equipment; // 设备信息
inspectorId: string; // 巡检人员ID
inspector: User; // 巡检人员信息
scheduledTime: Date; // 计划巡检时间
actualTime?: Date; // 实际巡检时间
status: 'PENDING' | 'IN_PROGRESS' | 'COMPLETED' | 'OVERDUE';
isOverdue: boolean; // 是否超期
// 补检记录
makeup?: {
actualTime: Date; // 实际补检时间
inspector: string; // 补检人员
overdueReason: string; // 超期原因
result: string; // 巡检结果
recordedAt: Date;
};
}4.2 设备信息
typescript
interface Equipment {
id: string;
name: string; // 设备名称
code: string; // 设备编号
type: string; // 设备类型
location: string; // 设备位置
isCritical: boolean; // 是否重点设备
// 巡检配置
inspectionConfig: {
frequency: 'DAILY' | 'WEEKLY' | 'MONTHLY'; // 巡检频率
items: string[]; // 巡检项目
standardTime: number; // 标准巡检时长(分钟)
};
}4.3 巡检记录
typescript
interface InspectionRecord {
id: string;
planId: string; // 巡检计划ID
equipmentId: string;
inspectorId: string;
startTime: Date;
endTime: Date;
duration: number; // 巡检时长(分钟)
// 巡检结果
items: InspectionItem[]; // 巡检项目结果
overallResult: 'NORMAL' | 'ABNORMAL' | 'FAULT';
issues?: string[]; // 发现的问题
photos?: string[]; // 现场照片
// 签名确认
signature: string; // 巡检人员签名
confirmedAt: Date;
}
interface InspectionItem {
name: string; // 项目名称
result: 'PASS' | 'FAIL';
value?: string; // 检测值
remark?: string; // 备注
}5. 开发注意事项
- 时区处理:确保计划时间和当前时间使用相同时区。
- 定时任务:建议每分钟执行一次批量检测,确保及时发现超期。
- 重复报警:避免对同一计划重复报警,需要记录已报警状态。
- 补检流程:超期补检必须记录原因,不允许直接标记为完成。
- 性能优化:使用索引优化查询,避免全表扫描。
6. 测试用例
6.1 正常场景
- 提前30分钟 → 推送提醒
- 按时完成巡检 → 无报警
6.2 超期场景
- 超期10分钟(普通设备)→ 触发预警
- 超期40分钟(普通设备)→ 触发报警
- 超期3小时 → 触发严重报警
6.3 重点设备场景
- 重点设备到期(0分钟)→ 立即报警
- 重点设备超期10分钟 → 持续报警
6.4 补检场景
- 超期后补检 → 必须记录原因
- 补检记录缺少原因 → 抛出错误
7. 场景配置示例
7.1 矿山设备巡检
typescript
const MINING_EQUIPMENT_CONFIG = {
// 重点设备:通风机
ventilationFan: {
name: '主通风机',
isCritical: true,
inspectionConfig: {
frequency: 'DAILY',
items: [
'电机运行状态',
'轴承温度',
'振动情况',
'润滑油位',
'电流电压'
],
standardTime: 30 // 30分钟
},
alertConfig: {
reminderBefore: 1800, // 提前30分钟提醒
overdueBuffer: 0, // 到期立即报警
escalationTime: 1800 // 30分钟升级
}
},
// 重点设备:水泵
waterPump: {
name: '主排水泵',
isCritical: true,
inspectionConfig: {
frequency: 'DAILY',
items: [
'泵体运行状态',
'压力表读数',
'流量检查',
'密封情况',
'电机温度'
],
standardTime: 20
},
alertConfig: {
reminderBefore: 1800,
overdueBuffer: 0,
escalationTime: 1800
}
},
// 普通设备:皮带机
beltConveyor: {
name: '皮带输送机',
isCritical: false,
inspectionConfig: {
frequency: 'DAILY',
items: [
'皮带张紧度',
'托辊运转',
'清扫器状态',
'保护装置',
'润滑情况'
],
standardTime: 15
},
alertConfig: {
reminderBefore: 1800,
overdueBuffer: 1800, // 30分钟缓冲
escalationTime: 7200 // 2小时升级
}
}
};7.2 电力设施巡检
typescript
const POWER_EQUIPMENT_CONFIG = {
// 重点设备:变压器
transformer: {
name: '主变压器',
isCritical: true,
inspectionConfig: {
frequency: 'DAILY',
items: [
'油位油温',
'声音异常',
'渗漏情况',
'接地装置',
'冷却系统'
],
standardTime: 25
},
alertConfig: {
reminderBefore: 1800,
overdueBuffer: 0,
escalationTime: 1800
}
},
// 普通设备:配电柜
distributionCabinet: {
name: '配电柜',
isCritical: false,
inspectionConfig: {
frequency: 'WEEKLY',
items: [
'开关状态',
'指示灯显示',
'接线紧固',
'清洁情况',
'温度检测'
],
standardTime: 10
},
alertConfig: {
reminderBefore: 3600,
overdueBuffer: 1800,
escalationTime: 7200
}
}
};7.3 水利工程巡检
typescript
const WATER_FACILITY_CONFIG = {
// 重点设备:闸门
sluiceGate: {
name: '泄洪闸门',
isCritical: true,
inspectionConfig: {
frequency: 'DAILY',
items: [
'启闭机运行',
'闸门密封',
'钢丝绳状态',
'润滑系统',
'限位装置'
],
standardTime: 30
},
alertConfig: {
reminderBefore: 1800,
overdueBuffer: 0,
escalationTime: 1800
}
},
// 普通设备:监测仪器
monitoringDevice: {
name: '水位监测仪',
isCritical: false,
inspectionConfig: {
frequency: 'WEEKLY',
items: [
'设备供电',
'数据传输',
'传感器清洁',
'校准检查',
'防护设施'
],
standardTime: 15
},
alertConfig: {
reminderBefore: 3600,
overdueBuffer: 1800,
escalationTime: 7200
}
}
};7.4 建筑施工巡检
typescript
const CONSTRUCTION_EQUIPMENT_CONFIG = {
// 重点设备:塔吊
towerCrane: {
name: '塔式起重机',
isCritical: true,
inspectionConfig: {
frequency: 'DAILY',
items: [
'钢丝绳磨损',
'制动系统',
'限位装置',
'结构连接',
'电气系统'
],
standardTime: 40
},
alertConfig: {
reminderBefore: 1800,
overdueBuffer: 0,
escalationTime: 1800
}
},
// 普通设备:施工电梯
constructionElevator: {
name: '施工升降机',
isCritical: false,
inspectionConfig: {
frequency: 'DAILY',
items: [
'导轨架稳定',
'防坠装置',
'门联锁装置',
'电气控制',
'润滑情况'
],
standardTime: 20
},
alertConfig: {
reminderBefore: 1800,
overdueBuffer: 1800,
escalationTime: 7200
}
}
};
## 适用场景
- **矿山安全**:通风机、水泵、提升机等关键设备巡检
- **电力运维**:变压器、配电柜、输电线路巡检
- **水利工程**:闸门、泵站、监测设备巡检
- **建筑施工**:塔吊、施工电梯、脚手架巡检
- **制造工厂**:生产设备、安全设施定期巡检
## 技术迁移
### 复用已有资产
- **生鲜场景:冷库巡检规则** - 复用计划管理、到期提醒逻辑
- **医废场景:超期预警规则** - 复用时间窗口判断、分级报警机制
### 差异点
- 监测对象:冷库温度 → 设备状态
- 巡检频率:每小时 → 每天/每周/每月
- 重点管理:温度异常 → 重点设备到期立即报警
### 技术复用度
- 规则引擎逻辑:90%复用
- 报警推送逻辑:100%复用
- 计划管理逻辑:95%复用
- 补检流程:新增(10%新开发)
## 相关资产
- [人员定位异常监测规则 →](./personnel-location-anomaly) - 人员定位信号异常
- [环境监测异常预警规则 →](./environment-monitoring-alert) - 传感器数据异常监测
- [设备故障预测规则 →](./equipment-fault-prediction) - 设备健康状态监测
## 实施指南
### 步骤1:配置规则文件
```bash
# 创建规则文件目录
mkdir -p .cursor/rules
# 复制规则文件
cp inspection-overdue-alert.mdc .cursor/rules/步骤2:配置设备参数
typescript
// config/inspection-rules.ts
export const INSPECTION_CONFIGS = {
mining: MINING_EQUIPMENT_CONFIG,
power: POWER_EQUIPMENT_CONFIG,
water: WATER_FACILITY_CONFIG,
construction: CONSTRUCTION_EQUIPMENT_CONFIG
};步骤3:在Cursor中使用
在Cursor中告诉AI:
"参考 inspection-overdue-alert.mdc 中的业务规则,
实现设备巡检到期预警功能"
AI会基于规则生成准确的判断逻辑、报警逻辑等。步骤4:配置定时任务
typescript
// 每分钟执行一次检测
cron.schedule('* * * * *', async () => {
await checkAllInspectionPlans(new Date());
});步骤5:测试验证
bash
# 运行单元测试
npm test -- inspection-overdue
# 模拟超期场景
npm run simulate:overdue-inspection
