NewLife/NewLife.Remoting

IoTZero在线表增加长连接字段
大石头 authored at 2024-06-30 10:06:29
d6adf29
Tree
1 Parent(s) 4c5f91b
Summary: 7 changed files with 77 additions and 7 deletions.
Modified +2 -2
Modified +39 -1
Modified +11 -0
Modified +1 -0
Modified +16 -0
Modified +2 -2
Modified +6 -2
Modified +2 -2
diff --git a/NewLife.Remoting.Extensions/Controllers/BaseDeviceController.cs b/NewLife.Remoting.Extensions/Controllers/BaseDeviceController.cs
index 2a14817..a7e9de1 100644
--- a/NewLife.Remoting.Extensions/Controllers/BaseDeviceController.cs
+++ b/NewLife.Remoting.Extensions/Controllers/BaseDeviceController.cs
@@ -162,7 +162,7 @@ public class BaseDeviceController : BaseController
             info.Source = new Uri(new Uri(uri), info.Source) + "";
         }
 
-        return info;
+        return info!;
     }
     #endregion
 
@@ -188,7 +188,7 @@ public class BaseDeviceController : BaseController
             HttpContext.Response.StatusCode = 400;
     }
 
-    /// <summary>处理长连接</summary>
+    /// <summary>长连接处理</summary>
     /// <param name="socket"></param>
     /// <param name="token"></param>
     /// <returns></returns>
Modified +39 -1
diff --git a/Samples/IoTZero/Controllers/DeviceController.cs b/Samples/IoTZero/Controllers/DeviceController.cs
index 77753dd..c358f9b 100644
--- a/Samples/IoTZero/Controllers/DeviceController.cs
+++ b/Samples/IoTZero/Controllers/DeviceController.cs
@@ -1,4 +1,5 @@
-using IoT.Data;
+using System.Net.WebSockets;
+using IoT.Data;
 using IoTZero.Services;
 using Microsoft.AspNetCore.Mvc;
 using NewLife.IoT.Drivers;
@@ -7,6 +8,7 @@ using NewLife.IoT.ThingModels;
 using NewLife.Log;
 using NewLife.Remoting;
 using NewLife.Remoting.Extensions;
+using NewLife.Remoting.Extensions.Services;
 using NewLife.Remoting.Models;
 
 namespace IoTZero.Controllers;
@@ -20,6 +22,7 @@ public class DeviceController : BaseDeviceController
     /// <summary>当前设备</summary>
     public Device Device { get; set; }
 
+    private readonly MyDeviceService _deviceService;
     private readonly ThingService _thingService;
     private readonly ITracer _tracer;
 
@@ -32,6 +35,7 @@ public class DeviceController : BaseDeviceController
     /// <param name="tracer"></param>
     public DeviceController(IServiceProvider serviceProvider, ThingService thingService, ITracer tracer) : base(serviceProvider)
     {
+        _deviceService = serviceProvider.GetRequiredService<IDeviceService>() as MyDeviceService;
         _thingService = thingService;
         _tracer = tracer;
     }
@@ -62,6 +66,40 @@ public class DeviceController : BaseDeviceController
 
         return rs;
     }
+
+    /// <summary>长连接处理</summary>
+    /// <param name="socket"></param>
+    /// <param name="token"></param>
+    /// <returns></returns>
+    protected override async Task HandleNotify(WebSocket socket, String token)
+    {
+        DeviceOnline online = null;
+        var device = Device;
+        if (device != null)
+        {
+            // 上线打标记
+            online = _deviceService.GetOnline(device, UserHost);
+            if (online != null)
+            {
+                online.WebSocket = true;
+                online.Update();
+            }
+        }
+
+        try
+        {
+            await base.HandleNotify(socket, token);
+        }
+        finally
+        {
+            // 下线清除标记
+            if (online != null)
+            {
+                online.WebSocket = false;
+                online.Update();
+            }
+        }
+    }
     #endregion
 
     #region 设备通道
Modified +11 -0
diff --git a/Samples/IoTZero/Entity/IoT.htm b/Samples/IoTZero/Entity/IoT.htm
index 9841b83..4903f2e 100644
--- a/Samples/IoTZero/Entity/IoT.htm
+++ b/Samples/IoTZero/Entity/IoT.htm
@@ -783,6 +783,17 @@
         </tr>
 
         <tr>
+            <td>WebSocket</td>
+            <td>长连接</td>
+            <td>Boolean</td>
+            <td></td>
+            <td></td>
+            <td></td>
+            <td>N</td>
+            <td>WebSocket长连接</td>
+        </tr>
+
+        <tr>
             <td>Delay</td>
             <td>延迟</td>
             <td>Int32</td>
Modified +1 -0
diff --git a/Samples/IoTZero/Entity/Model.xml b/Samples/IoTZero/Entity/Model.xml
index 192b80f..56cd461 100644
--- a/Samples/IoTZero/Entity/Model.xml
+++ b/Samples/IoTZero/Entity/Model.xml
@@ -128,6 +128,7 @@
         <Column Name="IP" DataType="String" Length="200" Description="本地IP" />
         <Column Name="GroupPath" DataType="String" Description="分组" />
         <Column Name="Pings" DataType="Int32" Description="心跳" />
+        <Column Name="WebSocket" DataType="Boolean" Description="长连接。WebSocket长连接" />
         <Column Name="Delay" DataType="Int32" Description="延迟。网络延迟,单位ms" />
         <Column Name="Offset" DataType="Int32" Description="偏移。客户端时间减服务端时间,单位s" />
         <Column Name="LocalTime" DataType="DateTime" Description="本地时间" />
