# ResourceHUB [API]

> [!NOTE]
> **[DataCollector]** sends [**sensor's data] **like as *PH Level*, *Humidity*, *Temperature*, etc) as ++JSON++ to this **[API]**  
> **RESOURCE HUB - means  a Public Storage with Sensors Data**
> ![Resource hub](./images/Resource-hub-2.png)
> ```json
> {  
>   {
>     name: "pH sensor",
>     value: "5.5",
>     unit: "pH" // to measure the acidity or alkalinity of a solution
>   },
>   {
>     name: "DHT22",
>     value: "20.7",
>     unit: "RH" // Relative Humidity (RH) 
>   },
>   {
>     name: "DHT22",
>     value: "35.0",
>     unit: "Celsius" // or Fahrenheit Temperature of room
>   }
> }
> ```
> # Redis database save recevied [sensor-data] into Table Sensor_data[S5-1]
> - **[S5]** - means SECTOR-5, 
> - **"-1"** in [S5**-1**] means SECTOR-5-CELL-1
> ### In concept
> - SECTOR-5-CELL-1 can collect  data about tomatos
> - SECTOR-5-CELL-2 can collect data about potatos
>   > **In future can be added 'resource' journals,.**  
>   > **a. "how much" system make a 'tomatos', 'gabbages,.' : 10 tomatos/month, 40 gabbeges/month, etc,.)**  
>   > **b. "n/year"**  
>   > **c. **
> 
> | id  | Sector | Cell | sensor_name | value | unit    |
> | --- | ------ | ---- | ----------- | ----- | ------- |
> | 1   | 1      | 1    | pH sensor   | 5.5   | pH      |
> | 2   | 1      | 1    | DHT22       | 20.7  | RH      |
> | 3   | 1      | 1    | DHT22       | 35.0  | Celsius |
> | 4   | 1      | 2    | pH sensor   | 2.5   | pH      |
> | 5   | 1      | 2    | DHT22       | 20.0  | Celsius |

```mermaid
sequenceDiagram
    participant [WebUI/Gtk APP]
    participant [DataCollector]
    participant [ResourceHUB API] 
    participant Redis Database
    [WebUI/Gtk APP]->>[ResourceHUB API]: Get [sensor's data]
    [ResourceHUB API]-->>[WebUI/Gtk APP]: Send [sensor's data]
    [ResourceHUB API]->>Redis Database: Query to get [sensor's data]
    Redis Database-->>[ResourceHUB API]: Results with data
    [DataCollector]->>[ResourceHUB API]: Send [sensor's data]
    [ResourceHUB API]->>Redis Database: Query to save [sensor's data]
    Redis Database-->>[ResourceHUB API]: Results with save object and status saved!
```

```bash
# Instruction how to install and run ResourceHUB server.
# Time to install: 20-45min (in ~ideal scenarios, without troubleshoting)
# + 20min if try to fix troubles

# Step 1. 
# (as root, add "sudo" if need use docker as user)

# Install docker
curl -fsSL https://get.docker.com -o get-docker.sh
sh get-docker.sh

# Install docker-compose
apt update -y 
apt install docker-compose -y
```

```bash
# Step 2.
# Run Redis database (docker-compose)
chmod +x ./Database/run.sh && ./Database/run.sh
```

> [!NOTE]
> ```bash
> # To Run Redis database use ./Database/run.sh
> #    ./Database/run.sh
> 
> # To Stop Redis database use ./Database/stop.sh
> #    chmod +x ./Database/run.sh (this command allow exucute a bash script, use only first time)
> #   ./Database/stop.sh
> ```

```bash
# Step 3.
# Install rust and cargo  (optionaly if you need use Rust Book to install:  https://doc.rust-lang.org/book/ch01-01-installation.html)
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
```

```bash
# Step 4.
# Install libraries
cargo install
```

```bash
# Step 5.
# Okey now you have: 
# 1. Runned redis database in docker container (./Database/run.sh)
# 2. Intalled rust with cargo
# 3. Intalled project libraries 

# Let's Run a server!
cargo run
```

> [!TIP]
> Don't worry about errors, troubleshooting, etc, make git issue and then i try add new instructions, how to avoid and fix this.

> [!NOTE]
> ```bash
> #  get data 
> curl -v http://127.0.0.1:8000/api/v1/sensors/data
> ```
> ```bash
> .... # headers
> {"id":1,"name":"Sensor name","value":4.5,"unit":"C"} # <-- Json object with sensor data (in this example you see only one sensor)
> ```

> [!NOTE]
> **DATA COLLECTOR - means a mini PC or Micro-controller with connected sensors, get data from all connected sensors and sends data in json format**
> **to web server, likes RESOURCE HUB [REST API]**
> 
> [**DataCollector**]
> **Arduino->[USB]->OrangePiPC->Wifi/Eth  - - ->  [ResourceHUB API]**
> 
> **In my system now i use "arduino nano with DHT22 and pH sensor" connected to Orange Pi PC, arduino sends all data from sensors to Orange Pi PC**
> **Arduino nano connected by usb and use UART - serial port to send data to minipc, and then**
> **Orange Pi PC use wifi/ethernet to sends getted data to web server (To our RESOURCE HUB API)**
> 
> **[DataCollector]** sends a **Post** **requests**
> **json** - - **- > **http:123.123.123/api/v1/sensor/data
> 
> **[ResourceHUB] **save this data in redis dababase