Use historic data for watering level calculation with Dark Sky
This commit is contained in:
@@ -1,35 +1,29 @@
|
|||||||
|
import * as moment from "moment-timezone";
|
||||||
|
|
||||||
import { GeoCoordinates, WateringData, WeatherData, WeatherProvider } from "../../types";
|
import { GeoCoordinates, WateringData, WeatherData, WeatherProvider } from "../../types";
|
||||||
import { httpJSONRequest } from "../weather";
|
import { httpJSONRequest } from "../weather";
|
||||||
|
|
||||||
async function getDarkSkyWateringData( coordinates: GeoCoordinates ): Promise< WateringData > {
|
async function getDarkSkyWateringData( coordinates: GeoCoordinates ): Promise< WateringData > {
|
||||||
const DARKSKY_API_KEY = process.env.DARKSKY_API_KEY,
|
// The Unix timestamp of 24 hours ago.
|
||||||
forecastUrl = `https://api.darksky.net/forecast/${DARKSKY_API_KEY}/${coordinates[0]},${coordinates[1]}`;
|
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 {
|
try {
|
||||||
forecast = await httpJSONRequest( forecastUrl );
|
historicData = await httpJSONRequest( historicUrl );
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// Indicate watering data could not be retrieved if an API error occurs.
|
// Indicate watering data could not be retrieved if an API error occurs.
|
||||||
return undefined;
|
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 {
|
return {
|
||||||
temp: totalTemp / periods,
|
// Calculate average temperature for the day using hourly data.
|
||||||
humidity: totalHumidity / periods,
|
temp : historicData.hourly.data.reduce( ( sum, hourlyData ) => sum + hourlyData.temperature, 0 ) / historicData.hourly.data.length,
|
||||||
precip: totalPrecip,
|
humidity: historicData.daily.data[ 0 ].humidity * 100,
|
||||||
raining: forecast.currently.precipType === "rain"
|
precip: historicData.daily.data[ 0 ].precipIntensity * 24,
|
||||||
|
raining: historicData.currently.precipType === "rain"
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
13
types.ts
13
types.ts
@@ -48,14 +48,19 @@ export interface WeatherDataForecast {
|
|||||||
description: string;
|
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 {
|
export interface WateringData {
|
||||||
/** The average forecasted temperature over the next 30 hours (in Fahrenheit). */
|
/** The average temperature over the window (in Fahrenheit). */
|
||||||
temp: number;
|
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;
|
humidity: number;
|
||||||
/** The forecasted total precipitation over the next 30 hours (in inches). */
|
/** The total precipitation over the window (in inches). */
|
||||||
precip: number;
|
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;
|
raining: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user