Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added logging, check for existing files, added more files to prevent map rotation after load #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
21 changes: 17 additions & 4 deletions maploader.js
Original file line number Diff line number Diff line change
@@ -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:[email protected]',{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');
});
Expand All @@ -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);
}

Expand All @@ -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
}
}
}