---
# System prepended metadata

title: Backend - Simple NodeJS backend + Containerize + Deploy to K8s + ingress
tags: [PETICU, backend]

---

# Backend - Simple NodeJS backend + Containerize + Deploy to K8s + ingress
###### tags: `PETICU` `backend`
## CO2Module's data flow

### 開機
```sequence
IOT Device->Express server: call api\n(getModuleByDeviceId)
Express server-->IOT Device: response module's info\n(including Remaining time (hr))
```
### 執行中 (有連上網)
```sequence
IOT Device->Express server: update sensor and module remaing time \n(72..71..70..69..1)

Express server-->CloudDB: get paired module's id
Express server-->Express server: check if any module paired or not


Express server-->CloudDB: update paired module's info
```

## 換模組
```sequence
APP->Express Server:call api to exchange modle

Express Server->CloudDB:update the paird info

IOT Device->Express Server:call api to get new remaing time
IOT Device->Express Server:update remaining time when WIFI is connected
```

## 裝置離線重連
```sequence
APP->Express Server:post method to update modle's time

Express Server->CloudDB:update the paird info

IOT Device->Express Server:call api to get new remaing time
IOT Device->Express Server:update remaining time when WIFI is connected

```

app 交換CO2 module's:
- backend:更換pairs value
- device: call getModuleByDeviceId 會拿到新的value

交換模組的時候 
先檢查裝置是否連線 (確保device有上傳秒數)

接下來要做交換
坐之前我會給模組一個參數status :1 pending
此時是有人call getCO2ModuleByDDeviceId 會得到pending (這樣裝置舊知道有換過模組)
這時候硬體裝置要call post api 去上傳時間，然後再去getModuleById才會正常
上船後 我會將status 改為0 ok
裝置再去getModuleByDeviceId
等裝置上傳完數值後 會更新 這時候



tmp 秒數 --> 使用者要交換模組的時候會先呼叫這個職把它更新成realtime 再交換
post 方法會將 tmp time放進realtime裡面
real time ->(hr) -->前端