ota.js 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.OTATaskModel = void 0;
  4. const database_1 = require("../config/database");
  5. class OTATaskModel {
  6. static async create(taskData) {
  7. const query = `
  8. INSERT INTO ota_tasks (device_id, firmware_id, status, progress, error_message, start_time, end_time)
  9. VALUES (?, ?, ?, ?, ?, ?, ?)
  10. `;
  11. const values = [
  12. taskData.device_id,
  13. taskData.firmware_id,
  14. taskData.status || 'pending',
  15. taskData.progress || 0,
  16. taskData.error_message || null,
  17. taskData.start_time || null,
  18. taskData.end_time || null
  19. ];
  20. const result = await (0, database_1.executeQuery)(query, values);
  21. const insertId = result.insertId;
  22. return this.getById(insertId);
  23. }
  24. static async getById(id) {
  25. const query = 'SELECT * FROM ota_tasks WHERE id = ?';
  26. const tasks = await (0, database_1.executeQuery)(query, [id]);
  27. return tasks.length > 0 ? tasks[0] : null;
  28. }
  29. static async getByDeviceId(deviceId) {
  30. const query = 'SELECT * FROM ota_tasks WHERE device_id = ? ORDER BY created_at DESC';
  31. return await (0, database_1.executeQuery)(query, [deviceId]);
  32. }
  33. static async getAll() {
  34. const query = 'SELECT * FROM ota_tasks ORDER BY created_at DESC';
  35. return await (0, database_1.executeQuery)(query);
  36. }
  37. static async getByStatus(status) {
  38. const query = 'SELECT * FROM ota_tasks WHERE status = ? ORDER BY created_at DESC';
  39. return await (0, database_1.executeQuery)(query, [status]);
  40. }
  41. static async getByDeviceIdAndStatus(deviceId, status) {
  42. const query = 'SELECT * FROM ota_tasks WHERE device_id = ? AND status = ? ORDER BY created_at DESC';
  43. return await (0, database_1.executeQuery)(query, [deviceId, status]);
  44. }
  45. static async getPendingTasksByDeviceId(deviceId) {
  46. const query = 'SELECT * FROM ota_tasks WHERE device_id = ? AND status = ? ORDER BY created_at ASC';
  47. return await (0, database_1.executeQuery)(query, [deviceId, 'pending']);
  48. }
  49. static async getIncompleteTasksByDeviceId(deviceId) {
  50. const query = 'SELECT * FROM ota_tasks WHERE device_id = ? AND status NOT IN (?, ?) ORDER BY created_at ASC';
  51. return await (0, database_1.executeQuery)(query, [deviceId, 'success', 'failed']);
  52. }
  53. static async updateStatusAndProgress(id, status, progress) {
  54. const query = 'UPDATE ota_tasks SET status = ?, progress = ?, updated_at = NOW() WHERE id = ?';
  55. const result = await (0, database_1.executeQuery)(query, [status, progress, id]);
  56. return result.affectedRows > 0;
  57. }
  58. static async updateResult(id, status, errorMessage) {
  59. const query = 'UPDATE ota_tasks SET status = ?, progress = ?, error_message = ?, end_time = NOW() WHERE id = ?';
  60. const result = await (0, database_1.executeQuery)(query, [status, status === 'success' ? 100 : 0, errorMessage || null, id]);
  61. return result.affectedRows > 0;
  62. }
  63. static async delete(id) {
  64. const query = 'DELETE FROM ota_tasks WHERE id = ?';
  65. const result = await (0, database_1.executeQuery)(query, [id]);
  66. return result.affectedRows > 0;
  67. }
  68. static async createTable() {
  69. const query = `
  70. CREATE TABLE IF NOT EXISTS ota_tasks (
  71. id INT AUTO_INCREMENT PRIMARY KEY,
  72. device_id VARCHAR(255) NOT NULL,
  73. firmware_id INT NOT NULL,
  74. status ENUM('pending', 'downloading', 'installing', 'success', 'failed') DEFAULT 'pending',
  75. progress INT DEFAULT 0,
  76. error_message TEXT,
  77. start_time TIMESTAMP NULL,
  78. end_time TIMESTAMP NULL,
  79. created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  80. updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  81. INDEX idx_device_id (device_id),
  82. INDEX idx_firmware_id (firmware_id),
  83. INDEX idx_status (status),
  84. FOREIGN KEY (firmware_id) REFERENCES firmware_files(id) ON DELETE CASCADE
  85. )
  86. `;
  87. await (0, database_1.executeQuery)(query);
  88. }
  89. }
  90. exports.OTATaskModel = OTATaskModel;
  91. //# sourceMappingURL=ota.js.map