From d56f66e61f86212242614c8b2849f6db53f480a0 Mon Sep 17 00:00:00 2001 From: Matthew Oslan Date: Wed, 4 Mar 2020 22:36:43 -0500 Subject: [PATCH] Don't reset scale on error for manual mode --- routes/weather.ts | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/routes/weather.ts b/routes/weather.ts index eb5b192..95a8d12 100644 --- a/routes/weather.ts +++ b/routes/weather.ts @@ -210,7 +210,7 @@ export const getWateringData = async function( req: express.Request, res: expres adjustmentOptions = JSON.parse( "{" + adjustmentOptionsString + "}" ); } catch ( err ) { // If the JSON is not valid then abort the calculation - sendWateringError( res, new CodedError( ErrorCode.MalformedAdjustmentOptions ) ); + sendWateringError( res, new CodedError( ErrorCode.MalformedAdjustmentOptions ), adjustmentMethod != ManualAdjustmentMethod ); return; } @@ -219,7 +219,7 @@ export const getWateringData = async function( req: express.Request, res: expres try { coordinates = await resolveCoordinates( location ); } catch ( err ) { - sendWateringError( res, makeCodedError( err ) ); + sendWateringError( res, makeCodedError( err ), adjustmentMethod != ManualAdjustmentMethod ); return; } @@ -236,11 +236,11 @@ export const getWateringData = async function( req: express.Request, res: expres // Make sure that the PWS ID and API key look valid. if ( !pwsId ) { - sendWateringError( res, new CodedError( ErrorCode.InvalidPwsId ) ); + sendWateringError( res, new CodedError( ErrorCode.InvalidPwsId ), adjustmentMethod != ManualAdjustmentMethod ); return; } if ( !apiKey ) { - sendWateringError( res, new CodedError( ErrorCode.InvalidPwsApiKey ) ); + sendWateringError( res, new CodedError( ErrorCode.InvalidPwsApiKey ), adjustmentMethod != ManualAdjustmentMethod ); return; } @@ -278,7 +278,7 @@ export const getWateringData = async function( req: express.Request, res: expres adjustmentOptions, coordinates, weatherProvider, pws ); } catch ( err ) { - sendWateringError( res, makeCodedError( err ) ); + sendWateringError( res, makeCodedError( err ), adjustmentMethod != ManualAdjustmentMethod ); return; } @@ -293,7 +293,7 @@ export const getWateringData = async function( req: express.Request, res: expres try { wateringData = await weatherProvider.getWateringData( coordinates ); } catch ( err ) { - sendWateringError( res, makeCodedError( err ) ); + sendWateringError( res, makeCodedError( err ), adjustmentMethod != ManualAdjustmentMethod ); return; } } @@ -318,17 +318,20 @@ export const getWateringData = async function( req: express.Request, res: expres }; /** - * Sends a response to a watering scale request with an error code and a default watering scale of 100%. + * Sends a response to a watering scale request with an error code. * @param res The Express Response object to send the response through. * @param error The error code to send in the response body. + * @param resetScale Indicates if the `scale` field in the response should be set to 100. If this parameter is set to false, + * the field will be omitted. Newer firmware versions may ignore the value of this field since they will detect an error + * occurred, but older firmware versions will still update the watering scale accordingly. * @param useJson Indicates if the response body should use a JSON format instead of a format similar to URL query strings. */ -function sendWateringError( res: express.Response, error: CodedError, useJson: boolean = false ) { +function sendWateringError( res: express.Response, error: CodedError, resetScale: boolean = true, useJson: boolean = false ) { if ( error.errCode === ErrorCode.UnexpectedError ) { console.error( `An unexpected error occurred:`, error ); } - sendWateringData( res, { errCode: error.errCode, scale: 100 } ); + sendWateringData( res, { errCode: error.errCode, scale: resetScale ? 100 : undefined } ); } /**