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

View File

@@ -41,7 +41,8 @@ export default class DarkSkyWeatherProvider extends WeatherProvider {
];
// 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 );
}
@@ -61,8 +62,8 @@ export default class DarkSkyWeatherProvider extends WeatherProvider {
return {
weatherProvider: "DS",
temp: totals.temp / 24,
humidity: totals.humidity / 24 * 100,
temp: totals.temp / samples.length,
humidity: totals.humidity / samples.length * 100,
precip: totals.precip,
raining: samples[ samples.length - 1 ].precipIntensity > 0
};