Skip to content

Commit

Permalink
IoTZero在线表增加长连接字段
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Jun 30, 2024
1 parent 4c5f91b commit d6adf29
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public virtual IUpgradeInfo Upgrade(String? channel)
info.Source = new Uri(new Uri(uri), info.Source) + "";
}

return info;
return info!;
}
#endregion

Expand All @@ -188,7 +188,7 @@ public virtual async Task Notify()
HttpContext.Response.StatusCode = 400;
}

/// <summary>处理长连接</summary>
/// <summary>长连接处理</summary>
/// <param name="socket"></param>
/// <param name="token"></param>
/// <returns></returns>
Expand Down
40 changes: 39 additions & 1 deletion Samples/IoTZero/Controllers/DeviceController.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -7,6 +8,7 @@
using NewLife.Log;
using NewLife.Remoting;
using NewLife.Remoting.Extensions;
using NewLife.Remoting.Extensions.Services;
using NewLife.Remoting.Models;

namespace IoTZero.Controllers;
Expand All @@ -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;

Expand All @@ -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;
}
Expand Down Expand Up @@ -62,6 +66,40 @@ protected override IPingResponse OnPing(IPingRequest request)

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 设备通道
Expand Down
11 changes: 11 additions & 0 deletions Samples/IoTZero/Entity/IoT.htm
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,17 @@ <h3>设备在线(DeviceOnline)</h3>
<td></td>
</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>
Expand Down
1 change: 1 addition & 0 deletions Samples/IoTZero/Entity/Model.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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="本地时间" />
Expand Down
16 changes: 16 additions & 0 deletions Samples/IoTZero/Entity/设备在线.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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("延迟")]
Expand Down Expand Up @@ -177,6 +185,7 @@ public override Object this[String name]
"IP" => _IP,
"GroupPath" => _GroupPath,
"Pings" => _Pings,
"WebSocket" => _WebSocket,
"Delay" => _Delay,
"Offset" => _Offset,
"LocalTime" => _LocalTime,
Expand All @@ -200,6 +209,7 @@ public override Object this[String name]
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;
Expand Down Expand Up @@ -246,6 +256,9 @@ public partial class _
/// <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");

Expand Down Expand Up @@ -303,6 +316,9 @@ public partial class __
/// <summary>心跳</summary>
public const String Pings = "Pings";

/// <summary>长连接。WebSocket长连接</summary>
public const String WebSocket = "WebSocket";

/// <summary>延迟。网络延迟,单位ms</summary>
public const String Delay = "Delay";

Expand Down
4 changes: 2 additions & 2 deletions Samples/IoTZero/Services/MyDeviceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public IOnlineModel Ping(IDeviceModel device, IPingRequest? request, String toke
/// <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}");
Expand All @@ -265,7 +265,7 @@ protected virtual DeviceOnline GetOnline(Device device, String ip)
/// <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);
Expand Down
8 changes: 6 additions & 2 deletions Samples/ZeroServer/Controllers/NodeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ protected override Boolean OnAuthorize(String token)
}
#endregion

#region 心跳
/// <summary>心跳</summary>
/// <param name="request"></param>
/// <returns></returns>
Expand All @@ -60,12 +59,17 @@ protected override IPingResponse OnPing(IPingRequest? request)
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)
{
Expand All @@ -80,12 +84,12 @@ protected override async Task HandleNotify(WebSocket socket, String token)
}
finally
{
// 下线清除标记
if (online != null)
{
online.WebSocket = false;
online.Update();
}
}
}
#endregion
}

0 comments on commit d6adf29

Please sign in to comment.