From ad3f2921357fda4f38ad9a19e520743762c55306 Mon Sep 17 00:00:00 2001 From: Michael Woods Date: Wed, 30 Nov 2022 12:24:52 -0500 Subject: [PATCH] Improve error logging and command response output. --- src/index.js | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/src/index.js b/src/index.js index 0fd66d1..01c81ed 100644 --- a/src/index.js +++ b/src/index.js @@ -15,9 +15,11 @@ const onstarConfig = { username: process.env.ONSTAR_USERNAME, password: process.env.ONSTAR_PASSWORD, onStarPin: process.env.ONSTAR_PIN, - checkRequestStatus: process.env.ONSTAR_SYNC === "true" || true, + checkRequestStatus: _.get(process.env, 'ONSTAR_SYNC', 'true') === 'true', refreshInterval: parseInt(process.env.ONSTAR_REFRESH) || (30 * 60 * 1000), // 30 min - allowCommands: _.toLower(_.get(process, 'env.ONSTAR_ALLOW_COMMANDS', 'true')) === 'true' + requestPollingIntervalSeconds: parseInt(process.env.ONSTAR_POLL_INTERVAL) || 12, // 6 sec default + requestPollingTimeoutSeconds: parseInt(process.env.ONSTAR_POLL_TIMEOUT) || 120, // 60 sec default + allowCommands: _.get(process.env, 'ONSTAR_ALLOW_COMMANDS', 'true') === 'true' }; logger.info('OnStar Config', {onstarConfig}); @@ -86,15 +88,18 @@ const configureMQTT = async (commands, client, mqttHA) => { .then(data => { // TODO refactor the response handling for commands logger.info('Command completed', {command}); - const location = _.get(data, 'response.data.commandResponse.body.location'); - if (data && location) { - logger.info('Command response data', {location}); - const topic = mqttHA.getStateTopic({name: command}); - // TODO create device_tracker entity. MQTT device tracker doesn't support lat/lon and mqtt_json - // doesn't have discovery - client.publish(topic, - JSON.stringify({latitude: location.lat, longitude: location.long}), {retain: true}) - .then(() => logger.info('Published location to topic.', {topic})); + data = _.get(data, 'response.data'); + if (data) { + logger.info('Command response data', {data}); + const location = _.get(data, 'response.data.commandResponse.body.location'); + if (location) { + const topic = mqttHA.getStateTopic({name: command}); + // TODO create device_tracker entity. MQTT device tracker doesn't support lat/lon and mqtt_json + // doesn't have discovery + client.publish(topic, + JSON.stringify({latitude: location.lat, longitude: location.long}), {retain: true}) + .then(() => logger.info('Published location to topic.', {topic})); + } } }) .catch(err=> logger.error('Command error', {command, err})); @@ -169,7 +174,17 @@ const configureMQTT = async (commands, client, mqttHA) => { const main = async () => run() .then(() => logger.info('Updates complete, sleeping.')) - .catch(e => logger.error('Error', {error: e})) + .catch(e => { + if (e instanceof Error) { + logger.error('Error', {error: _.pick(e, [ + 'message', 'stack', + 'response.status', 'response.statusText', 'response.headers', 'response.data', + 'request.method', 'request.body', 'request.contentType', 'request.headers', 'request.url' + ])}); + } else { + logger.error('Error', {error: e}); + } + }); await main(); setInterval(main, onstarConfig.refreshInterval);