Skip to content

Commit

Permalink
Merge pull request #1063 from pkuehnel/feat/doNotLogLocationData
Browse files Browse the repository at this point in the history
feat(chore): only log location data on env var LogLocationData=true
  • Loading branch information
pkuehnel authored Jan 9, 2024
2 parents 3e8cca8 + 87fe4e4 commit 388df04
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 3 deletions.
22 changes: 22 additions & 0 deletions TeslaSolarCharger/Server/Helper/IgnorePropertiesResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json;
using System.Reflection;

namespace TeslaSolarCharger.Server.Helper;

public class IgnorePropertiesResolver(IEnumerable<string> propNamesToIgnore) : DefaultContractResolver
{
private readonly HashSet<string> _ignoreProps = new(propNamesToIgnore);

protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
var property = base.CreateProperty(member, memberSerialization);

if (property.PropertyName != null && _ignoreProps.Contains(property.PropertyName))
{
property.ShouldSerialize = _ => false;
}

return property;
}
}
21 changes: 19 additions & 2 deletions TeslaSolarCharger/Server/Services/ChargingService.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using System.Runtime.CompilerServices;
using TeslaSolarCharger.Model.Contracts;
using TeslaSolarCharger.Server.Contracts;
using TeslaSolarCharger.Server.Helper;
using TeslaSolarCharger.Server.Services.ApiServices.Contracts;
using TeslaSolarCharger.Server.Services.Contracts;
using TeslaSolarCharger.Shared.Contracts;
Expand Down Expand Up @@ -98,8 +100,23 @@ public async Task SetNewChargingValues()
.ThenBy(c => c.Id)
.ToList();

_logger.LogDebug("Relevant cars: {@relevantCars}", relevantCars);
_logger.LogDebug("Irrelevant cars: {@irrlevantCars}", irrelevantCars);
if (_configurationWrapper.LogLocationData())
{
_logger.LogDebug("Relevant cars: {@relevantCars}", relevantCars);
_logger.LogDebug("Irrelevant cars: {@irrelevantCars}", irrelevantCars);
}
else
{
var jsonSerializerSettings = new JsonSerializerSettings
{
ContractResolver = new IgnorePropertiesResolver(new[] { nameof(Car.CarState.Longitude), nameof(Car.CarState.Latitude) }),
};
var relevantCarsJson = JsonConvert.SerializeObject(relevantCars, jsonSerializerSettings);
_logger.LogDebug("Relevant cars: {relevantCarsJson}", relevantCarsJson);
var irrelevantCarsJson = JsonConvert.SerializeObject(irrelevantCars, jsonSerializerSettings);
_logger.LogDebug("Irrelevant cars: {irrelevantCarsJson}", irrelevantCarsJson);
}


if (relevantCarIds.Count < 1)
{
Expand Down
9 changes: 8 additions & 1 deletion TeslaSolarCharger/Server/Services/TeslaMateMqttService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,14 @@ public async Task ConnectMqttClient()
_mqttClient.ApplicationMessageReceivedAsync += e =>
{
var value = GetValueFromMessage(e.ApplicationMessage);
_logger.LogTrace("Car Id: {carId}, Topic: {topic}, Value: {value}", value.CarId, value.Topic, value.Value);
if ((!_configurationWrapper.LogLocationData()) && (string.Equals(value.Topic, TopicLongitude) || string.Equals(value.Topic, TopicLatitude)))
{
_logger.LogTrace("Car Id: {carId}, Topic: {topic}, Value: xx.xxxxx", value.CarId, value.Topic);
}
else
{
_logger.LogTrace("Car Id: {carId}, Topic: {topic}, Value: {value}", value.CarId, value.Topic, value.Value);
}
UpdateCar(value);
return Task.CompletedTask;
};
Expand Down
1 change: 1 addition & 0 deletions TeslaSolarCharger/Server/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"BackendApiBaseUrl": "https://www.teslasolarcharger.de/api/",
"TeslaFleetApiBaseUrl": "https://www.teslasolarcharger.de/teslaproxy/",
"UseFleetApiProxy": false,
"LogLocationData": false,
"AwattarBaseUrl": "https://api.awattar.de/v1/marketdata",
"GridPriceProvider": {
"EnergyProvider": "FixedPrice",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,5 @@ public interface IConfigurationWrapper
string RestoreTempDirectory();
string ConfigFileDirectory();
string AutoBackupsZipDirectory();
bool LogLocationData();
}
7 changes: 7 additions & 0 deletions TeslaSolarCharger/Shared/Wrappers/ConfigurationWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@ public bool UseFleetApiProxy()
return value;
}

public bool LogLocationData()
{
var environmentVariableName = "LogLocationData";
var value = configuration.GetValue<bool>(environmentVariableName);
return value;
}

public string BackendApiBaseUrl()
{
var environmentVariableName = "BackendApiBaseUrl";
Expand Down

0 comments on commit 388df04

Please sign in to comment.