# 원격 명령 처리

다음 목록의 원격 명령은 IoT 디바이스에서 필수적으로 제공되어야 합니다.

* **리부팅**

IoT 디바이스의 펌웨어는 IoT hub 와 MQTT 로 연결되어 있는데, MQTT 프로토콜은 커넥션을 시작하고 요청을 시작하는 주체와 관계 없이 양방향으로 신호를 주고 받을 수 있게 되어 있습니다. 그래서 서버에서 클라이언트로(cloud to device) 명령을 송신할 수 있습니다.

펌웨어에서는 필수 구현이 필요한 원격 명령을 수신할 수 있는 핸들러 로직을 구현하고 IoT hub client 객체에 등록해야 합니다.

```javascript
function main() {
    client = Client.fromConnectionString(deviceConnectionString, Protocol);
    client.open(onConnect);
}
 
function onConnect(err) {
    if(!!err) {
        console.error('Could not connect: ' + err.message);
    } else {
        console.log('Connected');
 
        // Direct method 인터페이스를 등록하고, 핸들러 펑션을 지정하고 있다.
        client.onDeviceMethod('reboot', onReboot);
    }
}
 
function onReboot(request, response) {
    console.log('다이렉트 메소드 실행');
 
    // complete the response
    response.send(200, '실행 완료', function(err) {
        if(!!err) {
            console.error('error' + err.toString());
        } else {
            console.log('successfully.' );
        }
    });
}
 
main();
```

위 Node.js 예제코드에서는 13라인에서 IoT hub client 객체의 onDeviceMethod 메소드로 reboot 라는 원격 명령을 등록합니다. 그리고 onReboot 함수가 핸들러로 등록됩니다. onReboot 함수는 HTTP 코드로 응답할 수 있게 되어 있습니다.

리부팅을 수행하는 코드이므로 정상적으로 메시지를 받았다는 200 을 리턴한 리부트를 수행하면 됩니다.

펌웨어에서 원격 명령을 수행하고 리턴해야 하는 코드는 다음과 같습니다.

| **상황**     | **HTTP 코드** | **응답 주체** |
| ---------- | ----------- | --------- |
| 수신 및 처리 성공 | 200         | 펌웨어       |
| 처리 실패      | 503         | 펌웨어       |


---

# 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://metatron-grandview.gitbook.io/meatatron-grandview/iotdevicedev/undefined-3.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.
