Files
SenMeshNet/electron/bridges/inspection/reportScheduleStore.cjs

79 lines
2.4 KiB
JavaScript
Raw Normal View History

"use strict";
/**
* 巡检报告调度配置存储
*
* 存储内容(report-schedule.json):
* {
* enabled: true,
* defaultIntervalHours: 1, // 全局默认巡检周期(小时)
* dailyReportTime: "23:00", // 日运行报告定时触发时间 HH:MM
* enabledDeviceIds: null | string[], // null/undefined = 全部;[] = 一个都不选;["id1","id2"] = 指定
* perDeviceInterval: { host_a: 2 }, // 单设备周期覆盖(小时)
* aiSummaryEnabled: true // AI 汇总分析开关
* }
*/
const { STORAGE_KEY_REPORT_SCHEDULE } = require("./storageKeys.cjs");
const DEFAULTS = {
enabled: false,
defaultIntervalHours: 1,
dailyReportTime: "23:00",
enabledDeviceIds: null, // null = 全部设备(向后兼容旧的 empty array 也按全部处理)
perDeviceInterval: {},
aiSummaryEnabled: true,
};
function _safeParse(raw, def) {
try { return JSON.parse(raw); }
catch { return def; }
}
function _normalize(cfg) {
// 旧数据兼容:undefined enabledDeviceIds → null(全部)
// 新约定:null = 全部;[] = 一个都不选;[id1,id2] = 指定 —— 不主动改动
if (cfg.enabledDeviceIds === undefined) {
cfg.enabledDeviceIds = null;
}
return cfg;
}
function getSchedule(readFn) {
const raw = readFn(STORAGE_KEY_REPORT_SCHEDULE);
if (!raw) return _normalize({ ...DEFAULTS });
const parsed = _safeParse(raw, {});
return _normalize({ ...DEFAULTS, ...parsed });
}
function saveSchedule(writeFn, config) {
const merged = _normalize({ ...DEFAULTS, ...config });
writeFn(STORAGE_KEY_REPORT_SCHEDULE, JSON.stringify(merged, null, 2));
return merged;
}
function getIntervalHours(readFn, deviceId) {
const cfg = getSchedule(readFn);
if (cfg.perDeviceInterval && cfg.perDeviceInterval[deviceId]) {
return Number(cfg.perDeviceInterval[deviceId]) || cfg.defaultIntervalHours;
}
return cfg.defaultIntervalHours;
}
function isDeviceEnabled(readFn, deviceId) {
const cfg = getSchedule(readFn);
if (!cfg.enabled) return false;
// null/undefined = 全部;空数组 = 都不选;非空数组 = 白名单
if (!cfg.enabledDeviceIds) return true;
if (cfg.enabledDeviceIds.length === 0) return false;
return cfg.enabledDeviceIds.includes(deviceId);
}
module.exports = {
DEFAULTS,
getSchedule,
saveSchedule,
getIntervalHours,
isDeviceEnabled,
};