# EVCamera与云微服务的交互MQTT协议定义

## Overview

```mermaid
sequenceDiagram
	participant EVCamera
	participant Broker
	participant CloudSvc
	
	EVCamera ->> Broker: connect: set_lastwillmessage
	EVCamera ->> Broker: sub: <<MD>> topic_request_evcamera
	CloudSvc ->> Broker: pub: <<DD>>CONFIG_T
	Broker ->> EVCamera: notify: request {"cmd": "config", ...}
	EVCamera ->> EVCamera: [APPLY_CONFIG]
	EVCamera ->> Broker: pub: <<MD>>REPORT
	
```

```mermaid
sequenceDiagram
	participant EVCamera
	participant Broker
	participant CloudSvc
	
	CloudSvc ->> Broker: sub: <<MD>> topic_response_report
	EVCamera ->> Broker: pub: <<TD>> REPORT_T
	Broker ->> CloudSvc: notify: REPORT_T
	CloudSvc ->> CloudSvc: [HANDLE_REPORT]
	
```

## Data and Message Type Definitions

### topic_request_evcamera

```
topic: evcamera/v1.0/request/<CAMERA_SN>
qos: 2
retain: false
payload: JSON
```

### topic_response_report

```
topic: evcamera/v1.0/res_rep
qos:2
retian: false
payload: JSON
```

### report: evcamera internal issues

```json
topic: topic_response_report
payload: 
{
  "time": 1567669674,
  "rid": "",  // rid empty means it is initiated by evcamera, and it is report, not response
  "type": "report",
  "sn": "A000000Z",
  "cata": "issues",
  "data": {
    "issues":[
      {
      	"module": "record",
        "msg": "no space left",
        "severity": "critical"
      }
    ]
  }
}
```

### request: config

**full configuration:**

```json
{
  "time": 1567669674,
  "cmd": "config",
  "rid": "<random_str>",
  "data": {
    "sn": "A000000Z",
    "vgw": "evcloudsvc.ilabservice.cloud:7123",
    "mqtt": "admin:password@evcloudsvc.ilabservice.cloud:11883",
    "upload": "evcloudsvc.ilabservice.cloud:10009",
    "features": {
      "push": 1, 
      "motion": {
        "enabled": 1,
        "region": {
          "maxX": 1,
          "maxY": 1,
          "minX": 0,
          "minY": 0
        },
        "level": 3
      },
      "recordLen": 30,
      "ai":{
        "enabled": 1,
        "faceThresh": 0.75,
        "humanThresh": 0.63,
        "region": {
          "maxX": 1,
          "maxY": 1,
          "minX": 0,
          "minY": 0
        }
      }
    }
  }
}
```



**patch configuration:**

```json
{
  "time": 1567669674,
  "cmd": "config",
  "rid": "<random_str>",
  "sn": "A000000Z",
  "data": {
    [
      {
        "op": "replace",
        "path": "/features/ai/face_thresh",
        "value": 0.5
      }
    ]
}
```



### Response: config

```json
{
  "time": ts,
  "rid": "<request_rid>",
  "type": "response",
  "category": "config",
  "sn": "A000000Z",
  "code": 0,
  "msg": "OK",
  "data": {
    "vgw": "evcloudsvc.ilabservice.cloud:7123",
    "mqtt": "admin:password@evcloudsvc.ilabservice.cloud:11883",
    "upload": "evcloudsvc.ilabservice.cloud:10009",
    "features": {
      "push": 0,
      "motion": {
        "enabled": 1,
        "region": {
          "maxX": 1,
          "maxY": 1,
          "minX": 0,
          "minY": 0
        },
        "level": 3  // 1 - 6
      },
      "recordLen": 30,    // seconds of one slice of local recording
      "ai":{
        "enabled": 1,
        "faceThresh": 0.75,  // 0 - 1
        "humanThresh": 0.63,  // 0 - 1
        "region": {
          "maxX": 1,
          "maxY": 1,
          "minX": 0,  
          "minY": 0
        }
      }
    }
  }
}
```





## Request: cmd upload_video

REQUEST_VIDEOS_T

```json
{
  "time": 1567669674,
  "cmd": "upload_video",
  "rid": "001231554A20",
  "data": {
      "start": 1592985060,
      "end": 1592985180,
      "type": 6
    }
}
```



### Response: cmd upload_video

```json
{
  "time": 1567669674,
  "type": "response",
  "rid": "<request_rid>",
  "cata": "upload_video",
  "sn": "A000000Z",
  "code": 1,
  "msg": "failed to upload video: file not exist",
  "data":{
    // empty
  }
}
```





### Report: AI Image

```json
{
   "category":"ai_image",
   "code":0,
   "data":{
      "data":"<base64_string>",
      "encoding":"base64",
      "format":"JPG",
      "type":"image"
   },
   "msg":"new ai capture: pic, idx = 0, toltal = 1",
   "rid":"",
   "sn":"EVC001231554A20",
   "time":1589537073532,
   "type":"report"
}
```

### Request: status

```json
{
  "time": 1567669674,
  "cmd": "status",
  "rid": "001231554A20",
  "data": {}
}
```

### Request PTZ

```json
{
   "time":1567669674,
   "cmd":"ptz",
   "rid":"001231554A20",
   "data":{
      "action":"left",
      "degree":10
   }
}
```



### Report: online message

topic: evcamera/v1.0/report

```json
{
   "category":"lastwill",
   "code":0,
   "data":{
      "offline":true,
      "timestamp_onlie":12121
   },
   "msg":"new ai capture: pic, idx = 0, toltal = 1",
   "rid":"001231554A20",
   "sn":"001231554A20",
   "time":1589537073532,
   "type":"report"
}
```



### Report: offline message (lwm)

topic: evcamera/v1.0/response/<SN>

```json

```

```json
{
   "time":1567669674,
   "cmd":"qr",
   "rid":"001231554A20",
   "data":{
      "duration":20
   }
}
```

