| | 1 | | using Microsoft.AspNetCore.Mvc; |
| | 2 | | using WebApi.Models; |
| | 3 | | using WebApi.Models.Dto; |
| | 4 | | using WebApi.Services; |
| | 5 | |
|
| | 6 | | namespace WebApi.Controllers |
| | 7 | | { |
| | 8 | | [ApiController] |
| | 9 | | [Route("[controller]")] |
| | 10 | | public class PostEnvironmentDataController : ControllerBase |
| | 11 | | { |
| | 12 | | private readonly ISensorDataService _sensorDataService; |
| | 13 | | private readonly ISensorGoalService _sensorGoalService; |
| | 14 | | private readonly IIOTControlService _iotControlService; |
| | 15 | |
|
| 2 | 16 | | public PostEnvironmentDataController(ISensorDataService sensorDataService, ISensorGoalService sensorGoalService, |
| 2 | 17 | | { |
| 2 | 18 | | _sensorDataService = sensorDataService; |
| 2 | 19 | | _sensorGoalService = sensorGoalService; |
| 2 | 20 | | _iotControlService = iotControlService; |
| 2 | 21 | | } |
| | 22 | |
|
| | 23 | | [HttpPost] |
| | 24 | | public async Task<IActionResult> PostSensorData([FromBody] IOTSensorDataDto data) |
| 2 | 25 | | { |
| | 26 | | try |
| 2 | 27 | | { |
| | 28 | | // Assuming your local time zone is +2 hours (Central European Time - CET) |
| 2 | 29 | | TimeZoneInfo localTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Europe/Copenhagen"); |
| | 30 | |
|
| 2 | 31 | | DateTime temperatureTimestamp = TimeZoneInfo.ConvertTime(DateTimeOffset.FromUnixTimeSeconds(data.Tempera |
| 2 | 32 | | DateTime humidityTimestamp = TimeZoneInfo.ConvertTime(DateTimeOffset.FromUnixTimeSeconds(data.Humidity_t |
| 2 | 33 | | DateTime co2Timestamp = TimeZoneInfo.ConvertTime(DateTimeOffset.FromUnixTimeSeconds(data.Co2_ts).UtcDate |
| | 34 | |
|
| | 35 | | // Get sensor goal for the current hallId |
| 2 | 36 | | SensorGoal? sensorGoal = await _sensorGoalService.GetSensorGoalAsync(data.HallId); |
| | 37 | |
|
| | 38 | | // Process incoming sensor data |
| 2 | 39 | | SensorData sensorData = new SensorData |
| 2 | 40 | | { |
| 2 | 41 | | HallId = data.HallId, |
| 2 | 42 | | Temperature = data.Temperature, |
| 2 | 43 | | Humidity = data.Humidity, |
| 2 | 44 | | CO2 = data.CO2, |
| 2 | 45 | | TemperatureTimestamp = temperatureTimestamp, |
| 2 | 46 | | HumidityTimestamp = humidityTimestamp, |
| 2 | 47 | | CO2Timestamp = co2Timestamp |
| 2 | 48 | | }; |
| | 49 | |
|
| 2 | 50 | | await _sensorDataService.AddSensorDataAsync(sensorData); |
| | 51 | |
|
| 1 | 52 | | bool? shouldWindowOpen = null; |
| | 53 | |
|
| 1 | 54 | | if (sensorGoal != null && sensorGoal.HallId == data.HallId) |
| 1 | 55 | | { |
| 1 | 56 | | if (_iotControlService.AreDesiredValuesReached( |
| 1 | 57 | | sensorData.Temperature, sensorGoal.DesiredTemperature, |
| 1 | 58 | | sensorData.Humidity, sensorGoal.DesiredHumidity, |
| 1 | 59 | | sensorData.CO2, sensorGoal.DesiredCo2)) |
| 0 | 60 | | { |
| 0 | 61 | | shouldWindowOpen = false; |
| 0 | 62 | | await _sensorGoalService.DeleteSensorGoalAsync(data.HallId); |
| 0 | 63 | | } |
| | 64 | | else |
| 1 | 65 | | { |
| 1 | 66 | | shouldWindowOpen = _iotControlService.ShouldWindowOpen( |
| 1 | 67 | | sensorData.Temperature, sensorGoal.DesiredTemperature, |
| 1 | 68 | | sensorData.Humidity, sensorGoal.DesiredHumidity, |
| 1 | 69 | | sensorData.CO2, sensorGoal.DesiredCo2 |
| 1 | 70 | | ); |
| 1 | 71 | | } |
| 1 | 72 | | } |
| | 73 | |
|
| 1 | 74 | | return Ok(new |
| 1 | 75 | | { |
| 1 | 76 | | success = true, |
| 1 | 77 | | code = 0, // Return zero for IoT device if method is successful |
| 1 | 78 | | hallId = data.HallId, |
| 1 | 79 | | openWindow = shouldWindowOpen ?? false |
| 1 | 80 | | }); |
| | 81 | |
|
| | 82 | | } |
| 1 | 83 | | catch (Exception e) |
| 1 | 84 | | { |
| 1 | 85 | | Console.WriteLine("Error: " + e.Message); |
| | 86 | | // Handle exceptions |
| 1 | 87 | | return BadRequest(new { success = false, error = e.Message }); |
| | 88 | | } |
| 2 | 89 | | } |
| | 90 | | } |
| | 91 | | } |