Files
jinko.yikatong/Src/Dlhis.Basic/Device/Services/Implements/DeviceConnectService.cs
liguoyang f8d1997ddd Squashed commit of the following:
commit 528cdd51c05d26437f0b23f415e06311b667e9f7
Author: liguoyang <Ligy.97@foxmail.com>
Date:   Wed Apr 17 15:37:30 2024 +0800

    feat 访客预约

commit e73a3fcd9307f86aba2c39578495c03cde1d41f9
Author: liguoyang <Ligy.97@foxmail.com>
Date:   Tue Apr 16 16:15:16 2024 +0800

    feat 业务curd

commit b98b6e2764fea875866e11f790f388ccf3c234b5
Merge: a3dcec3 965e84b
Author: liguoyang <Ligy.97@foxmail.com>
Date:   Tue Apr 16 14:56:30 2024 +0800

    Merge branch 'feat-visitorAppointments' of https://git.dlhis.com/liguoyang/jinko.yikatong into feat-visitorAppointments

commit a3dcec30e1289fbb09de8f85009d3931d971337a
Author: liguoyang <Ligy.97@foxmail.com>
Date:   Fri Apr 12 17:57:49 2024 +0800

    fix appsettings

commit 766fbec0c45a3852a75a7a37686b885ac3b0a77a
Author: liguoyang <Ligy.97@foxmail.com>
Date:   Fri Apr 12 17:37:01 2024 +0800

    add dockerfile

commit 86731d41fee97ba073065ed1b807987760a371a7
Author: liguoyang <Ligy.97@foxmail.com>
Date:   Sun Apr 14 20:11:56 2024 +0800

    fix 测试websocket链接

commit 1301b126a9ba9a7d97cf45967cf2915e1169c505
Author: liguoyang <Ligy.97@foxmail.com>
Date:   Fri Apr 12 17:57:49 2024 +0800

    fix appsettings

commit 081f3a67896c67891c3f80edde82a440b8736851
Author: liguoyang <Ligy.97@foxmail.com>
Date:   Fri Apr 12 17:37:01 2024 +0800

    add dockerfile

commit 271b6c5898b5732a35ccfa0629ce89442ac49143
Author: liguoyang <Ligy.97@foxmail.com>
Date:   Fri Apr 12 17:34:26 2024 +0800

    test 提交测试代码

commit 965e84b6f3b7f925f8021c9b996cce961b896dea
Merge: 578eb6a 6a6fad1
Author: liguoyang <Ligy.97@foxmail.com>
Date:   Mon Apr 15 17:06:45 2024 +0800

    Merge branch 'feat-visitorAppointments' of https://git.dlhis.com/liguoyang/jinko.yikatong into feat-visitorAppointments

commit 578eb6afbd96f0bfe4aaf8058fd4743309c74d62
Author: liguoyang <Ligy.97@foxmail.com>
Date:   Sun Apr 14 20:11:56 2024 +0800

    fix 测试websocket链接

commit 114511f1daea64b7dbf9c3c38b73e28b6d0301e6
Author: liguoyang <Ligy.97@foxmail.com>
Date:   Fri Apr 12 17:57:49 2024 +0800

    fix appsettings

commit 5eacfa8af8087eade8553266a12a2cf42305716b
Author: liguoyang <Ligy.97@foxmail.com>
Date:   Fri Apr 12 17:37:01 2024 +0800

    add dockerfile

commit b2ffff1f099be343d89690014d05cb859776acbb
Author: liguoyang <Ligy.97@foxmail.com>
Date:   Fri Apr 12 17:34:26 2024 +0800

    test 提交测试代码

commit 6a6fad1d1b8772eebf5df12cd74cfd5c955ff560
Author: liguoyang <Ligy.97@foxmail.com>
Date:   Sun Apr 14 20:11:56 2024 +0800

    fix 测试websocket链接

commit 9ce3c4d6f63957c175d65370d9a6b80e10f4254f
Author: liguoyang <Ligy.97@foxmail.com>
Date:   Fri Apr 12 17:57:49 2024 +0800

    fix appsettings

commit c637fd89850cf289d7da457c6c6f47354b5beed3
Author: liguoyang <Ligy.97@foxmail.com>
Date:   Fri Apr 12 17:37:01 2024 +0800

    add dockerfile

commit 58c39815b1c45a99aea0caf503fcb1365c957f03
Author: liguoyang <Ligy.97@foxmail.com>
Date:   Fri Apr 12 17:34:26 2024 +0800

    test 提交测试代码
2024-04-20 15:29:52 +08:00

165 lines
5.8 KiB
C#

using System.Net;
using System.Net.Sockets;
using Dlhis.Basic.Device.Request;
using Dlhis.Basic.Device.Response;
using Dlhis.Entity.Basic.Entity;
using Dlhis.Entity.Device.Entities;
using Dlhis.Entity.Device.Enums;
using Kurisu.AspNetCore.Utils.Extensions;
using Kurisu.Core.CustomClass;
using Kurisu.SqlSugar.Services;
using Mapster;
namespace Dlhis.Basic.Device.Services.Implements;
public class DeviceConnectService : IDeviceConnectService
{
private readonly IDbContext _dbContext;
public DeviceConnectService(IDbContext dbContext)
{
_dbContext = dbContext;
}
public async Task<string> ResigterAsync(DeviceResigterRequest request)
{
var businessPlace = await _dbContext.Queryable<BusinessPlaceEntity>().Where(x => x.CustomCode == request.BusinessCustomCode).FirstAsync();
businessPlace.ThrowIfNull("营业点不存在");
var device = await GetEnableDeviceAsync(request.UniqueNo, false, request.DeployPlace, businessPlace.TenantId);
var registry = await _dbContext.Queryable<DeviceRegistryEntity>().Where(x => x.DeviceUniqueNo == request.UniqueNo).FirstAsync();
if (registry == null)
{
registry = new DeviceRegistryEntity()
{
DeviceUniqueNo = request.UniqueNo,
LastHeartbeatTime = DateTime.Now,
RegisterTime = DateTime.Now,
BusinessPlaceCode = businessPlace?.Code,
TenantId = device.TenantId,
CreatedBy = 1,
CreateTime = DateTime.Now,
ModifiedBy = 1,
ModifiedTime = DateTime.Now,
};
await _dbContext.InsertAsync(registry);
return "设备注册成功";
}
return "请勿重复注册设备";
}
public async Task<List<DeviceNotSetBusinessPlaceResponse>> GetNotSetBusinessPlaceDevicesAsync()
{
var devices = await _dbContext.Queryable<DeviceEntity>().Where(x => x.Enable).ToListAsync();
devices = devices.Where(x => x.Type == DeviceTypeEnumType.Meal).ToList();
return devices.Adapt<List<DeviceNotSetBusinessPlaceResponse>>();
}
public async Task SetResigterBusinessPlaceAsync(DeviceSetResigterBusinessPlaceRequest request)
{
var device = await GetEnableDeviceAsync(request.UniqueNo);
(device.Type != DeviceTypeEnumType.Meal).ThrowIfTrue("非就餐类型设备,无法设置");
var registry = await _dbContext.Queryable<DeviceRegistryEntity>().Where(x => x.DeviceUniqueNo == request.UniqueNo).FirstAsync();
registry.ThrowIfNull($"{request.UniqueNo}的设备注册信息不存在,无法设置营业点");
if (request.BusinessPlaceCode.HasValue)
{
var businessPlace = await _dbContext.Queryable<BusinessPlaceEntity>().Where(x => x.Code == request.BusinessPlaceCode).FirstAsync();
businessPlace.ThrowIfNull("营业点不存在,无法设置到设备注册信息中");
}
//更新营业点信息
registry.BusinessPlaceCode = request.BusinessPlaceCode;
await _dbContext.UpdateAsync(registry);
}
public async Task HeartbeatAsync(DeviceHeartbeatRequest request)
{
var registry = await _dbContext.Queryable<DeviceRegistryEntity>().Where(x => x.DeviceUniqueNo == request.UniqueNo).FirstAsync();
registry.ThrowIfNull($"注册信息不存在,无法发起心跳");
registry.LastHeartbeatTime = DateTime.Now;
await _dbContext.UpdateAsync(registry);
}
public async Task<bool> TryConnectAsync(DeviceConnectionTryRequest request)
{
try
{
var ipInfo = request.ConnectionInfo.ToObject<IPPortDto>();
//超时时间
var CancellationTokenSource = new CancellationTokenSource(300);
await new TcpClient().ConnectAsync(IPAddress.Parse(ipInfo.IP), ipInfo.Port, CancellationTokenSource.Token);
return true;
}
catch (Exception)
{
return false;
}
}
//******************************************************** help methods **********************************************************
/// <summary>
/// 获取启用的设备
/// </summary>
/// <param name="uniqueNo"></param>
/// <param name="checkExists"></param>
/// <param name="deployPlace"></param>
/// <param name="tenantId"></param>
/// <returns></returns>
private async Task<DeviceEntity> GetEnableDeviceAsync(string uniqueNo, bool checkExists = true, string deployPlace = "", string tenantId = "")
{
var device = await _dbContext.Queryable<DeviceEntity>()
.Where(x => x.UniqueNo == uniqueNo)
.FirstAsync();
if (checkExists)
{
device.ThrowIfNull($"未知的设备唯一编码{uniqueNo}");
device.Enable.ThrowIfFalse("设备尚未启用");
}
else
{
if (device == null)
{
device = new DeviceEntity()
{
Code = Guid.NewGuid(),
ConnectionInfo = string.Empty,
TemplateCode = null,
UniqueNo = uniqueNo,
TenantId = tenantId,
Supplier = string.Empty,
Funcs = new List<DeviceFuncEnumType>(),
DeployPlace = deployPlace,
HeartbeatInterval = 0,
DeployDirection = string.Empty,
Enable = true,
DeployPosition = string.Empty,
CreatedBy = 1,
CreateTime = DateTime.Now,
ModifiedBy = 1,
ModifiedTime = DateTime.Now,
};
await _dbContext.InsertAsync(device);
}
}
return device;
}
}