初始化
This commit is contained in:
389
MQTTServerSideAPI/Controllers/MessageSendController.cs
Normal file
389
MQTTServerSideAPI/Controllers/MessageSendController.cs
Normal file
@@ -0,0 +1,389 @@
|
||||
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;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace MQTTServerSideAPI.Controllers
|
||||
{
|
||||
public class MessageSendController : ApiController
|
||||
{
|
||||
public static UInt16 snNum = 0;
|
||||
|
||||
// GET api/<controller>
|
||||
public List<string> Get(string productid,string devicename)
|
||||
{
|
||||
List<string> t = new List<string>();
|
||||
t = SQLiteDBHelper.GetLOGCallin(productid, devicename);
|
||||
return t;
|
||||
}
|
||||
|
||||
// GET api/<controller>/5
|
||||
public string Get(int id)
|
||||
{
|
||||
return "value";
|
||||
}
|
||||
|
||||
//public static string PostDownCmd6(String strDevice, UInt32 uintCustTSP, int rspType=1)
|
||||
//{
|
||||
// if (String.IsNullOrWhiteSpace(strDevice))
|
||||
// strDevice = "F305-00003";
|
||||
// string strTopic = "HICL5RNXAU/" + strDevice + "/control";
|
||||
// string strProductId = "HICL5RNXAU";
|
||||
// string strDeviceName = strDevice;
|
||||
// string strBytesPara1 = "00 00 00 00 00 00"
|
||||
// + "00 00 00 00 00 00"
|
||||
// + "00 00 00 00 00 00"
|
||||
// + "00 00 00 00 00 00"
|
||||
// + "00 00 00 00 00 00";
|
||||
|
||||
// DateTimeOffset tof = new DateTimeOffset(DateTime.UtcNow);
|
||||
// byte[] timeNow = BitConverter.GetBytes((UInt32)(tof.ToUnixTimeSeconds()));
|
||||
|
||||
// Logger.LogCalloutanaSide("\r\n命令06回复设备3,设备:" + strDevice + " TSP:" + uintCustTSP + " rspType:" + rspType);
|
||||
|
||||
// byte a;
|
||||
// int paraLen = 0;
|
||||
// byte[] tmpParaBt = new byte[400];
|
||||
// string strBytesPara = "";
|
||||
// if (rspType == 1) //MAC
|
||||
// {
|
||||
// tmpParaBt[paraLen] = 0x01;
|
||||
// paraLen++;
|
||||
|
||||
// strBytesPara = strBytesPara1;
|
||||
// strBytesPara = strBytesPara.Trim();
|
||||
// if (!String.IsNullOrWhiteSpace(strBytesPara))
|
||||
// {
|
||||
// strBytesPara = strBytesPara.Trim();
|
||||
// string[] hexes = strBytesPara.Split(' ');
|
||||
// for (int i = 0; i < hexes.Length; i++)
|
||||
// {
|
||||
// a = Convert.ToByte(hexes[i], 16);
|
||||
// tmpParaBt[paraLen] = a;
|
||||
// paraLen++;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// else // == 2, TimeStamp
|
||||
// {
|
||||
// tmpParaBt[paraLen] = 0x02;
|
||||
// paraLen++;
|
||||
|
||||
// if (uintCustTSP != 0)
|
||||
// {
|
||||
// byte[] timeNow1 = BitConverter.GetBytes(uintCustTSP);
|
||||
// tmpParaBt[paraLen] = timeNow1[0];
|
||||
// paraLen++;
|
||||
// tmpParaBt[paraLen] = timeNow1[1];
|
||||
// paraLen++;
|
||||
// tmpParaBt[paraLen] = timeNow1[2];
|
||||
// paraLen++;
|
||||
// tmpParaBt[paraLen] = timeNow1[3];
|
||||
// paraLen++;
|
||||
|
||||
// string downstr1 = "";
|
||||
// for (int i = paraLen-4; i < paraLen; i++)
|
||||
// {
|
||||
// downstr1 += tmpParaBt[i].ToString("X2");
|
||||
// }
|
||||
// Logger.LogCalloutanaSide("\r\n命令06回复设备3.5 ,时间戳字节:" + downstr1);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// tmpParaBt[paraLen] = timeNow[0];
|
||||
// paraLen++;
|
||||
// tmpParaBt[paraLen] = timeNow[1];
|
||||
// paraLen++;
|
||||
// tmpParaBt[paraLen] = timeNow[2];
|
||||
// paraLen++;
|
||||
// tmpParaBt[paraLen] = timeNow[3];
|
||||
// paraLen++;
|
||||
// }
|
||||
|
||||
// }
|
||||
|
||||
// Logger.LogCalloutanaSide("\r\n命令06回复设备4");
|
||||
|
||||
// byte[] fixedHead = new byte[12];
|
||||
// fixedHead[0] = 0xA6;
|
||||
|
||||
// snNum++;
|
||||
// byte[] btSn = BitConverter.GetBytes(snNum);
|
||||
// fixedHead[1] = btSn[0];
|
||||
// fixedHead[2] = btSn[1];
|
||||
// fixedHead[3] = timeNow[0];
|
||||
// fixedHead[4] = timeNow[1];
|
||||
// fixedHead[5] = timeNow[2];
|
||||
// fixedHead[6] = timeNow[3];
|
||||
// fixedHead[10] = 0x01;
|
||||
// fixedHead[11] = 0x06;
|
||||
|
||||
|
||||
// Logger.LogCalloutanaSide("\r\n命令06回复设备5");
|
||||
|
||||
// int resultLen = 0;
|
||||
// byte[] resultByte = new byte[500];
|
||||
// for (int i = 0; i < fixedHead.Length; i++)
|
||||
// {
|
||||
// resultByte[resultLen] = fixedHead[i];
|
||||
// resultLen++;
|
||||
// }
|
||||
// for (int i = 0; i < paraLen; i++)
|
||||
// {
|
||||
// resultByte[resultLen] = tmpParaBt[i];
|
||||
// resultLen++;
|
||||
// }
|
||||
// byte[] lenbtye = BitConverter.GetBytes((UInt16)resultByte.Length);
|
||||
// resultByte[7] = lenbtye[0];
|
||||
// resultByte[8] = lenbtye[1];
|
||||
|
||||
// //校验和
|
||||
// resultByte[9] = 0x00;
|
||||
|
||||
// resultByte[9] = GetCheckSum(resultByte, resultLen);
|
||||
|
||||
// string resultBase64 = Convert.ToBase64String(resultByte, 0, resultLen);
|
||||
|
||||
// Logger.LogCalloutanaSide("\r\n命令06回复设备6");
|
||||
|
||||
|
||||
// string downstr = "";
|
||||
// for (int i = 0; i < resultLen; i++)
|
||||
// {
|
||||
// downstr += resultByte[i].ToString("X2");
|
||||
// }
|
||||
// Logger.LogCalloutanaSide("\r\n下发命令字节:" + downstr);
|
||||
|
||||
// string retString = SendMQTTServerAPI(strProductId, strDeviceName, strTopic, resultBase64);
|
||||
|
||||
// Logger.LogCalloutanaSide("\r\n命令06回复设备7");
|
||||
|
||||
// return retString;
|
||||
//}
|
||||
|
||||
// POST api/<controller>
|
||||
public string Post([FromBody] JObject uisenddata)
|
||||
{
|
||||
string retString = "value";
|
||||
|
||||
string strTopic = (String)uisenddata["topic"];
|
||||
string strProductId = (String)uisenddata["productid"];
|
||||
string strDeviceName = (String)uisenddata["devicename"];
|
||||
string strCmd = (String)uisenddata["cmd"];
|
||||
string strBytesPara = (String)uisenddata["bytepara"];
|
||||
byte a;
|
||||
int paraLen = 0;
|
||||
byte[] tmpParaBt = new byte[400];
|
||||
strBytesPara = strBytesPara.Trim();
|
||||
if(!String.IsNullOrWhiteSpace(strBytesPara))
|
||||
{
|
||||
strBytesPara = strBytesPara.Trim();
|
||||
string[] hexes = strBytesPara.Split(' ');
|
||||
for(int i = 0; i < hexes.Length; i ++)
|
||||
{
|
||||
a = Convert.ToByte(hexes[i], 16);
|
||||
tmpParaBt[paraLen] = a;
|
||||
paraLen++;
|
||||
}
|
||||
}
|
||||
|
||||
DateTimeOffset tof = new DateTimeOffset(DateTime.UtcNow);
|
||||
byte[] fixedHead = new byte[12];
|
||||
fixedHead[0] = 0xA6;
|
||||
|
||||
snNum++;
|
||||
byte[] btSn = BitConverter.GetBytes(snNum);
|
||||
fixedHead[1] = btSn[0];
|
||||
fixedHead[2] = btSn[1];
|
||||
|
||||
byte[] timeNow = BitConverter.GetBytes((UInt32)(tof.ToUnixTimeSeconds()));
|
||||
fixedHead[3] = timeNow[0];
|
||||
fixedHead[4] = timeNow[1];
|
||||
fixedHead[5] = timeNow[2];
|
||||
fixedHead[6] = timeNow[3];
|
||||
fixedHead[10] = 0x01;
|
||||
if (strCmd == "06")
|
||||
fixedHead[11] = 0x06;
|
||||
else if (strCmd == "07")
|
||||
fixedHead[11] = 0x07;
|
||||
else if (strCmd == "08")
|
||||
fixedHead[11] = 0x08;
|
||||
else if (strCmd == "09")
|
||||
fixedHead[11] = 0x09;
|
||||
else
|
||||
fixedHead[11] = Convert.ToByte(strCmd, 16);
|
||||
|
||||
int resultLen = 0;
|
||||
byte[] resultByte = new byte[500];
|
||||
for(int i = 0; i < fixedHead.Length; i ++)
|
||||
{
|
||||
resultByte[resultLen] = fixedHead[i];
|
||||
resultLen++;
|
||||
}
|
||||
for (int i = 0; i < paraLen; i++)
|
||||
{
|
||||
resultByte[resultLen] = tmpParaBt[i];
|
||||
resultLen++;
|
||||
}
|
||||
byte[] lenbtye = BitConverter.GetBytes((UInt16)resultLen);
|
||||
resultByte[7] = lenbtye[0];
|
||||
resultByte[8] = lenbtye[1];
|
||||
|
||||
//校验和
|
||||
resultByte[9] = 0x00;
|
||||
|
||||
resultByte[9] = GetCheckSum(resultByte, resultLen);
|
||||
|
||||
string downstr = "";
|
||||
for(int i = 0; i < resultLen; i ++)
|
||||
{
|
||||
downstr += resultByte[i].ToString() + " ";
|
||||
}
|
||||
|
||||
Logger.LogCalloutanaSide("设备"+strDeviceName + "下发命令字节:" + downstr);
|
||||
|
||||
|
||||
string resultBase64 = Convert.ToBase64String(resultByte, 0, resultLen);
|
||||
|
||||
|
||||
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对象
|
||||
PublishMessageRequest req = new PublishMessageRequest();
|
||||
req.Topic = strTopic; // "HICL5RNXAU/F305-00002/control";
|
||||
req.Payload = resultBase64;
|
||||
req.ProductId = strProductId; // "HICL5RNXAU";
|
||||
req.DeviceName = strDeviceName; // "F305-00002";
|
||||
req.PayloadEncoding = "base64";
|
||||
|
||||
Logger.LogDownSide(String.Format("Send request: req.Topic:{0}, req.PayLoad:{1}, req.ProductId:{2}, req.DeviceName:{3}",
|
||||
req.Topic,
|
||||
req.Payload,
|
||||
req.ProductId,
|
||||
req.DeviceName
|
||||
));
|
||||
|
||||
|
||||
// 返回的resp是一个PublishMessageResponse的实例,与请求对象对应
|
||||
PublishMessageResponse resp = client.PublishMessageSync(req);
|
||||
// 输出json格式的字符串回包
|
||||
retString = "Success " + 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;
|
||||
}
|
||||
|
||||
//public static string SendMQTTServerAPI(string strProductId, string strDeviceName, string strTopic, string resultBase64)
|
||||
//{
|
||||
// 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对象
|
||||
// PublishMessageRequest req = new PublishMessageRequest();
|
||||
// req.Topic = strTopic; // "HICL5RNXAU/F305-00002/control";
|
||||
// req.Payload = resultBase64;
|
||||
// req.ProductId = strProductId; // "HICL5RNXAU";
|
||||
// req.DeviceName = strDeviceName; // "F305-00002";
|
||||
// req.PayloadEncoding = "base64";
|
||||
|
||||
// Logger.LogDownSide(String.Format("Send request: req.Topic:{0}, req.PayLoad:{1}, req.ProductId:{2}, req.DeviceName:{3}",
|
||||
// req.Topic,
|
||||
// req.Payload,
|
||||
// req.ProductId,
|
||||
// req.DeviceName
|
||||
// ));
|
||||
|
||||
|
||||
// // 返回的resp是一个PublishMessageResponse的实例,与请求对象对应
|
||||
// PublishMessageResponse resp = client.PublishMessageSync(req);
|
||||
// // 输出json格式的字符串回包
|
||||
// retString = "Success " + 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;
|
||||
//}
|
||||
public static byte GetCheckSum(byte[] data, int datalen)
|
||||
{
|
||||
List<byte> m = new List<byte>();
|
||||
int sum = 0;
|
||||
for (int i = 0; i < datalen; i++)
|
||||
{
|
||||
m.Add(data[i]);
|
||||
sum += data[i];
|
||||
}
|
||||
|
||||
sum = sum % 256;
|
||||
//sum = ~sum + 1; 跟设备端操作对过,不用加1
|
||||
sum = ~sum;
|
||||
|
||||
return (byte)sum;
|
||||
}
|
||||
|
||||
// PUT api/<controller>/5
|
||||
public void Put(int id, [FromBody] string value)
|
||||
{
|
||||
}
|
||||
|
||||
// DELETE api/<controller>/5
|
||||
public void Delete(int id)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user