支持ThingsCloud

This commit is contained in:
dd 2022-04-20 14:05:44 +08:00
parent 184288a64e
commit ecd6827295
11 changed files with 100 additions and 6 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -31,7 +31,8 @@ namespace IoTGateway.Model
[Display(Name = "百度物联网通信")]
BaiduIoTCore =4,
[Display(Name = "中移OneNet")]
OneNET =5
OneNET = 5,
[Display(Name = "ThingsCloud")]
ThingsCloud = 6
}
}

View File

@ -5,5 +5,5 @@
<wt:row items-per-row="ItemsPerRowEnum.Three">
</wt:row>
</wt:searchpanel>
<wt:quote>注意目前实现了iotsharp和tb的遥测、属性上传以及RPC功能</wt:quote>
<wt:quote>注意目前实现了iotsharp、thingsboard、thingscloud的遥测、属性上传以及RPC功能</wt:quote>
<wt:grid vm="@Model" url="/Config/SystemConfig/Search" />

Binary file not shown.

View File

@ -258,8 +258,8 @@ namespace Plugin
if (task != null)
{
_myMqttClient.OnExcRpc -= MyMqttClient_OnExcRpc;
_driver.Close();
tokenSource.Cancel();
_driver.Close();
}
}

View File

@ -120,6 +120,13 @@ namespace Plugin
//Message: {"device": "Device A", "data": {"attribute1": "value1", "attribute2": 42}}
Client.SubscribeAsync("devices/+/attributes/response/+", MqttQualityOfServiceLevel.ExactlyOnce);
break;
case IoTPlatformType.ThingsCloud:
Client.SubscribeAsync("gateway/attributes/response", MqttQualityOfServiceLevel.ExactlyOnce);
Client.SubscribeAsync("gateway/attributes/get/response", MqttQualityOfServiceLevel.ExactlyOnce);
Client.SubscribeAsync("gateway/attributes/push", MqttQualityOfServiceLevel.ExactlyOnce);
Client.SubscribeAsync("gateway/event/response", MqttQualityOfServiceLevel.ExactlyOnce);
Client.SubscribeAsync("gateway/command/send", MqttQualityOfServiceLevel.ExactlyOnce);
break;
case IoTPlatformType.AliCloudIoT:
break;
case IoTPlatformType.TencentIoTHub:
@ -149,6 +156,9 @@ namespace Plugin
else if (e.ApplicationMessage.Topic.StartsWith($"devices/") && e.ApplicationMessage.Topic.Contains("/rpc/request/"))
{
ReceiveIsRpc(e);
}else if(e.ApplicationMessage.Topic== "gateway/command/send")
{
ReceiveTcRpc(e);
}
}
catch (Exception ex)
@ -158,6 +168,10 @@ namespace Plugin
return Task.CompletedTask;
}
/// <summary>
/// thingsboard rpc
/// </summary>
/// <param name="e"></param>
private void ReceiveTbRpc(MqttApplicationMessageReceivedEventArgs e)
{
TBRpcRequest tBRpcRequest;
@ -179,6 +193,30 @@ namespace Plugin
}
/// <summary>
/// thingscloud rpc
/// </summary>
/// <param name="e"></param>
private void ReceiveTcRpc(MqttApplicationMessageReceivedEventArgs e)
{
TCRpcRequest tCRpcRequest;
try
{
tCRpcRequest = JsonConvert.DeserializeObject<TCRpcRequest>(e.ApplicationMessage.ConvertPayloadToString());
OnExcRpc?.Invoke(Client, new RpcRequest()
{
Method = tCRpcRequest.RequestData.Method,
DeviceName = tCRpcRequest.DeviceName,
RequestId = tCRpcRequest.RequestData.RequestId,
Params = tCRpcRequest.RequestData.Params
});
}
catch (Exception ex)
{
_logger.LogError($"ReceiveTbRpc:Topic:{e.ApplicationMessage.Topic},Payload:{e.ApplicationMessage.ConvertPayloadToString()}", ex);
}
}
private void ReceiveIsRpc(MqttApplicationMessageReceivedEventArgs e)
{
try
@ -217,6 +255,15 @@ namespace Plugin
.WithQualityOfServiceLevel(MqttQualityOfServiceLevel.ExactlyOnce).Build());
}
private Task ResponseTCRpc(TCRpcRequest tCRpcResponse)
{
string topic = $"command/reply/{tCRpcResponse.RequestData.RequestId}";
return Client.PublishAsync(new MqttApplicationMessageBuilder()
.WithTopic(topic)
.WithPayload(JsonConvert.SerializeObject(tCRpcResponse))
.WithQualityOfServiceLevel(MqttQualityOfServiceLevel.ExactlyOnce).Build());
}
private Task ResponseISRpc(ISRpcResponse rpcResult)
{
///IoTSharp/Clients/RpcClient.cs#L65 var responseTopic = $"/devices/{deviceid}/rpc/response/{methodName}/{rpcid}";
@ -269,11 +316,16 @@ namespace Plugin
}
public Task UploadTelemetryDataAsync(string _devicename, object obj)
public Task UploadISTelemetryDataAsync(string _devicename, object obj)
{
return Client.PublishAsync(new MqttApplicationMessageBuilder().WithTopic($"devices/{_devicename}/telemetry").WithPayload(Newtonsoft.Json.JsonConvert.SerializeObject(obj)).Build());
}
public Task UploadTCTelemetryDataAsync(string _devicename, object obj)
{
var toSend = new Dictionary<string, object> { { _devicename, obj } };
return Client.PublishAsync("gateway/attributes", JsonConvert.SerializeObject(toSend));
}
public void ResponseRpc(RpcResponse rpcResponse)
{
try
@ -298,6 +350,9 @@ namespace Plugin
Data = JsonConvert.SerializeObject(new Dictionary<string, object> { { "success", rpcResponse.IsSuccess }, { "description", rpcResponse.Description } })
});
break;
case IoTPlatformType.ThingsCloud:
//官网API不需要回复的
break;
case IoTPlatformType.AliCloudIoT:
break;
case IoTPlatformType.TencentIoTHub:
@ -372,7 +427,13 @@ namespace Plugin
case IoTPlatformType.IoTSharp:
foreach (var payload in SendModel[device.DeviceName])
{
UploadTelemetryDataAsync(device.DeviceName, payload.Values);
UploadISTelemetryDataAsync(device.DeviceName, payload.Values);
}
break;
case IoTPlatformType.ThingsCloud:
foreach (var payload in SendModel[device.DeviceName])
{
UploadTCTelemetryDataAsync(device.DeviceName, payload.Values);
}
break;
case IoTPlatformType.AliCloudIoT:
@ -418,6 +479,9 @@ namespace Plugin
break;
case IoTPlatformType.OneNET:
break;
case IoTPlatformType.ThingsCloud:
await Client.PublishAsync("gateway/connect", JsonConvert.SerializeObject(new Dictionary<string, string> { { "device", DeviceName } }));
break;
default:
break;
}
@ -446,6 +510,9 @@ namespace Plugin
break;
case IoTPlatformType.OneNET:
break;
case IoTPlatformType.ThingsCloud:
await Client.PublishAsync("gateway/disconnect", JsonConvert.SerializeObject(new Dictionary<string, string> { { "device", DeviceName } }));
break;
default:
break;
}

View File

@ -0,0 +1,26 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PluginInterface.ThingsBoard
{
public class TCRpcRequest
{
[JsonProperty(PropertyName = "device")]
public string DeviceName { get; set; }
[JsonProperty(PropertyName = "command")]
public TBRpcData RequestData { get; set; }
}
public class TCRpcData
{
[JsonProperty(PropertyName = "id")]
public string RequestId { get; set; }
[JsonProperty(PropertyName = "method")]
public string Method { get; set; }
[JsonProperty(PropertyName = "params")]
public Dictionary<string, object> Params { get; set; }
}
}

Binary file not shown.