Skip to content

Commit

Permalink
implement env variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Joren-vanGoethem committed Dec 20, 2023
1 parent fb0f708 commit 8eb5ee4
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 71 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/docker-portainer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ jobs:
steps:
- name: Call Webhooks for Portainer redeployment
run: |
IFS=',' read -ra WEBHOOKS <<< "${WEBHOOK_URLS}"
for webhook in "${WEBHOOKS[@]}"; do
curl -X POST "$webhook"
done
IFS=',' read -ra WEBHOOKS <<< "${WEBHOOK_URLS}"
for webhook in "${WEBHOOKS[@]}"; do
curl -X POST "$webhook"
done
env:
WEBHOOK_URLS: ${{ secrets.WEBHOOK_URL }}
WEBHOOK_URLS: ${{ secrets.WEBHOOK_URL }}
22 changes: 11 additions & 11 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 6.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore
- name: Test
run: dotnet test --no-build --verbosity normal
- uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 6.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore
- name: Test
run: dotnet test --no-build --verbosity normal
6 changes: 0 additions & 6 deletions Importer/Importer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.3"/>
</ItemGroup>

<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\DKSRDomain\DKSRDomain.csproj"/>
<ProjectReference Include="..\FrostApi\FrostApi.csproj"/>
Expand Down
29 changes: 17 additions & 12 deletions Importer/Importers/Importer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,29 @@
using FrostApi.ResponseModels.ObservedProperty;
using FrostApi.ResponseModels.Sensor;
using FrostApi.ResponseModels.Thing;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace Importer.Importers;

public abstract class Importer
{
private readonly string _dataStreamName;
private readonly FrostApi.FrostApi _frostApi;
protected readonly HttpClient Client;
protected readonly string DataType;
private readonly FrostApi.FrostApi _frostApi;
protected readonly ILogger Logger;

protected Importer(ILogger logger, IConfiguration config, string dataType, string dataStreamName)
protected Importer(ILogger logger, string dataType, string dataStreamName)
{
_frostApi = new FrostApi.FrostApi(config["FrostBaseUrl"] ?? throw new ArgumentNullException("FrostBaseUrl"));
Username = config["Authentication:Username"] ?? throw new ArgumentNullException("Username");
Password = config["Authentication:Password"] ?? throw new ArgumentNullException("Password");
_frostApi = new FrostApi.FrostApi(Environment.GetEnvironmentVariable("FROST_SERVER_BASE_URL") ??
throw new Exception("FROST_SERVER_BASE_URL not set"));
Username = Environment.GetEnvironmentVariable("DKSR_HISTORIC_USERNAME") ?? throw new Exception("DKSR_HISTORIC_USERNAME not set");
Password = Environment.GetEnvironmentVariable("DKSR_HISTORIC_PASSWORD") ?? throw new Exception("DKSR_HISTORIC_PASSWORD not set");
Client = SetupHttpClient();
Logger = logger;
DataType = dataType;
_dataStreamName = dataStreamName;

Logger.LogInformation($"Starting {DataType} Sensor Data Collection");
}

Expand Down Expand Up @@ -68,6 +68,7 @@ protected Task CreateNewThing(Thing thing)
Logger.LogError($"{DataType} {thing.Name} with Id {thing.Properties["Id"]} could not be created");
return Task.CompletedTask;
}

protected async Task Update(Thing thing)
{
Logger.LogDebug($"{DataType} {thing.Name} with Id {thing.Id} found in Frost, updating...");
Expand All @@ -90,7 +91,7 @@ protected async Task Update(Thing thing)
}
}


private async Task CreateLocationIfNotExists(Thing thing)
{
var locations = await _frostApi.Locations.GetLocationsForThing(thing.Id);
Expand All @@ -106,6 +107,7 @@ private async Task CreateLocationIfNotExists(Thing thing)
}
}
}

private async Task CreateNewLocation(Thing thing)
{
var location = new ThingLocation
Expand Down Expand Up @@ -174,6 +176,7 @@ await CreateNewSensor(new Sensor

return sensorResponse;
}

private async Task CreateNewSensor(Sensor sensor)
{
var response = await _frostApi.Sensors.PostSensor(sensor);
Expand Down Expand Up @@ -210,6 +213,7 @@ await CreateNewObservedProperty(new ObservedProperty

return healthStateObservedPropertyResponse;
}

private async Task CreateNewObservedProperty(ObservedProperty observedProperty)
{
var response = await _frostApi.ObservedProperties.PostObservedProperty(observedProperty);
Expand All @@ -221,7 +225,7 @@ private async Task CreateNewObservedProperty(ObservedProperty observedProperty)
$"ObservedProperty {observedProperty.Name} with Id {observedProperty.Id} could not be created");
}


private async Task AddObservation(Thing thing, DataStream dataStream)
{
var observations = await _frostApi.Observations.GetObservationsForDataStream(dataStream.Id);
Expand All @@ -248,8 +252,8 @@ private async Task AddObservation(Thing thing, DataStream dataStream)
Logger.LogError(response.Content.ReadAsStringAsync().Result);
}
}


private async Task<GetDataStreamsResponse> GetOrCreateDataStream(Thing thing)
{
var dataStreams = await GetFrostDataStreamData(thing.Id);
Expand All @@ -267,6 +271,7 @@ private async Task<GetDataStreamsResponse> GetOrCreateDataStream(Thing thing)

return dataStreams;
}