Modified +16 -0
diff --git "a/Samples/IoTZero/Entity/\350\256\276\345\244\207\345\234\250\347\272\277.cs" "b/Samples/IoTZero/Entity/\350\256\276\345\244\207\345\234\250\347\272\277.cs"
index 23168ac..9fc0382 100644
--- "a/Samples/IoTZero/Entity/\350\256\276\345\244\207\345\234\250\347\272\277.cs"
+++ "b/Samples/IoTZero/Entity/\350\256\276\345\244\207\345\234\250\347\272\277.cs"
@@ -88,6 +88,14 @@ public partial class DeviceOnline
     [BindColumn("Pings", "心跳", "")]
     public Int32 Pings { get => _Pings; set { if (OnPropertyChanging("Pings", value)) { _Pings = value; OnPropertyChanged("Pings"); } } }
 
+    private Boolean _WebSocket;
+    /// <summary>长连接。WebSocket长连接</summary>
+    [DisplayName("长连接")]
+    [Description("长连接。WebSocket长连接")]
+    [DataObjectField(false, false, false, 0)]
+    [BindColumn("WebSocket", "长连接。WebSocket长连接", "")]
+    public Boolean WebSocket { get => _WebSocket; set { if (OnPropertyChanging("WebSocket", value)) { _WebSocket = value; OnPropertyChanged("WebSocket"); } } }
+
     private Int32 _Delay;
     /// <summary>延迟。网络延迟,单位ms</summary>
     [DisplayName("延迟")]
@@ -177,6 +185,7 @@ public partial class DeviceOnline
             "IP" => _IP,
             "GroupPath" => _GroupPath,
             "Pings" => _Pings,
+            "WebSocket" => _WebSocket,
             "Delay" => _Delay,
             "Offset" => _Offset,
             "LocalTime" => _LocalTime,
@@ -200,6 +209,7 @@ public partial class DeviceOnline
                 case "IP": _IP = Convert.ToString(value); break;
                 case "GroupPath": _GroupPath = Convert.ToString(value); break;
                 case "Pings": _Pings = value.ToInt(); break;
+                case "WebSocket": _WebSocket = value.ToBoolean(); break;
                 case "Delay": _Delay = value.ToInt(); break;
                 case "Offset": _Offset = value.ToInt(); break;
                 case "LocalTime": _LocalTime = value.ToDateTime(); break;
@@ -246,6 +256,9 @@ public partial class DeviceOnline
         /// <summary>心跳</summary>
         public static readonly Field Pings = FindByName("Pings");
 
+        /// <summary>长连接。WebSocket长连接</summary>
+        public static readonly Field WebSocket = FindByName("WebSocket");
+
         /// <summary>延迟。网络延迟,单位ms</summary>
         public static readonly Field Delay = FindByName("Delay");
 
@@ -303,6 +316,9 @@ public partial class DeviceOnline
         /// <summary>心跳</summary>
         public const String Pings = "Pings";
 
+        /// <summary>长连接。WebSocket长连接</summary>
+        public const String WebSocket = "WebSocket";
+
         /// <summary>延迟。网络延迟,单位ms</summary>
         public const String Delay = "Delay";
 
Modified +2 -2
diff --git a/Samples/IoTZero/Services/MyDeviceService.cs b/Samples/IoTZero/Services/MyDeviceService.cs
index 9494145..720d54b 100644
--- a/Samples/IoTZero/Services/MyDeviceService.cs
+++ b/Samples/IoTZero/Services/MyDeviceService.cs
@@ -248,7 +248,7 @@ public class MyDeviceService : IDeviceService
     /// <param name="device"></param>
     /// <param name="ip"></param>
     /// <returns></returns>
-    protected virtual DeviceOnline GetOnline(Device device, String ip)
+    public virtual DeviceOnline GetOnline(Device device, String ip)
     {
         var sid = $"{device.Id}@{ip}";
         var olt = _cache.Get<DeviceOnline>($"DeviceOnline:{sid}");
@@ -265,7 +265,7 @@ public class MyDeviceService : IDeviceService
     /// <param name="device"></param>
     /// <param name="ip"></param>
     /// <returns></returns>
-    protected virtual DeviceOnline CreateOnline(Device device, String ip)
+    public virtual DeviceOnline CreateOnline(Device device, String ip)
     {
         var sid = $"{device.Id}@{ip}";
         var olt = DeviceOnline.GetOrAdd(sid);
Modified +6 -2
diff --git a/Samples/ZeroServer/Controllers/NodeController.cs b/Samples/ZeroServer/Controllers/NodeController.cs
index 982dee1..99b22c3 100644
--- a/Samples/ZeroServer/Controllers/NodeController.cs
+++ b/Samples/ZeroServer/Controllers/NodeController.cs
@@ -45,7 +45,6 @@ public class NodeController : BaseDeviceController
     }
     #endregion
 
-    #region 心跳
     /// <summary>心跳</summary>
     /// <param name="request"></param>
     /// <returns></returns>
@@ -60,12 +59,17 @@ public class NodeController : BaseDeviceController
         return rs;
     }
 
+    /// <summary>长连接处理</summary>
+    /// <param name="socket"></param>
+    /// <param name="token"></param>
+    /// <returns></returns>
     protected override async Task HandleNotify(WebSocket socket, String token)
     {
         NodeOnline online = null;
         var node = Node;
         if (node != null)
         {
+            // 上线打标记
             online = _nodeService.GetOnline(node, UserHost);
             if (online != null)
             {
@@ -80,6 +84,7 @@ public class NodeController : BaseDeviceController
         }
         finally
         {
+            // 下线清除标记
             if (online != null)
             {
                 online.WebSocket = false;
@@ -87,5 +92,4 @@ public class NodeController : BaseDeviceController
             }
         }
     }
-    #endregion
 }
\ No newline at end of file