Skip to content

Commit

Permalink
Added support for start/end othert than sunrise or sunset
Browse files Browse the repository at this point in the history
  • Loading branch information
fergusbown committed Jan 27, 2019
1 parent c07e871 commit 67050f7
Show file tree
Hide file tree
Showing 13 changed files with 418 additions and 34 deletions.
2 changes: 1 addition & 1 deletion Crochet.sln
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Global
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{9198FCBC-3BF8-42EA-AE71-486DB1FE3009} = {7C73C3B3-E4A0-48E4-82AF-5D7025B6D2C0}
{03A93B1D-983F-49C2-9D86-5E97E529AE03} = {F6A44F50-AC07-4ADA-AAC2-CBC7AE3201AA}
{03A93B1D-983F-49C2-9D86-5E97E529AE03} = {7C73C3B3-E4A0-48E4-82AF-5D7025B6D2C0}
{471965C7-577F-421C-A33B-B3AB1938A361} = {F6A44F50-AC07-4ADA-AAC2-CBC7AE3201AA}
{6BCD74B4-1076-48BC-8B15-D68DA8F4426A} = {F6A44F50-AC07-4ADA-AAC2-CBC7AE3201AA}
EndGlobalSection
Expand Down
2 changes: 2 additions & 0 deletions FF.Temperature.Lib/FF.Temperature.Lib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,11 @@
<ItemGroup>
<Compile Include="CefBrowserWrapper.cs" />
<Compile Include="InvokingWebBrowser.cs" />
<Compile Include="ITimeRangeProvider.cs" />
<Compile Include="IUserInteraction.cs" />
<Compile Include="IWebBrowser.cs" />
<Compile Include="SunriseSunset.cs" />
<Compile Include="TimeRangeProviderFactory.cs" />
<Compile Include="WeatherInformation.cs" />
<Compile Include="WeatherUnderground.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
13 changes: 13 additions & 0 deletions FF.Temperature.Lib/ITimeRangeProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FF.Temperature.Lib
{
public interface ITimeRangeProvider
{
WeatherInformation GetInformation(IUserInteraction userInteraction, DateTime date, double latitude, double longitude);
}
}
4 changes: 2 additions & 2 deletions FF.Temperature.Lib/SunriseSunset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace FF.Temperature.Lib
{
public static class SunriseSunset
public class SunriseSunset : ITimeRangeProvider
{

private static bool ParseSunTime(DateTime date, string timeText, out DateTime result)
Expand All @@ -30,7 +30,7 @@ private static bool ParseSunTime(DateTime date, string timeText, out DateTime re
}
}

public static WeatherInformation GetInformation(IUserInteraction userInteraction, DateTime date, double latitude, double longitude)
public WeatherInformation GetInformation(IUserInteraction userInteraction, DateTime date, double latitude, double longitude)
{
string address = $"https://api.sunrise-sunset.org/json?lng={longitude}&lat={latitude}&date={date.ToString("yyyy-MM-dd")}";

Expand Down
82 changes: 82 additions & 0 deletions FF.Temperature.Lib/TimeRangeProviderFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FF.Temperature.Lib
{
public static class TimeRangeProviderFactory
{
private class FixedTimeProvider : ITimeRangeProvider
{
private int startTime;
private int endTime;

public FixedTimeProvider(int startTime, int endTime)
{
this.startTime = startTime;
this.endTime = endTime;
}

public WeatherInformation GetInformation(IUserInteraction userInteraction, DateTime date, double latitude, double longitude)
{
return new WeatherInformation(
date.Date.AddHours(startTime),
date.Date.AddHours(endTime + 12),
Enumerable.Empty<WeatherReading>());
}
}

private class CompositeProvider : ITimeRangeProvider
{
private ITimeRangeProvider startTime;
private ITimeRangeProvider endTime;

public CompositeProvider(ITimeRangeProvider startTime, ITimeRangeProvider endTime)
{
this.startTime = startTime;
this.endTime = endTime;
}

public WeatherInformation GetInformation(IUserInteraction userInteraction, DateTime date, double latitude, double longitude)
{
WeatherInformation start = this.startTime.GetInformation(userInteraction, date, latitude, longitude);
WeatherInformation end = this.endTime.GetInformation(userInteraction, date, latitude, longitude);
return new WeatherInformation(
start.StartTime,
end.EndTime,
Enumerable.Empty<WeatherReading>());
}
}

public static ITimeRangeProvider GetProvider(int startTime, int endTime)
{
if (startTime == 0 && endTime == 0)
{
return new SunriseSunset();
}
else
{
var fixedTimeProvider = new FixedTimeProvider(startTime, endTime);

if (startTime == 0)
{
return new CompositeProvider(
new SunriseSunset(),
fixedTimeProvider);
}
else if (endTime == 0)
{
return new CompositeProvider(
fixedTimeProvider,
new SunriseSunset());
}
else
{
return fixedTimeProvider;
}
}
}
}
}
14 changes: 7 additions & 7 deletions FF.Temperature.Lib/WeatherInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,25 @@ public static WeatherReading FromDegrees(DateTime readingTime, double degrees)

public class WeatherInformation
{
public DateTime Sunrise { get; }
public DateTime StartTime { get; }

public DateTime Sunset { get; }
public DateTime EndTime { get; }

public IEnumerable<WeatherReading> Readings { get; }

public WeatherInformation(DateTime sunrise, DateTime sunset, IEnumerable<WeatherReading> readings)
public WeatherInformation(DateTime startTime, DateTime endTime, IEnumerable<WeatherReading> readings)
{
this.Sunrise = sunrise;
this.Sunset = sunset;
this.StartTime = startTime;
this.EndTime = endTime;
this.Readings = readings.ToList();
}

public int AverageDaytimeDegrees
public int AverageDegrees
{
get
{
var average = this.Readings
.Where(r => r.ReadingTime >= this.Sunrise && r.ReadingTime <= this.Sunset)
.Where(r => r.ReadingTime >= this.StartTime && r.ReadingTime <= this.EndTime)
.Select(r => r.Degrees)
.Average();

Expand Down
14 changes: 10 additions & 4 deletions FF.Temperature.Lib/WeatherUnderground.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,20 @@ public class WeatherUnderground
private readonly string location;
private readonly IUserInteraction userInteraction;
private readonly IWebBrowser browser;
private readonly ITimeRangeProvider timeRangeProvider;
private const string observationTableXPath = "//div[contains(@class, 'observation-table')]//table";


public WeatherUnderground(string location, IUserInteraction userInteraction, IWebBrowser browser)
public WeatherUnderground(
string location,
IUserInteraction userInteraction,
IWebBrowser browser,
ITimeRangeProvider timeRangeProvider)
{
this.location = location;
this.userInteraction = userInteraction;
this.browser = browser;
this.timeRangeProvider = timeRangeProvider;
}

private bool IsDocumentFullyLoaded(HtmlDocument htmlDocument, out int remaining)
Expand Down Expand Up @@ -181,13 +187,13 @@ public async Task<WeatherInformation> ReadWeatherInformation(DateTime date)
if (GetReadings(htmlDocument, date, out List<WeatherReading> readings, out int dummy)
&& GetLocationInformation(htmlDocument, out double latitude, out double longitude, out int dummy2))
{
WeatherInformation daylight = SunriseSunset.GetInformation(this.userInteraction, date, latitude, longitude);
WeatherInformation daylight = this.timeRangeProvider.GetInformation(this.userInteraction, date, latitude, longitude);

if (daylight != null)
{
return new WeatherInformation(
daylight.Sunrise,
daylight.Sunset,
daylight.StartTime,
daylight.EndTime,
readings);
}
}
Expand Down
6 changes: 6 additions & 0 deletions TemperatureClient/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
<setting name="LastDate" serializeAs="String">
<value />
</setting>
<setting name="StartTime" serializeAs="String">
<value>0</value>
</setting>
<setting name="EndTime" serializeAs="String">
<value>0</value>
</setting>
</TemperatureClient.Properties.Settings>
</userSettings>
</configuration>
Loading

0 comments on commit 67050f7

Please sign in to comment.