| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.OTATaskModel = void 0;
- const database_1 = require("../config/database");
- class OTATaskModel {
- static async create(taskData) {
- const query = `
- INSERT INTO ota_tasks (device_id, firmware_id, status, progress, error_message, start_time, end_time)
- VALUES (?, ?, ?, ?, ?, ?, ?)
- `;
- const values = [
- taskData.device_id,
- taskData.firmware_id,
- taskData.status || 'pending',
- taskData.progress || 0,
- taskData.error_message || null,
- taskData.start_time || null,
- taskData.end_time || null
- ];
- const result = await (0, database_1.executeQuery)(query, values);
- const insertId = result.insertId;
- return this.getById(insertId);
- }
- static async getById(id) {
- const query = 'SELECT * FROM ota_tasks WHERE id = ?';
- const tasks = await (0, database_1.executeQuery)(query, [id]);
- return tasks.length > 0 ? tasks[0] : null;
- }
- static async getByDeviceId(deviceId) {
- const query = 'SELECT * FROM ota_tasks WHERE device_id = ? ORDER BY created_at DESC';
- return await (0, database_1.executeQuery)(query, [deviceId]);
- }
- static async getAll() {
- const query = 'SELECT * FROM ota_tasks ORDER BY created_at DESC';
- return await (0, database_1.executeQuery)(query);
- }
- static async getByStatus(status) {
- const query = 'SELECT * FROM ota_tasks WHERE status = ? ORDER BY created_at DESC';
- return await (0, database_1.executeQuery)(query, [status]);
- }
- static async getByDeviceIdAndStatus(deviceId, status) {
- const query = 'SELECT * FROM ota_tasks WHERE device_id = ? AND status = ? ORDER BY created_at DESC';
- return await (0, database_1.executeQuery)(query, [deviceId, status]);
- }
- static async getPendingTasksByDeviceId(deviceId) {
- const query = 'SELECT * FROM ota_tasks WHERE device_id = ? AND status = ? ORDER BY created_at ASC';
- return await (0, database_1.executeQuery)(query, [deviceId, 'pending']);
- }
- static async getIncompleteTasksByDeviceId(deviceId) {
- const query = 'SELECT * FROM ota_tasks WHERE device_id = ? AND status NOT IN (?, ?) ORDER BY created_at ASC';
- return await (0, database_1.executeQuery)(query, [deviceId, 'success', 'failed']);
- }
- static async updateStatusAndProgress(id, status, progress) {
- const query = 'UPDATE ota_tasks SET status = ?, progress = ?, updated_at = NOW() WHERE id = ?';
- const result = await (0, database_1.executeQuery)(query, [status, progress, id]);
- return result.affectedRows > 0;
- }
- static async updateResult(id, status, errorMessage) {
- const query = 'UPDATE ota_tasks SET status = ?, progress = ?, error_message = ?, end_time = NOW() WHERE id = ?';
- const result = await (0, database_1.executeQuery)(query, [status, status === 'success' ? 100 : 0, errorMessage || null, id]);
- return result.affectedRows > 0;
- }
- static async delete(id) {
- const query = 'DELETE FROM ota_tasks WHERE id = ?';
- const result = await (0, database_1.executeQuery)(query, [id]);
- return result.affectedRows > 0;
- }
- static async createTable() {
- const query = `
- CREATE TABLE IF NOT EXISTS ota_tasks (
- id INT AUTO_INCREMENT PRIMARY KEY,
- device_id VARCHAR(255) NOT NULL,
- firmware_id INT NOT NULL,
- status ENUM('pending', 'downloading', 'installing', 'success', 'failed') DEFAULT 'pending',
- progress INT DEFAULT 0,
- error_message TEXT,
- start_time TIMESTAMP NULL,
- end_time TIMESTAMP NULL,
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
- updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
- INDEX idx_device_id (device_id),
- INDEX idx_firmware_id (firmware_id),
- INDEX idx_status (status),
- FOREIGN KEY (firmware_id) REFERENCES firmware_files(id) ON DELETE CASCADE
- )
- `;
- await (0, database_1.executeQuery)(query);
- }
- }
- exports.OTATaskModel = OTATaskModel;
- //# sourceMappingURL=ota.js.map
|