roomba-iot

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 3  →  ?path2? @ 4
/main.js
@@ -1,5 +1,5 @@
#!/usr/bin/env nodejs
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
// Copyright (C) Wizardry and Steamworks 2018 - License: GNU GPLv3 //
// Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
// rights of fair usage, the disclaimer and warranty conditions. //
@@ -19,9 +19,12 @@
const config = YAML.load('config.yml')
 
// Set up logger.
winston.add(winston.transports.File, {
filename: config.log
})
const logger = winston.createLogger({
transports: [
new winston.transports.Console(),
new winston.transports.File({ filename: config.log })
]
});
 
// Initiate connection to MQTT.
const mqttClient = mqtt.connect(config.mqtt.url)
@@ -38,7 +41,7 @@
})
 
// Set up sensor packet assembler.
port.on('data', AssembleSensorPacket);
port.on('data', PublishSensorPacket);
 
// Set up GPIO wake pin.
const wakeGPIO = new Gpio(config.wake, 'out')
@@ -48,7 +51,7 @@
RoombaSafe(function () {
// Poll sensors periodically.
setInterval(function () {
port.write(new Buffer(COMMAND_SEQUENCES['query_all_sensors']))
port.write(new Buffer.from(COMMAND_SEQUENCES['query_all_sensors']))
}, config.sensor_poll_interval)
})
})
@@ -55,12 +58,12 @@
 
// Subscribe to configured topic when client connects and start polling sensors.
mqttClient.on('connect', function () {
winston.info('Connected to MQTT server')
logger.info('Connected to MQTT server')
mqttClient.subscribe(config.mqtt.topic, function (error) {
if (error) {
winston.error('Unable to subscribe to MQTT topic')
logger.error('Unable to subscribe to MQTT topic')
}
winston.info("Subscribed to MQTT topic " + config.mqtt.topic)
logger.info("Subscribed to MQTT topic " + config.mqtt.topic)
})
})
 
@@ -71,20 +74,24 @@
 
message = message.toString()
 
winston.debug('Received message: ' + message)
logger.debug('Received message: ' + message)
 
// Skip commands that do not figure in the command sequence table.
if (!(message in COMMAND_SEQUENCES))
return
 
winston.info('Executing Roomba command: ' + message)
logger.info('Executing Roomba command: ' + message)
 
// Execute Roomba command in safe mode.
RoombaSafe(function () {
port.write(new Buffer(COMMAND_SEQUENCES[message]))
port.write(new Buffer.from(COMMAND_SEQUENCES[message]))
})
})
 
mqttClient.on('reconnect', () => {
logger.info('Reconnecting to MQTT server...')
})
 
/*
* Wake Roomba device.
* Sequence:
@@ -97,7 +104,7 @@
function RoombaWake(gpio, callback) {
gpio.write(1, err => {
if (err) {
winston.error('Could not wake Roomba!')
logger.error('Could not wake Roomba!')
return
}
 
@@ -104,13 +111,13 @@
setTimeout(function () {
gpio.write(0, function (err) {
if (err) {
winston.error('Could not wake Roomba!')
logger.error('Could not wake Roomba!')
return
}
setTimeout(function () {
gpio.write(1, function (err) {
if (err) {
winston.error('Could not wake Roomba!')
logger.error('Could not wake Roomba!')
return
}
callback()
@@ -126,9 +133,9 @@
*/
function RoombaSafe(callback) {
setTimeout(function () {
port.write(new Buffer(COMMAND_SEQUENCES['start']))
port.write(new Buffer.from(COMMAND_SEQUENCES['start']))
setTimeout(function () {
port.write(new Buffer(COMMAND_SEQUENCES['safe']))
port.write(new Buffer.from(COMMAND_SEQUENCES['safe']))
setTimeout(callback, 500)
}, 100)
}, 100)
@@ -139,7 +146,7 @@
*/
var packetBuffer = null
 
function AssembleSensorPacket(data) {
function PublishSensorPacket(data) {
if (packetBuffer) {
data = Buffer.concat(
[packetBuffer, data], packetBuffer.length + data.length)
@@ -150,7 +157,7 @@
 
decoder.decode_all_sensors(packet, function (result, position, error) {
if (error) {
winston.error('Packet assembly failed with error:' + error)
logger.error('Packet assembly failed with error:' + error)
return
}