Skip to content

Commit

Permalink
adjust creation of historic data
Browse files Browse the repository at this point in the history
  • Loading branch information
juuwel committed May 28, 2024
1 parent 97e24fd commit 4359ef5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Shared/Dtos/CreateConditionsLogDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ public class CreateConditionsLogDto
public double Light { get; set; }
public double Temperature { get; set; }
public double Humidity { get; set; }
public long DeviceId { get; set; }
public required string DeviceId { get; set; }
}
3 changes: 1 addition & 2 deletions api/Core/Services/MqttPublisherService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ public MqttPublisherService(IOptions<MqttOptions> options)
if (string.IsNullOrEmpty(_options.Value.Username) || _options.Value.Username == "FILL_ME_IN")
throw new Exception("MQTT username not set in appsettings.json");
}
public async Task PublishAsync(MoodDto mood, long deviceId)
public async Task PublishAsync(MoodDto mood, string deviceId)
{
var mqttFactory = new MqttFactory();


using var mqttClient = mqttFactory.CreateMqttClient();
var mqttClientOptions = new MqttClientOptionsBuilder()
Expand Down
20 changes: 16 additions & 4 deletions api/DbInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private async Task CreateCollections()
var succulents = await collectionService.CreateCollection(
new CreateCollectionDto
{
Name = "Ferns",
Name = "Succulents",
},
DefaultUserEmail
);
Expand Down Expand Up @@ -152,13 +152,14 @@ private async Task CreatePlants()

private async Task CreateHistoricalData()
{
// Create 100 logs for each plant
var conditionsLogService = _scope.ServiceProvider.GetRequiredService<ConditionsLogsService>();
for (var i = 0; i < 100; i++)
{
await conditionsLogService.CreateConditionsLogAsync(
new CreateConditionsLogDto
{
DeviceId = long.Parse(_plants["Aloe Vera"].DeviceId!),
DeviceId = _plants["Aloe Vera"].DeviceId!,
SoilMoisturePercentage = GetRandomLevelValue(),
Light = GetRandomLevelValue(),
Temperature = GetRandomTemperature(),
Expand All @@ -168,7 +169,7 @@ await conditionsLogService.CreateConditionsLogAsync(
await conditionsLogService.CreateConditionsLogAsync(
new CreateConditionsLogDto
{
DeviceId = long.Parse(_plants["Prickly Pear"].DeviceId!),
DeviceId = _plants["Prickly Pear"].DeviceId!,
SoilMoisturePercentage = GetRandomLevelValue(),
Light = GetRandomLevelValue(),
Temperature = GetRandomTemperature(),
Expand All @@ -177,12 +178,23 @@ await conditionsLogService.CreateConditionsLogAsync(
);
}

// Adjust the timestamps to be in past
var db = await _scope.ServiceProvider.GetRequiredService<IDbContextFactory<ApplicationDbContext>>().CreateDbContextAsync();
var logs = await db.ConditionsLogs.ToListAsync();
for (var i = 0; i < 100; i++)
{
logs[i].TimeStamp = logs[i].TimeStamp.AddDays(-i);
logs[i + 100].TimeStamp = logs[i + 100].TimeStamp.AddDays(-i);
}
db.UpdateRange(logs);
await db.SaveChangesAsync();

var dyingPlant = _plants["Dying plant"];
await conditionsLogService.CreateConditionsLogAsync(

new CreateConditionsLogDto
{
DeviceId = long.Parse(dyingPlant.DeviceId!),
DeviceId = dyingPlant.DeviceId!,
SoilMoisturePercentage = GetValueOutsideOfIdealRange(dyingPlant.Requirements!.SoilMoistureLevel),
Light = GetValueOutsideOfIdealRange(dyingPlant.Requirements!.LightLevel),
Temperature = dyingPlant.Requirements!.TemperatureLevel - 10,
Expand Down

0 comments on commit 4359ef5

Please sign in to comment.