Refactor watering scale adjustment method selection
This commit is contained in:
@@ -18,6 +18,12 @@ const filters = {
|
||||
timezone: /^()()()()()()([+-])(\d{2})(\d{2})/
|
||||
};
|
||||
|
||||
// Enum of available watering scale adjustment methods.
|
||||
const ADJUSTMENT_METHOD = {
|
||||
ZIMMERMAN: 1,
|
||||
RAIN_DELAY: 2
|
||||
};
|
||||
|
||||
/**
|
||||
* Resolves a location description to geographic coordinates.
|
||||
* @param location A partial zip/city/country or a coordinate pair.
|
||||
@@ -110,17 +116,13 @@ function getTimeData( coordinates: GeoCoordinates ): TimeData {
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates how much watering should be scaled based on weather and adjustment options.
|
||||
* @param adjustmentMethod The method to use to calculate the watering percentage. The only supported method is 1, which
|
||||
* corresponds to the Zimmerman method. If an invalid adjustmentMethod is used, this method will return -1.
|
||||
* Calculates how much watering should be scaled based on weather and adjustment options using the Zimmerman method.
|
||||
* @param adjustmentOptions Options to tweak the calculation, or undefined/null if no custom values are to be used.
|
||||
* @param wateringData The weather to use to calculate watering percentage.
|
||||
* @return The percentage that watering should be scaled by, or -1 if an invalid adjustmentMethod was provided.
|
||||
* @return The percentage that watering should be scaled by.
|
||||
*/
|
||||
function calculateWeatherScale( adjustmentMethod: number, adjustmentOptions: AdjustmentOptions, wateringData: WateringData ): number {
|
||||
function calculateZimmermanWateringScale( adjustmentOptions: AdjustmentOptions, wateringData: WateringData ): number {
|
||||
|
||||
// Zimmerman method
|
||||
if ( adjustmentMethod === 1 ) {
|
||||
let humidityBase = 30, tempBase = 70, precipBase = 0;
|
||||
|
||||
// Check to make sure valid data exists for all factors
|
||||
@@ -159,9 +161,6 @@ function calculateWeatherScale( adjustmentMethod: number, adjustmentOptions: Adj
|
||||
return Math.floor( Math.min( Math.max( 0, 100 + humidityFactor + tempFactor + precipFactor ), 200 ) );
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the weather data meets any of the restrictions set by OpenSprinkler. Restrictions prevent any watering
|
||||
* from occurring and are similar to 0% watering level. Known restrictions are:
|
||||
@@ -281,21 +280,23 @@ export const getWateringData = async function( req: express.Request, res: expres
|
||||
}
|
||||
}
|
||||
|
||||
let scale: number = calculateWeatherScale( adjustmentMethod, adjustmentOptions, wateringData ),
|
||||
rainDelay: number = -1;
|
||||
let scale = -1, rainDelay = -1;
|
||||
|
||||
if ( adjustmentMethod === ADJUSTMENT_METHOD.ZIMMERMAN ) {
|
||||
scale = calculateZimmermanWateringScale( adjustmentOptions, wateringData );
|
||||
}
|
||||
|
||||
if (wateringData) {
|
||||
// Check for any user-set restrictions and change the scale to 0 if the criteria is met
|
||||
if (checkWeatherRestriction(req.params[0], wateringData)) {
|
||||
scale = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// If any weather adjustment is being used, check the rain status
|
||||
if ( adjustmentMethod > 0 && wateringData && wateringData.raining ) {
|
||||
if ( adjustmentMethod > 0 && wateringData.raining ) {
|
||||
|
||||
// If it is raining and the user has weather-based rain delay as the adjustment method then apply the specified delay
|
||||
if ( adjustmentMethod === 2 ) {
|
||||
if ( adjustmentMethod === ADJUSTMENT_METHOD.RAIN_DELAY ) {
|
||||
|
||||
rainDelay = ( adjustmentOptions && adjustmentOptions.hasOwnProperty( "d" ) ) ? adjustmentOptions.d : 24;
|
||||
} else {
|
||||
@@ -304,6 +305,7 @@ export const getWateringData = async function( req: express.Request, res: expres
|
||||
scale = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const data = {
|
||||
scale: scale,
|
||||
|
||||
Reference in New Issue
Block a user