Improve error logging and command response output.
This commit is contained in:
parent
ab4e5c7f24
commit
ad3f292135
39
src/index.js
39
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);
|
||||
|
Loading…
Reference in New Issue
Block a user