Convert local to a WeatherProvider
This commit is contained in:
@@ -5,7 +5,6 @@ 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 * as local from "./local";
|
|
||||||
import { AdjustmentOptions, GeoCoordinates, TimeData, WateringData, WeatherData, WeatherProvider } from "../types";
|
import { AdjustmentOptions, GeoCoordinates, TimeData, WateringData, WeatherData, WeatherProvider } from "../types";
|
||||||
import CompositeWeatherProvider from "./weatherProviders/CompositeWeatherProvider";
|
import CompositeWeatherProvider from "./weatherProviders/CompositeWeatherProvider";
|
||||||
|
|
||||||
@@ -87,16 +86,6 @@ export async function httpJSONRequest(url: string ): Promise< any > {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves weather data necessary for watering level calculations from the a local record.
|
|
||||||
* @param coordinates The coordinates to retrieve the watering data for.
|
|
||||||
* @return A Promise that will be resolved with WateringData.
|
|
||||||
*/
|
|
||||||
async function getLocalWateringData( coordinates: GeoCoordinates ): Promise< WateringData > {
|
|
||||||
// TODO is this type assertion safe?
|
|
||||||
return local.getLocalWeather() as WateringData;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculates timezone and sunrise/sunset for the specified coordinates.
|
* Calculates timezone and sunrise/sunset for the specified coordinates.
|
||||||
* @param coordinates The coordinates to use to calculate time data.
|
* @param coordinates The coordinates to use to calculate time data.
|
||||||
@@ -262,12 +251,7 @@ export const getWateringData = async function( req: express.Request, res: expres
|
|||||||
|
|
||||||
// Continue with the weather request
|
// Continue with the weather request
|
||||||
let timeData: TimeData = getTimeData( coordinates );
|
let timeData: TimeData = getTimeData( coordinates );
|
||||||
let wateringData: WateringData;
|
let wateringData: WateringData = await weatherProvider.getWateringData(coordinates);
|
||||||
if ( local.useLocalWeather() ) {
|
|
||||||
wateringData = await getLocalWateringData( coordinates );
|
|
||||||
} else {
|
|
||||||
wateringData = await weatherProvider.getWateringData(coordinates);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Process data to retrieve the resulting scale, sunrise/sunset, timezone,
|
// Process data to retrieve the resulting scale, sunrise/sunset, timezone,
|
||||||
@@ -327,7 +311,9 @@ export const getWateringData = async function( req: express.Request, res: expres
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if ( local.useLocalWeather() ) {
|
/* Note: The local WeatherProvider will never return undefined, so there's no need to worry about this condition
|
||||||
|
failing to be met if the local WeatherProvider is used but wateringData is falsy (since it will never happen). */
|
||||||
|
if ( wateringData && wateringData.weatherProvider === "local" ) {
|
||||||
console.log( "OpenSprinkler Weather Response: %s", JSON.stringify( data ) );
|
console.log( "OpenSprinkler Weather Response: %s", JSON.stringify( data ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import * as express from "express";
|
import * as express from "express";
|
||||||
import { CronJob } from "cron";
|
import { CronJob } from "cron";
|
||||||
|
import { GeoCoordinates, WateringData, WeatherProvider } from "../../types";
|
||||||
|
|
||||||
const count = { temp: 0, humidity: 0 };
|
const count = { temp: 0, humidity: 0 };
|
||||||
|
|
||||||
@@ -42,25 +43,20 @@ export const captureWUStream = function( req: express.Request, res: express.Resp
|
|||||||
res.send( "success\n" );
|
res.send( "success\n" );
|
||||||
};
|
};
|
||||||
|
|
||||||
export const useLocalWeather = function(): boolean {
|
export const getLocalWateringData = function(): WateringData {
|
||||||
return process.env.PWS ? true : false;
|
const result: WateringData = {
|
||||||
};
|
...yesterday as WateringData,
|
||||||
|
// Use today's weather if we dont have information for yesterday yet (i.e. on startup)
|
||||||
export const getLocalWeather = function(): LocalWeather {
|
...today,
|
||||||
const result: LocalWeather = {};
|
// PWS report "buckets" so consider it still raining if last bucket was less than an hour ago
|
||||||
|
raining: last_bucket !== undefined ? ( ( Date.now() - +last_bucket ) / 1000 / 60 / 60 < 1 ) : undefined,
|
||||||
// Use today's weather if we dont have information for yesterday yet (i.e. on startup)
|
weatherProvider: "local"
|
||||||
Object.assign( result, today, yesterday);
|
};
|
||||||
|
|
||||||
if ( "precip" in yesterday && "precip" in today ) {
|
if ( "precip" in yesterday && "precip" in today ) {
|
||||||
result.precip = yesterday.precip + today.precip;
|
result.precip = yesterday.precip + today.precip;
|
||||||
}
|
}
|
||||||
|
|
||||||
// PWS report "buckets" so consider it still raining if last bucket was less than an hour ago
|
|
||||||
if ( last_bucket !== undefined ) {
|
|
||||||
result.raining = ( ( Date.now() - +last_bucket ) / 1000 / 60 / 60 < 1 );
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -79,6 +75,9 @@ interface PWSStatus {
|
|||||||
precip?: number;
|
precip?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface LocalWeather extends PWSStatus {
|
const LocalWeatherProvider: WeatherProvider = {
|
||||||
raining?: boolean;
|
getWateringData: async function ( coordinates: GeoCoordinates ) {
|
||||||
}
|
return getLocalWateringData();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
export default LocalWeatherProvider;
|
||||||
@@ -5,7 +5,7 @@ import * as express from "express";
|
|||||||
import * as cors from "cors";
|
import * as cors from "cors";
|
||||||
|
|
||||||
import * as weather from "./routes/weather";
|
import * as weather from "./routes/weather";
|
||||||
import * as local from "./routes/local";
|
import * as local from "./routes/weatherProviders/local";
|
||||||
|
|
||||||
let host = process.env.HOST || "127.0.0.1",
|
let host = process.env.HOST || "127.0.0.1",
|
||||||
port = parseInt( process.env.PORT ) || 3000;
|
port = parseInt( process.env.PORT ) || 3000;
|
||||||
|
|||||||
6
types.ts
6
types.ts
@@ -92,7 +92,7 @@ export interface WeatherProvider {
|
|||||||
* @return A Promise that will be resolved with the WateringData if it is successfully retrieved,
|
* @return A Promise that will be resolved with the WateringData if it is successfully retrieved,
|
||||||
* or resolved with undefined if an error occurs while retrieving the WateringData.
|
* or resolved with undefined if an error occurs while retrieving the WateringData.
|
||||||
*/
|
*/
|
||||||
getWateringData( coordinates : GeoCoordinates ): Promise< WateringData >;
|
getWateringData?( coordinates : GeoCoordinates ): Promise< WateringData >;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieves the current weather data for usage in the mobile app.
|
* Retrieves the current weather data for usage in the mobile app.
|
||||||
@@ -100,7 +100,7 @@ export interface WeatherProvider {
|
|||||||
* @return A Promise that will be resolved with the WeatherData if it is successfully retrieved,
|
* @return A Promise that will be resolved with the WeatherData if it is successfully retrieved,
|
||||||
* or resolved with undefined if an error occurs while retrieving the WeatherData.
|
* or resolved with undefined if an error occurs while retrieving the WeatherData.
|
||||||
*/
|
*/
|
||||||
getWeatherData( coordinates : GeoCoordinates ): Promise< WeatherData >;
|
getWeatherData?( coordinates : GeoCoordinates ): Promise< WeatherData >;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type WeatherProviderId = "OWM" | "DarkSky";
|
export type WeatherProviderId = "OWM" | "DarkSky" | "local";
|
||||||
|
|||||||
Reference in New Issue
Block a user