sensorData.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.SensorDataModel = void 0;
  4. const database_1 = require("../config/database");
  5. class SensorDataModel {
  6. static async getAll(limit = 100, offset = 0) {
  7. const query = `
  8. SELECT * FROM sensor_data
  9. ORDER BY timestamp DESC
  10. LIMIT ? OFFSET ?
  11. `;
  12. return await (0, database_1.executeQuery)(query, [limit, offset]);
  13. }
  14. static async getByDeviceId(deviceId, limit = 100) {
  15. const query = `
  16. SELECT * FROM sensor_data
  17. WHERE device_id = ?
  18. ORDER BY timestamp DESC
  19. LIMIT ?
  20. `;
  21. return await (0, database_1.executeQuery)(query, [deviceId, limit]);
  22. }
  23. static async getByDeviceIdAndType(deviceId, dataType, limit = 100) {
  24. const query = `
  25. SELECT * FROM sensor_data
  26. WHERE device_id = ? AND data_type = ?
  27. ORDER BY timestamp DESC
  28. LIMIT ?
  29. `;
  30. return await (0, database_1.executeQuery)(query, [deviceId, dataType, limit]);
  31. }
  32. static async getByType(dataType, limit = 100) {
  33. const query = `
  34. SELECT * FROM sensor_data
  35. WHERE data_type = ?
  36. ORDER BY timestamp DESC
  37. LIMIT ?
  38. `;
  39. return await (0, database_1.executeQuery)(query, [dataType, limit]);
  40. }
  41. static async getByTimeRange(deviceId, dataType, hours = 24) {
  42. const query = `
  43. SELECT * FROM sensor_data
  44. WHERE device_id = ? AND data_type = ? AND timestamp >= DATE_SUB(NOW(), INTERVAL ? HOUR)
  45. ORDER BY timestamp ASC
  46. `;
  47. return await (0, database_1.executeQuery)(query, [deviceId, dataType, hours]);
  48. }
  49. static async getLatestByDevice(deviceId) {
  50. const query = `
  51. SELECT * FROM sensor_data
  52. WHERE device_id = ?
  53. ORDER BY timestamp DESC
  54. LIMIT 1
  55. `;
  56. return await (0, database_1.executeQuery)(query, [deviceId]);
  57. }
  58. static async getCount() {
  59. const query = 'SELECT COUNT(*) as count FROM sensor_data';
  60. const result = await (0, database_1.executeQuery)(query);
  61. return result[0].count;
  62. }
  63. static async getCountByDeviceId(deviceId) {
  64. const query = 'SELECT COUNT(*) as count FROM sensor_data WHERE device_id = ?';
  65. const result = await (0, database_1.executeQuery)(query, [deviceId]);
  66. return result[0].count;
  67. }
  68. static async getCountByType(dataType) {
  69. const query = 'SELECT COUNT(*) as count FROM sensor_data WHERE data_type = ?';
  70. const result = await (0, database_1.executeQuery)(query, [dataType]);
  71. return result[0].count;
  72. }
  73. static async insert(data) {
  74. const query = `
  75. INSERT INTO sensor_data (device_id, topic, data_type, value, timestamp)
  76. VALUES (?, ?, ?, ?, ?)
  77. `;
  78. const values = [
  79. data.device_id,
  80. data.topic || '',
  81. data.data_type,
  82. data.value,
  83. data.timestamp || new Date()
  84. ];
  85. const result = await (0, database_1.executeQuery)(query, values);
  86. return {
  87. id: result.insertId,
  88. ...data,
  89. created_at: new Date()
  90. };
  91. }
  92. static async updateLatestByDeviceAndType(deviceId, dataType, value) {
  93. const query = `
  94. UPDATE sensor_data
  95. SET value = ?, timestamp = NOW()
  96. WHERE id = (
  97. SELECT id FROM (
  98. SELECT id FROM sensor_data
  99. WHERE device_id = ? AND data_type = ?
  100. ORDER BY timestamp DESC
  101. LIMIT 1
  102. ) as temp
  103. )
  104. `;
  105. const result = await (0, database_1.executeQuery)(query, [value, deviceId, dataType]);
  106. return result.affectedRows > 0;
  107. }
  108. static async upsertByDeviceAndType(deviceId, dataType, value, topic) {
  109. const existingData = await this.getByDeviceIdAndType(deviceId, dataType, 1);
  110. if (existingData.length > 0) {
  111. await this.updateLatestByDeviceAndType(deviceId, dataType, value);
  112. }
  113. else {
  114. await this.insert({
  115. device_id: deviceId,
  116. topic: topic || `device/${deviceId}/${dataType}`,
  117. data_type: dataType,
  118. value: value,
  119. timestamp: new Date()
  120. });
  121. }
  122. }
  123. }
  124. exports.SensorDataModel = SensorDataModel;
  125. //# sourceMappingURL=sensorData.js.map