Skip to content

Commit

Permalink
Daily check-in, implementing dependency injection for data connection…
Browse files Browse the repository at this point in the history
…. Work in progress
  • Loading branch information
Didosa committed May 5, 2024
1 parent 4a49618 commit aae4c7f
Show file tree
Hide file tree
Showing 30 changed files with 574 additions and 181 deletions.
2 changes: 1 addition & 1 deletion GPSDataCreator/GPSDataCreator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Configuration.ConfigurationManager" Version="6.0.0" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
12 changes: 6 additions & 6 deletions PiLot.sln
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PiLotBackupClient", "PiLotB
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PiLotConfig", "PiLotConfig\PiLotConfig.csproj", "{4A3189CC-C622-4C21-853B-4D3AFC131C33}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PiLotDemoDataSender", "PiLotDemoDataSender\PiLotDemoDataSender.csproj", "{74031253-0084-4FD8-BBF4-15E5E26FF65C}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "sql", "sql", "{B438012B-4438-45DA-99BE-1089E803FAF7}"
ProjectSection(SolutionItems) = preProject
installScripts\sql\pilotDb.sql = installScripts\sql\pilotDb.sql
Expand All @@ -135,6 +133,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PiLotGPIO", "PiLotGPIO\PiLo
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PiLotTilesDownloader", "PiLotTilesDownloader\PiLotTilesDownloader.csproj", "{A1E25012-96D1-49AE-9E13-119B4239FB0D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PiLotData", "PiLotData\PiLotData.csproj", "{3E0B653F-0490-4D5A-A6EE-DA2F4BD3B192}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -205,10 +205,6 @@ Global
{4A3189CC-C622-4C21-853B-4D3AFC131C33}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4A3189CC-C622-4C21-853B-4D3AFC131C33}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4A3189CC-C622-4C21-853B-4D3AFC131C33}.Release|Any CPU.Build.0 = Release|Any CPU
{74031253-0084-4FD8-BBF4-15E5E26FF65C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{74031253-0084-4FD8-BBF4-15E5E26FF65C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{74031253-0084-4FD8-BBF4-15E5E26FF65C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{74031253-0084-4FD8-BBF4-15E5E26FF65C}.Release|Any CPU.Build.0 = Release|Any CPU
{5519B99B-064C-4271-A4B8-73100EEA7386}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5519B99B-064C-4271-A4B8-73100EEA7386}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5519B99B-064C-4271-A4B8-73100EEA7386}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -217,6 +213,10 @@ Global
{A1E25012-96D1-49AE-9E13-119B4239FB0D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A1E25012-96D1-49AE-9E13-119B4239FB0D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A1E25012-96D1-49AE-9E13-119B4239FB0D}.Release|Any CPU.Build.0 = Release|Any CPU
{3E0B653F-0490-4D5A-A6EE-DA2F4BD3B192}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3E0B653F-0490-4D5A-A6EE-DA2F4BD3B192}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3E0B653F-0490-4D5A-A6EE-DA2F4BD3B192}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3E0B653F-0490-4D5A-A6EE-DA2F4BD3B192}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
11 changes: 11 additions & 0 deletions PiLotAPICore/Controllers/AnchorWatchController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using PiLot.API.ActionFilters;
using PiLot.API.Workers;
using PiLot.Model.Nav;
using System;

