| | 1 | | using MongoDB.Driver; |
| | 2 | | using WebApi.Models; |
| | 3 | |
|
| | 4 | |
|
| | 5 | | namespace WebApi.DAO |
| | 6 | | { |
| | 7 | | public class SensorDataDAO : ISensorDataDAO |
| | 8 | | { |
| | 9 | | private readonly IMongoCollection<SensorData> _sensorDataMongoCollection; |
| | 10 | |
|
| | 11 | |
|
| 3 | 12 | | public SensorDataDAO(MongoDbContext context) |
| 3 | 13 | | { |
| 3 | 14 | | _sensorDataMongoCollection = context.Database.GetCollection<SensorData>("SensorData"); |
| 3 | 15 | | } |
| | 16 | |
|
| | 17 | | public async Task<List<SensorData>> GetSensorDataAsync(int hallId) |
| 3 | 18 | | { |
| | 19 | | try |
| 3 | 20 | | { |
| 3 | 21 | | return await _sensorDataMongoCollection.Find(s => s.HallId == hallId).ToListAsync(); |
| | 22 | | } |
| 0 | 23 | | catch (Exception ex) |
| 0 | 24 | | { |
| 0 | 25 | | throw new NullReferenceException(ex.Message); |
| | 26 | | } |
| 3 | 27 | | } |
| | 28 | | public async Task<List<SensorData>> GetSensorDataAsync(int hallId, int limit) |
| 0 | 29 | | { |
| | 30 | | try |
| 0 | 31 | | { |
| 0 | 32 | | return await _sensorDataMongoCollection.Find(s => s.HallId == hallId).Limit(limit).ToListAsync(); |
| | 33 | | } |
| 0 | 34 | | catch (Exception ex) |
| 0 | 35 | | { |
| 0 | 36 | | throw new NullReferenceException(ex.Message); |
| | 37 | | } |
| 0 | 38 | | } |
| | 39 | |
|
| | 40 | | public async Task<List<SensorData>> GetSensorDataAsync(int hallId, DateTime startDate, DateTime endDate) |
| 0 | 41 | | { |
| | 42 | | try |
| 0 | 43 | | { |
| 0 | 44 | | var filter = Builders<SensorData>.Filter.And( |
| 0 | 45 | | Builders<SensorData>.Filter.Eq(s => s.HallId, hallId), |
| 0 | 46 | | Builders<SensorData>.Filter.Or( |
| 0 | 47 | | Builders<SensorData>.Filter.And( |
| 0 | 48 | | Builders<SensorData>.Filter.Gte(s => s.TemperatureTimestamp, startDate), |
| 0 | 49 | | Builders<SensorData>.Filter.Lte(s => s.TemperatureTimestamp, endDate) |
| 0 | 50 | | ), |
| 0 | 51 | | Builders<SensorData>.Filter.And( |
| 0 | 52 | | Builders<SensorData>.Filter.Gte(s => s.HumidityTimestamp, startDate), |
| 0 | 53 | | Builders<SensorData>.Filter.Lte(s => s.HumidityTimestamp, endDate) |
| 0 | 54 | | ), |
| 0 | 55 | | Builders<SensorData>.Filter.And( |
| 0 | 56 | | Builders<SensorData>.Filter.Gte(s => s.CO2Timestamp, startDate), |
| 0 | 57 | | Builders<SensorData>.Filter.Lte(s => s.CO2Timestamp, endDate) |
| 0 | 58 | | ) |
| 0 | 59 | | ) |
| 0 | 60 | | ); |
| | 61 | |
|
| 0 | 62 | | return await _sensorDataMongoCollection.Find(filter).ToListAsync(); } |
| 0 | 63 | | catch (Exception ex) |
| 0 | 64 | | { |
| 0 | 65 | | throw new NullReferenceException(ex.Message); |
| | 66 | | } |
| 0 | 67 | | } |
| | 68 | |
|
| | 69 | | public async Task AddSensorDataAsync(SensorData sensorData) |
| 5 | 70 | | { |
| | 71 | | try |
| 5 | 72 | | { |
| 5 | 73 | | if (sensorData == null) |
| 0 | 74 | | { |
| 0 | 75 | | throw new ArgumentNullException(nameof(sensorData)); |
| | 76 | | } |
| | 77 | |
|
| | 78 | | // Check for duplicate data |
| 5 | 79 | | var duplicateData = await _sensorDataMongoCollection.Find(s => s.HallId == sensorData.HallId && ( |
| 5 | 80 | | s.TemperatureTimestamp == sensorData.TemperatureTimestamp || |
| 5 | 81 | | s.HumidityTimestamp == sensorData.HumidityTimestamp || |
| 5 | 82 | | s.CO2Timestamp == sensorData.CO2Timestamp) |
| 5 | 83 | | ).FirstOrDefaultAsync(); |
| 5 | 84 | | if (duplicateData != null) |
| 1 | 85 | | { |
| 1 | 86 | | throw new Exception("Duplicate data"); |
| | 87 | | } |
| | 88 | |
|
| | 89 | | //Add data to the collection |
| 4 | 90 | | await _sensorDataMongoCollection.InsertOneAsync(sensorData); |
| | 91 | | //Return 0 if successful |
| 4 | 92 | | return; |
| | 93 | | } |
| 1 | 94 | | catch (Exception ex) |
| 1 | 95 | | { |
| 1 | 96 | | throw new Exception(ex.Message); |
| | 97 | | } |
| 4 | 98 | | } |
| | 99 | |
|
| | 100 | | } |
| | 101 | | } |