Skip to content
dcyoung edited this page Aug 18, 2017 · 4 revisions

Work in Progress...

Design Philosophy

  • Simple dependencies or platform requirements
  • Simple messaging (ie: printing stringied JSON)
  • Simple languages (Javascript)
  • Familiar/standard environments (Node + express)

Express Routing + Jade Templating

Express handles all of the routing. The app.js file sets up all the routes. Currently there are 4 major pages:

  • index
  • scan
  • file_manager
  • component_testing

Each page has a file in the routes/ directory that manages the backend for that page. For example, routes/scans.js handles all of the backend logic for the scan page.

The front end design/html is handled via Jade, the default for express views. Each page has a jade file in the views/ directory. Each jade file extends a base template (views/layout.jade) which is where all the common includes are called. The actual stylesheets reside in public/stylsheets/.

Lastly, the browser (client-side) javascript for each page resides in a public/javascript/pages/<page_name>.js file. For example, the public/javascript/pages/scan.js handles all of the client-side logic for requesting updates about scan-progress and visually updating the progress bar.

To summarize, for each page:

  • routes/<page_name>.js: routing logic + backend
  • views/<page_name>.jade: front-end design template
  • public/javascript/pages/<page_name>.js: client-side logic

Launching Scanner Scripts as Child Processes

The node webapp cannot interact with the hardware directly. Instead it calls other processes and attempts to capture their status updates/progress to report to the user.

Currently the scanner processes are all written as python scripts, which can be executed by spawning a child process from within the node app. To stay informed about the child process, the stdoutof the child process (python) is piped into the stdin of the node process (webapp). That is a fancy way of saying, the python script prints a message and the node webapp receives the message as an event. This piping of stdin/stdout is essentially a poor man's IPC (inter process communication).

However, the node webapp must be able to interpret the output of a python process. Currently this is accomplished by having all python processes (scanner scripts) print stringified JSON messages with a consistent structure. Although it is has drawbacks, JSON was chosen here for a few reasons:

  1. keeps the communication simple and beginner friendly
  2. keeps the messages human readable on a terminal
  3. incredibly easy parsing within the node-app
  4. easy to hand off/pass to the client side JavaScript

Requesting Status Updates from Client Side JavaScript

The client (browser) side JavaScript has no way to tell what's happening on the Raspberry Pi unless the webapp provides that information. To avoid re-routing, the client JavaScript (ex: public/javascript/pages/scan.js) will request status updates at regular intervals via AJAX.

The back end (ex: routes/scan.js) receives the request and sends back an update with the latest status message it has received from the child process that is executing a python script (ex: scanner/scanner.py).