< Summary

Information
Class: WebApi.Controllers.PostEnvironmentDataController
Assembly: IndeklimaWebApi
File(s): /home/runner/work/SEP4/SEP4/backend/microservices/indeklima/WebApi/Controllers/IOT_Endpoints/PostEnvironmentDataController.cs
Line coverage
92%
Covered lines: 51
Uncovered lines: 4
Coverable lines: 55
Total lines: 91
Line coverage: 92.7%
Branch coverage
66%
Covered branches: 4
Total branches: 6
Branch coverage: 66.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
PostSensorData()66.66%6.02691.83%

File(s)

/home/runner/work/SEP4/SEP4/backend/microservices/indeklima/WebApi/Controllers/IOT_Endpoints/PostEnvironmentDataController.cs

#LineLine coverage
 1using Microsoft.AspNetCore.Mvc;
 2using WebApi.Models;
 3using WebApi.Models.Dto;
 4using WebApi.Services;
 5
 6namespace 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
 216        public PostEnvironmentDataController(ISensorDataService sensorDataService, ISensorGoalService sensorGoalService,
 217        {
 218            _sensorDataService = sensorDataService;
 219            _sensorGoalService = sensorGoalService;
 220            _iotControlService = iotControlService;
 221        }
 22
 23        [HttpPost]
 24        public async Task<IActionResult> PostSensorData([FromBody] IOTSensorDataDto data)
 225        {
 26            try
 227            {
 28                // Assuming your local time zone is +2 hours (Central European Time - CET)
 229                TimeZoneInfo localTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Europe/Copenhagen");
 30
 231                DateTime temperatureTimestamp = TimeZoneInfo.ConvertTime(DateTimeOffset.FromUnixTimeSeconds(data.Tempera
 232                DateTime humidityTimestamp = TimeZoneInfo.ConvertTime(DateTimeOffset.FromUnixTimeSeconds(data.Humidity_t
 233                DateTime co2Timestamp = TimeZoneInfo.ConvertTime(DateTimeOffset.FromUnixTimeSeconds(data.Co2_ts).UtcDate
 34
 35                // Get sensor goal for the current hallId
 236                SensorGoal? sensorGoal = await _sensorGoalService.GetSensorGoalAsync(data.HallId);
 37
 38                // Process incoming sensor data
 239                SensorData sensorData = new SensorData
 240                {
 241                    HallId = data.HallId,
 242                    Temperature = data.Temperature,
 243                    Humidity = data.Humidity,
 244                    CO2 = data.CO2,
 245                    TemperatureTimestamp = temperatureTimestamp,
 246                    HumidityTimestamp = humidityTimestamp,
 247                    CO2Timestamp = co2Timestamp
 248                };
 49
 250                await _sensorDataService.AddSensorDataAsync(sensorData);
 51
 152                bool? shouldWindowOpen = null;
 53
 154                if (sensorGoal != null && sensorGoal.HallId == data.HallId)
 155                {
 156                    if (_iotControlService.AreDesiredValuesReached(
 157                            sensorData.Temperature, sensorGoal.DesiredTemperature,
 158                            sensorData.Humidity, sensorGoal.DesiredHumidity,
 159                            sensorData.CO2, sensorGoal.DesiredCo2))
 060                    {
 061                        shouldWindowOpen = false;
 062                        await _sensorGoalService.DeleteSensorGoalAsync(data.HallId);
 063                    }
 64                    else
 165                    {
 166                        shouldWindowOpen = _iotControlService.ShouldWindowOpen(
 167                            sensorData.Temperature, sensorGoal.DesiredTemperature,
 168                            sensorData.Humidity, sensorGoal.DesiredHumidity,
 169                            sensorData.CO2, sensorGoal.DesiredCo2
 170                        );
 171                    }
 172                }
 73
 174                return Ok(new
 175                {
 176                    success = true,
 177                    code = 0, // Return zero for IoT device if method is successful
 178                    hallId = data.HallId,
 179                    openWindow = shouldWindowOpen ?? false
 180                });
 81
 82            }
 183            catch (Exception e)
 184            {
 185                Console.WriteLine("Error: " + e.Message);
 86                // Handle exceptions
 187                return BadRequest(new { success = false, error = e.Message });
 88            }
 289        }
 90    }
 91}