Convert WeatherProvider from interface to class
This commit is contained in:
@@ -5,8 +5,9 @@ import * as SunCalc from "suncalc";
|
|||||||
import * as moment from "moment-timezone";
|
import * as moment from "moment-timezone";
|
||||||
import * as geoTZ from "geo-tz";
|
import * as geoTZ from "geo-tz";
|
||||||
|
|
||||||
import { AdjustmentOptions, GeoCoordinates, TimeData, WateringData, WeatherData, WeatherProvider } from "../types";
|
import { AdjustmentOptions, GeoCoordinates, TimeData, WateringData, WeatherData } from "../types";
|
||||||
const weatherProvider: WeatherProvider = require("./weatherProviders/" + ( process.env.WEATHER_PROVIDER || "OWM" ) ).default;
|
import { WeatherProvider } from "./weatherProviders/WeatherProvider";
|
||||||
|
const weatherProvider: WeatherProvider = new ( require("./weatherProviders/" + ( process.env.WEATHER_PROVIDER || "OWM" ) ).default )();
|
||||||
|
|
||||||
// Define regex filters to match against location
|
// Define regex filters to match against location
|
||||||
const filters = {
|
const filters = {
|
||||||
|
|||||||
@@ -1,22 +1,25 @@
|
|||||||
import * as moment from "moment-timezone";
|
import * as moment from "moment-timezone";
|
||||||
|
|
||||||
import { GeoCoordinates, WateringData, WeatherData, WeatherProvider } from "../../types";
|
import { GeoCoordinates, WateringData, WeatherData } from "../../types";
|
||||||
import { httpJSONRequest } from "../weather";
|
import { httpJSONRequest } from "../weather";
|
||||||
|
import { WeatherProvider } from "./WeatherProvider";
|
||||||
|
|
||||||
async function getDarkSkyWateringData( coordinates: GeoCoordinates ): Promise< WateringData > {
|
export default class DarkSkyWeatherProvider extends WeatherProvider {
|
||||||
|
|
||||||
|
public async getWateringData( coordinates: GeoCoordinates ): Promise< WateringData > {
|
||||||
// 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 todayTimestamp: number = moment().unix();
|
||||||
|
|
||||||
const DARKSKY_API_KEY = process.env.DARKSKY_API_KEY,
|
const DARKSKY_API_KEY = process.env.DARKSKY_API_KEY,
|
||||||
yesterdayUrl = `https://api.darksky.net/forecast/${DARKSKY_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`;
|
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, todayData;
|
||||||
try {
|
try {
|
||||||
yesterdayData = await httpJSONRequest( yesterdayUrl );
|
yesterdayData = await httpJSONRequest( yesterdayUrl );
|
||||||
todayData = await httpJSONRequest( todayUrl );
|
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."
|
||||||
}
|
}
|
||||||
@@ -51,21 +54,21 @@ async function getDarkSkyWateringData( coordinates: GeoCoordinates ): Promise< W
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
weatherProvider: "DarkSky",
|
weatherProvider: "DarkSky",
|
||||||
temp : totals.temp / 24,
|
temp: totals.temp / 24,
|
||||||
humidity: totals.humidity / 24 * 100,
|
humidity: totals.humidity / 24 * 100,
|
||||||
precip: totals.precip,
|
precip: totals.precip,
|
||||||
raining: samples[ samples.length - 1 ].precipIntensity > 0
|
raining: samples[ samples.length - 1 ].precipIntensity > 0
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getDarkSkyWeatherData( coordinates: GeoCoordinates ): Promise< WeatherData > {
|
public async getWeatherData( coordinates: GeoCoordinates ): Promise< WeatherData > {
|
||||||
const DARKSKY_API_KEY = process.env.DARKSKY_API_KEY,
|
const DARKSKY_API_KEY = process.env.DARKSKY_API_KEY,
|
||||||
forecastUrl = `https://api.darksky.net/forecast/${DARKSKY_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 {
|
||||||
forecast = await httpJSONRequest( forecastUrl );
|
forecast = await httpJSONRequest( forecastUrl );
|
||||||
} 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."
|
||||||
}
|
}
|
||||||
@@ -88,7 +91,7 @@ async function getDarkSkyWeatherData( coordinates: GeoCoordinates ): Promise< We
|
|||||||
minTemp: Math.floor( forecast.daily.data[ 0 ].temperatureMin ),
|
minTemp: Math.floor( forecast.daily.data[ 0 ].temperatureMin ),
|
||||||
maxTemp: Math.floor( forecast.daily.data[ 0 ].temperatureMax ),
|
maxTemp: Math.floor( forecast.daily.data[ 0 ].temperatureMax ),
|
||||||
precip: forecast.daily.data[ 0 ].precipIntensity * 24,
|
precip: forecast.daily.data[ 0 ].precipIntensity * 24,
|
||||||
forecast: [ ]
|
forecast: []
|
||||||
};
|
};
|
||||||
|
|
||||||
for ( let index = 0; index < forecast.daily.data.length; index++ ) {
|
for ( let index = 0; index < forecast.daily.data.length; index++ ) {
|
||||||
@@ -103,11 +106,5 @@ async function getDarkSkyWeatherData( coordinates: GeoCoordinates ): Promise< We
|
|||||||
}
|
}
|
||||||
|
|
||||||
return weather;
|
return weather;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const DarkSkyWeatherProvider: WeatherProvider = {
|
|
||||||
getWateringData: getDarkSkyWateringData,
|
|
||||||
getWeatherData: getDarkSkyWeatherData
|
|
||||||
};
|
|
||||||
export default DarkSkyWeatherProvider;
|
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
import { GeoCoordinates, WateringData, WeatherData, WeatherProvider } from "../../types";
|
import { GeoCoordinates, WateringData, WeatherData } from "../../types";
|
||||||
import { httpJSONRequest } from "../weather";
|
import { httpJSONRequest } from "../weather";
|
||||||
|
import { WeatherProvider } from "./WeatherProvider";
|
||||||
|
|
||||||
async function getOWMWateringData( coordinates: GeoCoordinates ): Promise< WateringData > {
|
export default class OWMWeatherProvider extends WeatherProvider {
|
||||||
|
|
||||||
|
public async getWateringData( coordinates: GeoCoordinates ): Promise< WateringData > {
|
||||||
const OWM_API_KEY = process.env.OWM_API_KEY,
|
const OWM_API_KEY = process.env.OWM_API_KEY,
|
||||||
forecastUrl = "http://api.openweathermap.org/data/2.5/forecast?appid=" + OWM_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 ];
|
||||||
|
|
||||||
@@ -9,7 +12,7 @@ async function getOWMWateringData( coordinates: GeoCoordinates ): Promise< Water
|
|||||||
let forecast;
|
let forecast;
|
||||||
try {
|
try {
|
||||||
forecast = await httpJSONRequest( forecastUrl );
|
forecast = await httpJSONRequest( forecastUrl );
|
||||||
} catch (err) {
|
} catch ( err ) {
|
||||||
console.error( "Error retrieving weather information from OWM:", err );
|
console.error( "Error retrieving weather information from OWM:", err );
|
||||||
throw "An error occurred while retrieving weather information from OWM."
|
throw "An error occurred while retrieving weather information from OWM."
|
||||||
}
|
}
|
||||||
@@ -23,7 +26,7 @@ async function getOWMWateringData( coordinates: GeoCoordinates ): Promise< Water
|
|||||||
totalHumidity = 0,
|
totalHumidity = 0,
|
||||||
totalPrecip = 0;
|
totalPrecip = 0;
|
||||||
|
|
||||||
const periods = Math.min(forecast.list.length, 8);
|
const periods = Math.min( forecast.list.length, 8 );
|
||||||
for ( let index = 0; index < periods; index++ ) {
|
for ( let index = 0; index < periods; index++ ) {
|
||||||
totalTemp += parseFloat( forecast.list[ index ].main.temp );
|
totalTemp += parseFloat( forecast.list[ index ].main.temp );
|
||||||
totalHumidity += parseInt( forecast.list[ index ].main.humidity );
|
totalHumidity += parseInt( forecast.list[ index ].main.humidity );
|
||||||
@@ -37,9 +40,9 @@ async function getOWMWateringData( coordinates: GeoCoordinates ): Promise< Water
|
|||||||
precip: totalPrecip / 25.4,
|
precip: totalPrecip / 25.4,
|
||||||
raining: ( forecast.list[ 0 ].rain ? ( parseFloat( forecast.list[ 0 ].rain[ "3h" ] || 0 ) > 0 ) : false )
|
raining: ( forecast.list[ 0 ].rain ? ( parseFloat( forecast.list[ 0 ].rain[ "3h" ] || 0 ) > 0 ) : false )
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getOWMWeatherData( coordinates: GeoCoordinates ): Promise< WeatherData > {
|
public async getWeatherData( coordinates: GeoCoordinates ): Promise< WeatherData > {
|
||||||
const OWM_API_KEY = process.env.OWM_API_KEY,
|
const OWM_API_KEY = process.env.OWM_API_KEY,
|
||||||
currentUrl = "http://api.openweathermap.org/data/2.5/weather?appid=" + OWM_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=" + OWM_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 ];
|
||||||
@@ -48,7 +51,7 @@ async function getOWMWeatherData( coordinates: GeoCoordinates ): Promise< Weathe
|
|||||||
try {
|
try {
|
||||||
current = await httpJSONRequest( currentUrl );
|
current = await httpJSONRequest( currentUrl );
|
||||||
forecast = await httpJSONRequest( forecastDailyUrl );
|
forecast = await httpJSONRequest( forecastDailyUrl );
|
||||||
} catch (err) {
|
} catch ( err ) {
|
||||||
console.error( "Error retrieving weather information from OWM:", err );
|
console.error( "Error retrieving weather information from OWM:", err );
|
||||||
throw "An error occurred while retrieving weather information from OWM."
|
throw "An error occurred while retrieving weather information from OWM."
|
||||||
}
|
}
|
||||||
@@ -63,8 +66,8 @@ async function getOWMWeatherData( coordinates: GeoCoordinates ): Promise< Weathe
|
|||||||
temp: parseInt( current.main.temp ),
|
temp: parseInt( current.main.temp ),
|
||||||
humidity: parseInt( current.main.humidity ),
|
humidity: parseInt( current.main.humidity ),
|
||||||
wind: parseInt( current.wind.speed ),
|
wind: parseInt( current.wind.speed ),
|
||||||
description: current.weather[0].description,
|
description: current.weather[ 0 ].description,
|
||||||
icon: current.weather[0].icon,
|
icon: current.weather[ 0 ].icon,
|
||||||
|
|
||||||
region: forecast.city.country,
|
region: forecast.city.country,
|
||||||
city: forecast.city.name,
|
city: forecast.city.name,
|
||||||
@@ -85,10 +88,5 @@ async function getOWMWeatherData( coordinates: GeoCoordinates ): Promise< Weathe
|
|||||||
}
|
}
|
||||||
|
|
||||||
return weather;
|
return weather;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const OWMWeatherProvider: WeatherProvider = {
|
|
||||||
getWateringData: getOWMWateringData,
|
|
||||||
getWeatherData: getOWMWeatherData
|
|
||||||
};
|
|
||||||
export default OWMWeatherProvider;
|
|
||||||
|
|||||||
19
routes/weatherProviders/WeatherProvider.ts
Normal file
19
routes/weatherProviders/WeatherProvider.ts
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
import { GeoCoordinates, WateringData, WeatherData } from "../../types";
|
||||||
|
|
||||||
|
export class WeatherProvider {
|
||||||
|
/**
|
||||||
|
* Retrieves weather data necessary for watering level calculations.
|
||||||
|
* @param coordinates The coordinates to retrieve the watering data for.
|
||||||
|
* @return A Promise that will be resolved with the WateringData if it is successfully retrieved,
|
||||||
|
* or rejected with an error message if an error occurs while retrieving the WateringData.
|
||||||
|
*/
|
||||||
|
getWateringData?( coordinates : GeoCoordinates ): Promise< WateringData >;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves the current weather data for usage in the mobile app.
|
||||||
|
* @param coordinates The coordinates to retrieve the weather for
|
||||||
|
* @return A Promise that will be resolved with the WeatherData if it is successfully retrieved,
|
||||||
|
* or rejected with an error message if an error occurs while retrieving the WeatherData.
|
||||||
|
*/
|
||||||
|
getWeatherData?( coordinates : GeoCoordinates ): Promise< WeatherData >;
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
import * as express from "express";
|
import * as express from "express";
|
||||||
import { CronJob } from "cron";
|
import { CronJob } from "cron";
|
||||||
import { GeoCoordinates, WateringData, WeatherProvider } from "../../types";
|
import { GeoCoordinates, WateringData } from "../../types";
|
||||||
|
import { WeatherProvider } from "./WeatherProvider";
|
||||||
|
|
||||||
const count = { temp: 0, humidity: 0 };
|
const count = { temp: 0, humidity: 0 };
|
||||||
|
|
||||||
@@ -43,7 +44,9 @@ export const captureWUStream = function( req: express.Request, res: express.Resp
|
|||||||
res.send( "success\n" );
|
res.send( "success\n" );
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getLocalWateringData = function(): WateringData {
|
export default class LocalWeatherProvider extends WeatherProvider {
|
||||||
|
|
||||||
|
public async getWateringData( coordinates: GeoCoordinates ): Promise< WateringData > {
|
||||||
const result: WateringData = {
|
const result: WateringData = {
|
||||||
...yesterday as WateringData,
|
...yesterday as WateringData,
|
||||||
// Use today's weather if we dont have information for yesterday yet (i.e. on startup)
|
// Use today's weather if we dont have information for yesterday yet (i.e. on startup)
|
||||||
@@ -58,7 +61,8 @@ export const getLocalWateringData = function(): WateringData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
|
||||||
new CronJob( "0 0 0 * * *", function() {
|
new CronJob( "0 0 0 * * *", function() {
|
||||||
|
|
||||||
@@ -74,10 +78,3 @@ interface PWSStatus {
|
|||||||
humidity?: number;
|
humidity?: number;
|
||||||
precip?: number;
|
precip?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
const LocalWeatherProvider: WeatherProvider = {
|
|
||||||
getWateringData: async function ( coordinates: GeoCoordinates ) {
|
|
||||||
return getLocalWateringData();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
export default LocalWeatherProvider;
|
|
||||||
|
|||||||
18
types.ts
18
types.ts
@@ -85,22 +85,4 @@ export interface AdjustmentOptions {
|
|||||||
d?: number;
|
d?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface WeatherProvider {
|
|
||||||
/**
|
|
||||||
* Retrieves weather data necessary for watering level calculations.
|
|
||||||
* @param coordinates The coordinates to retrieve the watering data for.
|
|
||||||
* @return A Promise that will be resolved with the WateringData if it is successfully retrieved,
|
|
||||||
* or rejected with an error message if an error occurs while retrieving the WateringData.
|
|
||||||
*/
|
|
||||||
getWateringData?( coordinates : GeoCoordinates ): Promise< WateringData >;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the current weather data for usage in the mobile app.
|
|
||||||
* @param coordinates The coordinates to retrieve the weather for
|
|
||||||
* @return A Promise that will be resolved with the WeatherData if it is successfully retrieved,
|
|
||||||
* or rejected with an error message if an error occurs while retrieving the WeatherData.
|
|
||||||
*/
|
|
||||||
getWeatherData?( coordinates : GeoCoordinates ): Promise< WeatherData >;
|
|
||||||
}
|
|
||||||
|
|
||||||
export type WeatherProviderId = "OWM" | "DarkSky" | "local";
|
export type WeatherProviderId = "OWM" | "DarkSky" | "local";
|
||||||
|
|||||||
Reference in New Issue
Block a user