private async Task CreateNewDataStream(Thing thing)
{
var observedPropertyResponse = await GetOrCreateObservedProperty();
Expand Down Expand Up @@ -305,7 +310,7 @@ protected Task<GetThingsResponse> GetFrostThingData(int id)
{
return _frostApi.Things.GetAllThings($"?$filter=description eq '{DataType}' &$filter= properties/Id eq '{id}'");
}

protected Task<GetThingsResponse> GetFrostThingData(string id)
{
return _frostApi.Things.GetAllThings($"?$filter=description eq '{DataType}' &$filter= properties/Id eq '{id}'");
Expand Down
9 changes: 4 additions & 5 deletions Importer/Importers/ParkingLotImporter.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using DKSRDomain;
using FrostApi.Models.Thing;
using Importer.Constants;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace Importer.Importers;
Expand All @@ -10,11 +9,11 @@ public class ParkingLotImporter : Importer
{
private Timer _importerTimer;

public ParkingLotImporter(ILogger logger, IConfiguration config) : base(logger, config, "ParkingLot", "Occupancy")
public ParkingLotImporter(ILogger logger) : base(logger, "ParkingLot", "Occupancy")
{
_importerTimer = new Timer(Import, null, 0, 60 * 1000); // every minute
_importerTimer = new Timer(Import, null, 0, 60 * 1000 * 2); // every 2 minutes
}

protected override async void Import(object? _) // object? required for running in a timer
{
try
Expand All @@ -32,7 +31,7 @@ public ParkingLotImporter(ILogger logger, IConfiguration config) : base(logger,
await CreateNewThing(thing);
frostThing = await GetFrostThingData(dksrParkingLot.Sid);
}

if (frostThing.Value.Count < 1)
throw new Exception($"Creating new thing with id {dksrParkingLot.Sid} seems to have failed...");

Expand Down
8 changes: 2 additions & 6 deletions Importer/Importers/TreeImporter.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using DKSRDomain;
using FrostApi.Models.Thing;
using Importer.Constants;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace Importer.Importers;
Expand All @@ -10,9 +9,9 @@ public class TreeImporter : Importer
{
private Timer _importerTimer;

public TreeImporter(ILogger logger, IConfiguration config) : base(logger, config, "Tree", "HealthState")
public TreeImporter(ILogger logger) : base(logger, "Tree", "HealthState")
{
_importerTimer = new Timer(Import, null, 0, 60 * 1000); // every minute
_importerTimer = new Timer(Import, null, 0, 60 * 1000 * 60); // every hour
}

protected override async void Import(object? _)
Expand All @@ -22,7 +21,6 @@ protected override async void Import(object? _)
Logger.LogInformation($"Updating {DataType} Data...");
var data = await GetDksrData();
foreach (var dksrTree in data.SensorData)
{
try
{
Thing thing;
Expand All @@ -45,8 +43,6 @@ protected override async void Import(object? _)
{
Logger.LogError(e.ToString());
}
}

}
catch (Exception e)
{
Expand Down
10 changes: 5 additions & 5 deletions Importer/Mappers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ public static class Mappers
public static Thing MapDksrResponse(TreeSenseSensorData treeData, string dataType)
{
var properties = new Dictionary<string, string> { { "Id", treeData.Sid } };

var observation = new Observation { Result = treeData.HealthState, PhenomenonTime = treeData.Timestamp };

return Thing.Create(dataType, treeData.Sid, properties, treeData.Lat, treeData.Lng, observation);
}

public static Thing MapDksrResponse(ParkingLotSensorData parkingLot, string dataType)
{
var properties = new Dictionary<string, string>
{
{ "Id", parkingLot.Sid}, { "ParkingSpaceId", parkingLot.ParkingSpaceId.ToString() },
{ "Id", parkingLot.Sid }, { "ParkingSpaceId", parkingLot.ParkingSpaceId.ToString() },
{ "ParkingLotId", parkingLot.ParkingLotId.ToString() }
};

var observation = new Observation { Result = parkingLot.Occupied, PhenomenonTime = parkingLot.Timestamp };

return Thing.Create(dataType, parkingLot.Sid, properties, parkingLot.Lat, parkingLot.Lon, observation);
}

Expand Down
18 changes: 4 additions & 14 deletions Importer/Program.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,25 @@
using System.Diagnostics;
using Importer.Importers;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace Importer;

internal class Program
{
private static ILogger<Program> _logger;
private static IConfiguration _config;
private static ILogger<Program>? _logger;

private static async Task<int> Main(string[] args)
private static void Main(string[] args)
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", false);

_config = builder.Build();

var services = new ServiceCollection();
ConfigureServices(services);
IServiceProvider serviceProvider = services.BuildServiceProvider();
_logger = serviceProvider.GetRequiredService<ILogger<Program>>();

var treeImporter = new TreeImporter(_logger, _config);
var parkingLotImporter = new ParkingLotImporter(_logger, _config);
var treeImporter = new TreeImporter(_logger);
var parkingLotImporter = new ParkingLotImporter(_logger);

Process.GetCurrentProcess().WaitForExit();

return 1;
}

private static void ConfigureServices(IServiceCollection services)
Expand Down
7 changes: 0 additions & 7 deletions Importer/appsettings.json

This file was deleted.

2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
build:
context: .
dockerfile: Importer/Dockerfile
env_file:
- stack.env
restart: unless-stopped

0 comments on commit 8eb5ee4

Please sign in to comment.