Refactor watering scale logic flow

This commit is contained in:
Matthew Oslan
2019-06-28 00:33:00 -04:00
parent 95dadf601d
commit dc171ebe68
11 changed files with 70 additions and 50 deletions

View File

@@ -1,12 +1,12 @@
import * as moment from "moment-timezone";
import { GeoCoordinates, WateringData, WeatherData } from "../../types";
import { GeoCoordinates, WeatherData, ZimmermanWateringData } from "../../types";
import { httpJSONRequest } from "../weather";
import { WeatherProvider } from "./WeatherProvider";
export default class DarkSkyWeatherProvider extends WeatherProvider {
public async getWateringData( coordinates: GeoCoordinates ): Promise< WateringData > {
public async getWateringData( coordinates: GeoCoordinates ): Promise< ZimmermanWateringData > {
// The Unix timestamp of 24 hours ago.
const yesterdayTimestamp: number = moment().subtract( 1, "day" ).unix();
const todayTimestamp: number = moment().unix();
@@ -47,9 +47,16 @@ export default class DarkSkyWeatherProvider extends WeatherProvider {
const totals = { temp: 0, humidity: 0, precip: 0 };
for ( const sample of samples ) {
/*
* If temperature or humidity is missing from a sample, the total will become NaN. This is intended since
* calculateWateringScale will treat NaN as a missing value and temperature/humidity can't be accurately
* calculated when data is missing from some samples (since they follow diurnal cycles and will be
* significantly skewed if data is missing for several consecutive hours).
*/
totals.temp += sample.temperature;
totals.humidity += sample.humidity;
totals.precip += sample.precipIntensity
// This field may be missing from the response if it is snowing.
totals.precip += sample.precipIntensity || 0;
}
return {

View File

@@ -1,10 +1,10 @@
import { GeoCoordinates, WateringData, WeatherData } from "../../types";
import { GeoCoordinates, WeatherData, ZimmermanWateringData } from "../../types";
import { httpJSONRequest } from "../weather";
import { WeatherProvider } from "./WeatherProvider";
export default class OWMWeatherProvider extends WeatherProvider {
public async getWateringData( coordinates: GeoCoordinates ): Promise< WateringData > {
public async getWateringData( coordinates: GeoCoordinates ): Promise< ZimmermanWateringData > {
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 ];

View File

@@ -1,14 +1,14 @@
import { GeoCoordinates, WateringData, WeatherData } from "../../types";
import { GeoCoordinates, ZimmermanWateringData, WeatherData } from "../../types";
export class WeatherProvider {
/**
* Retrieves weather data necessary for watering level calculations.
* Retrieves weather data necessary for Zimmerman 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 or the WeatherProvider
* @return A Promise that will be resolved with the ZimmermanWateringData if it is successfully retrieved,
* or rejected with an error message if an error occurs while retrieving the ZimmermanWateringData or the WeatherProvider
* does not support this method.
*/
getWateringData( coordinates : GeoCoordinates ): Promise< WateringData > {
getWateringData( coordinates : GeoCoordinates ): Promise< ZimmermanWateringData > {
throw "Selected WeatherProvider does not support getWateringData";
}

View File

@@ -1,6 +1,6 @@
import * as express from "express";
import { CronJob } from "cron";
import { GeoCoordinates, WateringData } from "../../types";
import { GeoCoordinates, ZimmermanWateringData } from "../../types";
import { WeatherProvider } from "./WeatherProvider";
const count = { temp: 0, humidity: 0 };
@@ -46,9 +46,9 @@ export const captureWUStream = function( req: express.Request, res: express.Resp
export default class LocalWeatherProvider extends WeatherProvider {
public async getWateringData( coordinates: GeoCoordinates ): Promise< WateringData > {
const result: WateringData = {
...yesterday as WateringData,
public async getWateringData( coordinates: GeoCoordinates ): Promise< ZimmermanWateringData > {
const result: ZimmermanWateringData = {
...yesterday as ZimmermanWateringData,
// Use today's weather if we dont have information for yesterday yet (i.e. on startup)
...today,
// PWS report "buckets" so consider it still raining if last bucket was less than an hour ago