Merge pull request #87 from OpenSprinkler/dont-reset-scale

Don't reset scale on error for manual mode
This commit is contained in:
Samer Albahra
2020-03-09 13:59:40 -07:00
committed by GitHub

View File

@@ -210,7 +210,7 @@ export const getWateringData = async function( req: express.Request, res: expres
adjustmentOptions = JSON.parse( "{" + adjustmentOptionsString + "}" ); adjustmentOptions = JSON.parse( "{" + adjustmentOptionsString + "}" );
} catch ( err ) { } catch ( err ) {
// If the JSON is not valid then abort the calculation // 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; return;
} }
@@ -219,7 +219,7 @@ export const getWateringData = async function( req: express.Request, res: expres
try { try {
coordinates = await resolveCoordinates( location ); coordinates = await resolveCoordinates( location );
} catch ( err ) { } catch ( err ) {
sendWateringError( res, makeCodedError( err ) ); sendWateringError( res, makeCodedError( err ), adjustmentMethod != ManualAdjustmentMethod );
return; 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. // Make sure that the PWS ID and API key look valid.
if ( !pwsId ) { if ( !pwsId ) {
sendWateringError( res, new CodedError( ErrorCode.InvalidPwsId ) ); sendWateringError( res, new CodedError( ErrorCode.InvalidPwsId ), adjustmentMethod != ManualAdjustmentMethod );
return; return;
} }
if ( !apiKey ) { if ( !apiKey ) {
sendWateringError( res, new CodedError( ErrorCode.InvalidPwsApiKey ) ); sendWateringError( res, new CodedError( ErrorCode.InvalidPwsApiKey ), adjustmentMethod != ManualAdjustmentMethod );
return; return;
} }
@@ -278,7 +278,7 @@ export const getWateringData = async function( req: express.Request, res: expres
adjustmentOptions, coordinates, weatherProvider, pws adjustmentOptions, coordinates, weatherProvider, pws
); );
} catch ( err ) { } catch ( err ) {
sendWateringError( res, makeCodedError( err ) ); sendWateringError( res, makeCodedError( err ), adjustmentMethod != ManualAdjustmentMethod );
return; return;
} }
@@ -293,7 +293,7 @@ export const getWateringData = async function( req: express.Request, res: expres
try { try {
wateringData = await weatherProvider.getWateringData( coordinates ); wateringData = await weatherProvider.getWateringData( coordinates );
} catch ( err ) { } catch ( err ) {
sendWateringError( res, makeCodedError( err ) ); sendWateringError( res, makeCodedError( err ), adjustmentMethod != ManualAdjustmentMethod );
return; 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 res The Express Response object to send the response through.
* @param error The error code to send in the response body. * @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. * @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 ) { if ( error.errCode === ErrorCode.UnexpectedError ) {
console.error( `An unexpected error occurred:`, error ); console.error( `An unexpected error occurred:`, error );
} }
sendWateringData( res, { errCode: error.errCode, scale: 100 } ); sendWateringData( res, { errCode: error.errCode, scale: resetScale ? 100 : undefined } );
} }
/** /**