diff --git a/TeslaSolarCharger/Server/Helper/IgnorePropertiesResolver.cs b/TeslaSolarCharger/Server/Helper/IgnorePropertiesResolver.cs new file mode 100644 index 000000000..2aaa38572 --- /dev/null +++ b/TeslaSolarCharger/Server/Helper/IgnorePropertiesResolver.cs @@ -0,0 +1,22 @@ +using Newtonsoft.Json.Serialization; +using Newtonsoft.Json; +using System.Reflection; + +namespace TeslaSolarCharger.Server.Helper; + +public class IgnorePropertiesResolver(IEnumerable propNamesToIgnore) : DefaultContractResolver +{ + private readonly HashSet _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; + } +} diff --git a/TeslaSolarCharger/Server/Services/ChargingService.cs b/TeslaSolarCharger/Server/Services/ChargingService.cs index a468437ce..e1451843d 100644 --- a/TeslaSolarCharger/Server/Services/ChargingService.cs +++ b/TeslaSolarCharger/Server/Services/ChargingService.cs @@ -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; @@ -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) { diff --git a/TeslaSolarCharger/Server/Services/TeslaMateMqttService.cs b/TeslaSolarCharger/Server/Services/TeslaMateMqttService.cs index 3690c6f26..ee9ab64aa 100644 --- a/TeslaSolarCharger/Server/Services/TeslaMateMqttService.cs +++ b/TeslaSolarCharger/Server/Services/TeslaMateMqttService.cs @@ -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; }; diff --git a/TeslaSolarCharger/Server/appsettings.json b/TeslaSolarCharger/Server/appsettings.json index 179ab3778..50137bb6d 100644 --- a/TeslaSolarCharger/Server/appsettings.json +++ b/TeslaSolarCharger/Server/appsettings.json @@ -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", diff --git a/TeslaSolarCharger/Shared/Contracts/IConfigurationWrapper.cs b/TeslaSolarCharger/Shared/Contracts/IConfigurationWrapper.cs index 6457173e3..214619f92 100644 --- a/TeslaSolarCharger/Shared/Contracts/IConfigurationWrapper.cs +++ b/TeslaSolarCharger/Shared/Contracts/IConfigurationWrapper.cs @@ -96,4 +96,5 @@ public interface IConfigurationWrapper string RestoreTempDirectory(); string ConfigFileDirectory(); string AutoBackupsZipDirectory(); + bool LogLocationData(); } diff --git a/TeslaSolarCharger/Shared/Wrappers/ConfigurationWrapper.cs b/TeslaSolarCharger/Shared/Wrappers/ConfigurationWrapper.cs index 0385d21b6..e11bcba7c 100644 --- a/TeslaSolarCharger/Shared/Wrappers/ConfigurationWrapper.cs +++ b/TeslaSolarCharger/Shared/Wrappers/ConfigurationWrapper.cs @@ -136,6 +136,13 @@ public bool UseFleetApiProxy() return value; } + public bool LogLocationData() + { + var environmentVariableName = "LogLocationData"; + var value = configuration.GetValue(environmentVariableName); + return value; + } + public string BackendApiBaseUrl() { var environmentVariableName = "BackendApiBaseUrl";