Skip to content

Commit

Permalink
Improve error logging and command response output.
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelwoods committed Nov 30, 2022
1 parent ab4e5c7 commit ad3f292
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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});

Expand Down Expand Up @@ -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}));
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit ad3f292

Please sign in to comment.