11using System . Net ;
22using System . Text . Json ;
33using System . Text . RegularExpressions ;
4+ using Serilog ;
45using VattenfallDynamicPriceApi . Extensions ;
56using VattenfallDynamicPriceApi . Models . Evcc ;
67using VattenfallDynamicPriceApi . Models . Vattenfall ;
78
89namespace VattenfallDynamicPriceApi . Services ;
910
10- public partial class VattenfallDataService
11+ public partial class VattenfallDataService : IDisposable , IAsyncDisposable
1112{
12- private static readonly TimeSpan CacheDuration = TimeSpan . FromHours ( 1 ) ;
13+ private static readonly TimeSpan CacheDuration = TimeSpan . FromSeconds ( 60 ) ;
1314
1415 public FlexTariffData [ ] ? Data { get ; private set ; } = [ ] ;
1516
1617 public EvccApiHourlyData [ ] ? EvccData { get ; private set ; } = [ ] ;
18+
19+ private Timer ? _timer ;
1720
1821 public async Task InitializeAsync ( )
1922 {
20- Console . WriteLine ( "Refresh interval: " + CacheDuration ) ;
23+ Log . Information ( "Refresh interval: " + CacheDuration ) ;
2124
2225 await UpdateDataAsync ( ) ;
23- _ = new Timer ( RefreshTimerElapsed , null , CacheDuration , CacheDuration ) ;
26+ _timer = new Timer ( RefreshTimerElapsed , null , CacheDuration , CacheDuration ) ;
2427 }
2528
2629 private decimal GetCurrentTariffForProductType ( string productType , string description )
2730 {
2831 var productData = Data ? . FirstOrDefault ( d => d . Product == productType ) ;
2932 if ( productData == null )
3033 {
31- Console . WriteLine ( $ "Could not get current { description } tariff, no data") ;
34+ Log . Error ( "Could not get current {Description } tariff, no data" , description ) ;
3235 return 999 ;
3336 }
3437
@@ -38,7 +41,7 @@ private decimal GetCurrentTariffForProductType(string productType, string descri
3841 return currentTariff . AmountInclVat ;
3942
4043 var highestTariff = productData . TariffData . Max ( t => t . AmountInclVat ) ;
41- Console . WriteLine ( $ "Could not get current { description } tariff, no value found for current time, returning highest value:" + highestTariff ) ;
44+ Log . Error ( "Could not get current {Description } tariff, no value found for current time, returning highest value: {HighestTariff}" , description , highestTariff ) ;
4245
4346 return highestTariff ;
4447 }
@@ -53,13 +56,13 @@ private void RefreshTimerElapsed(object? _)
5356 {
5457 try
5558 {
56- Console . WriteLine ( "Updating data" ) ;
59+ Log . Information ( "Updating data" ) ;
5760 Task . Run ( UpdateDataAsync ) . Wait ( ) ;
58- Console . WriteLine ( "Updated data" ) ;
61+ Log . Information ( "Updated data" ) ;
5962 }
6063 catch ( Exception e )
6164 {
62- Console . WriteLine ( "Failed to update data: " + e ) ;
65+ Log . Error ( e , "Failed to update data" ) ;
6366 }
6467 }
6568
@@ -71,7 +74,7 @@ private async Task UpdateDataAsync()
7174 var electricityData = Data . FirstOrDefault ( d => d . Product == "E" ) ;
7275 if ( electricityData == null )
7376 {
74- Console . WriteLine ( "Could not find electricity data in API response" ) ;
77+ Log . Error ( "Could not find electricity data in API response" ) ;
7578 return ;
7679 }
7780
@@ -110,7 +113,7 @@ private static async Task<FlexTariffData[]> GetFlexTariffDataAsync(string apiBas
110113 }
111114 catch ( Exception e )
112115 {
113- Console . WriteLine ( "Failed to get API URL and key dynamically, falling back to known values: " + e ) ;
116+ Log . Error ( e , "Failed to get API URL and key dynamically, falling back to known values" ) ;
114117 return ( SettingsProvider . Instance . Settings . KnownApiBaseUrl , SettingsProvider . Instance . Settings . KnownApiKey ) ;
115118 }
116119 }
@@ -133,22 +136,22 @@ private static async Task<FlexTariffData[]> GetFlexTariffDataAsync(string apiBas
133136 if ( string . IsNullOrWhiteSpace ( scriptUrl ) || ! Uri . IsWellFormedUriString ( scriptUrl , UriKind . Absolute ) )
134137 throw new Exception ( "Could not find the epi-es2015.js script URL" ) ;
135138
136- Console . WriteLine ( "Found epi JS script: " + scriptUrl ) ;
139+ Log . Information ( "Found epi JS script: {Url}" , scriptUrl ) ;
137140
138141 // Find API base URL in page script
139142 var js = await httpClient . GetStringAsync ( scriptUrl ) ;
140143 var apiBaseUrl = ApiBaseUrlRegex ( ) . Match ( js ) . Groups [ "url" ] . Value . TrimEnd ( '/' ) ;
141144 if ( string . IsNullOrWhiteSpace ( apiBaseUrl ) )
142145 throw new Exception ( "Could not find the API base URL" ) ;
143146
144- Console . WriteLine ( "API base URL: " + apiBaseUrl ) ;
147+ Log . Information ( "API base URL: {ApiBaseUrl}" , apiBaseUrl ) ;
145148
146149 // Find API key in page script
147150 var apiKey = TariffApiKeyRegex ( ) . Match ( js ) . Groups [ "key" ] . Value ;
148151 if ( string . IsNullOrWhiteSpace ( apiKey ) )
149152 throw new Exception ( "Could not find the API key" ) ;
150153
151- Console . WriteLine ( "API key: " + apiKey ) ;
154+ Log . Information ( "API key: {ApiKey}" , apiKey ) ;
152155
153156 // Update known values
154157 SettingsProvider . Instance . Settings . KnownApiBaseUrl = apiBaseUrl ;
@@ -165,4 +168,15 @@ private static async Task<FlexTariffData[]> GetFlexTariffDataAsync(string apiBas
165168
166169 [ GeneratedRegex ( @"ocpApimSubscriptionFeaturesDynamicTariffsKey:""(?<key>[^""]*)" ) ]
167170 private static partial Regex TariffApiKeyRegex ( ) ;
171+
172+ public void Dispose ( )
173+ {
174+ _timer ? . Dispose ( ) ;
175+ }
176+
177+ public async ValueTask DisposeAsync ( )
178+ {
179+ if ( _timer != null )
180+ await _timer . DisposeAsync ( ) ;
181+ }
168182}
0 commit comments