Merge pull request #68 from Derpthemeus/minor-fixes

Make minor improvements to WeatherProviders
This commit is contained in:
Samer Albahra
2019-07-02 15:09:44 -07:00
committed by GitHub
3 changed files with 32 additions and 23 deletions

View File

@@ -4,6 +4,10 @@ import * as MockExpressRequest from 'mock-express-request';
import * as MockExpressResponse from 'mock-express-response'; import * as MockExpressResponse from 'mock-express-response';
import * as MockDate from 'mockdate'; import * as MockDate from 'mockdate';
// The tests don't use OWM, but the WeatherProvider API key must be set to prevent an error from being thrown on startup.
process.env.WEATHER_PROVIDER = "OWM";
process.env.OWM_API_KEY = "NO_KEY";
import { getWateringData } from './weather'; import { getWateringData } from './weather';
import { GeoCoordinates, ZimmermanWateringData, WeatherData } from "../types"; import { GeoCoordinates, ZimmermanWateringData, WeatherData } from "../types";
import { WeatherProvider } from "./weatherProviders/WeatherProvider"; import { WeatherProvider } from "./weatherProviders/WeatherProvider";

View File

@@ -6,38 +6,36 @@ import { WeatherProvider } from "./WeatherProvider";
export default class DarkSkyWeatherProvider extends WeatherProvider { export default class DarkSkyWeatherProvider extends WeatherProvider {
private readonly API_KEY: string;
public constructor() {
super();
this.API_KEY = process.env.DARKSKY_API_KEY;
if (!this.API_KEY) {
throw "DARKSKY_API_KEY environment variable is not defined.";
}
}
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 DARKSKY_API_KEY = process.env.DARKSKY_API_KEY, const yesterdayUrl = `https://api.darksky.net/forecast/${ this.API_KEY }/${ coordinates[ 0 ] },${ coordinates[ 1 ] },${ yesterdayTimestamp }?exclude=currently,minutely,daily,alerts,flags`;
yesterdayUrl = `https://api.darksky.net/forecast/${ DARKSKY_API_KEY }/${ coordinates[ 0 ] },${ coordinates[ 1 ] },${ yesterdayTimestamp }?exclude=currently,minutely,daily,alerts,flags`,
todayUrl = `https://api.darksky.net/forecast/${ DARKSKY_API_KEY }/${ coordinates[ 0 ] },${ coordinates[ 1 ] },${ todayTimestamp }?exclude=currently,minutely,daily,alerts,flags`;
let yesterdayData, todayData; let yesterdayData;
try { try {
yesterdayData = await httpJSONRequest( yesterdayUrl ); yesterdayData = await httpJSONRequest( yesterdayUrl );
todayData = await httpJSONRequest( todayUrl );
} 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.
@@ -69,8 +67,7 @@ export default class DarkSkyWeatherProvider extends WeatherProvider {
} }
public async getWeatherData( coordinates: GeoCoordinates ): Promise< WeatherData > { public async getWeatherData( coordinates: GeoCoordinates ): Promise< WeatherData > {
const DARKSKY_API_KEY = process.env.DARKSKY_API_KEY, const forecastUrl = `https://api.darksky.net/forecast/${ this.API_KEY }/${ coordinates[ 0 ] },${ coordinates[ 1 ] }?exclude=minutely,alerts,flags`;
forecastUrl = `https://api.darksky.net/forecast/${ DARKSKY_API_KEY }/${ coordinates[ 0 ] },${ coordinates[ 1 ] }?exclude=minutely,alerts,flags`;
let forecast; let forecast;
try { try {

View File

@@ -4,9 +4,18 @@ import { WeatherProvider } from "./WeatherProvider";
export default class OWMWeatherProvider extends WeatherProvider { export default class OWMWeatherProvider extends WeatherProvider {
private readonly API_KEY: string;
public constructor() {
super();
this.API_KEY = process.env.OWM_API_KEY;
if (!this.API_KEY) {
throw "OWM_API_KEY environment variable is not defined.";
}
}
public async getWateringData( coordinates: GeoCoordinates ): Promise< ZimmermanWateringData > { public async getWateringData( coordinates: GeoCoordinates ): Promise< ZimmermanWateringData > {
const OWM_API_KEY = process.env.OWM_API_KEY, const forecastUrl = `http://api.openweathermap.org/data/2.5/forecast?appid=${ this.API_KEY }&units=imperial&lat=${ coordinates[ 0 ] }&lon=${ coordinates[ 1 ] }`;
forecastUrl = "http://api.openweathermap.org/data/2.5/forecast?appid=" + OWM_API_KEY + "&units=imperial&lat=" + coordinates[ 0 ] + "&lon=" + coordinates[ 1 ];
// Perform the HTTP request to retrieve the weather data // Perform the HTTP request to retrieve the weather data
let forecast; let forecast;
@@ -43,9 +52,8 @@ export default class OWMWeatherProvider extends WeatherProvider {
} }
public async getWeatherData( coordinates: GeoCoordinates ): Promise< WeatherData > { public async getWeatherData( coordinates: GeoCoordinates ): Promise< WeatherData > {
const OWM_API_KEY = process.env.OWM_API_KEY, const currentUrl = `http://api.openweathermap.org/data/2.5/weather?appid=${ this.API_KEY }&units=imperial&lat=${ coordinates[ 0 ] }&lon=${ coordinates[ 1 ] }`,
currentUrl = "http://api.openweathermap.org/data/2.5/weather?appid=" + OWM_API_KEY + "&units=imperial&lat=" + coordinates[ 0 ] + "&lon=" + coordinates[ 1 ], forecastDailyUrl = `http://api.openweathermap.org/data/2.5/forecast/daily?appid=${ this.API_KEY }&units=imperial&lat=${ coordinates[ 0 ] }&lon=${ coordinates[ 1 ] }`;
forecastDailyUrl = "http://api.openweathermap.org/data/2.5/forecast/daily?appid=" + OWM_API_KEY + "&units=imperial&lat=" + coordinates[ 0 ] + "&lon=" + coordinates[ 1 ];
let current, forecast; let current, forecast;
try { try {