Backend: Difference between revisions

From Rsewiki
 
(27 intermediate revisions by the same user not shown)
Line 1: Line 1:
== Backend ==
== Backend documentation ==


__TOC__
__TOC__


=== Introduction ===
== Backend Codebase Overview ==


The backend of the Robot Dashboard is responsible for managing communication between the frontend and the Robobot. It is developed using Node.js and Express, with MQTT handling communication with the robot and Socket.IO providing real-time updates to the frontend.
The backend source code of the Robot Dashboard is located in the <code>back-end-node-js/</code> directory, which serves as the root directory of the backend application.


The backend acts as an intermediary between the robot and the web application, processing incoming robot data and forwarding relevant information to the dashboard.
The codebase is organized into several directories and files, each responsible for specific functionality, including API handling, MQTT communication, security, and real-time communication. This separation helps developers navigate the codebase, understand the responsibilities of individual modules, and identify relevant files for maintenance or further development.


The backend consists of several files and components, each serving a specific purpose. '''Some files can be modified to customize the dashboard's functionality, while others contain core functionality and should generally remain unchanged''' to ensure the system operates correctly.
The following sections provide an overview of the backend directory structure and the responsibilities of its main files and modules.


This section provides an overview of the backend architecture, its file structure, and guidelines for configuring and modifying the system.
=== Configuration and Customization ===


== Backend Architecutre ==
The backend contains several configuration settings that allow developers to adapt the system to different requirements.


Some settings can be modified without affecting the underlying application logic, while others require changes across multiple files to maintain proper communication between the frontend, backend, and Robobot


The backend of the Robot Dashboard is built using Node.js and Express and acts as a communication layer between the Robobot and the frontend application.
This section outlines the available configuration options, their purposes, and the considerations developers should be aware of before making changes.


The architecture consists of three main components:
==== Environment Variables ====


# '''Express:''' Handles HTTP requests, including user authentication and session management.
The backend contains two <code>.env</code> files located in different directories, each serving a different purpose:
#'''MQTT:''' Enables communication between the backend and the Tuype robot through a publish-subscribe messaging system.
# '''Socket.IO:''' Establishes real-time, bidirectional communication between the backend and frontend, allowing robot data to be displayed without manually refreshing the page.


=== Communication Flow ===
* '''Root directory (<code>back-end-node-js/.env</code>):''' Contains the environment variables used by the main backend application, including <code>AdminPassword</code> and <code>SessionSecret</code>. These variables are used for authentication and session management.


The backend acts as an intermediary between the frontend and Robobot.
* '''Scripts directory (<code>back-end-node-js/scripts/.env</code>):''' Contains environment variables used specifically by scripts and test files within the <code>scripts/</code> directory. These settings are separate from the main backend configuration.


# The Robobot publishes data to specific MQTT topics through an MQTT broker.
===== Main Backend Environment Variables =====
# The backend subscribes to relevant topics and receives incoming robot data.
# The backend processes the received messages and forwards relevant information to the frontend using Socket.IO.
# The frontend receives these updates and displays them in the corresponding dashboard widgets.


For interactions that require sending information or commands to the robot, the communication can also work in the opposite direction, with the backend publishing messages to the appropriate MQTT topics.
The main backend <code>.env</code> file contains two environment variables:


=== Model-View-Controller===
* '''AdminPassword:''' Stores the bcrypt-hashed administrator password. This value should only be modified when changing the dashboard's login password. The new password must first be hashed using bcrypt before replacing the existing value.


The Robot Dashboard was designed using the Model–View–Controller (MVC) architectural pattern to separate the application's user interface, application logic, and communication handling.
* '''SessionSecret:''' Used to sign session cookies and protect their integrity. This value should generally remain unchanged, as modifying it may invalidate existing user sessions. It should only be changed when necessary, such as when rotating a compromised secret.


The architecture consists of three main components:
Both variables contain sensitive information and should not be publicly shared or included in version control.


*'''Model:''' Represents the backend logic and is responsible for processing data and managing the application's functionality.
When modifying environment variables, ensure that the correct <code>.env</code> file is updated, as changes to one file may not affect the other.
*'''View:''' Represents the frontend user interface, where users can interact with the dashboard and visualize real-time robot data.
*'''Controller:''' Acts as an intermediary between the Model, View, and Tuype robot. It handles communication through REST APIs, Socket.IO, and MQTT, as well as session management.


