Merge pull request #25 from Derpthemeus/use-historical-data

Use historic data instead of forecasted data in Dark Sky WeatherProvider
This commit is contained in:
Samer Albahra
2019-05-13 18:59:24 -05:00
committed by GitHub
4 changed files with 26 additions and 26 deletions

View File

@@ -174,7 +174,8 @@ function checkWeatherRestriction( adjustmentValue: number, weather: WateringData
if ( californiaRestriction ) {
// TODO this is currently checking if the forecasted precipitation over the next 30 hours is >0.1 inches
// TODO depending on which WeatherProvider is used, this might be checking if rain is forecasted in th next 24
// hours rather than checking if it has rained in the past 48 hours.
// If the California watering restriction is in use then prevent watering
// if more then 0.1" of rain has accumulated in the past 48 hours
if ( weather.precip > 0.1 ) {

View File

@@ -1,35 +1,29 @@
import * as moment from "moment-timezone";
import { GeoCoordinates, WateringData, WeatherData, WeatherProvider } from "../../types";
import { httpJSONRequest } from "../weather";
async function getDarkSkyWateringData( coordinates: GeoCoordinates ): Promise< WateringData > {
const DARKSKY_API_KEY = process.env.DARKSKY_API_KEY,
forecastUrl = `https://api.darksky.net/forecast/${DARKSKY_API_KEY}/${coordinates[0]},${coordinates[1]}`;
// The Unix timestamp of 24 hours ago.
const timestamp: number = moment().subtract( 1, "day" ).unix();
let forecast;
const DARKSKY_API_KEY = process.env.DARKSKY_API_KEY,
historicUrl = `https://api.darksky.net/forecast/${DARKSKY_API_KEY}/${coordinates[0]},${coordinates[1]},${timestamp}`;
let historicData;
try {
forecast = await httpJSONRequest( forecastUrl );
historicData = await httpJSONRequest( historicUrl );
} catch (err) {
// Indicate watering data could not be retrieved if an API error occurs.
return undefined;
}
let totalTemp = 0,
totalHumidity = 0,
totalPrecip = 0;
const periods = Math.min( forecast.hourly.data.length, 30 );
for ( let index = 0; index < periods; index++ ) {
totalTemp += forecast.hourly.data[ index ].temperature;
totalHumidity += forecast.hourly.data[ index ].humidity * 100;
totalPrecip += forecast.hourly.data[ index ].precipIntensity;
}
return {
temp: totalTemp / periods,
humidity: totalHumidity / periods,
precip: totalPrecip,
raining: forecast.currently.precipType === "rain"
// Calculate average temperature for the day using hourly data.
temp : historicData.hourly.data.reduce( ( sum, hourlyData ) => sum + hourlyData.temperature, 0 ) / historicData.hourly.data.length,
humidity: historicData.daily.data[ 0 ].humidity * 100,
precip: historicData.daily.data[ 0 ].precipIntensity * 24,
raining: historicData.currently.precipType === "rain"
};
}

View File

@@ -23,7 +23,7 @@ async function getOWMWateringData( coordinates: GeoCoordinates ): Promise< Water
totalHumidity = 0,
totalPrecip = 0;
const periods = Math.min(forecast.list.length, 10);
const periods = Math.min(forecast.list.length, 8);
for ( let index = 0; index < periods; index++ ) {
totalTemp += parseFloat( forecast.list[ index ].main.temp );
totalHumidity += parseInt( forecast.list[ index ].main.humidity );

View File

@@ -48,14 +48,19 @@ export interface WeatherDataForecast {
description: string;
}
/**
* Data from a 24 hour window that is used to calculate how watering levels should be scaled. This should ideally use
* historic data from the past day, but may also use forecasted data for the next day if historical data is not
* available.
*/
export interface WateringData {
/** The average forecasted temperature over the next 30 hours (in Fahrenheit). */
/** The average temperature over the window (in Fahrenheit). */
temp: number;
/** The average forecasted humidity over the next 30 hours (as a percentage). */
/** The average humidity over the window (as a percentage). */
humidity: number;
/** The forecasted total precipitation over the next 30 hours (in inches). */
/** The total precipitation over the window (in inches). */
precip: number;
/** A boolean indicating if it is currently raining. */
/** A boolean indicating if it is raining at the time that this data was retrieved. */
raining: boolean;
}