Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bridge and API server support #1

Merged
merged 35 commits into from
Jun 17, 2024
Merged

Conversation

vitorfdl
Copy link
Member

@vitorfdl vitorfdl commented May 28, 2024

This pull request adds support for bridges and an API server. It includes the necessary code changes and configuration updates.

flowchart TD
    A[Main Routine] --> B[Fetch Relay Configurations]
    B --> C[Verify Network Token for Each Relay]
    C --> D[Initialize Relay List and Tasks]
    D --> E[Start HTTP Server]
    E --> F[Listen on Configured Port]
    F --> G[Handle Incoming Publish Requests]
    G --> H[Check Relay ID and Send Message to MQTT Relay]

    D --> I[Start Relay Tasks Loop]
    I --> J[Read Relay List]
    J --> K[Check and Start MQTT Relay Connection for Each Relay]
    K --> L[Run MQTT Relay Connection]
    L --> M[Initialize MQTT Options]
    L --> N[Subscribe to Topics]
    L --> O[Handle MQTT Connection]
    L --> P[Publish Messages to MQTT Broker]
    L --> Q[Process Incoming MQTT Messages]

    subgraph HTTP Server
        E
        F
        G
    end

    subgraph MQTT Relay Connection
        M
        N
        O
        P
        Q
    end
Loading

Tutorial: Setting Up MQTT Relay within TagoIO

This tutorial will guide you through setting up an MQTT Relay within TagoIO. This setup allows for seamless communication between your devices and the TagoIO platform using MQTT. Follow the steps carefully to ensure proper configuration and functionality.

Step 1: Setup a Broker

  1. Obtain a Broker:
    • You can either set up your own MQTT broker or use a public one like HiveMQ.
    • Get the Addres, Port, username and password of the broker.

Step 2: Create a Network in TagoIO

  1. Navigate to Profile Integrations:
    • Go to your TagoIO account and access the Profile Integrations section.
  2. Create a Network:
    • Enable the Serial option.
    • Fill in the Middleware Endpoint with the Address and Port of the Relay (for downlink behavior only).
  3. Write a Payload Parser:
    • Ensure your payload parser outputs a serial. Use the following example:
    if (Array.isArray(payload)) {
      const payload_received = payload.find(x => x.variable === "payload");
      // Get the serial from the topic such as "device/123456" 
      serial = payload_received?.metadata.topic.split("/").pop();
    }
  4. Generate a Token:
    • Generate a token for your network and copy it for later use.

Step 3: Create a Connector

  1. Create a Connector:
    • No special configurations are needed for this step. Simply create a connector for your network.

Step 4: Create a Device

  1. Create a Device:
    • Use the connector created in the previous step.
    • Enter the serial that will identify your device.

Step 5: Generate Device Authorization

  1. Generate Authorization:
    • Go to your Device List page.
    • Generate an authorization token and copy it for later use.

Step 6: Get the TagoIO API Server

  1. Clone the Repository:
  2. Run the API:
    • Use the following commands:
      make run api
      make run-rust payload-rpc
      

Step 7: Configure the Relay

  1. Download the Repository:
    • Download the repository and open the config.toml file.
  2. Configure the File:
    • Write the configuration for the Broker Address, Port, and subscribe topics.
    • Write the configuration for the TagoIO API URL and the PORT.
    • Write the configuration for the network token and authorization that you copied in Steps 2 and 5.

Step 8: Run the Relay

  1. Run the Relay:
    • Execute the following command:
      cargo run
      
  2. Check Connections:
    • Verify that everything is connected properly.

Step 9: Connect to the Broker

  1. Connect as a Client:
    • Connect to the broker as a client and publish any message to the subscribed topics of the Relay.

Step 10: Monitor Data

  1. Check Data:
    • Observe the data coming in or check the Relay for any errors.

Step 11: Test Downlink Behavior

  1. Send a POST Request:
    • To test the downlink behavior, send a POST request to /integration/network/publish.
    • Use the following header and body format:
      • Header:
        { Authorization: Network-Token }
        
      • Body:
        {
          "topic": "mqtt-topic",
          "message": "message-content",
          "qos": 0,
          "retain": false
        }

