Use fixed calendar day window in Dark Sky WeatherProvider

This commit is contained in:
Matthew Oslan
2019-07-02 17:15:09 -04:00
parent dd92b8ecf8
commit a92f488ec3

View File

@@ -19,33 +19,23 @@ export default class DarkSkyWeatherProvider extends WeatherProvider {
public async getWateringData( coordinates: GeoCoordinates ): Promise< ZimmermanWateringData > { public async getWateringData( coordinates: GeoCoordinates ): Promise< ZimmermanWateringData > {
// The Unix timestamp of 24 hours ago. // The Unix timestamp of 24 hours ago.
const yesterdayTimestamp: number = moment().subtract( 1, "day" ).unix(); const yesterdayTimestamp: number = moment().subtract( 1, "day" ).unix();
const todayTimestamp: number = moment().unix();
const yesterdayUrl = `https://api.darksky.net/forecast/${ this.API_KEY }/${ coordinates[ 0 ] },${ coordinates[ 1 ] },${ yesterdayTimestamp }?exclude=currently,minutely,daily,alerts,flags`, const yesterdayUrl = `https://api.darksky.net/forecast/${ this.API_KEY }/${ coordinates[ 0 ] },${ coordinates[ 1 ] },${ yesterdayTimestamp }?exclude=currently,minutely,daily,alerts,flags`;
todayUrl = `https://api.darksky.net/forecast/${ this.API_KEY }/${ coordinates[ 0 ] },${ coordinates[ 1 ] },${ todayTimestamp }?exclude=currently,minutely,daily,alerts,flags`;
let yesterdayData, todayData; let yesterdayData;
try { try {
[ yesterdayData, todayData ] = await Promise.all( [ httpJSONRequest( yesterdayUrl ), httpJSONRequest( todayUrl ) ] ); yesterdayData = await httpJSONRequest( yesterdayUrl );
} catch ( err ) { } catch ( err ) {
console.error( "Error retrieving weather information from Dark Sky:", err ); console.error( "Error retrieving weather information from Dark Sky:", err );
throw "An error occurred while retrieving weather information from Dark Sky." throw "An error occurred while retrieving weather information from Dark Sky."
} }
if ( !todayData.hourly || !todayData.hourly.data || !yesterdayData.hourly || !yesterdayData.hourly.data ) { if ( !yesterdayData.hourly || !yesterdayData.hourly.data ) {
throw "Necessary field(s) were missing from weather information returned by Dark Sky."; throw "Necessary field(s) were missing from weather information returned by Dark Sky.";
} }
/* The number of hourly forecasts to use from today's data. This will only include elements that contain historic
data (not forecast data). */
// Find the first element that contains forecast data.
const todayElements = Math.min( 24, todayData.hourly.data.findIndex( ( data ) => data.time > todayTimestamp - 60 * 60 ) );
/* Take as much data as possible from the first elements of today's data and take the remaining required data from
the remaining data from the last elements of yesterday's data. */
const samples = [ const samples = [
...yesterdayData.hourly.data.slice( todayElements - 24 ), ...yesterdayData.hourly.data
...todayData.hourly.data.slice( 0, todayElements )
]; ];
// Fail if not enough data is available. // Fail if not enough data is available.