
# 添加数采模板
#### 功能介绍
添加数采模板
#### 调试
您可以在[API Explorer](https://console.huaweicloud.com/apiexplorer/#/openapi/IoTEdge/doc?api=AddOtTemplates)中调试该接口，支持自动认证鉴权。API Explorer可以自动生成SDK代码示例，并提供SDK代码示例调试功能。
#### URI
POST /v2/{project_id}/templates/ots/data-sources
表1路径参数 
| 参数         | 是否必选 | 参数类型   | 描述                                                                                        |
|:---|:---|:---|:---|
| project_id | 是    | String | 项目ID。获取方法请参见 [获取项目ID](https://support.huaweicloud.com/api-iotedge/iotedge_api_0032.html)。 |
   
#### 请求参数
表2请求Header参数 
| 参数           | 是否必选 | 参数类型   | 描述                                                                                                                                                                                                                                         |
|:---|:---|:---|:---|
| X-Auth-Token | 是    | String | 用户Token。通过调用IAM服务 [获取IAM用户Token](https://support.huaweicloud.com/api-iam/iam_30_0001.html)接口获取，接口返回的响应消息头中"X-Subject-Token"就是需要获取的用户Token。简要的获取方法样例请参见 [Token认证](https://support.huaweicloud.com/api-iothub/iot_06_v5_0091.html#section0)。 |
   
表3请求Body参数 
| 参数              | 是否必选 | 参数类型   | 描述                        |
|:---|:---|:---|:---|
| tpl_id          | 是    | String | 模板id                      |
| name            | 是    | String | 模板名称，允许中、数字、英文大小写、下划线、中划线 |
| description     | 是    | String | 描述                        |
| datasource_meta | 是    | Object | 数据源元数据                    |
| point_meta      | 是    | Object | 点位表元数据                    |
   
#### 响应参数
**状态码：200**
表4响应Body参数 
| 参数              | 参数类型   | 描述       |
|:---|:---|:---|
| tpl_id          | String | 模板id     |
| name            | String | 模板名称     |
| description     | String | 描述       |
| datasource_meta | Object | 数据源元数据   |
| point_meta      | Object | 点位表元数据   |
| create_time     | String | 创建时间     |
| update_time     | String | 最后一次修改时间 |
   
#### 请求示例
```
GET https://{endpoint}/v2/{project_id}/templates/ots/data-sources
{
  "tpl_id" : "sys_general_opcua",
  "name" : "sys_general_opcua",
  "description" : "通用opcua模板",
  "datasource_meta" : {
    "config_tabs" : [ {
      "key" : "connection_info",
      "name" : "连接信息",
      "description" : "连接信息",
      "config_items" : [ {
        "key" : "endpoint",
        "name" : "服务端点",
        "description" : "服务完整URL",
        "data_type" : "string",
        "required" : true,
        "crypted" : false,
        "max_length" : 128,
        "example" : "opc.tcp://127.0.0.1:53530/OPCUA"
      }, {
        "key" : "username",
        "name" : "用户名",
        "description" : "OPCUA服务器认证用户名",
        "data_type" : "string",
        "required" : false,
        "crypted" : false,
        "max_length" : 128,
        "example" : "admin"
      }, {
        "key" : "password",
        "name" : "密码",
        "description" : "OPCUA服务器认证密码",
        "data_type" : "string",
        "required" : false,
        "crypted" : true,
        "max_length" : 512,
        "example" : "******"
      } ]
    }, {
      "key" : "collection_paras",
      "name" : "连接附加参数",
      "description" : "连接附加参数",
      "config_items" : [ {
        "key" : "default_cycle",
        "name" : "采集周期",
        "description" : "采集周期",
        "data_type" : "int",
        "required" : true,
        "crypted" : false,
        "max_length" : 65535,
        "example" : 10000
      } ]
    } ],
    "default_values" : {
      "drivername" : "OPCUA"
    }
  },
  "point_meta" : {
    "config_items" : [ {
      "key" : "address",
      "name" : "点位地址",
      "description" : "采集点位的地址",
      "data_type" : "string",
      "required" : true,
      "crypted" : false,
      "max_length" : 256,
      "example" : "ns=2;s=Root/Motor/Voltage"
    }, {
      "key" : "data_type",
      "name" : "点位值的数据类型",
      "description" : "点位地址存储值的数据类型",
      "data_type" : "string",
      "required" : true,
      "crypted" : false,
      "max_length" : 128,
      "example" : "int，decimal"
    }, {
      "key" : "cycle",
      "name" : "点位采集周期",
      "description" : "点位采集周期",
      "data_type" : "int",
      "required" : true,
      "crypted" : false,
      "max_length" : 65535,
      "example" : 10000
    } ]
  }
}
```
#### 响应示例
**状态码：200**
OK
```
{
  "tpl_id" : "sys_general_opcua",
  "name" : "sys_general_opcua",
  "description" : "通用opcua模板",
  "datasource_meta" : {
    "config_tabs" : [ {
      "key" : "connection_info",
      "name" : "连接信息",
      "description" : "连接信息",
      "config_items" : [ {
        "key" : "endpoint",
        "name" : "服务端点",
        "description" : "服务完整URL",
        "example" : "opc.tcp://127.0.0.1:53530/OPCUA"
      }, {
        "key" : "username",
        "name" : "用户名",
        "description" : "OPCUA服务器认证用户名",
        "example" : "admin"
      }, {
        "key" : "password",
        "name" : "密码",
        "description" : "OPCUA服务器认证密码",
        "data_type" : "string"
      } ]
    }, {
      "key" : "collection_paras",
      "name" : "连接附加参数",
      "description" : "连接附加参数",
      "config_items" : [ {
        "key" : "default_cycle",
        "name" : "采集周期",
        "description" : "采集周期"
      } ]
    } ],
    "default_values" : {
      "drivername" : "OPCUA"
    }
  },
  "point_meta" : {
    "config_items" : [ {
      "key" : "address",
      "name" : "点位地址",
      "description" : "采集点位的地址",
      "example" : "ns=2;s=Root/Motor/Voltage"
    }, {
      "key" : "data_type",
      "name" : "点位值的数据类型",
      "description" : "点位地址存储值的数据类型"
    }, {
      "key" : "cycle",
      "name" : "点位采集周期",
      "description" : "点位采集周期"
    } ]
  }
}
```
#### 状态码
| 状态码 | 描述          |
|:---|:---|
| 200 | OK          |
| 400 | Bad Request |
| 404 | Not Found   |
   
#### 错误码
请参见[错误码](https://support.huaweicloud.com/api-iotedge/ErrorCode.html)。
