thingboard告警信息格式美化

原始报警json内容:

javascript 复制代码
{ 

	"severity": "CRITICAL",
	"acknowledged": false,
	"cleared": false,
	"assigneeId": null,
	"startTs": 1739801102349,
	"endTs": 1739801102349,
	"ackTs": 0,
	"clearTs": 0,
	"assignTs": 0,
	"propagate": false,
	"propagateToOwner": false,
	"propagateToTenant": false,
	"propagateRelationTypes": [],
	"originatorName": "DTU-863569063344204",
	"originatorLabel": "DTU-863569063344204",
	"assignee": null,
	"name": "Inactivity TimeOut",
	"status": "ACTIVE_UNACK",
	"details": {}
}

报警格式美化js代码:

javascript 复制代码
var date = new Date();
var year = date.getFullYear();
var month = ('0' + (date.getMonth() + 1)).slice(-2);
var day = ('0' + date.getDate()).slice(-2);
var hours = ('0' + date.getHours()).slice(-2);
var minutes = ('0' + date.getMinutes()).slice(-2);
var seconds = ('0' + date.getSeconds()).slice(-2);

var currentTime = year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;

// 解析告警时间
function formatUnixTime(unixTimestamp) {
    var date = new Date(unixTimestamp);
    var year = date.getFullYear();
    var month = ('0' + (date.getMonth() + 1)).slice(-2);
    var day = ('0' + date.getDate()).slice(-2);
    var hours = ('0' + date.getHours()).slice(-2);
    var minutes = ('0' + date.getMinutes()).slice(-2);
    var seconds = ('0' + date.getSeconds()).slice(-2);
    return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
}

// 格式化报警级别
var severityMap = {
    "CRITICAL": "🔥 **严重告警**",
    "MAJOR": "⚠ **主要告警**",
    "MINOR": "🔶 **次要告警**",
    "WARNING": "ℹ **警告**"
};
var severity = severityMap[msg.severity] || "⚠ **未知级别**";

// 确认状态
var acknowledged = msg.acknowledged ? "✅ 已确认" : "❌ 未确认";
var cleared = msg.cleared ? "✅ 已清除" : "❌ 未清除";

// 组装格式化的告警信息
var messageContent =
    "📢 **设备告警通知** 📢\n" +
    "------------------------------\n" +
    "📅 **当前时间**: " + currentTime + "\n" +
    "📍 **设备名称**: " + msg.originatorName + "\n" +
    "🔖 **设备标识**: " + msg.originatorLabel + "\n" +
    "⚠ **告警名称**: " + msg.name + "\n" +
    "🚨 **告警级别**: " + severity + "\n" +
    "🕒 **告警开始时间**: " + formatUnixTime(msg.startTs) + "\n" +
    "🕒 **告警结束时间**: " + formatUnixTime(msg.endTs) + "\n" +
    "🔎 **告警状态**: " + msg.status + "\n" +
    "📌 **确认状态**: " + acknowledged + "\n" +
    "📌 **清除状态**: " + cleared + "\n" +
    "------------------------------";

var newMsg = {
    "msgtype": "text",
    "text": {
        "content": messageContent,
        "mentioned_list": ["wangqing", "@all"],
        "mentioned_mobile_list": ["13800001111", "@all"]
    }
};

return {
    msg: newMsg
};

输出到企业微信消息:

相关推荐
腾讯TNTWeb前端团队5 小时前
helux v5 发布了,像pinia一样优雅地管理你的react状态吧
前端·javascript·react.js
mghio7 小时前
Dubbo 中的集群容错
java·微服务·dubbo
范文杰9 小时前
AI 时代如何更高效开发前端组件?21st.dev 给了一种答案
前端·ai编程
拉不动的猪9 小时前
刷刷题50(常见的js数据通信与渲染问题)
前端·javascript·面试
拉不动的猪9 小时前
JS多线程Webworks中的几种实战场景演示
前端·javascript·面试
FreeCultureBoy10 小时前
macOS 命令行 原生挂载 webdav 方法
前端
uhakadotcom10 小时前
Astro 框架:快速构建内容驱动型网站的利器
前端·javascript·面试
uhakadotcom10 小时前
了解Nest.js和Next.js:如何选择合适的框架
前端·javascript·面试
uhakadotcom10 小时前
React与Next.js:基础知识及应用场景
前端·面试·github