
# 示例三：在指定资源空间下创建设备
#### 场景描述
本章节指导用户通过API在指定资源空间下创建设备。API的调用方法参见[如何调用API](https://support.huaweicloud.com/api-iothub/iot_06_v5_0004.html)。
物联网平台默认为用户创建了一个资源空间，创建设备时默认将设备归属于默认资源空间，如果用户需要对设备分资源空间管理，可以指定资源空间创建设备。
#### 涉及接口
- [创建资源空间 - AddApplication](https://support.huaweicloud.com/api-iothub/iot_06_v5_6012.html)：创建非系统默认的资源空间。
- [创建产品 - CreateProduct](https://support.huaweicloud.com/api-iothub/iot_06_v5_0050.html)：在指定资源空间下创建产品。
- [创建设备 - AddDevice](https://support.huaweicloud.com/api-iothub/iot_06_v5_0046.html)：在指定资源空间下创建设备。
 
#### 操作步骤
1. 创建非系统默认的资源空间。
   
   1. 创建资源空间
      - 接口信息 URL：POST /v5/iot/{project_id}/apps
        详情参见[创建资源空间 - AddApplication](https://support.huaweicloud.com/api-iothub/iot_06_v5_6012.html)
        
      
      - 请求示例
        ```
        POST https://{Endpoint}/v5/iot/{project_id}/apps
        Content-Type: application/json
        X-Auth-Token: ********
        Instance-Id: ********
        {
          "app_name" : "testApp"
        }
        ```
        
      
      - 响应示例 Status Code: 201 Created
        ```
        Content-Type: application/json
        {
          "applications" : [ {
            "app_id" : "0ab87ceecbfc49acbcc8d5acdef3c68c",
            "app_name" : "testApp",
            "create_time" : "20151212T121212Z",
            "default_app" : true
          } ]
        }
        ```
        
       
   
   2. 记录返回结果中资源空间id，即"app_id"。
   
   
   
   
2. 在[1]创建的资源空间下创建产品。
   
   1. 使用[1.b]记录的app_id创建产品。
      - 接口信息 URL：POST /v5/iot/{project_id}/products
        详情参见[创建产品 - CreateProduct](https://support.huaweicloud.com/api-iothub/iot_06_v5_0050.html)
        
      
      - 请求示例
        ```
        POST https://{Endpoint}/v5/iot/{project_id}/products
        Content-Type: application/json
        X-Auth-Token: ********
        Instance-Id: ********
        {
          "product_id" : "5ba24f5ebbe8f56f5a14f605",
          "name" : "Thermometer",
          "device_type" : "Thermometer",
          "protocol_type" : "CoAP",
          "data_format" : "binary",
          "manufacturer_name" : "ABC",
          "industry" : "smartCity",
          "description" : "this is a thermometer produced",
          "service_capabilities" : [ {
            "service_type" : "temperature",
            "service_id" : "temperature",
            "description" : "temperature",
            "properties" : [ {
              "unit" : "centigrade",
              "min" : "1",
              "method" : "R",
              "max" : "100",
              "data_type" : "decimal",
              "description" : "force",
              "step" : 0.1,
              "default_value" : {
                "color" : "red",
                "size" : 1
              },
              "enum_list" : [ "string" ],
              "required" : true,
              "property_name" : "temperature",
              "max_length" : 100
            } ],
            "commands" : [ {
              "command_name" : "reboot",
              "responses" : [ {
                "response_name" : "ACK",
                "paras" : [ {
                  "unit" : "km/h",
                  "min" : "1",
                  "max" : "100",
                  "para_name" : "force",
                  "data_type" : "string",
                  "description" : "force",
                  "step" : 0.1,
                  "enum_list" : [ "string" ],
                  "required" : false,
                  "max_length" : 100
                } ]
              } ],
              "paras" : [ {
                "unit" : "km/h",
                "min" : "1",
                "max" : "100",
                "para_name" : "force",
                "data_type" : "string",
                "description" : "force",
                "step" : 0.1,
                "enum_list" : [ "string" ],
                "required" : false,
                "max_length" : 100
              } ]
            } ],
            "events" : [ {
              "event_type" : "reboot",
              "paras" : [ {
                "unit" : "km/h",
                "min" : "1",
                "max" : "100",
                "para_name" : "force",
                "data_type" : "string",
                "description" : "force",
                "step" : 0.1,
                "enum_list" : [ "string" ],
                "required" : false,
                "max_length" : 100
              } ]
            } ],
            "option" : "Mandatory"
          } ],
          "app_id" : "jeQDJQZltU8iKgFFoW060F5SGZka"
        }
        ```
        
      
      - 响应示例 Status Code: 201 Created
        ```
        Content-Type: application/json
        {
          "app_id" : "jeQDJQZltU8iKgFFoW060F5SGZka",
          "app_name" : "testAPP01",
          "product_id" : "5ba24f5ebbe8f56f5a14f605",
          "name" : "Thermometer",
          "device_type" : "Thermometer",
          "protocol_type" : "CoAP",
          "data_format" : "binary",
          "manufacturer_name" : "ABC",
          "industry" : "smartCity",
          "description" : "this is a thermometer produced",
          "service_capabilities" : [ {
            "service_id" : "temperature",
            "service_type" : "temperature",
            "properties" : [ {
              "property_name" : "temperature",
              "required" : true,
              "data_type" : "decimal",
              "min" : 1,
              "max" : 100,
              "max_length" : 100,
              "step" : 0.1,
              "unit" : "centigrade",
              "method" : "R",
              "description" : "force",
              "default_value" : {
                "color" : "red",
                "size" : 1
              }
            } ],
            "commands" : [ {
              "command_name" : "reboot",
              "paras" : [ {
                "para_name" : "force",
                "required" : false,
                "data_type" : "string",
                "min" : 1,
                "max" : 100,
                "max_length" : 100,
                "step" : 0.1,
                "unit" : "km/h",
                "description" : "force"
              } ],
              "responses" : [ {
                "paras" : [ {
                  "para_name" : "force",
                  "required" : false,
                  "data_type" : "string",
                  "min" : 1,
                  "max" : 100,
                  "max_length" : 100,
                  "step" : 0.1,
                  "unit" : "km/h",
                  "description" : "force"
                } ],
                "response_name" : "ACK"
              } ]
            } ],
            "events" : [ {
              "event_type" : "reboot",
              "paras" : [ {
                "para_name" : "force",
                "required" : false,
                "data_type" : "string",
                "min" : 1,
                "max" : 100,
                "max_length" : 100,
                "step" : 0.1,
                "unit" : "km/h",
                "description" : "force"
              } ]
            } ],
            "description" : "temperature",
            "option" : "Mandatory"
          } ],
          "create_time" : "20190303T081011Z"
        }
        ```
        
       
   
   2. 记录返回结果中产品id，即"product_id"。
   
   
   
   
3. 在[1.a]创建的资源空间下创建设备。
   
   1. 使用[1.b]记录的"app_id"和[2.b]记录的"product_id"创建设备。
      - 接口信息 URL：POST /v5/iot/{project_id}/devices
        详情参见[创建设备 - AddDevice](https://support.huaweicloud.com/api-iothub/iot_06_v5_0046.html)
        
      
      - 请求示例
        ```
        POST https://{Endpoint}/v5/iot/{project_id}/devices
        Content-Type: application/json
        X-Auth-Token: ********
        Instance-Id: ********
        {
          "device_id" : "d4922d8a-6c8e-4396-852c-164aefa6638f",
          "node_id" : "ABC123456789",
          "device_name" : "dianadevice",
          "product_id" : "b640f4c203b7910fc3cbd446ed437cbd",
          "auth_info" : {
            "auth_type" : "SECRET",
            "secure_access" : true,
            "fingerprint" : "dc0f1016f495157344ac5f1296335cff725ef22f",
            "secret" : "3b935a250c50dc2c6d481d048cefdc3c",
            "timeout" : 300
          },
          "description" : "watermeter device",
          "gateway_id" : "d4922d8a-6c8e-4396-852c-164aefa6638f",
          "app_id" : "jeQDJQZltU8iKgFFoW060F5SGZka",
          "extension_info" : {
            "aaa" : "xxx",
            "bbb" : 0
          },
          "shadow" : [ {
            "desired" : {
              "temperature" : "60"
            },
            "service_id" : "WaterMeter"
          } ]
        }
        ```
        
      
      - 响应示例 Status Code: 201 Created
        ```
        Content-Type: application/json
        {
          "app_id" : "jeQDJQZltU8iKgFFoW060F5SGZka",
          "app_name" : "testAPP01",
          "device_id" : "d4922d8a-6c8e-4396-852c-164aefa6638f",
          "node_id" : "ABC123456789",
          "gateway_id" : "d4922d8a-6c8e-4396-852c-164aefa6638f",
          "device_name" : "dianadevice",
          "node_type" : "ENDPOINT",
          "description" : "watermeter device",
          "fw_version" : "1.1.0",
          "sw_version" : "1.1.0",
          "auth_info" : {
            "auth_type" : "SECRET",
            "secure_access" : true,
            "fingerprint" : "dc0f1016f495157344ac5f1296335cff725ef22f",
            "secret" : "3b935a250c50dc2c6d481d048cefdc3c",
            "timeout" : 300
          },
          "product_id" : "b640f4c203b7910fc3cbd446ed437cbd",
          "product_name" : "Thermometer",
          "status" : "INACTIVE",
          "create_time" : "20190303T081011Z",
          "tags" : [ {
            "tag_value" : "testTagValue",
            "tag_key" : "testTagName"
          } ],
          "extension_info" : {
            "aaa" : "xxx",
            "bbb" : 0
          }
        }
        ```
        
       
   
   2. 根据响应确认设备创建结果。
   
   
   
   
 
