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,
|
username: process.env.ONSTAR_USERNAME,
|
||||||
password: process.env.ONSTAR_PASSWORD,
|
password: process.env.ONSTAR_PASSWORD,
|
||||||
onStarPin: process.env.ONSTAR_PIN,
|
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
|
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});
|
logger.info('OnStar Config', {onstarConfig});
|
||||||
|
|
||||||
@ -86,15 +88,18 @@ const configureMQTT = async (commands, client, mqttHA) => {
|
|||||||
.then(data => {
|
.then(data => {
|
||||||
// TODO refactor the response handling for commands
|
// TODO refactor the response handling for commands
|
||||||
logger.info('Command completed', {command});
|
logger.info('Command completed', {command});
|
||||||
const location = _.get(data, 'response.data.commandResponse.body.location');
|
data = _.get(data, 'response.data');
|
||||||
if (data && location) {
|
if (data) {
|
||||||
logger.info('Command response data', {location});
|
logger.info('Command response data', {data});
|
||||||
const topic = mqttHA.getStateTopic({name: command});
|
const location = _.get(data, 'response.data.commandResponse.body.location');
|
||||||
// TODO create device_tracker entity. MQTT device tracker doesn't support lat/lon and mqtt_json
|
if (location) {
|
||||||
// doesn't have discovery
|
const topic = mqttHA.getStateTopic({name: command});
|
||||||
client.publish(topic,
|
// TODO create device_tracker entity. MQTT device tracker doesn't support lat/lon and mqtt_json
|
||||||
JSON.stringify({latitude: location.lat, longitude: location.long}), {retain: true})
|
// doesn't have discovery
|
||||||
.then(() => logger.info('Published location to topic.', {topic}));
|
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}));
|
.catch(err=> logger.error('Command error', {command, err}));
|
||||||
@ -169,7 +174,17 @@ const configureMQTT = async (commands, client, mqttHA) => {
|
|||||||
|
|
||||||
const main = async () => run()
|
const main = async () => run()
|
||||||
.then(() => logger.info('Updates complete, sleeping.'))
|
.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();
|
await main();
|
||||||
setInterval(main, onstarConfig.refreshInterval);
|
setInterval(main, onstarConfig.refreshInterval);
|
||||||
|
Loading…
Reference in New Issue
Block a user