From c979ba9299ff416bc695e566ac949179256338f5 Mon Sep 17 00:00:00 2001 From: iioter <535915157@qq.com> Date: Mon, 13 Jan 2025 09:08:33 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AF=B9=E6=8E=A5thingspanel?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- IoTGateway.Model/SystemConfig.cs | 4 +- .../PlatformHandler/PlatformHandlerFactory.cs | 2 + .../PlatformHandler/ThingsPanelHandler.cs | 95 +++++++++++++++++++ 3 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 Plugins/Plugin/PlatformHandler/ThingsPanelHandler.cs diff --git a/IoTGateway.Model/SystemConfig.cs b/IoTGateway.Model/SystemConfig.cs index f056949..ba89e1a 100644 --- a/IoTGateway.Model/SystemConfig.cs +++ b/IoTGateway.Model/SystemConfig.cs @@ -54,6 +54,8 @@ namespace IoTGateway.Model [Display(Name = "HuaWeiCloud")] HuaWei = 7, [Display(Name = "IoTGateway")] - IoTGateway = 8 + IoTGateway = 8, + [Display(Name = "ThingsPanel")] + ThingsPanel = 9 } } \ No newline at end of file diff --git a/Plugins/Plugin/PlatformHandler/PlatformHandlerFactory.cs b/Plugins/Plugin/PlatformHandler/PlatformHandlerFactory.cs index 72faecf..629c224 100644 --- a/Plugins/Plugin/PlatformHandler/PlatformHandlerFactory.cs +++ b/Plugins/Plugin/PlatformHandler/PlatformHandlerFactory.cs @@ -15,6 +15,8 @@ namespace Plugin.PlatformHandler return new ThingsBoardHandler(mqttClient, logger, onExcRpc); case IoTPlatformType.ThingsCloud: return new ThingsCloudHandler(mqttClient, logger, onExcRpc); + case IoTPlatformType.ThingsPanel: + return new ThingsPanelHandler(mqttClient, logger, onExcRpc); case IoTPlatformType.IoTSharp: default: return new IoTSharpHandler(mqttClient, logger, onExcRpc); diff --git a/Plugins/Plugin/PlatformHandler/ThingsPanelHandler.cs b/Plugins/Plugin/PlatformHandler/ThingsPanelHandler.cs new file mode 100644 index 0000000..cc39d45 --- /dev/null +++ b/Plugins/Plugin/PlatformHandler/ThingsPanelHandler.cs @@ -0,0 +1,95 @@ +using IoTGateway.Model; +using Microsoft.Extensions.Logging; +using MQTTnet; +using MQTTnet.Client; +using MQTTnet.Extensions.ManagedClient; +using MQTTnet.Protocol; +using Newtonsoft.Json; +using PluginInterface; + +namespace Plugin.PlatformHandler +{ + public class ThingsPanelHandler : IPlatformHandler + { + public IManagedMqttClient MqttClient { get; } + public ILogger Logger { get; } + + public event EventHandler OnExcRpc; + private readonly DateTime _tsStartDt = new(1970, 1, 1); + + public ThingsPanelHandler(IManagedMqttClient mqttClient, ILogger logger, EventHandler onExcRpc) + { + MqttClient = mqttClient; + Logger = logger; + OnExcRpc = onExcRpc; + } + + + public async Task ClientConnected() + { + await MqttClient.SubscribeAsync("gateway/telemetry/control/+", MqttQualityOfServiceLevel.ExactlyOnce); + } + public void ReceiveRpc(MqttApplicationMessageReceivedEventArgs e) + { + } + + public async Task ResponseRpcAsync(RpcResponse rpcResponse) + { + await Task.CompletedTask; + } + + public async Task PublishTelemetryAsync(string deviceName, Device device, Dictionary> sendModel) + { + foreach (var payload in sendModel[deviceName]) + { + if (payload.Values != null) + { + var telemetryData = new Dictionary>() + { + { + "sub_device_data", new Dictionary() + { + { deviceName, payload.Values } + } + } + + }; + await MqttClient.EnqueueAsync(new MqttApplicationMessageBuilder().WithTopic($"gateway/telemetry") + .WithPayload(JsonConvert.SerializeObject(telemetryData)).Build()); + } + } + } + + public Task UploadAttributeAsync(string deviceName, object obj) + { + return Task.CompletedTask; + } + + public async Task RequestAttributes(string deviceName, bool anySide, params string[] args) + { + await Task.CompletedTask; + } + + public async Task DeviceConnected(string deviceName, Device device) + { + await Task.CompletedTask; + } + + public async Task DeviceDisconnected(string deviceName, Device device) + { + + await Task.CompletedTask; + } + + public Task DeviceAdded(Device device) + { + return Task.CompletedTask; + } + + public Task DeviceDeleted(Device device) + { + return Task.CompletedTask; + } + + } +}