Skip to content

Commit

Permalink
Make Ecowitt historic data download cancellable
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Crossley committed Dec 29, 2022
1 parent d893fd4 commit 1f17a4b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
17 changes: 13 additions & 4 deletions CumulusMX/EcowittApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Reactive;
using System.Runtime.Serialization;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using ServiceStack;
using ServiceStack.Text;
Expand Down Expand Up @@ -65,7 +66,7 @@ public EcowittApi(Cumulus cuml, WeatherStation stn)
}


internal bool GetHistoricData(DateTime startTime, DateTime endTime)
internal bool GetHistoricData(DateTime startTime, DateTime endTime, CancellationToken token)
{
var data = new EcowittHistoricData();

Expand Down Expand Up @@ -267,7 +268,7 @@ internal bool GetHistoricData(DateTime startTime, DateTime endTime)
}

cumulus.LogMessage("API.GetHistoricData: System Busy or Rate Limited, waiting 5 secs before retry...");
System.Threading.Thread.Sleep(5000);
Task.Delay(5000, token).Wait();
}
else
{
Expand All @@ -292,7 +293,10 @@ internal bool GetHistoricData(DateTime startTime, DateTime endTime)
//
} while (!success);

ProcessHistoryData(data);
if (!token.IsCancellationRequested)
{
ProcessHistoryData(data, token);
}

return true;
}
Expand All @@ -305,7 +309,7 @@ internal bool GetHistoricData(DateTime startTime, DateTime endTime)

}

private void ProcessHistoryData(EcowittHistoricData data)
private void ProcessHistoryData(EcowittHistoricData data, CancellationToken token)
{
// allocate a dictionary of data objects, keyed on the timestamp
var buffer = new SortedDictionary<DateTime, EcowittApi.HistoricData>();
Expand Down Expand Up @@ -1163,6 +1167,11 @@ private void ProcessHistoryData(EcowittHistoricData data)

foreach (var rec in buffer)
{
if (token.IsCancellationRequested)
{
return;
}

cumulus.LogMessage("Processing data for " + rec.Key);

var h = rec.Key.Hour;
Expand Down
2 changes: 1 addition & 1 deletion CumulusMX/GW1000Station.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ private void GetHistoricData()
maxArchiveRuns++;
}

EcowittApi.GetHistoricData(startTime, endTime);
EcowittApi.GetHistoricData(startTime, endTime, cancellationToken);
}

private Discovery DiscoverGW1000()
Expand Down
11 changes: 9 additions & 2 deletions CumulusMX/HttpStationEcowitt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace CumulusMX
Expand All @@ -20,6 +21,8 @@ class HttpStationEcowitt : WeatherStation
private readonly WeatherStation station;
private bool starting = true;
private bool stopping = false;
private readonly CancellationTokenSource tokenSource = new CancellationTokenSource();
private CancellationToken cancellationToken;
private readonly NumberFormatInfo invNum = CultureInfo.InvariantCulture.NumberFormat;
private bool reportStationType = true;
private int lastMinute = -1;
Expand Down Expand Up @@ -117,7 +120,7 @@ public HttpStationEcowitt(Cumulus cumulus, WeatherStation station = null) : base
cumulus.Units.LeafWetnessUnitText = "%";
}


cancellationToken = tokenSource.Token;

// Only perform the Start-up if we are a proper station, not a Extra Sensor
if (mainStation)
Expand Down Expand Up @@ -150,6 +153,10 @@ public override void Stop()
stopping = true;
Api.stationEcowitt = null;
Api.stationEcowittExtra = null;
if (tokenSource != null)
{
tokenSource.Cancel();
}
if (station == null)
{
StopMinuteTimer();
Expand Down Expand Up @@ -211,7 +218,7 @@ private void GetHistoricData()
maxArchiveRuns++;
}

ecowittApi.GetHistoricData(startTime, endTime);
ecowittApi.GetHistoricData(startTime, endTime, cancellationToken);

}

Expand Down
12 changes: 8 additions & 4 deletions Updates.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ New
<#MySqlRealtimeTime> = time of last real-time table insert
<#MySqlIntervalTime> = time of last interval data table insert
- Most weather value web tags now perform unit conversion. Specify the web tag parameter: unit=[xx]
Where [xx] is one of the following (case sensitive):
Where [xx] is one of the following (case insensitive):
Temperature: C | F
Pressure : hPa | mb | kPa | inHg
Wind : mph | kph | ms | kt
Expand All @@ -34,9 +34,13 @@ Fixed
- Davis VP2 logger downloading entire contents if CMX was restarted before the next console log interval
- Stop second instance on Linux, this now uses a lock file to prevent a second instance from running
- Fix Custom HTTP intervals from trying to process null entries
- Fix for Davis WLL missed broadcast package counter not being incremented
- Fix for Davis WLL processing broadcast messages twice or more on systems with both IPv4 and IPv6 enabled
- Fix for missing average temperature calculation during Ecowitt catch-up
- Davis WLL
- Missed broadcast package counter not being incremented
- No longer processes broadcast messages twice or more on systems with multiple network interfaces
- Improved collision avoidance between broadcasts and API requests
- Ecowitt
- Fix for missing average temperature calculation during Ecowitt historic catch-up
- Historic catch-up is now cancellable with Ctrl-C


3.22.4 - b3215
Expand Down

0 comments on commit 1f17a4b

Please sign in to comment.