2026-01-23 18:22:05 +08:00
|
|
|
const { v4: uuidv4 } = require('uuid');
|
2026-01-21 13:34:42 +08:00
|
|
|
const db = require('./db');
|
|
|
|
|
|
|
|
|
|
const logHostResult = async (data) => {
|
2026-01-23 18:22:05 +08:00
|
|
|
const entryId = uuidv4();
|
2026-01-21 13:34:42 +08:00
|
|
|
const query = `
|
|
|
|
|
INSERT INTO upgrade_log (
|
2026-01-23 18:22:05 +08:00
|
|
|
uuid, session_id, start_time, roomtype_id, host_str, filename, status,
|
2026-01-21 13:34:42 +08:00
|
|
|
end_time, file_type, config_version, firmware_version
|
2026-01-23 18:22:05 +08:00
|
|
|
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
|
2026-01-21 13:34:42 +08:00
|
|
|
`;
|
|
|
|
|
const values = [
|
2026-01-23 18:22:05 +08:00
|
|
|
entryId,
|
2026-01-21 13:34:42 +08:00
|
|
|
data.uuid,
|
|
|
|
|
data.start_time,
|
|
|
|
|
data.roomtype_id,
|
|
|
|
|
data.host_str,
|
|
|
|
|
data.filename,
|
|
|
|
|
data.status,
|
|
|
|
|
data.end_time,
|
|
|
|
|
data.file_type,
|
|
|
|
|
data.config_version,
|
|
|
|
|
data.firmware_version
|
|
|
|
|
];
|
|
|
|
|
await db.query(query, values);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const getUpgradeState = async (key) => {
|
|
|
|
|
const res = await db.query('SELECT * FROM upgrade_state WHERE state_key = $1', [key]);
|
|
|
|
|
if (res.rows.length > 0) {
|
|
|
|
|
return res.rows[0];
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const updateUpgradeState = async (key, currentRoomtypeIndex, executionCount) => {
|
|
|
|
|
const query = `
|
|
|
|
|
INSERT INTO upgrade_state (state_key, current_roomtype_index, execution_count, last_updated)
|
|
|
|
|
VALUES ($1, $2, $3, NOW())
|
|
|
|
|
ON CONFLICT (state_key)
|
|
|
|
|
DO UPDATE SET
|
|
|
|
|
current_roomtype_index = EXCLUDED.current_roomtype_index,
|
|
|
|
|
execution_count = EXCLUDED.execution_count,
|
|
|
|
|
last_updated = NOW();
|
|
|
|
|
`;
|
|
|
|
|
await db.query(query, [key, currentRoomtypeIndex, executionCount]);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
logHostResult,
|
|
|
|
|
getUpgradeState,
|
|
|
|
|
updateUpgradeState
|
|
|
|
|
};
|