namespace PiLot.API.Controllers {

Expand All @@ -22,6 +23,16 @@ public AnchorWatch Get() {
return AnchorWatchWorker.Instance.GetAnchorWatch();
}

/// <summary>
/// plays the alarm sound to test
/// </summary>
[Route(Program.APIROOT + "[controller]/test")]
[HttpGet]
[ServiceFilter(typeof(ReadAuthorizationFilter))]
public String GetTestSound(Int32? alarmIndex) {
return AnchorWatchWorker.Instance.PlayTestAlarm(alarmIndex);
}

/// <summary>
/// Saves the anchorWatch
/// </summary>
Expand Down
9 changes: 5 additions & 4 deletions PiLotAPICore/Controllers/PoiCategoriesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.AspNetCore.Mvc;

using PiLot.API.ActionFilters;
using PiLot.API.Helpers;
using PiLot.Data.Postgres.Nav;
using PiLot.Model.Nav;
using PiLot.Utils.Logger;
Expand All @@ -23,7 +24,7 @@ public class PoiCategoriesController : ControllerBase {
[ServiceFilter(typeof(ReadAuthorizationFilter))]
public List<PoiCategory> GetCategories( ) {
Logger.Log("PoisController.GetPoiCategories", LogLevels.DEBUG);
return new PoiDataConnector().ReadPoiCategories();
return DataConnectionHelper.PoiDataConnector.ReadPoiCategories();
}

/// <summary>
Expand All @@ -35,7 +36,7 @@ public List<PoiCategory> GetCategories( ) {
[ServiceFilter(typeof(WriteAuthorizationFilter))]
public Int32 PutCategory(PoiCategory category) {
Logger.Log("PoisController.PutCategory", LogLevels.DEBUG);
return new PoiDataConnector().SavePoiCategory(category);
return DataConnectionHelper.PoiDataConnector.SavePoiCategory(category);
}

/// <summary>
Expand All @@ -48,8 +49,8 @@ public Int32 PutCategory(PoiCategory category) {
[ServiceFilter(typeof(WriteAuthorizationFilter))]
public ActionResult DeleteCategory(Int32 categoryId) {
Logger.Log("PoisController.DeleteCategory", LogLevels.DEBUG);
Int32 result = new PoiDataConnector().DeletePoiCategory(categoryId);
if(result == 1) {
Boolean result = DataConnectionHelper.PoiDataConnector.DeletePoiCategory(categoryId);
if(result) {
return this.Ok();
} else {
return this.BadRequest("The poi category is in use");
Expand Down
10 changes: 5 additions & 5 deletions PiLotAPICore/Controllers/PoiFeaturesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Microsoft.AspNetCore.Mvc;

using PiLot.API.ActionFilters;
using PiLot.Data.Postgres.Nav;
using PiLot.API.Helpers;
using PiLot.Model.Nav;
using PiLot.Utils.Logger;

Expand All @@ -23,7 +23,7 @@ public class PoiFeaturesController : ControllerBase {
[ServiceFilter(typeof(ReadAuthorizationFilter))]
public List<PoiFeature> GetFeatures() {
Logger.Log("PoisController.GetPoiFeatures", LogLevels.DEBUG);
return new PoiDataConnector().ReadPoiFeatures();
return DataConnectionHelper.PoiDataConnector.ReadPoiFeatures();
}

/// <summary>
Expand All @@ -35,7 +35,7 @@ public List<PoiFeature> GetFeatures() {
[ServiceFilter(typeof(WriteAuthorizationFilter))]
public Int32 PutFeature(PoiFeature feature) {
Logger.Log("PoisController.PutFeature", LogLevels.DEBUG);
return new PoiDataConnector().SavePoiFeature(feature);
return DataConnectionHelper.PoiDataConnector.SavePoiFeature(feature);
}

/// <summary>
Expand All @@ -47,8 +47,8 @@ public Int32 PutFeature(PoiFeature feature) {
[ServiceFilter(typeof(WriteAuthorizationFilter))]
public ActionResult DeleteFeature(Int32 featureId) {
Logger.Log("PoisController.DeleteFeature", LogLevels.DEBUG);
Int32 result = new PoiDataConnector().DeletePoiFeature(featureId);
if (result == 1) {
Boolean result = DataConnectionHelper.PoiDataConnector.DeletePoiFeature(featureId);
if (result) {
return this.Ok();
} else {
return this.BadRequest("The poi feature is in use");
Expand Down
13 changes: 7 additions & 6 deletions PiLotAPICore/Controllers/PoisController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.AspNetCore.Mvc;

using PiLot.API.ActionFilters;
using PiLot.API.Helpers;
using PiLot.Data.Postgres.Nav;
using PiLot.Model.Nav;
using PiLot.Utils.Logger;
Expand Down Expand Up @@ -48,7 +49,7 @@ [FromQuery] String features
Logger.Log(ex, "PoisController.Get");
throw;
}
return new PoiDataConnector().FindPois(minLat, minLon, maxLat, maxLon, categoriesInt, featuresInt);
return DataConnectionHelper.PoiDataConnector.FindPois(minLat, minLon, maxLat, maxLon, categoriesInt, featuresInt);
}

/// <summary>
Expand All @@ -59,7 +60,7 @@ [FromQuery] String features
[ServiceFilter(typeof(ReadAuthorizationFilter))]
public List<Poi> Get() {
Logger.Log("PoisController.Get", LogLevels.DEBUG);
return new PoiDataConnector().ReadPois();
return DataConnectionHelper.PoiDataConnector.ReadPois();
}

/// <summary>
Expand All @@ -71,7 +72,7 @@ public List<Poi> Get() {
[Route(Program.APIROOT + "[controller]")]
[ServiceFilter(typeof(WriteAuthorizationFilter))]
public Int64 Put(Poi poi) {
return new PoiDataConnector().SavePoi(poi);
return DataConnectionHelper.PoiDataConnector.SavePoi(poi);
}

/// <summary>
Expand All @@ -83,7 +84,7 @@ public Int64 Put(Poi poi) {
[ServiceFilter(typeof(ReadAuthorizationFilter))]
public Object[] Get(Int64 id) {
Logger.Log("PoisController.Get", LogLevels.DEBUG);
return new PoiDataConnector().ReadPoi(id);
return DataConnectionHelper.PoiDataConnector.ReadPoi(id);
}

/// <summary>
Expand All @@ -97,7 +98,7 @@ public Object[] Get(Int64 id) {
[ServiceFilter(typeof(ReadAuthorizationFilter))]
public Object[] Get(String id, String source) {
Logger.Log("PoisController.Get (external)", LogLevels.DEBUG);
return new PoiDataConnector().ReadExternalPoi(source, id);
return DataConnectionHelper.PoiDataConnector.ReadExternalPoi(source, id);
}

/// <summary>
Expand All @@ -108,7 +109,7 @@ public Object[] Get(String id, String source) {
[ServiceFilter(typeof(WriteAuthorizationFilter))]
public void Delete(Int64 id) {
Logger.Log("PoisController.Delete", LogLevels.DEBUG);
new PoiDataConnector().DeletePoi(id);
DataConnectionHelper.PoiDataConnector.DeletePoi(id);
}

/// <summary>
Expand Down
3 changes: 0 additions & 3 deletions PiLotAPICore/Controllers/PositionController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public void PutPosition(TrackPoint pTrackPoint) {
if (pTrackPoint != null) {
Int32? trackId = GPSCache.Instance.AddTrackPoint(pTrackPoint);
if(trackId != null) {
//TrackStatisticsWorker.Instance.EnsureStatistics(trackId.Value);
TrackStatisticsHelper.UpdateStatistics(trackId.Value);
}
}
Expand All @@ -67,7 +66,6 @@ public void PutPositionsLocal(TrackPoint pTrackPoint) {
GpsTimeSync.Instance.HandleGPSRecord(pTrackPoint);
Int32? trackId = GPSCache.Instance.AddTrackPoint(pTrackPoint);
if (trackId != null) {
//TrackStatisticsWorker.Instance.EnsureStatistics(trackId.Value);
TrackStatisticsHelper.UpdateStatistics(trackId.Value);
}
}
Expand All @@ -92,7 +90,6 @@ public void PutPositions(TrackPoint[] pTrackPoints) {
trackId = cache.AddTrackPoint(aTrackPoint);
if (trackId != null && (lastTrackId == null || lastTrackId != trackId)){
if(lastTrackId != null) {
//TrackStatisticsWorker.Instance.EnsureStatistics(trackId.Value);
TrackStatisticsHelper.UpdateStatistics(trackId.Value);
}
lastTrackId = trackId;
Expand Down
42 changes: 6 additions & 36 deletions PiLotAPICore/Controllers/TracksController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

using PiLot.API.ActionFilters;
using PiLot.API.Helpers;
using PiLot.API.Workers;

using PiLot.Data.Postgres.Nav;
using PiLot.Model.Nav;
Expand Down Expand Up @@ -32,24 +31,6 @@ public List<Track> GetTracks(Int64 startTime, Int64 endTime, Boolean isBoatTime)
return tracks;
}

/// <summary>
/// Gets the track segments for a track defined by a certain range
/// </summary>
/// <param name="startTime">Starttime in ms utc or boatTime</param>
/// <param name="endTime">Endtime in ms utc or boatTime</param>
/// <param name="isBoatTime">If true, start and end are BoatTime, else UTC</param>
[Route(Program.APIROOT + "[controller]/Segments")]
[HttpGet]
[ServiceFilter(typeof(ReadAuthorizationFilter))]
public List<TrackSegment> GetTrackSegments(Int32 startTime, Int64 endTime, Boolean isBoatTime) {
Track track = new PiLot.Data.Files.TrackDataConnector().ReadTrack(startTime, endTime, isBoatTime);
// todo: read persisted track segments, make sure the track is analyzed before. Then just
// load the segments based on the track id, loading the TrackSegmentTypes will not be needed.
List<TrackSegmentType> types = new TrackDataConnector().ReadTrackSegmentTypes();
List<TrackSegment> segments = new TrackAnalyzer(track).GetTrackSegments(types);
return segments;
}

/// <summary>
/// Gets the track segments for a track defined by its id.
/// </summary>
Expand All @@ -58,16 +39,7 @@ public List<TrackSegment> GetTrackSegments(Int32 startTime, Int64 endTime, Boole
[HttpGet]
[ServiceFilter(typeof(ReadAuthorizationFilter))]
public List<TrackSegment> GetTrackSegments(Int32 id) {
// todo: don't read the track first, but directly read the persisted segments
List<TrackSegment> segments;
Track track = new TrackDataConnector().ReadTrack(id);
if (track != null) {
List<TrackSegmentType> types = new TrackDataConnector().ReadTrackSegmentTypes();
segments = new TrackAnalyzer(track).GetTrackSegments(types);
} else {
segments = new();
}
return segments;
return new TrackDataConnector().ReadTrackSegments(id);
}

/// <summary>
Expand All @@ -82,17 +54,16 @@ public Boolean[] Get(Int32 year, Int32 month) {
}

/// <summary>
/// Saves a track
/// Saves a track. The Track must have ID=null. To update a track,
/// </summary>
/// <param name="positions">Array of UTC, BoatTime, Lat, Lng</param>
/// <param name="doOverwrite">true: any existing position within min/max utc of pTrack will be deleted</param>
/// <param name="Track">The track to save, ID must be null</param>
/// <returns>The id of the track</returns>
[Route(Program.APIROOT + "[controller]")]
[HttpPut]
[ServiceFilter(typeof(WriteAuthorizationFilter))]
public Int32 Put(Track pTrack) {
new PiLot.Data.Files.TrackDataConnector().SaveTrack(pTrack);
return -1;
public Int32 PutInsert(Track pTrack) {
new TrackDataConnector().InsertTrack(pTrack);
return pTrack.ID.Value;
}

/// <summary>
Expand All @@ -107,7 +78,6 @@ public Int32 Put(Track pTrack) {
[ServiceFilter(typeof(WriteAuthorizationFilter))]
public void DeleteTrackPoints(Int32 id, Int64 startTime, Int64 endTime, Boolean isBoatTime) {
new TrackDataConnector().DeleteTrackPoints(id, startTime, endTime, isBoatTime);
//TrackStatisticsWorker.Instance.EnsureStatistics(id);
TrackStatisticsHelper.UpdateStatistics(id);
}
}
Expand Down
42 changes: 42 additions & 0 deletions PiLotAPICore/Helpers/DataConnectionHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Configuration;
using PiLot.Data.Nav;

namespace PiLot.API.Helpers
{

/// <summary>
/// This helps using either a files based or db based data connection.
/// If a connectionString is defined, the db connectors will be used,
/// otherwise the files based connectors will be used. It's called
/// dependency injection, I quess.
/// </summary>
public class DataConnectionHelper {

public static IPoiDataConnector PoiDataConnector {
get {
IPoiDataConnector result = null;
String connectionString = ConfigurationManager.AppSettings["connectionString"];
if (!String.IsNullOrEmpty(connectionString)) {
result = new Data.Postgres.Nav.PoiDataConnector(connectionString);
} else {
result = new Data.Files.PoiDataConnector();
}
return result;
}
}

/*public static ITrackDataConnector TrackDataConnector {
get {
ITrackDataConnector result = null;
String connectionString = ConfigurationManager.AppSettings["connectionString"];
if (!String.IsNullOrEmpty(connectionString)) {
result = new Data.Postgres.Nav.TrackDataConnector(connectionString);
} else {
result = new Data.Files.TrackDataConnector();
}
return result;
}
}*/
}
}
Loading

0 comments on commit aae4c7f

Please sign in to comment.