Files
Web_F305_Mqtt_Prod/MQTTServerSideAPI/Controllers/DevicesController.cs

94 lines
3.5 KiB
C#
Raw Normal View History

2025-11-26 11:32:30 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using TencentCloud.Common;
using TencentCloud.Common.Profile;
using TencentCloud.Iotcloud.V20210408;
using TencentCloud.Iotcloud.V20210408.Models;
namespace MQTTServerSideAPI.Controllers
{
public class DevicesController : ApiController
{
// GET api/<controller>
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/<controller>/5
public string Get(string productid)
{
string strProductId = productid;
string retString = "";
try
{
// 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey此处还需注意密钥对的保密
// 代码泄露可能会导致 SecretId 和 SecretKey 泄露并威胁账号下所有资源的安全性。以下代码示例仅供参考建议采用更安全的方式来使用密钥请参见https://cloud.tencent.com/document/product/1278/85305
// 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
Credential cred = new Credential
{
SecretId = "AKIDKhz84sbEqFPRK1Uo0zUnwwjNp4884vc4",
SecretKey = "JQ8qZGxgQAtbCfvxb7VqzxujOtFVb1KT"
};
// 实例化一个client选项可选的没有特殊需求可以跳过
ClientProfile clientProfile = new ClientProfile();
// 实例化一个http选项可选的没有特殊需求可以跳过
HttpProfile httpProfile = new HttpProfile();
httpProfile.Endpoint = ("iotcloud.tencentcloudapi.com");
clientProfile.HttpProfile = httpProfile;
// 实例化要请求产品的client对象,clientProfile是可选的
IotcloudClient client = new IotcloudClient(cred, "ap-guangzhou", clientProfile);
// 实例化一个请求对象,每个接口都会对应一个request对象
DescribeDevicesRequest req = new DescribeDevicesRequest();
req.ProductId = "HICL5RNXAU"; //strProductId
req.Offset = 0;
req.Limit = 250;
Logger.LogDownSide(String.Format("Send request: req.ProductId:{0}, req.Offset:{1}, req.Limit:{2}",
req.ProductId,
req.Offset,
req.Limit
));
// 返回的resp是一个DescribeDevicesResponse的实例与请求对象对应
DescribeDevicesResponse resp = client.DescribeDevicesSync(req);
// 输出json格式的字符串回包
retString = AbstractModel.ToJsonString(resp);
Logger.LogDownSide("Success return !! " + retString);
}
catch (Exception e)
{
if (e.InnerException != null)
retString = "Error " + e.InnerException.ToString();
Logger.LogDownSide("Failed exception !! " + e.ToString());
}
return retString;
}
// POST api/<controller>
public void Post([FromBody] string value)
{
}
// PUT api/<controller>/5
public void Put(int id, [FromBody] string value)
{
}
// DELETE api/<controller>/5
public void Delete(int id)
{
}
}
}