diff --git a/client/src/Utils/analytics.js b/client/src/Utils/analytics.js deleted file mode 100644 index ab461aa5..00000000 --- a/client/src/Utils/analytics.js +++ /dev/null @@ -1,61 +0,0 @@ -export const parseXML = xml => { - const parser = new DOMParser(); - const xmlData = { - blocks: {}, - categories: {} - }; - let xmlDoc = parser.parseFromString(xml, "text/xml"); - const xmlBlocks = xmlDoc.querySelectorAll('block'); - for (const block of xmlBlocks) { - const blockType = block.getAttribute('type'); - if(xmlData.blocks[blockType]) { - xmlData.blocks[blockType].count++; - } else { - xmlData.blocks[blockType] = { - count: 1, - deleted: 0 - }; - } - } - return xmlData; -} - -export const compareXML = ({blocks: currentBlocks}, {blocks: previousBlocks}) => { - const blocks = diffObjects(currentBlocks, previousBlocks); - return { - blocks - } -} - -const diffObjects = (currentObj, previousObj) => { - const currentKeys = Object.keys(currentObj) - const prevKeys = Object.keys(previousObj) - // deleted all of one type of block - if (prevKeys.length > currentKeys.length) { - for(let key of prevKeys) { - if(currentKeys.indexOf(key) === -1) { - console.log('deleted all of one block'); - currentObj[key] = { - count: 0, - deleted: previousObj[key].deleted + 1 - } - } - } - } - for (let key of currentKeys) { - if (key in previousObj) { - if (currentObj[key].count < previousObj[key].count) { - currentObj[key].deleted = previousObj[key].deleted + 1; - console.log('a block was deleted'); - } - if(currentObj[key].deleted < previousObj[key].deleted) { - console.log('a block was reintroduced'); - currentObj[key] = { - count: 1, - deleted: previousObj[key].deleted - } - } - } - } - return currentObj -} \ No newline at end of file diff --git a/client/src/components/DayPanels/Utils/consoleHelpers.js b/client/src/components/DayPanels/Utils/consoleHelpers.js index fe8a3018..285f5024 100644 --- a/client/src/components/DayPanels/Utils/consoleHelpers.js +++ b/client/src/components/DayPanels/Utils/consoleHelpers.js @@ -59,7 +59,6 @@ const readUntilClose = async ( .pipeThrough(new window.TransformStream(new LineBreakTransformer())) .getReader(); - console.log('reader opened'); let string = ''; plotData = []; @@ -70,7 +69,6 @@ const readUntilClose = async ( reader.releaseLock(); break; } - console.log(value); if (type === 'notNewLine') { string += value; document.getElementById('console-content').innerHTML = string; @@ -100,7 +98,6 @@ export const writeToPort = async (data) => { writer = port.writable.getWriter(); data += '\n'; await writer.write(textEncoder.encode(data)); - console.log(textEncoder.encode(data)); writer.releaseLock(); }; diff --git a/client/src/components/DayPanels/consoleHelpers.js b/client/src/components/DayPanels/consoleHelpers.js deleted file mode 100644 index 10081017..00000000 --- a/client/src/components/DayPanels/consoleHelpers.js +++ /dev/null @@ -1,123 +0,0 @@ -let port; -let reader; -let writer; -let readableStreamClosed; - -class LineBreakTransformer { - constructor() { - this.container = ''; - } - - transform(chunk, controller) { - this.container += chunk; - const lines = this.container.split('\r\n'); - this.container = lines.pop(); - lines.forEach((line) => controller.enqueue(line)); - } - - flush(controller) { - controller.enqueue(this.container); - } -} - -export const openConnection = async (baudRate_, newLine) => { - //requesting port on the pop up window. - port = window['port']; - - var options = { - baudRate: baudRate_, - parity: 'none', - dataBits: 8, - stopBits: 1, - bufferSize: 1024, - }; - - // connect to port on baudRate 9600. - await port.open(options); - console.log(`port opened at baud rate: ${baudRate_} `); - document.getElementById('console-content').innerHTML = ''; - readUntilClose(newLine); -}; - -const readUntilClose = async (newLine) => { - const textDecoder = new window.TextDecoderStream(); - readableStreamClosed = port.readable.pipeTo(textDecoder.writable); - // reader = textDecoder.readable.getReader(); - reader = textDecoder.readable - .pipeThrough(new window.TransformStream(new LineBreakTransformer())) - .getReader(); - - console.log('reader opened'); - let string = ''; - while (true) { - const { value, done } = await reader.read(); - if (done) { - // Allow the serial port to be closed later. - reader.releaseLock(); - break; - } - console.log(value); - if (!newLine) { - string += value; - document.getElementById('console-content').innerHTML = string; - } else { - let newP = document.createElement('p'); - newP.innerHTML = value; - newP.style.margin = 0; - document.getElementById('console-content').appendChild(newP); - newP.scrollIntoView(); - } - } -}; - -export const writeToPort = async (data) => { - const textEncoder = new window.TextEncoder(); - writer = port.writable.getWriter(); - data += '\n'; - await writer.write(textEncoder.encode(data)); - console.log(textEncoder.encode(data)); - writer.releaseLock(); -}; - -export const disconnect = async () => { - reader.cancel(); - await readableStreamClosed.catch(() => { - /* Ignore the error */ - }); - if (typeof writer !== 'undefined') { - const textEncoder = new window.TextEncoder(); - writer = port.writable.getWriter(); - await writer.write(textEncoder.encode('')); - await writer.close(); - } - await port.close(); -}; - -export const connectToPort = async () => { - const filters = [ - { usbVendorId: 0x2341, usbProductId: 0x0043 }, - { usbVendorId: 0x2341, usbProductId: 0x0001 }, - ]; - try { - port = await navigator.serial.requestPort({ filters }); - } catch (e) { - console.error(e); - return; - } - window['port'] = port; -}; - -export const handleOpenConnection = async (baudRate, newLine) => { - if (typeof window['port'] === 'undefined') { - await connectToPort(); - if (typeof window['port'] === 'undefined') { - return; - } - } - await openConnection(baudRate, newLine); -}; - -export const handleCloseConnection = async () => { - console.log('Close connection'); - disconnect(); -}; diff --git a/client/src/components/DayPanels/helpers.js b/client/src/components/DayPanels/helpers.js deleted file mode 100644 index 3bc83881..00000000 --- a/client/src/components/DayPanels/helpers.js +++ /dev/null @@ -1,197 +0,0 @@ -import { - createSubmission, - getSubmission, - saveWorkspace, - updateDayTemplate, -} from '../../Utils/requests'; -import { message } from 'antd'; - -const AvrboyArduino = window.AvrgirlArduino; - -export const setLocalSandbox = (workspaceRef) => { - let workspaceDom = window.Blockly.Xml.workspaceToDom(workspaceRef); - let workspaceText = window.Blockly.Xml.domToText(workspaceDom); - const localActivity = JSON.parse(localStorage.getItem('sandbox-day')); - - let lastActivity = { ...localActivity, template: workspaceText }; - localStorage.setItem('sandbox-day', JSON.stringify(lastActivity)); -}; - -// Generates xml from blockly canvas -export const getXml = (workspaceRef, shouldAlert = true) => { - const { Blockly } = window; - - let xml = Blockly.Xml.workspaceToDom(workspaceRef); - let xml_text = Blockly.Xml.domToText(xml); - if (shouldAlert) alert(xml_text); - return xml_text; -}; - -// Generates javascript code from blockly canvas -export const getJS = (workspaceRef) => { - window.Blockly.JavaScript.INFINITE_LOOP_TRAP = null; - let code = window.Blockly.JavaScript.workspaceToCode(workspaceRef); - alert(code); - return code; -}; - -// Generates Arduino code from blockly canvas -export const getArduino = (workspaceRef, shouldAlert = true) => { - window.Blockly.Arduino.INFINITE_LOOP_TRAP = null; - let code = window.Blockly.Arduino.workspaceToCode(workspaceRef); - if (shouldAlert) alert(code); - return code; -}; - -let intervalId; -const compileFail = (setSelectedCompile, setCompileError, msg) => { - setSelectedCompile(false); - message.error('Compile Fail', 3); - setCompileError(msg); -}; -// Sends compiled arduino code to server and returns hex to flash board with -export const compileArduinoCode = async ( - workspaceRef, - setSelectedCompile, - setCompileError, - day, - isStudent -) => { - setSelectedCompile(true); - const sketch = getArduino(workspaceRef, false); - let workspaceDom = window.Blockly.Xml.workspaceToDom(workspaceRef); - let workspaceText = window.Blockly.Xml.domToText(workspaceDom); - let path; - isStudent ? (path = '/submissions') : (path = '/sandbox/submission'); - let id = isStudent ? day.id : undefined; - - // create an initial submission - const initialSubmission = await createSubmission( - id, - workspaceText, - sketch, - path, - isStudent - ); - - // if we fail to create submission - if (!initialSubmission.data) { - compileFail( - setSelectedCompile, - setCompileError, - 'Oops. Something went wrong, please check your internet connection.' - ); - return; - } - // Get the submission Id and send a request to get the submission every - // 0.25 second until the submission status equal to COMPLETE. - intervalId = setInterval( - () => - getAndFlashSubmission( - initialSubmission.data.id, - path, - isStudent, - setSelectedCompile, - setCompileError - ), - 250 - ); - - // Set a timeout of 20 second. If the submission status fail to update to - // COMPLETE, show error. - setTimeout(() => { - if (intervalId) { - clearInterval(intervalId); - intervalId = undefined; - compileFail( - setSelectedCompile, - setCompileError, - 'Oops. Something went wrong, please try again.' - ); - } - }, 20000); -}; - -const getAndFlashSubmission = async ( - id, - path, - isStudent, - setSelectedCompile, - setCompileError -) => { - // get the submission - const response = await getSubmission(id, path, isStudent); - // If we fail to retrive submission - if (!response.data) { - if (intervalId) { - clearInterval(intervalId); - intervalId = undefined; - } - compileFail( - setSelectedCompile, - setCompileError, - 'Oops. Something went wrong, please check your internet connection.' - ); - return; - } - - // if the submission is not complete, try again later - if (response.data.status !== 'COMPLETED') { - return; - } - - // If the submission is ready - if (intervalId) { - clearInterval(intervalId); - intervalId = undefined; - } - // flash the board with the output - await flashArduino(response, setSelectedCompile, setCompileError); -}; - -const flashArduino = async (response, setSelectedCompile, setCompileError) => { - if (response.data) { - // if we get a success status from the submission, send it to arduino - if (response.data.success) { - // converting base 64 to hex - let Hex = atob(response.data.hex).toString(); - - const avrgirl = new AvrboyArduino({ - board: 'uno', - debug: true, - }); - - avrgirl.flash(Hex, (err) => { - if (err) { - console.log(err); - } else { - console.log('done correctly.'); - message.success('Compile Success', 3); - setSelectedCompile(false); - } - }); - } - // else if there is error on the Arduino code, show error - else if (response.data.stderr) { - message.error('Compile Fail', 3); - setSelectedCompile(false); - setCompileError(response.data.stderr); - } - } else { - message.error(response.err); - } -}; - -// save current workspace -export const handleSave = async (dayId, workspaceRef, replay) => { - let xml = window.Blockly.Xml.workspaceToDom(workspaceRef.current); - let xml_text = window.Blockly.Xml.domToText(xml); - return await saveWorkspace(dayId, xml_text, replay); -}; - -export const handleCreatorSaveDay = async (dayId, workspaceRef, blocksList) => { - let xml = window.Blockly.Xml.workspaceToDom(workspaceRef.current); - let xml_text = window.Blockly.Xml.domToText(xml); - - return await updateDayTemplate(dayId, xml_text, blocksList); -}; diff --git a/client/src/components/DropdownMenu/ReportDropdown.js b/client/src/components/DropdownMenu/ReportDropdown.js deleted file mode 100644 index e8b5b812..00000000 --- a/client/src/components/DropdownMenu/ReportDropdown.js +++ /dev/null @@ -1,29 +0,0 @@ -import React from 'react'; -import './ReportDropdown.less'; -import { Form, Select } from 'antd'; -const { Option } = Select; - - -function handleChange(value) { - console.log(`selected ${value}`); -} - -export default function ReportDropdown({label, menuName, menuItems}) { - const menus = Object.entries(menuItems).map((key) => { - return ( - - ) - }) - - return ( -