183 lines
8.5 KiB
Markdown
183 lines
8.5 KiB
Markdown
# ResourceHUB [API]
|
||
|
||

|
||
|
||
## [API] Library
|
||
|
||
[ACTIX (actix-web)](https://actix.rs/docs/server)
|
||
|
||
> [!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,.**
|
||
> 
|
||
> ```json
|
||
> {
|
||
> {
|
||
> id: 1,
|
||
> city_codename_cords_sector_cell: "CERCELL-A1:[40.7128,-74.0060]::S1:C1"
|
||
> name: "Acidity‑alkalinity of a solution:[PH 0-14]",
|
||
> value: "5.5",
|
||
> unit: "pH", // to measure the acidity or alkalinity of a solution
|
||
> timestamp: 17000002 // decode_timestamp(17000002) -> (2024, 3, 8, 12, 34, 52) -> "2024.03.8:12:34:52" [year.month.day:hour:min:sec]
|
||
> },
|
||
> {
|
||
> id: 1,
|
||
> city_codename_cords_sector_cell: "CERCELL-A2:[70.7128,-56,00012]::S1:C1"
|
||
> name: "Acidity‑alkalinity of a solution:[PH 0-14]",
|
||
> value: "5.7",
|
||
> unit: "pH", // to measure the acidity or alkalinity of a solution
|
||
> timestamp: 17000035
|
||
> },
|
||
> ....
|
||
> {
|
||
> id: 2,
|
||
> city_codename_cords_sector_cell: "BETTA-A1:[40.7128,-74.0060]::S1:C1"
|
||
> name: "Humidity:[DHT22]",
|
||
> value: "20.7",
|
||
> unit: "RH", // Relative Humidity (RH)
|
||
> timestamp: 17000002
|
||
> },
|
||
> ...
|
||
> {
|
||
> id: 3,
|
||
> city_codename_cords_sector_cell: "ANLATIC-mAI:[40.7128,-74.0060]::S1:C1"
|
||
> name: "Temperature:[DHT22]",
|
||
> value: "35.0",
|
||
> unit: "°C", // or Fahrenheit Temperature of room '°C' preffered for UI Charts
|
||
> timestamp: 17000002
|
||
> }
|
||
> ...
|
||
> }
|
||
> ```
|
||
> # Redis database save recevied [sensor-data] into Table Sensor_data
|
||
> # CERCELL-A1:[40.7128,-74.0060]::S1:C1
|
||
> - **CERCELL** - "**City** **name**"
|
||
> - **-A1** - " City **codename" (can be empty)**
|
||
> - **[40.7128,-74.0060] **- City,. **Coordinates** [° N,° W]
|
||
> - [40.7128,-74.0060] ----> "**New York**"
|
||
> - **S1:C1** - Means **SECTOR**:**CELL**
|
||
> - **S1** - Means: **Sector** **1**
|
||
> - **C1** - Means: **Cell** **1**
|
||
> - **:** - field splitter (for future. May be parsers can use this for getting fields: cityid, cords, sector, cell)
|
||
> ### 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. **
|
||
> ## Sensors Data Table [**Example**]
|
||
>
|
||
> | id | city_codename_cords_sector_cell | sensor_name_and_model | value | unit | timestamp |
|
||
> | --- | -------------------------------------------------------------------------------- | ---------------------------------------------------- | ----- | ------- | --------- |
|
||
> | 1 | AQUA-A1:[[-18.7669,46.8691](https://maps.google.com/?q=-18.7669,46.8691)]:S1:C1 | Acidity‑alkalinity of a solution:[PH 0-14] | 5.5 | pH | 17000002 |
|
||
> | 2 | CERCELL:[40.7128,-74.0060]::S1:C1 | Humidity:[DHT22] | 20.7 | % RH | 158371335 |
|
||
> | 3 | AQUA:[[64.9631,-19.0208](https://maps.google.com/?q=64.9631,-19.0208)]::S2:C2 | O₂ sensor:[MiCS‑5524] | % vol | % vol | 23656547 |
|
||
> | 4 | AQUA:[[64.9631,-19.0208](https://maps.google.com/?q=64.9631,-19.0208)]::S1:C1 | Acidity‑alkalinity of a solution:[PH 0-14] | 2.5 | pH | 23657524 |
|
||
> | 5 | OCEAN:[[35.6895,139.6917](https://maps.google.com/?q=35.6895,139.6917)]::S1:C1 | Temperature:[DHT22] | 20.0 | °C | 45557562 |
|
||
> | 6 | AQUA-B2:[[12.9716,77.5946](https://maps.google.com/?q=12.9716,77.5946)]:S2:C2 | Humidity:[DHT22] | 45.2 | % RH | 51234567 |
|
||
> | 7 | DESERT:[[23.4241,53.8478](https://maps.google.com/?q=23.4241,53.8478)]:S1:C1 | Soil moisture:[Capacitive Soil Moisture Sensor v1.2] | 12.3 | % vol | 52345678 |
|
||
> | 8 | MOUNTAIN:[[35.3606,138.7274](https://maps.google.com/?q=35.3606,138.7274)]:S3:C3 | Sound level:[Adafruit MEMS Mic] | 68 | dB SPL | 53456789 |
|
||
> | 9 | RIVER:[[48.8566,2.3522](https://maps.google.com/?q=48.8566,2.3522)]:S1:C2 | Acidity‑alkalinity of a solution:[PH 0-14] | 6.8 | pH | 54567890 |
|
||
> | 10 | OCEAN:[[34.0522,-118.2437](https://maps.google.com/?q=34.0522,-118.2437)]:S2:C1 | Pressure:[BMP280] | 22.1 | hPa | 55678901 |
|
||
> | 11 | FOREST:[[51.5074,-0.1278](https://maps.google.com/?q=51.5074,-0.1278)]:S1:C3 | Wind speed:[Anemometer 3‑Axis 3000] | 30.5 | m / s | 56789012 |
|
||
> | 12 | TUNDRA:[[55.7558,37.6173](https://maps.google.com/?q=55.7558,37.6173)]:S2:C2 | Light sensor:[TSL2591] | 5.9 | lux | 57890123 |
|
||
> | 13 | VALLEY:[[40.7306,-73.9352](https://maps.google.com/?q=40.7306,-73.9352)]:S3:C1 | CO₂ sensor:[SenseAir S8] | 45.2 | RH | 58901234 |
|
||
> | 14 | PLAINS:[[41.8781,-87.6298](https://maps.google.com/?q=41.8781,-87.6298)]:S1:C2 | UV sensor:[VEML6075] | 7.1 | mW /cm² | 59012345 |
|
||
> | 15 | COAST:[[37.7749,-122.4194](https://maps.google.com/?q=37.7749,-122.4194)]:S2:C3 | Soil EC[EC‑Meter 5] | 1.8 | dS/m | 60123456 |
|
||
|
||
```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 |