By following these steps, you should have a fully functional MQTT Relay setup within TagoIO. This will enable efficient communication between your devices and the TagoIO platform.

Certificates

The API Endpoint for receiving publish messages from TagoIO requires the use of Certificates. TagoIO Certificates are currently stored at

Below are the instructions if you ever need to generate new certificates:

Step 1: Create the Root Certificate Authority (CA)

  1. Generate the root key:

    openssl genrsa -out rootCA.key 2048
  2. Create the root certificate:

    openssl req -x509 -new -nodes -key rootCA.key -sha256 -days 3650 -out rootCA.pem -subj "/C=US/ST=North Carolina/L=Raleigh/O=TagoIO Inc./OU=IT/CN=TagoIO Root CA"

Step 2: Create the Server Certificate

  1. Generate the server key:

    openssl genrsa -out server.key 2048
  2. Create a certificate signing request (CSR) for the server:

    openssl req -new -key server.key -out server.csr -subj "/C=US/ST=North Carolina/L=Raleigh/O=TagoIO Inc./OU=IT/CN=TagoIO Server"
  3. Create the server certificate using the root CA:

    openssl x509 -req -in server.csr -CA rootCA.pem -CAkey rootCA.key -CAcreateserial -out server.crt -days 3650 -sha256

Step 3: Create the Client Certificate

  1. Generate the client key:

    openssl genrsa -out client.key 2048
  2. Create a certificate signing request (CSR) for the client:

    openssl req -new -key client.key -out client.csr -subj "/C=US/ST=North Carolina/L=Raleigh/O=TagoIO Inc./OU=IT/CN=TagoIO Client"
  3. Create the client certificate using the root CA:

    openssl x509 -req -in client.csr -CA rootCA.pem -CAkey rootCA.key -CAcreateserial -out client.crt -days 365 -sha256

Step 4: Renew the Client Certificate

When the client certificate is about to expire, you can renew it by generating a new CSR and signing it with the root CA again.

  1. Generate a new CSR for the client:

    openssl req -new -key client.key -out client.csr -subj "/C=US/ST=North Carolina/L=Raleigh/O=TagoIO Inc./OU=IT/CN=TagoIO Client"
  2. Create a new client certificate using the root CA:

    openssl x509 -req -in client.csr -CA rootCA.pem -CAkey rootCA.key -CAcreateserial -out client.crt -days 365 -sha256

Step 2: Store Certificates in Environment Variables

  1. Store the Root CA Certificate in the Rust API environment:

    export ROOT_CA_CERT=$(cat rootCA.pem)
  2. Store the Client Certificate and Key in the NodeJS environment:

    export CLIENT_CERT=$(cat client.crt)
    export CLIENT_KEY=$(cat client.key)

Step 3: Renew Client Certificate Annually

  1. Generate a new Client Certificate signed by the Root CA by repeating step 1.2

  2. Update the environment variables with the new certificate and key:

    export CLIENT_CERT=$(cat new_client.crt)
    export CLIENT_KEY=$(cat new_client.key)

Copy link
Member

@RicardoStoklosa RicardoStoklosa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. There is a lot of unwraps on the code. You can handle all fatal errors or use expect to show a better message.
  2. You can improve the logs using env_logger or tracing-subscriber to make level of debug (info, debug, error, etc...)

Cargo.toml Outdated Show resolved Hide resolved
src/main.rs Outdated Show resolved Hide resolved
src/main.rs Outdated Show resolved Hide resolved
src/main.rs Outdated Show resolved Hide resolved
src/main.rs Outdated Show resolved Hide resolved
src/schema/mod.rs Outdated Show resolved Hide resolved
src/services/mqttrelay.rs Outdated Show resolved Hide resolved
src/services/tagoio.rs Outdated Show resolved Hide resolved
config.toml Outdated Show resolved Hide resolved
src/main.rs Outdated Show resolved Hide resolved
@vitorfdl vitorfdl merged commit 5071110 into master Jun 17, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants