# Adapter

## SendTCPResponse

Gửi bản tin phản hồi tới thiết bị đã kết nối tới adapter TCP, bản tin sẽ được convert sang định dạng bản tin của adapter (Hex/Json/Text) (không thể gọi trực tiếp trong màn Code Editor)

```typescript
Adapter().SendTCPResponse({message: string})
```

{% hint style="info" %}
Sau khi gửi response với entityId truyền vào, thì từ những bản tin tiếp theo, thông tin này sẽ là giá trị của trường "entity\_id" trong **entityInfo**.
{% endhint %}

<details>

<summary>Ví dụ</summary>

<pre class="language-typescript"><code class="lang-typescript"><strong>await Adapter().SendTCPResponse({
</strong><strong>   message:"{\"message\":\"Hello\"}"
</strong><strong>})
</strong></code></pre>

</details>

## SendMqttResponse

Gửi bản tin tới topic của broker mà adapter MQTT đang kết nối, bản tin sẽ được convert sang định dạng bản tin của adapter (Hex/Json/Text) (không thể gọi trực tiếp trong màn Code Editor)

```typescript
Adapter().SendMqttResponse({topic: string, message: string, config:{
      retain: boolean
   }})
```

{% hint style="info" %}
Trường **config** là tùy chọn có thể điền hoặc không, nếu không mặc định **retain** = false
{% endhint %}

<details>

<summary>Ví dụ</summary>

<pre class="language-typescript"><code class="lang-typescript"><strong>await Adapter().SendMqttResponse({
</strong><strong>   topic: "adapter/response", 
</strong>   message: "{\"message\":\"Hello\"}"
})
</code></pre>

</details>

## SendUDPResponse

Gửi bản tin phản hồi tới thiết bị đã kết nối tới adapter UDP, bản tin sẽ được convert sang định dạng bản tin của adapter (Hex/Json/Text)

<pre class="language-typescript"><code class="lang-typescript"><strong>Adapter().SendUDPResponse({message: string})
</strong></code></pre>

<details>

<summary>Ví dụ</summary>

<pre class="language-typescript"><code class="lang-typescript"><strong>await Adapter().SendUDPResponse({
</strong><strong>    message:"{\"message\":\"Hello\"}"
</strong><strong>})
</strong></code></pre>

</details>

## SendToAdapterTcp

Gửi bản tin phản hồi tới thiết bị đã kết nối tới adapter TCP, bản tin sẽ được convert sang định dạng bản tin của adapter (Hex/Json/Text). Điều kiện cần: Trong luồng xử lí bản tin trước đó đã sử dụng hàm **IdentifyInfo**, **IdentifyInfoByKey, IdentifyInfoByKeyWithAd** hoặc **CreateDevice** (có thể gọi trực tiếp trong màn Code Editor)

<pre class="language-typescript"><code class="lang-typescript"><strong>Adapter().SendToAdapterTcp({adapterId: string, entityId: string, message: string})
</strong></code></pre>

<details>

<summary>Ví dụ</summary>

```typescript
let res = await Adapter().SendToAdapterTcp({
    adapterId: "1e6d8794-7ecb-11ee-b962-0242ac120002",
    entityId: "cd06e0ff-1ea6-4b2d-a5a3-59c1f3caa21a", 
    message:"{\"message\":\"Hello\"}",
})
if (res.success){
  // Gửi thành công
}else{
  // Gửi thất bại
}
```

</details>

## SendToAdapterMqtt

Gửi bản tin phản hồi tới MQTT broker mà adapter kết nối tới, bản tin sẽ được convert sang định dạng bản tin của adapter (Hex/Json/Text) (có thể gọi trực tiếp trong màn Code Editor)

```typescript
Adapter().SendToAdapterMqtt({adapterId: string, topic: string, message: string, config:{
      retain: boolean
   }})
```

{% hint style="info" %}
Trường **config** là tùy chọn có thể điền hoặc không, nếu không mặc định **retain** = false
{% endhint %}

<details>

<summary>Ví dụ</summary>

```javascript
let res = await Adapter().SendToAdapterMqtt({
    adapterId: "1e6d8794-7ecb-11ee-b962-0242ac120002",
    topic: "adapter/response", 
    message:"{\"message\":\"Hello\"}",
    config:{
      retain:false
   }
})
if (res.success){
  // Gửi thành công
}else{
  // Gửi thất bại
}
```

</details>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://innoway.gitbook.io/innoway/tai-lieu/sdk/adapter.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
