-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from Team-Wilhelm/mqtt-business
Mqtt business
- Loading branch information
Showing
16 changed files
with
219 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
namespace api.Options; | ||
namespace Core.Options; | ||
|
||
public class MqttOptions | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
using Infrastructure.Repositories; | ||
using Shared.Dtos; | ||
using Shared.Models.Exceptions; | ||
using Shared.Models.Information; | ||
|
||
namespace Core.Services; | ||
|
||
public class ConditionsLogsService (ConditionsLogsRepository conditionsLogsRepository, PlantRepository plantRepository) | ||
{ | ||
public async Task CreateConditionsLogAsync(CreateConditionsLogDto createConditionsLogDto) | ||
{ | ||
var plantId = await plantRepository.GetPlantIdByDeviceIdAsync(createConditionsLogDto.DeviceId.ToString()); | ||
|
||
if (plantId == Guid.Empty) | ||
{ | ||
throw new RegisterDeviceException(); | ||
} | ||
var conditionsLog = new ConditionsLog | ||
{ | ||
ConditionsId = new Guid(), | ||
TimeStamp = DateTime.UtcNow, //TODO get this from the right place | ||
SoilMoisture = CalculateSoilMoistureLevel(createConditionsLogDto.SoilMoisturePercentage), | ||
LightLevel = CalculateLightLevel(createConditionsLogDto.LightLevel), | ||
Temperature = CalculateTemperatureLevel(createConditionsLogDto.Temperature), | ||
Humidity = CalculateHumidityLevel(createConditionsLogDto.Humidity), | ||
PlantId = plantId | ||
}; | ||
|
||
conditionsLog.Mood = CalculateMood(conditionsLog); | ||
|
||
Console.WriteLine("Conditions log created"); | ||
Console.WriteLine(conditionsLog); | ||
|
||
await conditionsLogsRepository.CreateConditionsLogAsync(conditionsLog); | ||
} | ||
|
||
private RequirementLevel CalculateTemperatureLevel (double value) | ||
{ | ||
return value switch | ||
{ | ||
//TODO fix these values | ||
< 1 => RequirementLevel.Low, | ||
> 2 => RequirementLevel.High, | ||
_ => RequirementLevel.Medium | ||
}; | ||
} | ||
|
||
private RequirementLevel CalculateLightLevel (double value) | ||
{ | ||
return value switch | ||
{ | ||
//TODO fix these values | ||
< 1 => RequirementLevel.Low, | ||
> 2 => RequirementLevel.High, | ||
_ => RequirementLevel.Medium | ||
}; | ||
} | ||
|
||
private RequirementLevel CalculateSoilMoistureLevel (double value) | ||
{ | ||
return value switch | ||
{ | ||
//TODO fix these values | ||
< 1 => RequirementLevel.Low, | ||
> 2 => RequirementLevel.High, | ||
_ => RequirementLevel.Medium | ||
}; | ||
} | ||
|
||
private RequirementLevel CalculateHumidityLevel (double value) | ||
{ | ||
return value switch | ||
{ | ||
//TODO fix these values | ||
< 1 => RequirementLevel.Low, | ||
> 2 => RequirementLevel.High, | ||
_ => RequirementLevel.Medium | ||
}; | ||
} | ||
|
||
private int CalculateMood (Conditions conditions) | ||
{ | ||
// Compare ideal requirements for humidity, temperature, soil moisture and light level with actual conditions and calculate mood from 0-4 | ||
// get ideal requirements from plant | ||
var requirementsForPlant = plantRepository.GetRequirementsForPlant(conditions.PlantId); | ||
// compare with actual conditions | ||
var mood = 0; | ||
// calculate mood | ||
mood += CalculateScore((int)requirementsForPlant.Result.Humidity, (int)conditions.Humidity); | ||
mood += CalculateScore((int)requirementsForPlant.Result.Temperature, (int)conditions.Temperature); | ||
mood += CalculateScore((int)requirementsForPlant.Result.SoilMoisture, (int)conditions.SoilMoisture); | ||
mood += CalculateScore((int)requirementsForPlant.Result.LightLevel, (int)conditions.LightLevel); | ||
|
||
if (mood == 0) | ||
{ | ||
return 0; | ||
} | ||
return mood / 4; | ||
} | ||
|
||
private int CalculateScore(int ideal, int actual) | ||
{ | ||
var difference = Math.Abs(ideal - actual); | ||
switch (difference) | ||
{ | ||
case 0: | ||
return 4; // Exact match | ||
case 1: | ||
return 2; // One away | ||
default: | ||
return 0; // Two away | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,14 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Shared.Models.Information; | ||
|
||
namespace Infrastructure.Repositories; | ||
|
||
public class ConditionsLogsRepository | ||
public class ConditionsLogsRepository (IDbContextFactory<ApplicationDbContext> dbContextFactory) | ||
{ | ||
public async Task CreateConditionsLogAsync(ConditionsLog conditionsLog) | ||
{ | ||
await using var context = await dbContextFactory.CreateDbContextAsync(); | ||
await context.ConditionsLogs.AddAsync(conditionsLog); | ||
await context.SaveChangesAsync(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace Shared.Dtos; | ||
|
||
public class CreateConditionsLogDto | ||
{ | ||
public DateTime TimeStamp { get; set; } | ||
public double SoilMoisturePercentage { get; set; } | ||
public double LightLevel { get; set; } | ||
public double Temperature { get; set; } | ||
public double Humidity { get; set; } | ||
public long DeviceId { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace Shared.Models.Exceptions; | ||
|
||
public class RegisterDeviceException: AppException | ||
{ | ||
public RegisterDeviceException() : base("Please provide a device ID to the relevant plant.") | ||
{ | ||
} | ||
|
||
public RegisterDeviceException(string message) : base(message) | ||
{ | ||
} | ||
|
||
public RegisterDeviceException(string message, Exception innerException) : base(message, innerException) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
namespace api.Events.Global; | ||
|
||
public class ServerRespondsRegisterDevice : ServerSendsErrorMessage | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters