mirror of
http://git.xinwangdao.com/cnnc-embedded-parts-detect/detect.git
synced 2025-06-24 05:24:12 +08:00
Device client
This commit is contained in:
parent
335ea7c121
commit
25fd499773
@ -4,6 +4,10 @@ namespace detect.device;
|
||||
|
||||
public class DeviceEvent
|
||||
{
|
||||
public const string EventAnalyzerData = "event_analyzer_data";
|
||||
public const string EventDeviceConnected = "event_device_connected";
|
||||
public const string EventDeviceDisConnected = "event_device_disconnected";
|
||||
|
||||
public string Name { get; set; }
|
||||
public IDictionary<string, object> Result { get; set; }
|
||||
}
|
||||
|
@ -42,11 +42,19 @@ public class DeviceClientSocket : TcpClient, IDeviceClient
|
||||
protected override void OnConnected()
|
||||
{
|
||||
Log.Information("({address}:{port})OnConnected.", Address, Port);
|
||||
PublishEvent(new DeviceEvent
|
||||
{
|
||||
Name = detect.device.DeviceEvent.EventDeviceConnected
|
||||
});
|
||||
}
|
||||
|
||||
protected override void OnDisconnected()
|
||||
{
|
||||
Log.Information("({address}:{port})OnDisconnected", Address, Port);
|
||||
PublishEvent(new DeviceEvent
|
||||
{
|
||||
Name = detect.device.DeviceEvent.EventDeviceDisConnected
|
||||
});
|
||||
Thread.Sleep(1000);
|
||||
if (!_stop)
|
||||
ConnectAsync();
|
||||
|
@ -36,6 +36,17 @@ public class DeviceClientService
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 断开所有连接
|
||||
/// </summary>
|
||||
public void DisConnectAllDevices()
|
||||
{
|
||||
foreach (var (key, client) in _deviceClients)
|
||||
{
|
||||
client.DisConnectAsync();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据设备id获取连接客户端
|
||||
@ -48,6 +59,19 @@ public class DeviceClientService
|
||||
return deviceClient;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 刷新连接客户端,设备信息更新后调用
|
||||
/// </summary>
|
||||
/// <param name="deviceId"></param>
|
||||
/// <returns></returns>
|
||||
public IDeviceClient? RefreshDeviceClient(long deviceId)
|
||||
{
|
||||
_deviceClients.TryGetValue(deviceId, out var deviceClient);
|
||||
deviceClient?.DisConnectAsync();
|
||||
var eventService = Locator.Current.GetService<DetectDeviceService>()!;
|
||||
return ConnectDevice(eventService.ListById(deviceId).Result!);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 连接云台设备
|
||||
/// </summary>
|
||||
@ -55,33 +79,34 @@ public class DeviceClientService
|
||||
/// <returns></returns>
|
||||
public IDeviceClient? ConnectDevice(DeviceEntity device)
|
||||
{
|
||||
// if (string.IsNullOrEmpty(device.DeviceIp) || string.IsNullOrEmpty(device.DevicePort))
|
||||
// {
|
||||
// Log.Warning("设备【{Name}】没有配置ip和端口,不连接", device.Name);
|
||||
// return null;
|
||||
// }
|
||||
if (string.IsNullOrEmpty(device.DeviceIp))
|
||||
{
|
||||
Log.Warning("设备【{Name}】没有配置ip,不连接", device.Name);
|
||||
return null;
|
||||
}
|
||||
_deviceClients.TryGetValue(device.Id!.Value, out var deviceClient);
|
||||
// if (deviceClient == null)
|
||||
// {
|
||||
// deviceClient = new DeviceClientSocket(device.DeviceIp!, Convert.ToInt32(device.DevicePort));
|
||||
// deviceClient.ConnectAsync();
|
||||
// _deviceClients.Add(device.Id!.Value, deviceClient);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// if (!deviceClient.Connected())
|
||||
// {
|
||||
// deviceClient.ConnectAsync();
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// deviceClient.DeviceEvent += DeviceClientOnDeviceEvent;
|
||||
//
|
||||
if (deviceClient == null)
|
||||
{
|
||||
deviceClient = new DeviceClientSocket(device.DeviceIp!, 13000);
|
||||
deviceClient.ConnectAsync();
|
||||
_deviceClients.Add(device.Id!.Value, deviceClient);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!deviceClient.Connected())
|
||||
{
|
||||
deviceClient.ConnectAsync();
|
||||
}
|
||||
}
|
||||
|
||||
deviceClient.DeviceEvent += DeviceClientOnDeviceEvent;
|
||||
|
||||
return deviceClient;
|
||||
}
|
||||
|
||||
private void DeviceClientOnDeviceEvent(object? sender, DeviceEvent e)
|
||||
{
|
||||
Log.Information("DeviceEvent-[{Name}]:{@Result}", e.Name, e.Result);
|
||||
// var deviceService = Locator.Current.GetService<VapDeviceService>()!;
|
||||
// var eventService = Locator.Current.GetService<VapEventService>()!;
|
||||
// var constantService = Locator.Current.GetService<VapConstantService>()!;
|
||||
|
Loading…
Reference in New Issue
Block a user