| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.SensorDataModel = void 0;
- const database_1 = require("../config/database");
- class SensorDataModel {
- static async getAll(limit = 100, offset = 0) {
- const query = `
- SELECT * FROM sensor_data
- ORDER BY timestamp DESC
- LIMIT ? OFFSET ?
- `;
- return await (0, database_1.executeQuery)(query, [limit, offset]);
- }
- static async getByDeviceId(deviceId, limit = 100) {
- const query = `
- SELECT * FROM sensor_data
- WHERE device_id = ?
- ORDER BY timestamp DESC
- LIMIT ?
- `;
- return await (0, database_1.executeQuery)(query, [deviceId, limit]);
- }
- static async getByDeviceIdAndType(deviceId, dataType, limit = 100) {
- const query = `
- SELECT * FROM sensor_data
- WHERE device_id = ? AND data_type = ?
- ORDER BY timestamp DESC
- LIMIT ?
- `;
- return await (0, database_1.executeQuery)(query, [deviceId, dataType, limit]);
- }
- static async getByType(dataType, limit = 100) {
- const query = `
- SELECT * FROM sensor_data
- WHERE data_type = ?
- ORDER BY timestamp DESC
- LIMIT ?
- `;
- return await (0, database_1.executeQuery)(query, [dataType, limit]);
- }
- static async getByTimeRange(deviceId, dataType, hours = 24) {
- const query = `
- SELECT * FROM sensor_data
- WHERE device_id = ? AND data_type = ? AND timestamp >= DATE_SUB(NOW(), INTERVAL ? HOUR)
- ORDER BY timestamp ASC
- `;
- return await (0, database_1.executeQuery)(query, [deviceId, dataType, hours]);
- }
- static async getLatestByDevice(deviceId) {
- const query = `
- SELECT * FROM sensor_data
- WHERE device_id = ?
- ORDER BY timestamp DESC
- LIMIT 1
- `;
- return await (0, database_1.executeQuery)(query, [deviceId]);
- }
- static async getCount() {
- const query = 'SELECT COUNT(*) as count FROM sensor_data';
- const result = await (0, database_1.executeQuery)(query);
- return result[0].count;
- }
- static async getCountByDeviceId(deviceId) {
- const query = 'SELECT COUNT(*) as count FROM sensor_data WHERE device_id = ?';
- const result = await (0, database_1.executeQuery)(query, [deviceId]);
- return result[0].count;
- }
- static async getCountByType(dataType) {
- const query = 'SELECT COUNT(*) as count FROM sensor_data WHERE data_type = ?';
- const result = await (0, database_1.executeQuery)(query, [dataType]);
- return result[0].count;
- }
- static async insert(data) {
- const query = `
- INSERT INTO sensor_data (device_id, topic, data_type, value, timestamp)
- VALUES (?, ?, ?, ?, ?)
- `;
- const values = [
- data.device_id,
- data.topic || '',
- data.data_type,
- data.value,
- data.timestamp || new Date()
- ];
- const result = await (0, database_1.executeQuery)(query, values);
- return {
- id: result.insertId,
- ...data,
- created_at: new Date()
- };
- }
- static async updateLatestByDeviceAndType(deviceId, dataType, value) {
- const query = `
- UPDATE sensor_data
- SET value = ?, timestamp = NOW()
- WHERE id = (
- SELECT id FROM (
- SELECT id FROM sensor_data
- WHERE device_id = ? AND data_type = ?
- ORDER BY timestamp DESC
- LIMIT 1
- ) as temp
- )
- `;
- const result = await (0, database_1.executeQuery)(query, [value, deviceId, dataType]);
- return result.affectedRows > 0;
- }
- static async upsertByDeviceAndType(deviceId, dataType, value, topic) {
- const existingData = await this.getByDeviceIdAndType(deviceId, dataType, 1);
- if (existingData.length > 0) {
- await this.updateLatestByDeviceAndType(deviceId, dataType, value);
- }
- else {
- await this.insert({
- device_id: deviceId,
- topic: topic || `device/${deviceId}/${dataType}`,
- data_type: dataType,
- value: value,
- timestamp: new Date()
- });
- }
- }
- }
- exports.SensorDataModel = SensorDataModel;
- //# sourceMappingURL=sensorData.js.map
|