This commit is contained in:
UserX 2023-12-21 22:38:54 +02:00
parent eb7ac0eff1
commit 28c58d9700

View File

@ -32,7 +32,7 @@ namespace Plugin
_logger = logger; _logger = logger;
//_uaNodeManager = uaService.server.m_server.nodeManagers[0] as ReferenceNodeManager; //_uaNodeManager = uaService.server.m_server.nodeManagers[0] as ReferenceNodeManager;
//StartClientAsync().Wait(); StartClientAsync().Wait();
} }
public async Task StartClientAsync() public async Task StartClientAsync()
@ -76,10 +76,6 @@ namespace Plugin
private async Task Client_ConnectedAsync(MqttClientConnectedEventArgs arg) private async Task Client_ConnectedAsync(MqttClientConnectedEventArgs arg)
{ {
if (Client == null)
{
StartClientAsync().Wait();
}
_logger.LogInformation($"MQTT CONNECTED WITH SERVER "); _logger.LogInformation($"MQTT CONNECTED WITH SERVER ");
#region Topics #region Topics
try try
@ -127,10 +123,6 @@ namespace Plugin
private async Task Client_DisconnectedAsync(MqttClientDisconnectedEventArgs arg) private async Task Client_DisconnectedAsync(MqttClientDisconnectedEventArgs arg)
{ {
if (Client == null)
{
StartClientAsync().Wait();
}
try try
{ {
_logger.LogError($"MQTT DISCONNECTED WITH SERVER "); _logger.LogError($"MQTT DISCONNECTED WITH SERVER ");
@ -264,10 +256,6 @@ namespace Plugin
private async Task ResponseTbRpcAsync(TBRpcResponse tBRpcResponse) private async Task ResponseTbRpcAsync(TBRpcResponse tBRpcResponse)
{ {
if (Client == null)
{
StartClientAsync().Wait();
}
await Client.PublishAsync(new MqttApplicationMessageBuilder() await Client.PublishAsync(new MqttApplicationMessageBuilder()
.WithTopic(_tbRpcTopic) .WithTopic(_tbRpcTopic)
.WithPayload(JsonConvert.SerializeObject(tBRpcResponse)) .WithPayload(JsonConvert.SerializeObject(tBRpcResponse))
@ -276,10 +264,6 @@ namespace Plugin
private async Task ResponseTcRpcAsync(TCRpcRequest tCRpcResponse) private async Task ResponseTcRpcAsync(TCRpcRequest tCRpcResponse)
{ {
if (Client == null)
{
StartClientAsync().Wait();
}
var topic = $"command/reply/{tCRpcResponse.RequestData.RequestId}"; var topic = $"command/reply/{tCRpcResponse.RequestData.RequestId}";
await Client.PublishAsync(new MqttApplicationMessageBuilder() await Client.PublishAsync(new MqttApplicationMessageBuilder()
.WithTopic(topic) .WithTopic(topic)
@ -289,10 +273,6 @@ namespace Plugin
private async Task ResponseIsRpcAsync(ISRpcResponse rpcResult) private async Task ResponseIsRpcAsync(ISRpcResponse rpcResult)
{ {
if (Client == null)
{
StartClientAsync().Wait();
}
//var responseTopic = $"/devices/{deviceid}/rpc/response/{methodName}/{rpcid}"; //var responseTopic = $"/devices/{deviceid}/rpc/response/{methodName}/{rpcid}";
var topic = $"devices/{rpcResult.DeviceId}/rpc/response/{rpcResult.Method}/{rpcResult.ResponseId}"; var topic = $"devices/{rpcResult.DeviceId}/rpc/response/{rpcResult.Method}/{rpcResult.ResponseId}";
await Client.PublishAsync(new MqttApplicationMessageBuilder() await Client.PublishAsync(new MqttApplicationMessageBuilder()
@ -333,10 +313,6 @@ namespace Plugin
//Message: {"Device A":{"attribute1":"value1", "attribute2": 42}, "Device B":{"attribute1":"value1", "attribute2": 42} //Message: {"Device A":{"attribute1":"value1", "attribute2": 42}, "Device B":{"attribute1":"value1", "attribute2": 42}
try try
{ {
if (Client == null)
{
StartClientAsync().Wait();
}
return Client.PublishAsync(new MqttApplicationMessageBuilder() return Client.PublishAsync(new MqttApplicationMessageBuilder()
.WithTopic($"devices/{deviceName}/attributes").WithPayload(JsonConvert.SerializeObject(obj)) .WithTopic($"devices/{deviceName}/attributes").WithPayload(JsonConvert.SerializeObject(obj))
.WithQualityOfServiceLevel(MqttQualityOfServiceLevel.AtMostOnce) .WithQualityOfServiceLevel(MqttQualityOfServiceLevel.AtMostOnce)
@ -352,20 +328,12 @@ namespace Plugin
public async Task UploadIsTelemetryDataAsync(string deviceName, object obj) public async Task UploadIsTelemetryDataAsync(string deviceName, object obj)
{ {
if (Client == null)
{
StartClientAsync().Wait();
}
await Client.PublishAsync(new MqttApplicationMessageBuilder().WithTopic($"devices/{deviceName}/telemetry") await Client.PublishAsync(new MqttApplicationMessageBuilder().WithTopic($"devices/{deviceName}/telemetry")
.WithPayload(JsonConvert.SerializeObject(obj)).Build()); .WithPayload(JsonConvert.SerializeObject(obj)).Build());
} }
public async Task UploadTcTelemetryDataAsync(string deviceName, object obj) public async Task UploadTcTelemetryDataAsync(string deviceName, object obj)
{ {
if (Client == null)
{
StartClientAsync().Wait();
}
var toSend = new Dictionary<string, object> { { deviceName, obj } }; var toSend = new Dictionary<string, object> { { deviceName, obj } };
await Client.PublishAsync(new MqttApplicationMessageBuilder().WithTopic($"gateway/attributes") await Client.PublishAsync(new MqttApplicationMessageBuilder().WithTopic($"gateway/attributes")
.WithPayload(JsonConvert.SerializeObject(toSend)).WithQualityOfServiceLevel(MqttQualityOfServiceLevel.ExactlyOnce).Build()); .WithPayload(JsonConvert.SerializeObject(toSend)).WithQualityOfServiceLevel(MqttQualityOfServiceLevel.ExactlyOnce).Build());
@ -393,10 +361,6 @@ namespace Plugin
{ {
Devices = hwTelemetry Devices = hwTelemetry
}; };
if (Client == null)
{
StartClientAsync().Wait();
}
await Client.PublishAsync(new MqttApplicationMessageBuilder().WithTopic($"/v1/devices/{_systemConfig.GatewayName}/datas") await Client.PublishAsync(new MqttApplicationMessageBuilder().WithTopic($"/v1/devices/{_systemConfig.GatewayName}/datas")
.WithPayload(JsonConvert.SerializeObject(hwTelemetrys)).WithQualityOfServiceLevel(MqttQualityOfServiceLevel.ExactlyOnce).Build()); .WithPayload(JsonConvert.SerializeObject(hwTelemetrys)).WithQualityOfServiceLevel(MqttQualityOfServiceLevel.ExactlyOnce).Build());
@ -454,10 +418,6 @@ namespace Plugin
{ {
try try
{ {
if (Client == null)
{
StartClientAsync().Wait();
}
string id = Guid.NewGuid().ToString(); string id = Guid.NewGuid().ToString();
switch (_systemConfig.IoTPlatformType) switch (_systemConfig.IoTPlatformType)
{ {
@ -552,10 +512,6 @@ namespace Plugin
{ {
try try
{ {
if (Client == null)
{
StartClientAsync().Wait();
}
if (CanPubTelemetry(deviceName, device, sendModel)) if (CanPubTelemetry(deviceName, device, sendModel))
{ {
switch (_systemConfig.IoTPlatformType) switch (_systemConfig.IoTPlatformType)
@ -627,10 +583,6 @@ namespace Plugin
{ {
try try
{ {
if (Client == null)
{
StartClientAsync().Wait();
}
switch (_systemConfig.IoTPlatformType) switch (_systemConfig.IoTPlatformType)
{ {
case IoTPlatformType.ThingsBoard: case IoTPlatformType.ThingsBoard:
@ -686,10 +638,6 @@ namespace Plugin
{ {
try try
{ {
if (Client == null)
{
StartClientAsync().Wait();
}
switch (_systemConfig.IoTPlatformType) switch (_systemConfig.IoTPlatformType)
{ {
case IoTPlatformType.ThingsBoard: case IoTPlatformType.ThingsBoard:
@ -745,10 +693,6 @@ namespace Plugin
{ {
try try
{ {
if (Client == null)
{
StartClientAsync().Wait();
}
switch (_systemConfig.IoTPlatformType) switch (_systemConfig.IoTPlatformType)
{ {
case IoTPlatformType.HuaWei: case IoTPlatformType.HuaWei:
@ -784,10 +728,6 @@ namespace Plugin
{ {
try try
{ {
if (Client == null)
{
StartClientAsync().Wait();
}
switch (_systemConfig.IoTPlatformType) switch (_systemConfig.IoTPlatformType)
{ {
case IoTPlatformType.HuaWei: case IoTPlatformType.HuaWei: