Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(chore): only log location data on env var LogLocationData=true #1063

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,11 +136,18 @@
return value;
}

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

public string BackendApiBaseUrl()
{
var environmentVariableName = "BackendApiBaseUrl";
var value = configuration.GetValue<string>(environmentVariableName);
return value;

Check warning on line 150 in TeslaSolarCharger/Shared/Wrappers/ConfigurationWrapper.cs

View workflow job for this annotation

GitHub Actions / Building TeslaSolarCharger image

Possible null reference return.
}

public bool IsDevelopmentEnvironment()
Expand Down
Loading