---
# System prepended metadata

title: event center整合說明

---

1. # event center整合說明 (2023/08/02修改)


| 日期 | 更新內容 | 更新者 |
| -------- | -------- | -------- |
| 2024.05.08     | 1. 加入message欄位，如果message欄位有帶的話，會取代掉原先event code上的範本     | cch     |
| 2024.05.02     | 1. 加入notifications的欄位，提供可以自訂通知用戶     | cch     |
| 2023.12.05     | 1. 更新欄位說明     | cch     |
| 2023.08.15     | 1. status修改為appStatus     | cch     |

## 傳輸方式
使用rabbitMQ傳送，相關設定如下:


| 參數     | 值           |                     說明                     |
| -------- | ------------ |:--------------------------------------------:|
| exchange | amq.topic    |                                              |
| topic    | ec."appName" | 如app name為andon，則發送的的topic為ec.andon |


## 欄位格式

```json=
{
        "source":<string>,
	"tenantID":<string>,
	"creator":<string>,
	"eventID": <string>,
	"subject":<string>,
	"description":<string>,
	"target":<string>,
        "targetID":<string>,
	"targetPath":<string>,
        "targetPathID":<string>,
	"type":<string>,
	"appStatus":<string>,
	"owners": <string[]>,
	"tags": <string[]>,
	"createdAt": <int64> （ms）,
        "level": <string>,
        "additionalData": map[string]interface{},
        "notifications":<string[]>,
        "message": <string[]>
}

```
## 欄位說明
|     名稱     | 型態     |           必填           | 說明                                      |
|:------------:| -------- |:------------------------:|:----------------------------------------- |
|    source    | string   |            是            | app name                                  |
|   tenantID   | string   |            是            | tenant id                                 |
|   creator    | string   |            是            | 產生事件的用戶，可以是用戶名也可以是email |
|   eventID    | string   |            是            | 該app下唯一識別id                         |
|  eventCode   | string    |            是            | event categories中定義的event code |
|   subject    | string   |            是            | 事件名稱                                  |
| description  | string   |            否            | 事件描述                                  |
|    target    | string   |            是            |            |
|   targetID   | string   |            是            |         |
|  targetPath  | string   |            是            |          |
|   targetPathID   | string   |            是            |       |
|     type     | string   |            是            | event的種類，如：火警                     |
|    level     | string   |            是            | event的等級；如：HH，LL                   |
|  appStatus   | string   |            是            | event的狀況，如：未分配，已處理           |
|    owners    | string[] |            否            | 處理人員                                  |
|     tags     | string[] |            否            | hash tag                                  |
|  createdAt   | int64    |            是            | epoch timestamp(ms) |
|  notifications   | string[]    |            否            | eventNotice的id  |
|  message   | string[]    |            否            | 要通知的訊息，如果有帶時會取代掉原先配在event code上範本。訊息必需要使用base64編碼  |
## Example
```go=
package main

import (
	"context"
	"encoding/json"
	"log"
	"time"

	amqp "github.com/rabbitmq/amqp091-go"
)

type Log struct {
	Source         string   `json:"source"`
	TenantID       string   `json:"tenantID"`
	Creator        string   `json:"creator"`
	EventID        string   `json:"eventID"`
	Subject        string   `json:"subject"`
	Description    string   `json:"description"`
	WorkUnit       string   `json:"target"`
	WorkUnitID     string   `json:"targetID"`
	WorkUnitPath   string   `bson:"targetPath"`
	WorkUnitPathID string   `json:"targetPathID"`
	Type           string   `json:"type"`
    Level          string   `json:"level"`
	Status         string   `json:"status"`
	Owners         []string `json:"owners"`
	Tags           []string `json:"tags"`
	CreatedAt      int64    `json:"createdAt"`
}

func failOnError(err error, msg string) {
	if err != nil {
		log.Panicf("%s: %s", msg, err)
	}
}

func main() {
	amqpUri := "amqp://admin:admin@172.16.8.9:5672/ec"
	exchange := "amq.topic"
	appName := "exampleApp"
	topic := "ec." + appName
	testData := Log{
		Source:         appName,
		TenantID:       "test_tenant",
		Creator:        "test_user",
		EventID:        "test_uni_id",
		Subject:        "test event",
		Description:    "This is a test event",
		WorkUnit:       "machine01",
		WorkUnitID:     "machine01-ID",
		WorkUnitPath:   "/aaa/bbb",
		WorkUnitPathID: "/aaa_id/bbb_id",
		Type:           "HH",
		Status:         "Done",
		Owners:         []string{"owner1", "owner2"},
		Tags:           []string{"test1", "test2"},
		CreatedAt:      time.Now().UnixMilli(),
	}

	conn, err := amqp.Dial(amqpUri)
	failOnError(err, "Failed to connect to RabbitMQ")
	defer conn.Close()

	ch, err := conn.Channel()
	failOnError(err, "Failed to open a channel")
	defer ch.Close()

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()
	body, _ := json.Marshal(testData)

	err = ch.PublishWithContext(ctx,
		exchange, // exchange
		topic,    // routing key
		false,    // mandatory
		false,    // immediate
		amqp.Publishing{
			ContentType: "application/json",
			Body:        []byte(body),
		})
	failOnError(err, "Failed to publish a message")
	log.Printf(" [x] Sent %s\n", body)
}


```

