diff --git a/README.md b/README.md index 22ebc9c..ff24c70 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,20 @@ # MapLoader Map Backup/Restore for Xiaomi Vacuum and Roborock +## + +## +This is a fork of Thyraz maploader! Thanks for his great work. + +## +Modifications made by me: +- added check wether file exists. If not --> skip + create error msg +- added all necessary files for transfer; Reason: reloading map worked fine but several times docking station was rotated. Made already defined zones useless +- added logging via mqtt. Subscribe to: rockrobo/map/log + + + + ## Description MapLoader is a NodeJS based service, running on the robot that listens to MQTT commands.\ Available topics: diff --git a/maploader.js b/maploader.js index b08f5cc..b5601df 100644 --- a/maploader.js +++ b/maploader.js @@ -1,10 +1,11 @@ const fs = require('fs'); const mqtt = require('mqtt'); -const client = mqtt.connect('mqtt://172.17.2.86'); +const client = mqtt.connect('mqtt://user:passwd@xxx.xxx.xxx.xxx',{clientId:"rockrobo"});//enter your mqtt-server credentials here // Subscribe Topics client.on('connect', () => { console.log("Connected"); + client.publish("rockrobo/map/log", "Connected");//added log via mqtt client.subscribe('rockrobo/map/load'); client.subscribe('rockrobo/map/save'); }); @@ -16,6 +17,8 @@ client.on('message', (topic, message) => { var destination = '/mnt/data/rockrobo/'; console.log("Received Load Request: " + message); + + client.publish("rockrobo/map/log", "Received Load Request: " + message);//added log via mqtt copyFiles(source, destination); } @@ -24,21 +27,31 @@ client.on('message', (topic, message) => { var destination = '/mnt/data/maploader/maps/' + message + '/'; console.log("Received Save Request: " + message); + client.publish("rockrobo/map/log", "Received Save Request: " + message);//added log via mqtt copyFiles(source, destination); } }); // Backup or restore map files function copyFiles(source, destination) { - const files = ['user_map0', 'last_map', 'PersistData_1.data', 'PersistData_2.data']; + const files = ['user_map0', 'last_map', 'PersistData_1.data', 'PersistData_2.data', 'ChargerPos.data', 'Rotater.cfg', 'StartPos.data'];//added all necessary files for complete transfer, otherwise docking station could be rotated to map which makes created zones useless if (!fs.existsSync(destination)){ fs.mkdirSync(destination); console.log('Created directory: ' + destination); + client.publish("rockrobo/map/log", 'Created directory: ' + destination);//added log via mqtt } for (i = 0; i < files.length; i++) { - fs.copyFileSync(source + files[i], destination + files[i]); - console.log('Copied ' + files[i] + ' from ' + source + ' to ' + destination); + if (!fs.existsSync(source + files[i])){//check if files not existing and create error msg + console.log('Error: ' + source + files[i] + ' not found'); + var options = {retain:true, qos:1}; + client.publish('Error: ' + "rockrobo/map/log", files[i] + ' not found', options);//added log via mqtt + } + else{//if file present, copy and create msg + fs.copyFileSync(source + files[i], destination + files[i]); + console.log('Copied ' + files[i] + ' from ' + source + ' to ' + destination); + client.publish("rockrobo/map/log", 'Copied ' + files[i] + ' from ' + source + ' to ' + destination);//added log via mqtt + } } }