154 lines
5.3 KiB
JavaScript
154 lines
5.3 KiB
JavaScript
let currentDbPath = '/home/zjk/project/H100/H100PowerManger/data/power_data.db';
|
|
|
|
// 数据库下载功能
|
|
document.getElementById('download-db').addEventListener('click', function() {
|
|
fetch('/api/download-db')
|
|
.then(response => {
|
|
if (!response.ok) {
|
|
throw new Error('下载失败:' + response.statusText);
|
|
}
|
|
return response.blob();
|
|
})
|
|
.then(blob => {
|
|
const url = window.URL.createObjectURL(blob);
|
|
const a = document.createElement('a');
|
|
a.href = url;
|
|
a.download = 'power_data.db';
|
|
document.body.appendChild(a);
|
|
a.click();
|
|
window.URL.revokeObjectURL(url);
|
|
})
|
|
.catch(error => {
|
|
console.error('下载数据库失败:', error);
|
|
alert('下载数据库失败:' + error.message);
|
|
});
|
|
});
|
|
|
|
// 更新页面数据的函数
|
|
function updateData() {
|
|
// 获取状态机数据
|
|
fetch('/api/state')
|
|
.then(response => response.json())
|
|
.then(stateData => {
|
|
// 更新状态机面板
|
|
const stateInfo = `
|
|
<span>更新时间</span> ${stateData.time}
|
|
<span>状态ID</span> ${stateData.state_id}
|
|
<span>状态名称</span> ${stateData.state_name}
|
|
<span>事件</span> ${stateData.event}
|
|
<span>事件ID</span> ${stateData.event_id}
|
|
<span>故障代码</span> ${stateData.fault_code}
|
|
<span>备注</span> ${stateData.note}
|
|
`;
|
|
document.getElementById('state-machine-state').innerHTML = stateInfo;
|
|
})
|
|
.catch(error => console.error('Error fetching state data:', error));
|
|
|
|
// 获取其他系统状态
|
|
fetch('/api/status')
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
// 更新DIS系统状态
|
|
document.getElementById('dis-sys-state').textContent = JSON.stringify(data.disSys, null, 2);
|
|
|
|
// 更新CCU状态
|
|
document.getElementById('ccu-state').textContent = JSON.stringify(data.ccuState, null, 2);
|
|
|
|
// 更新运行日志
|
|
document.getElementById('log-data').textContent = data.logs.join('\n');
|
|
})
|
|
.catch(error => console.error('Error fetching system data:', error));
|
|
}
|
|
|
|
// 检查MOOS数据库连接状态
|
|
function checkMoosDbConnection() {
|
|
fetch('/api/moos-db')
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
const moosDbStatus = document.getElementById('moos-db-status');
|
|
if (data.connected) {
|
|
moosDbStatus.className = 'db-status connected';
|
|
moosDbStatus.innerHTML = `<i class="fas fa-database"></i><span>已连接到MOOS数据库:${data.host}:${data.port}</span>`;
|
|
} else {
|
|
moosDbStatus.className = 'db-status disconnected';
|
|
moosDbStatus.innerHTML = `<i class="fas fa-database"></i><span>MOOS数据库连接失败</span>`;
|
|
}
|
|
})
|
|
.catch(error => {
|
|
const moosDbStatus = document.getElementById('moos-db-status');
|
|
moosDbStatus.className = 'db-status disconnected';
|
|
moosDbStatus.innerHTML = `<i class="fas fa-database"></i><span>MOOS数据库连接错误</span>`;
|
|
});
|
|
}
|
|
|
|
// 检查本地数据库连接状态
|
|
function checkDbConnection() {
|
|
fetch('/api/db-status')
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
const dbStatus = document.getElementById('db-status');
|
|
if (data.connected) {
|
|
dbStatus.className = 'db-status connected';
|
|
dbStatus.innerHTML = `<i class="fas fa-database"></i><span>已连接到数据库:${data.database}</span>`;
|
|
} else {
|
|
dbStatus.className = 'db-status disconnected';
|
|
dbStatus.innerHTML = `<i class="fas fa-database"></i><span>数据库连接失败</span>`;
|
|
}
|
|
})
|
|
.catch(error => {
|
|
const dbStatus = document.getElementById('db-status');
|
|
dbStatus.className = 'db-status disconnected';
|
|
dbStatus.innerHTML = `<i class="fas fa-database"></i><span>数据库连接错误</span>`;
|
|
});
|
|
}
|
|
|
|
// MOOS通知功能
|
|
function sendMoosNotify() {
|
|
const varValue = document.getElementById('moos-var').value.trim();
|
|
const keyValue = document.getElementById('moos-key').value.trim();
|
|
|
|
if (!varValue || !keyValue) {
|
|
alert('请填写Var和Key值');
|
|
return;
|
|
}
|
|
|
|
const payload = {
|
|
var: varValue,
|
|
key: keyValue
|
|
};
|
|
|
|
fetch('/api/moos-notify', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify(payload)
|
|
})
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
if (data.success) {
|
|
alert('发送成功');
|
|
} else {
|
|
alert('发送失败: ' + data.message);
|
|
}
|
|
})
|
|
.catch(error => {
|
|
console.error('Error:', error);
|
|
alert('发送请求失败');
|
|
});
|
|
}
|
|
|
|
document.getElementById('moos-send-btn').addEventListener('click', sendMoosNotify);
|
|
|
|
// 每1秒更新一次数据
|
|
setInterval(updateData, 1000);
|
|
updateData(); // 初始加载
|
|
|
|
// 每10秒检查一次数据库连接状态
|
|
setInterval(() => {
|
|
checkDbConnection();
|
|
checkMoosDbConnection();
|
|
}, 10000);
|
|
checkDbConnection(); // 初始检查
|
|
checkMoosDbConnection(); // 初始检查
|