From f1326d2e748d130bf8607c2e254057f093f59c78 Mon Sep 17 00:00:00 2001 From: finchiedev Date: Tue, 20 Aug 2019 21:51:05 +0800 Subject: [PATCH] Improve style of createServo.js --- App/JS/createServo.js | 118 +++++++++++++++++++++++------------------- package.json | 1 + yarn.lock | 5 ++ 3 files changed, 72 insertions(+), 52 deletions(-) diff --git a/App/JS/createServo.js b/App/JS/createServo.js index 2bc28c7..f8f334d 100644 --- a/App/JS/createServo.js +++ b/App/JS/createServo.js @@ -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 = ''; @@ -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 = ''; @@ -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 ${filename}.csv`; - 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!'); @@ -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!'); -} \ No newline at end of file +} diff --git a/package.json b/package.json index bad0f3a..9f73409 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/yarn.lock b/yarn.lock index bfe06a3..6240caf 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"