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 = `
更新时间 ${stateData.time}
状态ID ${stateData.state_id}
状态名称 ${stateData.state_name}
事件 ${stateData.event}
事件ID ${stateData.event_id}
故障代码 ${stateData.fault_code}
备注 ${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 = `已连接到MOOS数据库:${data.host}:${data.port}`;
} else {
moosDbStatus.className = 'db-status disconnected';
moosDbStatus.innerHTML = `MOOS数据库连接失败`;
}
})
.catch(error => {
const moosDbStatus = document.getElementById('moos-db-status');
moosDbStatus.className = 'db-status disconnected';
moosDbStatus.innerHTML = `MOOS数据库连接错误`;
});
}
// 检查本地数据库连接状态
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 = `已连接到数据库:${data.database}`;
} else {
dbStatus.className = 'db-status disconnected';
dbStatus.innerHTML = `数据库连接失败`;
}
})
.catch(error => {
const dbStatus = document.getElementById('db-status');
dbStatus.className = 'db-status disconnected';
dbStatus.innerHTML = `数据库连接错误`;
});
}
// 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(); // 初始检查