[[file: ModelViewController.png | 700px]]
For instructions on modifying these settings, see [[#Authentication and Session Configuration|Authentication and Session Configuration]].


When a user interacts with the dashboard, the input is sent from the View to the Controller. The Controller processes the request and communicates with the Model when application logic or data processing is required.
====  MQTT Configuration ====
The MQTT protocol is created in the "mqttConnection.js" file, and is the main file for communicating with the robot. To communicate different commands is used:
* mqttClient.subscribe
* mqttClient.unsubscribe
* mqttClient.publish
* mqttClient.on


For robot-related interactions, the Controller communicates with the Tuype robot through MQTT. The robot publishes data to MQTT topics, which the backend subscribes to and forwards to the frontend through Socket.IO.
===== mqttClient.subscribe =====
This is used to make the back-end begin to listen to a certain topic. The syntax of such message is:


The Controller is also responsible for sending updates back to the View, allowing the dashboard to display real-time information without requiring the user to refresh the page.
<code>
mqttClient.subscribe("dtubot/drive/T0/gyroo");
</code>


=== Authentication and Session Management ===
* "dtubot" identifies the robot platform.
* "drive" represents telemetry topics used to receive data from the robot.
* "T0" is dependent on the number of Teensy Boards and is zero-indexed. It mentions which Teensy board it will listen to.
* "gyroo" represents the topic that is subscribed to.


The backend implements a password-based authentication system to restrict access to the Robot Dashboard.
===== mqttClient.unsubscribe =====
This is used to make the back-end stop listening to a topic. The syntax of such message is:


User authentication is handled through Express, where the provided password is verified against a stored password hash using bcrypt. Upon successful authentication, a session is established using
<code>
<code>
express-session
mqttClient.unsubscribe("dtubot/drive/T0/gyroo");
</code>
</code>


The session is maintained through a cookie, allowing the backend to recognize authenticated users across subsequent requests without requiring them to log in repeatedly.
* "dtubot" identifies the robot platform.
* "drive" represents telemetry topics used to receive data from the robot.
* "T0" is dependent on the number of Teensy Boards and is zero-indexed. It mentions which Teensy board it will listen to.
* "gyroo" represents the topic that is subscribed to.


The current implementation uses a single shared password rather than individual user accounts.
===== mqttClient.publish =====
This is to send a message to a specific topic, which the robot will receive. The syntax of such message is:


== Backend Codebase Overview ==
<code>
mqttClient.publish("dtubot/cmd/T0", "gyroc Start");
</code>


The backend source code of the Robot Dashboard is located in the <code>back-end-node-js/</code> directory, which serves as the root directory of the backend application.
* "dtubot" identifies the robot platform.
* "cmd" represents telemetry topics used to send commands to the robot.
* "T0" is dependent on the number of Teensy Boards and is zero-indexed. It mentions which Teensy board it will listen to.
* "gyroc Start" is the command that is sent to the robot.


The codebase is organized into several directories and files, each responsible for specific functionality, including API handling, MQTT communication, security, and real-time communication. This separation helps developers navigate the codebase, understand the responsibilities of individual modules, and identify relevant files for maintenance or further development.
===== mqttClient.on =====
This function listens to certain MQTT messages from the protocol. The syntax of such message is:


The following sections provide an overview of the backend directory structure and the responsibilities of its main files and modules.
<code>
mqttClient.on(title, payload)
</code>


=== Configuration and Customization ===
The MQTT has different label titles i.e ("message" or "error") and payload is the content that follows that title. These determines what kind of output the MQTT protocol has sent.


The backend contains several configuration settings that allow developers to adapt the system to different requirements.
If the label title is "message" it will contain a topic and a message.  


Some settings can be modified without affecting the underlying application logic, while others require changes across multiple files to maintain proper communication between the frontend, backend, and Robobot
If the label title is "error" it will contain an error message.


This section outlines the available configuration options, their purposes, and the considerations developers should be aware of before making changes.
====  Service filles Configuration ====


==== Environment Variables ====
The <code>services/</code> directory contains files responsible for handling specific backend functionalities and communication between different components of the Robot Dashboard.


The <code>.env</code> file contains two environment variables: <code>AdminPassword</code> and <code>SessionSecret</code>. These variables are used for authentication and session management.
Each service is responsible for a particular task, helping separate the application's functionality and making the codebase easier to maintain.
The backend contains two `.env` files located in different directories. Each file serves a different purpose and contains configuration settings used by the application.


* '''`.env` in the backend root directory:''' [Explain its purpose and variables.]
===== Tab Service =====
*'''`.env` in the scripts directory:''' [Explain its purpose and variables.]


When modifying environment variables, ensure that the correct file is updated, as changes to one file may not affect the other.
The <code>tabService.js</code> file is responsible for managing MQTT subscriptions based on the active dashboard tab. It communicates with <code>mqttConnection.js</code> to start and stop subscriptions, ensuring that the backend only subscribes to the data required by the selected tab.


* '''AdminPassword:''' Stores the hashed administrator password. This value should only be modified when changing the dashboard's login password. When updating it, the new password must first be hashed using bcrypt before replacing the existing value.
The file contains three main functions:


* '''SessionSecret:''' Used to sign session cookies and protect their integrity. This value should generally remain unchanged, as modifying it may invalidate existing user sessions. It should only be changed when necessary, such as when rotating a compromised secret.
* '''tabChanged(previousTabTitle, newTabTitle):''' Handles switching between dashboard tabs by stopping subscriptions associated with the previously active tab and starting subscriptions required by the newly selected tab.
* '''onlyTabClosed(previousTabTitle):''' Handles closing the last open tab by stopping its associated MQTT subscriptions.
* '''tabChangedNoPrevious(newTabTitle):''' Handles opening a tab when no previous tab was active by starting the subscriptions required by the selected tab.


Both variables contain sensitive information and should not be publicly shared or included in version control.
Each dashboard tab is associated with specific MQTT subscriptions, such as heartbeat data, IMU sensor readings, robot information, and distance sensor data.


For instructions on modifying these settings, see [[#Authentication and Session Configuration|Authentication and Session Configuration]].
When adding a new tab that requires MQTT data, the corresponding subscription handling should be added to the relevant functions in <code>tabService.js</code>.


==== MQTT Configuration ====  
==== Socket.IO Configuration ====  
Explain how to configure the MQTT broker,
Sockets is the primary method of communication between the front-end and the back-end. There are two primary functions:
connection settings, and topics.
* socket.on (used in socket.js)
* io.emit (used in mqttConnection.js)
===== socket.on =====
"socket.on" listens for any commands that have been sent with a specific topic from the front end.  


==== API Configuration ====  
===== io.emit =====  
Explain configurable API settings and endpoints.
"io.emit" sends commands to the front-end with a package.


==== Socket.IO Configuration ====
Explain real-time communication settings.
====  Authentication and Session Configuration ====
Explain which authentication and session settings
can be modified.
=== Authentication and Session Configuration ===
=== Authentication and Session Configuration ===


This section explains how to modify the authentication settings used by the Robot Dashboard.
This section explains how to modify the authentication settings used by the Robot Dashboard.
The \<code>security/\</code> directory contains the script responsible for handling authentication. This script should remain unchanged.


==== Changing the Administrator Password ====
==== Changing the Administrator Password ====


The administrator password is stored as a bcrypt hash in the <code>.env</code> file.
The administrator password is stored as a bcrypt hash in the <code>.env</code> file located in the backend root directory.
 
To change the administrator password, follow these steps:
 
===== Step 1: Generate a New Password Hash =====


To change the password:
# Open <code>back-end-node-js/scripts/generateHash.js</code>.
# Locate the variable <code>userPassword</code> and replace its current value with the desired new password.
# Open a terminal and navigate to the <code>scripts/</code> directory.
# Run the following command to generate a new bcrypt hash:
#: <code>node generateHash.js</code>
# Copy the generated hash (the second output in the terminal).


# Generate a new bcrypt hash using the desired password.
===== Step 2: Update the Environment Variable =====
# Open the <code>.env</code> file located in the <code>back-end-node-js/</code> directory.
# Replace the existing <code>AdminPassword</code> value with the newly generated hash.
# Save the file and restart the backend server for the changes to take effect.
# Verify that the new password works by logging into the dashboard.


'''Note:''' The password must be hashed using bcrypt. Do not store the password in plaintext.
# Open the <code>.env</code> file located in the backend root directory (<code>back-end-node-js/.env</code>).
# Locate the <code>AdminPassword</code> variable.
# Replace its existing value with the newly generated bcrypt hash.
# Save the file and restart the backend application to apply the changes.
# To restart the backend application, go to the robot's terminal and type <code>sudo systemctl restart webapp.service</code>
 
===== Step 3: Verify the New Password =====
 
# Open <code>back-end-node-js/scripts/testHash.js</code>.
# Update the test password to match the newly chosen administrator password.
# Ensure that the test script compares the new password against the newly generated hash.
# Run the test script from the <code>scripts/</code> directory:
#: <code>node testHash.js</code>
# The script should return <code>true</code> if the password matches the hash, or <code>false</code> if they do not match.
 
'''Note:''' The administrator password must always be hashed using bcrypt before being stored in the <code>.env</code> file. Never store plaintext passwords in environment variables or commit passwords and hashes to version control.


==== Session Secret ====
==== Session Secret ====
Line 137: Line 173:
If the secret needs to be rotated, replace it with a new, securely generated random value. Changing the secret may invalidate existing sessions, requiring users to log in again.
If the secret needs to be rotated, replace it with a new, securely generated random value. Changing the secret may invalidate existing sessions, requiring users to log in again.


== Core files and Restrictions ==
If a new session needs to be generated, type from the root directory
 
<code> node -e "console.log(require('crypto').randomBytes(32).toString('hex'))" </code>
== Running the backend ==

Latest revision as of 13:41, 13 October 2026

Backend documentation

Backend Codebase Overview

The backend source code of the Robot Dashboard is located in the back-end-node-js/ directory, which serves as the root directory of the backend application.

The codebase is organized into several directories and files, each responsible for specific functionality, including API handling, MQTT communication, security, and real-time communication. This separation helps developers navigate the codebase, understand the responsibilities of individual modules, and identify relevant files for maintenance or further development.

The following sections provide an overview of the backend directory structure and the responsibilities of its main files and modules.

Configuration and Customization

The backend contains several configuration settings that allow developers to adapt the system to different requirements.

Some settings can be modified without affecting the underlying application logic, while others require changes across multiple files to maintain proper communication between the frontend, backend, and Robobot

This section outlines the available configuration options, their purposes, and the considerations developers should be aware of before making changes.

Environment Variables

The backend contains two .env files located in different directories, each serving a different purpose:

  • Root directory (back-end-node-js/.env): Contains the environment variables used by the main backend application, including AdminPassword and SessionSecret. These variables are used for authentication and session management.
  • Scripts directory (back-end-node-js/scripts/.env): Contains environment variables used specifically by scripts and test files within the scripts/ directory. These settings are separate from the main backend configuration.
Main Backend Environment Variables

The main backend .env file contains two environment variables:

  • AdminPassword: Stores the bcrypt-hashed administrator password. This value should only be modified when changing the dashboard's login password. The new password must first be hashed using bcrypt before replacing the existing value.
  • SessionSecret: Used to sign session cookies and protect their integrity. This value should generally remain unchanged, as modifying it may invalidate existing user sessions. It should only be changed when necessary, such as when rotating a compromised secret.

Both variables contain sensitive information and should not be publicly shared or included in version control.

When modifying environment variables, ensure that the correct .env file is updated, as changes to one file may not affect the other.

For instructions on modifying these settings, see Authentication and Session Configuration.

MQTT Configuration

The MQTT protocol is created in the "mqttConnection.js" file, and is the main file for communicating with the robot. To communicate different commands is used:

  • mqttClient.subscribe
  • mqttClient.unsubscribe
  • mqttClient.publish
  • mqttClient.on
mqttClient.subscribe

This is used to make the back-end begin to listen to a certain topic. The syntax of such message is:

mqttClient.subscribe("dtubot/drive/T0/gyroo");

  • "dtubot" identifies the robot platform.
  • "drive" represents telemetry topics used to receive data from the robot.
  • "T0" is dependent on the number of Teensy Boards and is zero-indexed. It mentions which Teensy board it will listen to.
  • "gyroo" represents the topic that is subscribed to.
mqttClient.unsubscribe

This is used to make the back-end stop listening to a topic. The syntax of such message is:

mqttClient.unsubscribe("dtubot/drive/T0/gyroo");

  • "dtubot" identifies the robot platform.
  • "drive" represents telemetry topics used to receive data from the robot.
  • "T0" is dependent on the number of Teensy Boards and is zero-indexed. It mentions which Teensy board it will listen to.
  • "gyroo" represents the topic that is subscribed to.
mqttClient.publish

This is to send a message to a specific topic, which the robot will receive. The syntax of such message is:

mqttClient.publish("dtubot/cmd/T0", "gyroc Start");

  • "dtubot" identifies the robot platform.
  • "cmd" represents telemetry topics used to send commands to the robot.
  • "T0" is dependent on the number of Teensy Boards and is zero-indexed. It mentions which Teensy board it will listen to.
  • "gyroc Start" is the command that is sent to the robot.
mqttClient.on

This function listens to certain MQTT messages from the protocol. The syntax of such message is:

mqttClient.on(title, payload)

The MQTT has different label titles i.e ("message" or "error") and payload is the content that follows that title. These determines what kind of output the MQTT protocol has sent.

If the label title is "message" it will contain a topic and a message.

If the label title is "error" it will contain an error message.

Service filles Configuration

The services/ directory contains files responsible for handling specific backend functionalities and communication between different components of the Robot Dashboard.

Each service is responsible for a particular task, helping separate the application's functionality and making the codebase easier to maintain.

Tab Service

The tabService.js file is responsible for managing MQTT subscriptions based on the active dashboard tab. It communicates with mqttConnection.js to start and stop subscriptions, ensuring that the backend only subscribes to the data required by the selected tab.

The file contains three main functions:

  • tabChanged(previousTabTitle, newTabTitle): Handles switching between dashboard tabs by stopping subscriptions associated with the previously active tab and starting subscriptions required by the newly selected tab.
  • onlyTabClosed(previousTabTitle): Handles closing the last open tab by stopping its associated MQTT subscriptions.
  • tabChangedNoPrevious(newTabTitle): Handles opening a tab when no previous tab was active by starting the subscriptions required by the selected tab.

Each dashboard tab is associated with specific MQTT subscriptions, such as heartbeat data, IMU sensor readings, robot information, and distance sensor data.

When adding a new tab that requires MQTT data, the corresponding subscription handling should be added to the relevant functions in tabService.js.

Socket.IO Configuration

Sockets is the primary method of communication between the front-end and the back-end. There are two primary functions:

  • socket.on (used in socket.js)
  • io.emit (used in mqttConnection.js)
socket.on

"socket.on" listens for any commands that have been sent with a specific topic from the front end.

io.emit

"io.emit" sends commands to the front-end with a package.

Authentication and Session Configuration

This section explains how to modify the authentication settings used by the Robot Dashboard.

The \security/\ directory contains the script responsible for handling authentication. This script should remain unchanged.

Changing the Administrator Password

The administrator password is stored as a bcrypt hash in the .env file located in the backend root directory.

To change the administrator password, follow these steps:

Step 1: Generate a New Password Hash
  1. Open back-end-node-js/scripts/generateHash.js.
  2. Locate the variable userPassword and replace its current value with the desired new password.
  3. Open a terminal and navigate to the scripts/ directory.
  4. Run the following command to generate a new bcrypt hash:
    node generateHash.js
  5. Copy the generated hash (the second output in the terminal).
Step 2: Update the Environment Variable
  1. Open the .env file located in the backend root directory (back-end-node-js/.env).
  2. Locate the AdminPassword variable.
  3. Replace its existing value with the newly generated bcrypt hash.
  4. Save the file and restart the backend application to apply the changes.
  5. To restart the backend application, go to the robot's terminal and type sudo systemctl restart webapp.service
Step 3: Verify the New Password
  1. Open back-end-node-js/scripts/testHash.js.
  2. Update the test password to match the newly chosen administrator password.
  3. Ensure that the test script compares the new password against the newly generated hash.
  4. Run the test script from the scripts/ directory:
    node testHash.js
  5. The script should return true if the password matches the hash, or false if they do not match.

Note: The administrator password must always be hashed using bcrypt before being stored in the .env file. Never store plaintext passwords in environment variables or commit passwords and hashes to version control.

Session Secret

The SessionSecret variable is used to sign session cookies. It should not be modified during normal configuration.

If the secret needs to be rotated, replace it with a new, securely generated random value. Changing the secret may invalidate existing sessions, requiring users to log in again.

If a new session needs to be generated, type from the root directory node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"