Merge branch 'master' into reduce-response-size

# Conflicts:
#	routes/weatherProviders/DarkSky.ts
This commit is contained in:
Matthew Oslan
2020-03-21 16:40:35 -04:00
3 changed files with 53 additions and 30 deletions

55
package-lock.json generated
View File

@@ -1,6 +1,6 @@
{ {
"name": "os-weather-service", "name": "os-weather-service",
"version": "1.0.4", "version": "2.0.1",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {
@@ -1140,7 +1140,8 @@
"ansi-regex": { "ansi-regex": {
"version": "2.1.1", "version": "2.1.1",
"bundled": true, "bundled": true,
"dev": true "dev": true,
"optional": true
}, },
"aproba": { "aproba": {
"version": "1.2.0", "version": "1.2.0",
@@ -1161,12 +1162,14 @@
"balanced-match": { "balanced-match": {
"version": "1.0.0", "version": "1.0.0",
"bundled": true, "bundled": true,
"dev": true "dev": true,
"optional": true
}, },
"brace-expansion": { "brace-expansion": {
"version": "1.1.11", "version": "1.1.11",
"bundled": true, "bundled": true,
"dev": true, "dev": true,
"optional": true,
"requires": { "requires": {
"balanced-match": "^1.0.0", "balanced-match": "^1.0.0",
"concat-map": "0.0.1" "concat-map": "0.0.1"
@@ -1181,17 +1184,20 @@
"code-point-at": { "code-point-at": {
"version": "1.1.0", "version": "1.1.0",
"bundled": true, "bundled": true,
"dev": true "dev": true,
"optional": true
}, },
"concat-map": { "concat-map": {
"version": "0.0.1", "version": "0.0.1",
"bundled": true, "bundled": true,
"dev": true "dev": true,
"optional": true
}, },
"console-control-strings": { "console-control-strings": {
"version": "1.1.0", "version": "1.1.0",
"bundled": true, "bundled": true,
"dev": true "dev": true,
"optional": true
}, },
"core-util-is": { "core-util-is": {
"version": "1.0.2", "version": "1.0.2",
@@ -1308,7 +1314,8 @@
"inherits": { "inherits": {
"version": "2.0.3", "version": "2.0.3",
"bundled": true, "bundled": true,
"dev": true "dev": true,
"optional": true
}, },
"ini": { "ini": {
"version": "1.3.5", "version": "1.3.5",
@@ -1320,6 +1327,7 @@
"version": "1.0.0", "version": "1.0.0",
"bundled": true, "bundled": true,
"dev": true, "dev": true,
"optional": true,
"requires": { "requires": {
"number-is-nan": "^1.0.0" "number-is-nan": "^1.0.0"
} }
@@ -1334,6 +1342,7 @@
"version": "3.0.4", "version": "3.0.4",
"bundled": true, "bundled": true,
"dev": true, "dev": true,
"optional": true,
"requires": { "requires": {
"brace-expansion": "^1.1.7" "brace-expansion": "^1.1.7"
} }
@@ -1341,12 +1350,14 @@
"minimist": { "minimist": {
"version": "0.0.8", "version": "0.0.8",
"bundled": true, "bundled": true,
"dev": true "dev": true,
"optional": true
}, },
"minipass": { "minipass": {
"version": "2.3.5", "version": "2.3.5",
"bundled": true, "bundled": true,
"dev": true, "dev": true,
"optional": true,
"requires": { "requires": {
"safe-buffer": "^5.1.2", "safe-buffer": "^5.1.2",
"yallist": "^3.0.0" "yallist": "^3.0.0"
@@ -1365,6 +1376,7 @@
"version": "0.5.1", "version": "0.5.1",
"bundled": true, "bundled": true,
"dev": true, "dev": true,
"optional": true,
"requires": { "requires": {
"minimist": "0.0.8" "minimist": "0.0.8"
} }
@@ -1445,7 +1457,8 @@
"number-is-nan": { "number-is-nan": {
"version": "1.0.1", "version": "1.0.1",
"bundled": true, "bundled": true,
"dev": true "dev": true,
"optional": true
}, },
"object-assign": { "object-assign": {
"version": "4.1.1", "version": "4.1.1",
@@ -1457,6 +1470,7 @@
"version": "1.4.0", "version": "1.4.0",
"bundled": true, "bundled": true,
"dev": true, "dev": true,
"optional": true,
"requires": { "requires": {
"wrappy": "1" "wrappy": "1"
} }
@@ -1542,7 +1556,8 @@
"safe-buffer": { "safe-buffer": {
"version": "5.1.2", "version": "5.1.2",
"bundled": true, "bundled": true,
"dev": true "dev": true,
"optional": true
}, },
"safer-buffer": { "safer-buffer": {
"version": "2.1.2", "version": "2.1.2",
@@ -1578,6 +1593,7 @@
"version": "1.0.2", "version": "1.0.2",
"bundled": true, "bundled": true,
"dev": true, "dev": true,
"optional": true,
"requires": { "requires": {
"code-point-at": "^1.0.0", "code-point-at": "^1.0.0",
"is-fullwidth-code-point": "^1.0.0", "is-fullwidth-code-point": "^1.0.0",
@@ -1597,6 +1613,7 @@
"version": "3.0.1", "version": "3.0.1",
"bundled": true, "bundled": true,
"dev": true, "dev": true,
"optional": true,
"requires": { "requires": {
"ansi-regex": "^2.0.0" "ansi-regex": "^2.0.0"
} }
@@ -1640,12 +1657,14 @@
"wrappy": { "wrappy": {
"version": "1.0.2", "version": "1.0.2",
"bundled": true, "bundled": true,
"dev": true "dev": true,
"optional": true
}, },
"yallist": { "yallist": {
"version": "3.0.3", "version": "3.0.3",
"bundled": true, "bundled": true,
"dev": true "dev": true,
"optional": true
} }
} }
}, },
@@ -2084,9 +2103,9 @@
} }
}, },
"lodash": { "lodash": {
"version": "4.17.11", "version": "4.17.15",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.11.tgz", "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz",
"integrity": "sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg==" "integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A=="
}, },
"lowercase-keys": { "lowercase-keys": {
"version": "1.0.1", "version": "1.0.1",
@@ -2219,9 +2238,9 @@
"dev": true "dev": true
}, },
"mixin-deep": { "mixin-deep": {
"version": "1.3.1", "version": "1.3.2",
"resolved": "https://registry.npmjs.org/mixin-deep/-/mixin-deep-1.3.1.tgz", "resolved": "https://registry.npmjs.org/mixin-deep/-/mixin-deep-1.3.2.tgz",
"integrity": "sha512-8ZItLHeEgaqEvd5lYBXfm4EZSFCX29Jb9K+lAHhDKzReKBQKj3R+7NOF6tjqYi9t4oI8VUfaWITJQm86wnXGNQ==", "integrity": "sha512-WRoDn//mXBiJ1H40rqa3vH0toePwSsGb45iInWlTySa+Uu4k3tYUSxa2v1KqAiLtvlrSzaExqS1gtk96A9zvEA==",
"dev": true, "dev": true,
"requires": { "requires": {
"for-in": "^1.0.2", "for-in": "^1.0.2",

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 } );
} }
/** /**

View File

@@ -41,7 +41,8 @@ export default class DarkSkyWeatherProvider extends WeatherProvider {
]; ];
// Fail if not enough data is available. // Fail if not enough data is available.
if ( samples.length !== 24 ) { // There will only be 23 samples on the day that daylight saving time begins.
if ( samples.length !== 24 && samples.length !== 23 ) {
throw new CodedError( ErrorCode.InsufficientWeatherData ); throw new CodedError( ErrorCode.InsufficientWeatherData );
} }
@@ -61,8 +62,8 @@ export default class DarkSkyWeatherProvider extends WeatherProvider {
return { return {
weatherProvider: "DS", weatherProvider: "DS",
temp: totals.temp / 24, temp: totals.temp / samples.length,
humidity: totals.humidity / 24 * 100, humidity: totals.humidity / samples.length * 100,
precip: totals.precip, precip: totals.precip,
raining: samples[ samples.length - 1 ].precipIntensity > 0 raining: samples[ samples.length - 1 ].precipIntensity > 0
}; };