Skip to content

Commit

Permalink
Improve style of createServo.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Finchiedev committed Aug 20, 2019
1 parent 9f1f697 commit f1326d2
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 52 deletions.
118 changes: 66 additions & 52 deletions App/JS/createServo.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ let filename = '';

const client = new net.Socket();
client.on('error', function(err) {
console.error('Unable to connect to the fileserver!' + '\n' +
'Ensure that the robot is powered and active.');
})
console.error('Unable to connect to the fileserver!' + '\n' +
' Ensure that the robot is powered and active.');
});

/**
* Parses the header of a given table
* @param {Array} table
* @returns {String} The header of the specified table
* @param {Array} table
* @return {String} The header of the specified table
*/
function parseDynamixelHeader(table) {
let build = '';
Expand All @@ -36,7 +36,7 @@ function parseDynamixelHeader(table) {
* @param {Object} table The table returned when executing parsetable
* @param {Boolean} includeHeader If the CSV should include headings - use
* false if this is not the top item in the table
* @returns {String} A string of CSV data with the control table
* @return {String} A string of CSV data with the control table
*/
function parseDynamixelTable(table) {
let build = '';
Expand All @@ -54,72 +54,86 @@ function parseDynamixelTable(table) {
return build;
}

/**
* Computes all HTML tables from indexes and converts to CSV
* @param {String} html The downloaded e-Manual document as HTML
* @param {Array} indexes The indexes of all tables involving the control table
* @return {String} The CSV-formatted table containing all of the control table
*/
function generateCSV(html, indexes) {
if (indexes == null || indexes == undefined || indexes.length == 0) {
console.error('Invalid argument!' +
console.error('Invalid argument!' +
'Ensure that the argument "indexes" is valid!');
return '';
}

let fileData = '';
const $ = cheerio.load(html);
cheerioTableparser($);
const cheerioTable = $('table');

fileData += parseDynamixelHeader(cheerioTable.eq(
indexes[0]).parsetable(true, true, true));
for (var i = 0; i < indexes.length; i++) {
indexes[0]).parsetable(true, true, true));
for (let i = 0; i < indexes.length; i++) {
fileData += parseDynamixelTable(cheerioTable.eq(
indexes[i]).parsetable(true, true, true));
}

return fileData;
indexes[i]).parsetable(true, true, true));
}

function getFile() {
const url = document.getElementById('url').value;
const re = /^https?:\/\/emanual\.robotis\.com\/docs\/en\/dxl\/[a-z0-9]+\/[a-z0-9]+-?[a-z0-9]+/
const res = url.match(re)
if (res !== null && res !== undefined) {
link = res[0];
console.log(`Extracted URL as ${link}`);
rp(link)
.then(function(html) {
console.log('Successfully downloaded the site!' +

return fileData;
}

/**
* Downloads and converts the link to a CSV, saving it to Resources/Servos and
* storing it in the variable fileData
*/
function getFile() {
const url = document.getElementById('url').value;
const re = /^https?:\/\/emanual\.robotis\.com\/docs\/en\/dxl\/[a-z0-9]+\/[a-z0-9]+-?[a-z0-9]+/;
const res = url.match(re);
if (res !== null && res !== undefined) {
link = res[0];
console.log(`Extracted URL as ${link}`);
rp(link)
.then(function(html) {
console.log('Successfully downloaded the site!' +
' Now scraping the data...');
fileData = generateCSV(html, [1, 2]);
console.log('Downloaded table!');

const linkPieces = link.split('/');
if (linkPieces[linkPieces.length - 1] == '') {
linkPieces.pop(linkPieces.length - 1);
}
filename = linkPieces[linkPieces.length - 1].replace('-', '');
fileData = generateCSV(html, [1, 2]);
console.log('Downloaded table!');

fs.writeFile(`App/JS/Resources/Servos/${filename}.csv`, fileData, function(err) {
if (err) {
console.error('Failed to write file! Do you have access?');
const linkPieces = link.split('/');
if (linkPieces[linkPieces.length - 1] == '') {
linkPieces.pop(linkPieces.length - 1);
}
console.log(`Saved the file as ${filename}.csv`);
});
filename = linkPieces[linkPieces.length - 1].replace('-', '');

fs.writeFile(`App/JS/Resources/Servos/${filename}.csv`,
fileData, function(err) {
if (err) {
console.error('Failed to write file! Do you have access?');
}
console.log(`Saved the file as ${filename}.csv`);
});

uploadButton = document.getElementById('upload')
uploadButton.innerHTML =
uploadButton = document.getElementById('upload');
uploadButton.innerHTML =
`Upload <strong>${filename}.csv</strong>`;
uploadButton.style.display = null;
})
.catch(function(err) {
console.error('An error has occured!' + '\n' +
uploadButton.style.display = null;
})
.catch(function(err) {
console.error('An error has occured!' + '\n' +
'Check that you have an active internet connection' + '\n' +
'Additionally, check that you are able to access sites' + '\n' +
'This may be due to you being connected to the robot' + '\n' +
`Recieved error: \n${err}`);
});
});
} else {
console.error('It seems that your URL is invalid!');
}
}
}

/**
* Uploads the data stored in fileData through TCP to a listening fileserver
*/
function uploadFile() {
if (fileData == null || fileData == undefined || fileData == '') {
console.error('There is no downloaded file!');
Expand All @@ -128,13 +142,13 @@ function uploadFile() {

client.connect({
port: 5004,
address: '127.0.0.1'
address: '127.0.0.1',
});

client.on('connect', function() {
client.write(`${filename}\n${fileData}`);
console.log('Wrote file to server!');
})
});

console.log('Uploaded table!');
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"cheerio": "^1.0.0-rc.3",
"cheerio-tableparser": "^1.0.1",
"electron": "^6.0.2",
"eslint-config-google": "^0.13.0",
"request-promise": "^4.2.4"
}
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,11 @@ error-ex@^1.2.0:
dependencies:
is-arrayish "^0.2.1"

eslint-config-google@^0.13.0:
version "0.13.0"
resolved "https://registry.yarnpkg.com/eslint-config-google/-/eslint-config-google-0.13.0.tgz#e277d16d2cb25c1ffd3fd13fb0035ad7421382fe"
integrity sha512-ELgMdOIpn0CFdsQS+FuxO+Ttu4p+aLaXHv9wA9yVnzqlUGV7oN/eRRnJekk7TCur6Cu2FXX0fqfIXRBaM14lpQ==

extend@~3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/extend/-/extend-3.0.2.tgz#f8b1136b4071fbd8eb140aff858b1019ec2915fa"
Expand Down

0 comments on commit f1326d2

Please sign in to comment.