###### tags: `legacy system`
# DC/DMS/API GW Introduction
### Architecture

### DC
- MQTT Client
- Foward message to DMS via ZMQ (as client)
### DMS
- ZMQ Server/Client
- Sever receive message from DC
- Client transmit message to Alerto
- Core system
- Message server
- dmServer.js
- initHttpServer()
- 建立ZMQ socket, 並監聽從DC來的訊息
- 根據訊息內容丟到對應的deviceManager (dmBase, dmVm) // one instance per imei
- Trip business logical (-> Tripo)
- dmBase.js // prototype
- processingEvent() // 當parseRawDataFlow()結束後, 會呼叫這個function, 根據report更新對應的資訊至vehicle_status
- processing2ndLevelEvent() // 這邊主要是決定要不要create trip point, simple trip point, gps point, 以及要不要更新vehicle_status (比較像是準備寫入資料進DB的function)
- dmVm.js // 解析的部分寫在這支js
- parseRawDataFlow() // convert raw data -> this.obd_data via binary parser
- dmEventHandler/
- eventProcess // 當收到raw data時會以這邊為進入點
- updateCurrentVehicleStatus.js
- createTripPoint.js // 之前是在這邊加入Driver record相關的數據
- createSimpleTripPoint.js
- createTriRecord.js
- Driver business logical (-> Drivo)
- Relative collection
- vehicle-status // 即時資訊
- score的部分在thisTrip.safetyScore, thisTrip.ecoScore
- trip-point // 跟旅程有關
- score的部分在thisTrip.safetyScore, thisTrip.ecoScore
- simple-trip-point // 跟旅程有關
- gps-point // 跟旅程有關
- driver_recrods // Driver的紀錄
- 這邊其實是直接copy trip point而已
- driver_obd2_pair // Driver - OBD的綁定關係
### API GW
- Driver APIS
- driver.js // list, daily, weekly, monthly
- driverobd2.js // pairing
### System setup
- Install [ZMQ](https://github.com/zeromq/libzmq)
- Clone DC (branch: develop)
- Change default config setting
- MQTT broker dev.mqtt.roadmio.com
- DB 這邊請用local的 (不然會寫兩份資料)
- Launch DC with command:
```
pm2 start pm2.json
```
- Clone DMS (branch: 2.x develop)
- Change default config setting
- DB 這邊請用local的 (不然會寫兩份資料)
- Launch DMS with command:
```
npm run localhost
```
- Clone API GW (branch: develop)
- Change default config setting
- 確認DB address, 下面幾個屬性都是相關的 (如果要以現有的資料做測試, 可以連到Dev_DMS (34.254.100.199), 如果要用自己local的測試則改成自己的位址)
- db
- sessions
- logs
- Launch API GW with command:
```
npm run start
```
- Debug with command:
```
pm2 ls
pm2 log $appId (e.g., pm2 log 1)
```