添加Socket通信
This commit is contained in:
parent
e1f0bd4c7d
commit
28d7322ce7
@ -1,7 +0,0 @@
|
||||
namespace UIStandardWebApi.Entity
|
||||
{
|
||||
public class Class1
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
32
UIStandardWebApi.Entity/Sys_BaseModel.cs
Normal file
32
UIStandardWebApi.Entity/Sys_BaseModel.cs
Normal file
@ -0,0 +1,32 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace UIStandardWebApi.Entity
|
||||
{
|
||||
/// <summary>
|
||||
/// 公共基础类
|
||||
/// </summary>
|
||||
public abstract class Sys_BaseModel
|
||||
{
|
||||
/// <summary>
|
||||
/// 创建时间
|
||||
/// </summary>
|
||||
public DateTime CreateTime { get; set; } = DateTime.Now;
|
||||
|
||||
/// <summary>
|
||||
/// 修改时间
|
||||
/// </summary>
|
||||
[SugarColumn(IsNullable = true)]
|
||||
public DateTime? ModifyTime { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
///状态
|
||||
/// </summary>
|
||||
public int Status { set; get; }
|
||||
}
|
||||
}
|
||||
30
UIStandardWebApi.Entity/Sys_Socket.cs
Normal file
30
UIStandardWebApi.Entity/Sys_Socket.cs
Normal file
@ -0,0 +1,30 @@
|
||||
using SqlSugar;
|
||||
|
||||
namespace UIStandardWebApi.Entity
|
||||
{
|
||||
/// <summary>
|
||||
/// ip 端口号保存
|
||||
/// </summary>
|
||||
[SugarTable("Sys_Socket")]
|
||||
public class Sys_Socket : Sys_BaseModel
|
||||
{
|
||||
/// <summary>
|
||||
/// ID
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "Id", IsIdentity = true, IsPrimaryKey = true)]
|
||||
public int Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// iP
|
||||
/// </summary>
|
||||
[SugarColumn(IsNullable = true)]
|
||||
public string? IP { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 端口号
|
||||
/// </summary>
|
||||
[SugarColumn(IsNullable = true)]
|
||||
public string? Port { get; set; }
|
||||
|
||||
}
|
||||
}
|
||||
@ -6,4 +6,8 @@
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="SqlSugarCore" Version="5.1.4.188" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
24
UIStandardWebApi.EntityDto/BaseDTO.cs
Normal file
24
UIStandardWebApi.EntityDto/BaseDTO.cs
Normal file
@ -0,0 +1,24 @@
|
||||
namespace UIStandardWebApi.EntityDto
|
||||
{
|
||||
/// <summary>
|
||||
/// 基础类
|
||||
/// </summary>
|
||||
public abstract class BaseDTO
|
||||
{
|
||||
/// <summary>
|
||||
/// 创建时间
|
||||
/// </summary>
|
||||
public DateTime CreateTime { get; set; } = DateTime.Now;
|
||||
|
||||
/// <summary>
|
||||
/// 修改时间
|
||||
/// </summary>
|
||||
public DateTime? ModifyTime { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
///状态
|
||||
/// </summary>
|
||||
public int Status { set; get; }
|
||||
}
|
||||
}
|
||||
@ -1,7 +0,0 @@
|
||||
namespace UIStandardWebApi.EntityDto
|
||||
{
|
||||
public class Class1
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
29
UIStandardWebApi.EntityDto/Sys_SocketDto.cs
Normal file
29
UIStandardWebApi.EntityDto/Sys_SocketDto.cs
Normal file
@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace UIStandardWebApi.EntityDto
|
||||
{
|
||||
/// <summary>
|
||||
/// ip 端口号保存
|
||||
/// </summary>
|
||||
public class Sys_SocketDto : BaseDTO
|
||||
{
|
||||
/// <summary>
|
||||
/// ID
|
||||
/// </summary>
|
||||
public int Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// iP
|
||||
/// </summary>
|
||||
public string? IP { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 端口号
|
||||
/// </summary>
|
||||
public string? Port { get; set; }
|
||||
}
|
||||
}
|
||||
130
UIStandardWebApi.IService/IBaseService.cs
Normal file
130
UIStandardWebApi.IService/IBaseService.cs
Normal file
@ -0,0 +1,130 @@
|
||||
using SqlSugar;
|
||||
using System.Linq.Expressions;
|
||||
using UIStandardWebApi.Entity;
|
||||
|
||||
namespace UIStandardWebApi.IService
|
||||
{
|
||||
public interface IBaseService
|
||||
{
|
||||
#region Query
|
||||
/// <summary>
|
||||
/// 主键查询
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
T Find<T>(int id) where T : class;
|
||||
|
||||
/// <summary>
|
||||
/// 主键查询-异步版本
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
Task<T> FindAsync<T>(int id) where T : class;
|
||||
|
||||
/// <summary>
|
||||
/// 提供对单表的查询
|
||||
/// </summary>
|
||||
/// <returns>ISugarQueryable类型集合</returns>
|
||||
[Obsolete("尽量避免使用,using 带表达式目录树的 代替")]
|
||||
ISugarQueryable<T> Set<T>() where T : class;
|
||||
|
||||
/// <summary>
|
||||
/// 条件查询
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="funcWhere"></param>
|
||||
/// <returns></returns>
|
||||
ISugarQueryable<T> Query<T>(Expression<Func<T, bool>> funcWhere) where T : class;
|
||||
|
||||
/// <summary>
|
||||
/// 分页查询
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="funcWhere"></param>
|
||||
/// <param name="pageSize"></param>
|
||||
/// <param name="pageIndex"></param>
|
||||
/// <param name="funcOrderby"></param>
|
||||
/// <param name="isAsc"></param>
|
||||
/// <returns></returns>
|
||||
PagingData<T> QueryPage<T>(Expression<Func<T, bool>> funcWhere, int pageSize, int pageIndex, Expression<Func<T, object>> funcOrderby, bool isAsc = true) where T : class;
|
||||
#endregion
|
||||
|
||||
#region Add
|
||||
|
||||
/// <summary>
|
||||
/// 新增数据-同步版本
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="t"></param>
|
||||
/// <returns></returns>
|
||||
T Insert<T>(T t) where T : class, new();
|
||||
|
||||
/// <summary>
|
||||
/// 新增数据-异步版本
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="t"></param>
|
||||
/// <returns></returns>
|
||||
Task<T> InsertAsync<T>(T t) where T : class, new();
|
||||
|
||||
/// <summary>
|
||||
/// 批量新增
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="tList"></param>
|
||||
/// <returns></returns>
|
||||
Task<bool> InsertList<T>(List<T> tList) where T : class, new();
|
||||
#endregion
|
||||
|
||||
#region Update
|
||||
/// <summary>
|
||||
/// 更新数据
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="t"></param>
|
||||
/// <returns></returns>
|
||||
Task<bool> UpdateAsync<T>(T t) where T : class, new();
|
||||
|
||||
/// <summary>
|
||||
/// 更新数据,即时Commit
|
||||
/// </summary>
|
||||
/// <param name="tList"></param>
|
||||
void Update<T>(List<T> tList) where T : class, new();
|
||||
#endregion
|
||||
|
||||
#region Delete
|
||||
/// <summary>
|
||||
/// 根据主键删除数据
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="pId"></param>
|
||||
/// <returns></returns>
|
||||
bool Delete<T>(object pId) where T : class, new();
|
||||
|
||||
/// <su+mary>
|
||||
/// 删除数据,即时Commit
|
||||
/// </summary>
|
||||
/// <param name="t"></param>
|
||||
void Delete<T>(T t) where T : class, new();
|
||||
|
||||
/// <summary>
|
||||
/// 删除数据,即时Commit
|
||||
/// </summary>
|
||||
/// <param name="tList"></param>
|
||||
void Delete<T>(List<T> tList) where T : class;
|
||||
#endregion
|
||||
|
||||
#region Other
|
||||
|
||||
/// <summary>
|
||||
/// 执行sql 返回集合
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="sql"></param>
|
||||
/// <returns></returns>
|
||||
ISugarQueryable<T> ExcuteQuery<T>(string sql) where T : class, new();
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
29
UIStandardWebApi.IService/ISocketConnectionService.cs
Normal file
29
UIStandardWebApi.IService/ISocketConnectionService.cs
Normal file
@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UIStandardWebApi.Entity;
|
||||
|
||||
namespace UIStandardWebApi.IService
|
||||
{
|
||||
/// <summary>
|
||||
/// Socket通信连接接口
|
||||
/// </summary>
|
||||
public interface ISocketConnectionService : IBaseService
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取通信连接的ip及端口号
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public Sys_Socket ObtainAddress();
|
||||
|
||||
/// <summary>
|
||||
/// 新增通信ip及端口号
|
||||
/// </summary>
|
||||
/// <param name="ip"></param>
|
||||
/// <param name="port"></param>
|
||||
/// <returns></returns>
|
||||
public int InserAddress(string ip, string port);
|
||||
}
|
||||
}
|
||||
21
UIStandardWebApi.IService/PagingData.cs
Normal file
21
UIStandardWebApi.IService/PagingData.cs
Normal file
@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace UIStandardWebApi.IService
|
||||
{
|
||||
public class PagingData<T> where T : class
|
||||
{
|
||||
public int RecordCount { get; set; }
|
||||
|
||||
public int PageIndex { get; set; }
|
||||
|
||||
public int PageSize { get; set; }
|
||||
|
||||
public List<T>? DataList { get; set; }
|
||||
|
||||
public string? SearchString { get; set; }
|
||||
}
|
||||
}
|
||||
14
UIStandardWebApi.IService/UIStandardWebApi.IService.csproj
Normal file
14
UIStandardWebApi.IService/UIStandardWebApi.IService.csproj
Normal file
@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\UIStandardWebApi.EntityDto\UIStandardWebApi.EntityDto.csproj" />
|
||||
<ProjectReference Include="..\UIStandardWebApi.Entity\UIStandardWebApi.Entity.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
200
UIStandardWebApi.Service/BaseService.cs
Normal file
200
UIStandardWebApi.Service/BaseService.cs
Normal file
@ -0,0 +1,200 @@
|
||||
using SqlSugar;
|
||||
using System.Linq.Expressions;
|
||||
using UIStandardWebApi.Entity;
|
||||
using UIStandardWebApi.IService;
|
||||
|
||||
namespace UIStandardWebApi.Service
|
||||
{
|
||||
public abstract class BaseService : IBaseService
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
protected ISqlSugarClient _Client { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
public BaseService(ISqlSugarClient client)
|
||||
{
|
||||
_Client = client;
|
||||
}
|
||||
|
||||
#region Query
|
||||
|
||||
/// <summary>
|
||||
/// 主键查询
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
public T Find<T>(int id) where T : class
|
||||
{
|
||||
return _Client.Queryable<T>().InSingle(id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 主键查询-异步版本
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<T> FindAsync<T>(int id) where T : class
|
||||
{
|
||||
return await _Client.Queryable<T>().InSingleAsync(id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 不应该暴露给上端使用者,尽量少用
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
[Obsolete("尽量避免使用,using 带表达式目录树的代替")]
|
||||
public ISugarQueryable<T> Set<T>() where T : class
|
||||
{
|
||||
return _Client.Queryable<T>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 条件查询
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="funcWhere"></param>
|
||||
/// <returns></returns>
|
||||
public ISugarQueryable<T> Query<T>(Expression<Func<T, bool>> funcWhere) where T : class
|
||||
{
|
||||
return _Client.Queryable<T>().Where(funcWhere);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 分页查询
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="funcWhere"></param>
|
||||
/// <param name="pageSize"></param>
|
||||
/// <param name="pageIndex"></param>
|
||||
/// <param name="funcOrderby"></param>
|
||||
/// <param name="isAsc"></param>
|
||||
/// <returns></returns>
|
||||
public PagingData<T> QueryPage<T>(Expression<Func<T, bool>> funcWhere, int pageSize, int pageIndex, Expression<Func<T, object>> funcOrderby, bool isAsc = true) where T : class
|
||||
{
|
||||
var list = _Client.Queryable<T>();
|
||||
if (funcWhere != null)
|
||||
{
|
||||
list = list.Where(funcWhere);
|
||||
}
|
||||
list = list.OrderByIF(true, funcOrderby, isAsc ? OrderByType.Asc : OrderByType.Desc);
|
||||
PagingData<T> result = new PagingData<T>()
|
||||
{
|
||||
DataList = list.ToPageList(pageIndex, pageSize),
|
||||
PageIndex = pageIndex,
|
||||
PageSize = pageSize,
|
||||
RecordCount = list.Count(),
|
||||
};
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Insert
|
||||
|
||||
/// <summary>
|
||||
/// 新增数据-同步版本
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="t"></param>
|
||||
/// <returns></returns>
|
||||
public T Insert<T>(T t) where T : class, new()
|
||||
{
|
||||
return _Client.Insertable(t).ExecuteReturnEntity();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 新增数据-异步版本
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="t"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<T> InsertAsync<T>(T t) where T : class, new()
|
||||
{
|
||||
return await _Client.Insertable(t).ExecuteReturnEntityAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 批量新增-事务执行
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="tList"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<bool> InsertList<T>(List<T> tList) where T : class, new()
|
||||
{
|
||||
return await _Client.Insertable(tList.ToList()).ExecuteCommandIdentityIntoEntityAsync();
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Update
|
||||
/// <summary>
|
||||
/// 是没有实现查询,直接更新的,需要Attach和State
|
||||
///
|
||||
/// 如果是已经在context,只能再封装一个(在具体的service)
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="t"></param>
|
||||
public async Task<bool> UpdateAsync<T>(T t) where T : class, new()
|
||||
{
|
||||
if (t == null) throw new Exception("t is null");
|
||||
|
||||
return await _Client.Updateable(t).ExecuteCommandHasChangeAsync();
|
||||
}
|
||||
|
||||
public void Update<T>(List<T> tList) where T : class, new()
|
||||
{
|
||||
_Client.Updateable(tList).ExecuteCommand();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Delete
|
||||
/// <summary>
|
||||
/// 先附加 再删除
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="t"></param>
|
||||
public void Delete<T>(T t) where T : class, new()
|
||||
{
|
||||
_Client.Deleteable(t).ExecuteCommand();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据主键删除
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="pId"></param>
|
||||
/// <returns></returns>
|
||||
public bool Delete<T>(object pId) where T : class, new()
|
||||
{
|
||||
T t = _Client.Queryable<T>().InSingle(pId);
|
||||
return _Client.Deleteable<T>(t).ExecuteCommand() > 0;
|
||||
}
|
||||
|
||||
public void Delete<T>(List<T> tList) where T : class
|
||||
{
|
||||
_Client.Deleteable(tList).ExecuteCommand();
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Other
|
||||
ISugarQueryable<T> IBaseService.ExcuteQuery<T>(string sql) where T : class
|
||||
{
|
||||
return _Client.SqlQueryable<T>(sql);
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
if (_Client != null)
|
||||
{
|
||||
_Client.Dispose();
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
58
UIStandardWebApi.Service/SocketConnectionService.cs
Normal file
58
UIStandardWebApi.Service/SocketConnectionService.cs
Normal file
@ -0,0 +1,58 @@
|
||||
using AutoMapper;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UIStandardWebApi.Entity;
|
||||
using UIStandardWebApi.IService;
|
||||
|
||||
namespace UIStandardWebApi.Service
|
||||
{
|
||||
/// <summary>
|
||||
/// Socket通信连接服务层
|
||||
/// </summary>
|
||||
public class SocketConnectionService : BaseService, ISocketConnectionService
|
||||
{
|
||||
private readonly IMapper _IMapper;
|
||||
private readonly IMemoryCache _IMemoryCache;
|
||||
|
||||
public SocketConnectionService(ISqlSugarClient client, IMapper mapper, IMemoryCache iMemoryCache) : base(client)
|
||||
{
|
||||
_IMapper = mapper;
|
||||
_IMemoryCache = iMemoryCache;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取通信连接的ip及端口号
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public Sys_Socket ObtainAddress()
|
||||
{
|
||||
Sys_Socket socket = _Client.Queryable<Sys_Socket>() .OrderBy(c => c.Id, OrderByType.Desc).First();
|
||||
return socket;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 新增通信信息
|
||||
/// </summary>
|
||||
/// <param name="ip"></param>
|
||||
/// <param name="port"></param>
|
||||
/// <returns></returns>
|
||||
public int InserAddress(string ip, string port)
|
||||
{
|
||||
int inItSocketId = 0;
|
||||
inItSocketId = _Client.Insertable<Sys_Socket>(new Sys_Socket()
|
||||
{
|
||||
IP = ip,
|
||||
Port = port,
|
||||
}).ExecuteReturnIdentity();
|
||||
|
||||
return inItSocketId;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
21
UIStandardWebApi.Service/UIStandardWebApi.Service.csproj
Normal file
21
UIStandardWebApi.Service/UIStandardWebApi.Service.csproj
Normal file
@ -0,0 +1,21 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoMapper" Version="14.0.0" />
|
||||
<PackageReference Include="CSRedisCore" Version="3.8.804" />
|
||||
<PackageReference Include="log4net" Version="3.0.4" />
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="9.0.3" />
|
||||
<PackageReference Include="SqlSugarCore" Version="5.1.4.188" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\UIStandardWebApi.IService\UIStandardWebApi.IService.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@ -4,6 +4,9 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UIStandardWebApi.Entity;
|
||||
using UIStandardWebApi.EntityDto;
|
||||
using UIStandardWebApi.IService;
|
||||
|
||||
namespace UIStandardWebApi.WebCore.AutoMapExtend
|
||||
{
|
||||
@ -17,7 +20,8 @@ namespace UIStandardWebApi.WebCore.AutoMapExtend
|
||||
/// </summary>
|
||||
public AutoMapperConfigs()
|
||||
{
|
||||
|
||||
CreateMap<Sys_Socket, Sys_SocketDto>();
|
||||
CreateMap<PagingData<Sys_Socket>, PagingData<Sys_SocketDto>>().ReverseMap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -13,4 +13,10 @@
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\UIStandardWebApi.EntityDto\UIStandardWebApi.EntityDto.csproj" />
|
||||
<ProjectReference Include="..\UIStandardWebApi.Entity\UIStandardWebApi.Entity.csproj" />
|
||||
<ProjectReference Include="..\UIStandardWebApi.IService\UIStandardWebApi.IService.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@ -11,6 +11,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UIStandardWebApi.Entity", "
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UIStandardWebApi.EntityDto", "UIStandardWebApi.EntityDto\UIStandardWebApi.EntityDto.csproj", "{F8D34A35-F030-48FF-927A-F370F803D46A}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UIStandardWebApi.IService", "UIStandardWebApi.IService\UIStandardWebApi.IService.csproj", "{4A5263D2-FAD5-4707-AF6E-5733830AABEE}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UIStandardWebApi.Service", "UIStandardWebApi.Service\UIStandardWebApi.Service.csproj", "{F8BE6A09-AC85-4F0D-BD20-DF379FA82059}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@ -33,6 +37,14 @@ Global
|
||||
{F8D34A35-F030-48FF-927A-F370F803D46A}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{F8D34A35-F030-48FF-927A-F370F803D46A}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{F8D34A35-F030-48FF-927A-F370F803D46A}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{4A5263D2-FAD5-4707-AF6E-5733830AABEE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{4A5263D2-FAD5-4707-AF6E-5733830AABEE}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{4A5263D2-FAD5-4707-AF6E-5733830AABEE}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{4A5263D2-FAD5-4707-AF6E-5733830AABEE}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{F8BE6A09-AC85-4F0D-BD20-DF379FA82059}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{F8BE6A09-AC85-4F0D-BD20-DF379FA82059}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{F8BE6A09-AC85-4F0D-BD20-DF379FA82059}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{F8BE6A09-AC85-4F0D-BD20-DF379FA82059}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
||||
@ -6,6 +6,6 @@
|
||||
<!--数据库连接信息-->
|
||||
<connectionStrings>
|
||||
<!--<add name="db" connectionString="Data Source=10.20.98.142;Initial Catalog=InSituLaboratoryWeb;User ID=sa;Password=zttZTT123!;MultipleActiveResultSets=true" />-->
|
||||
<add name="db" connectionString="Data Source= NB10920;Initial Catalog=InSituLaboratoryWeb;User ID=sa;Password=zttZTT123;MultipleActiveResultSets=true" />
|
||||
<add name="db" connectionString="server=127.0.0.1;port=3306;user=root;password=zttZTT1234;database=20250418_UIStandardWebApi;Allow User Variables=True;" />
|
||||
</connectionStrings>
|
||||
</configuration>
|
||||
21
UIStandardWebApi/Common/PagingData.cs
Normal file
21
UIStandardWebApi/Common/PagingData.cs
Normal file
@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace UIStandardWebApi.Common
|
||||
{
|
||||
public class PagingData<T> where T : class
|
||||
{
|
||||
public int RecordCount { get; set; }
|
||||
|
||||
public int PageIndex { get; set; }
|
||||
|
||||
public int PageSize { get; set; }
|
||||
|
||||
public List<T>? DataList { get; set; }
|
||||
|
||||
public string? SearchString { get; set; }
|
||||
}
|
||||
}
|
||||
@ -7,6 +7,11 @@ namespace UIStandardWebApi.Common.SocketModel
|
||||
/// </summary>
|
||||
public static class SocketStatic
|
||||
{
|
||||
/// <summary>
|
||||
/// Socket静态类
|
||||
/// </summary>
|
||||
public static Client _client = new Client();
|
||||
|
||||
/// <summary>
|
||||
/// 定义数据解析类
|
||||
/// </summary>
|
||||
|
||||
@ -0,0 +1,127 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using UIStandardWebApi.Common.SocketModel;
|
||||
using UIStandardWebApi.Common;
|
||||
using UIStandardWebApi.WebCore.SwaggerExtend;
|
||||
using UIStandardWebApi.IService;
|
||||
using UIStandardWebApi.Entity;
|
||||
using System.Text;
|
||||
using AutoMapper;
|
||||
|
||||
namespace UIStandardWebApi.Controllers.Communication
|
||||
{
|
||||
/// <summary>
|
||||
/// Socket通信控制器
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
[ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
|
||||
public class SocketConnectionController : ControllerBase
|
||||
{
|
||||
/// <summary>
|
||||
/// 查询最新一次的通信配置
|
||||
/// </summary>
|
||||
/// <param name="socketConnectionService"></param>
|
||||
/// <param name="mapper"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
[Route("ObtainAddress")]
|
||||
public async Task<JsonResult> ObtainAddressAsync([FromServices] ISocketConnectionService socketConnectionService, [FromServices] IMapper mapper)
|
||||
{
|
||||
Sys_Socket sys_Socket = socketConnectionService.ObtainAddress();
|
||||
|
||||
return await Task.FromResult(new JsonResult(new ApiDataResult<Sys_Socket>() { Data = sys_Socket, Success = false, Message = "成功返回最新一次的通信配置查询" }));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Socket连接
|
||||
/// </summary>
|
||||
[HttpGet]
|
||||
[Route("GetSocket/{ip}/{port}")]
|
||||
public async Task<JsonResult> SocketConnectionAsync([FromServices] ISocketConnectionService socketConnectionService, string ip, string port)
|
||||
{
|
||||
//连接Socket
|
||||
SocketStatic._client.InitSocket(ip, Convert.ToInt32(port));
|
||||
bool isConnected = SocketStatic._client.Connect();
|
||||
if (isConnected)
|
||||
{
|
||||
int dateid = 0;
|
||||
Sys_Socket sys_Socket = socketConnectionService.ObtainAddress();
|
||||
//判断所连接的ip及端口号是否为上一次所连接的,如果不是则新增一条
|
||||
if (sys_Socket == null)
|
||||
{
|
||||
dateid = socketConnectionService.InserAddress(ip, port);
|
||||
}
|
||||
else if (!ip.Equals(sys_Socket.IP) || !port.Equals(sys_Socket.Port))
|
||||
{
|
||||
dateid = socketConnectionService.InserAddress(ip, port);
|
||||
}
|
||||
|
||||
SocketStatic._client.pushSockets = ReceiveMess;//注册推送器
|
||||
return await Task.FromResult(new JsonResult(new ApiResult() { Success = true, Message = "Socket通信连接成功" }));
|
||||
}
|
||||
else
|
||||
{
|
||||
return await Task.FromResult(new JsonResult(new ApiResult() { Success = false, Message = "Socket通信连接失败,请检查IP及端口号后重新尝试!" }));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Socket断开连接
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
[Route("Stop")]
|
||||
public async Task<JsonResult> SocketConnectionStopAsync()
|
||||
{
|
||||
SocketStatic._client.Stop();
|
||||
return await Task.FromResult(new JsonResult(new ApiResult() { Success = true, Message = "Socket通信连接已断开" }));
|
||||
}
|
||||
|
||||
|
||||
#region 客户端数据接收
|
||||
|
||||
/// <summary>
|
||||
/// 客户端数据接收
|
||||
/// </summary>
|
||||
/// <param name="sks"></param>
|
||||
private void ReceiveMess(Sockets sks)
|
||||
{
|
||||
if (sks.ex != null)
|
||||
{
|
||||
if (sks.ClientDispose == true)
|
||||
{
|
||||
//由于未知原因引发异常.导致客户端下线. 比如网络故障.或服务器断开连接.
|
||||
//wirte_textbox(string.Format("客户端下线.!异常消息:{0}\r\n", sks.ex));
|
||||
}
|
||||
else
|
||||
{
|
||||
//SetClientState(string.Format("异常消息:{0}\r\n", sks.ex));
|
||||
}
|
||||
//timerConnect.Enabled = true;
|
||||
}
|
||||
else if (sks.Offset == 0)
|
||||
{
|
||||
//客户端主动下线
|
||||
//wirte_textbox("客户端下线!");
|
||||
}
|
||||
else
|
||||
{
|
||||
byte[] buffer = new byte[sks.Offset];
|
||||
Array.Copy(sks.RecBuffer, buffer, sks.Offset);
|
||||
string str = Encoding.UTF8.GetString(buffer);
|
||||
|
||||
try
|
||||
{
|
||||
SocketStatic.dataParsing.ParsingData(new List<byte>(buffer));
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
@ -1,35 +0,0 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using UIStandardWebApi.WebCore.SwaggerExtend;
|
||||
|
||||
namespace UIStandardWebApi.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
|
||||
[Route("api/[controller]")]
|
||||
public class WeatherForecastController : ControllerBase
|
||||
{
|
||||
private static readonly string[] Summaries = new[]
|
||||
{
|
||||
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
|
||||
};
|
||||
|
||||
private readonly ILogger<WeatherForecastController> _logger;
|
||||
|
||||
public WeatherForecastController(ILogger<WeatherForecastController> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
[HttpGet(Name = "GetWeatherForecast")]
|
||||
public IEnumerable<WeatherForecast> Get()
|
||||
{
|
||||
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
|
||||
{
|
||||
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
|
||||
TemperatureC = Random.Shared.Next(-20, 55),
|
||||
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
|
||||
})
|
||||
.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -29,6 +29,7 @@ namespace UIStandardWebApi
|
||||
//Automapper×¢²á
|
||||
builder.Services.AddAutoMapper(typeof(AutoMapperConfigs));
|
||||
|
||||
|
||||
builder.Services.AddMemoryCache();
|
||||
|
||||
//¿çÓò
|
||||
|
||||
@ -24,7 +24,7 @@ namespace UIStandardWebApi.SqlSuggar
|
||||
|
||||
ConnectionConfig connection = new ConnectionConfig()
|
||||
{
|
||||
DbType = DbType.SqlServer,
|
||||
DbType = DbType.MySql,
|
||||
IsAutoCloseConnection = true,
|
||||
ConnectionString = connectionString
|
||||
};
|
||||
|
||||
@ -21,7 +21,7 @@ namespace UIStandardWebApi.SqlSuggar
|
||||
ConnectionConfig connection = new ConnectionConfig()
|
||||
{
|
||||
ConnectionString = customConnectionConfig.ConnectionString,
|
||||
DbType = DbType.SqlServer,
|
||||
DbType = DbType.MySql,
|
||||
IsAutoCloseConnection = true,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
SlaveConnectionConfigs = customConnectionConfig.SlaveConnectionConfigs.Select(c => new SlaveConnectionConfig() { ConnectionString = c.ConnectionString, HitRate = c.CustomHitRate }).ToList()
|
||||
|
||||
@ -23,7 +23,10 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\UIStandardWebApi.EntityDto\UIStandardWebApi.EntityDto.csproj" />
|
||||
<ProjectReference Include="..\UIStandardWebApi.Entity\UIStandardWebApi.Entity.csproj" />
|
||||
<ProjectReference Include="..\UIStandardWebApi.IService\UIStandardWebApi.IService.csproj" />
|
||||
<ProjectReference Include="..\UIStandardWebApi.Service\UIStandardWebApi.Service.csproj" />
|
||||
<ProjectReference Include="..\UIStandardWebApi.WebCore\UIStandardWebApi.WebCore.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@ -3,6 +3,8 @@ using Newtonsoft.Json;
|
||||
using System.Text.Encodings.Web;
|
||||
using System.Text.Unicode;
|
||||
using UIStandardWebApi.Common.JwtService;
|
||||
using UIStandardWebApi.IService;
|
||||
using UIStandardWebApi.Service;
|
||||
|
||||
namespace UIStandardWebApi.Utility.RegisterExt
|
||||
{
|
||||
@ -17,7 +19,7 @@ namespace UIStandardWebApi.Utility.RegisterExt
|
||||
/// <param name="builder"></param>
|
||||
public static void RegistService(this WebApplicationBuilder builder)
|
||||
{
|
||||
//builder.Services.AddTransient<ISys_SamplingService, Sys_SamplingService>();
|
||||
builder.Services.AddTransient<ISocketConnectionService, SocketConnectionService>();
|
||||
|
||||
|
||||
builder.Services.Configure<JWTTokenOptions>(builder.Configuration.GetSection("JWTTokenOptions"));
|
||||
|
||||
@ -1,13 +0,0 @@
|
||||
namespace UIStandardWebApi
|
||||
{
|
||||
public class WeatherForecast
|
||||
{
|
||||
public DateOnly Date { get; set; }
|
||||
|
||||
public int TemperatureC { get; set; }
|
||||
|
||||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
|
||||
|
||||
public string? Summary { get; set; }
|
||||
}
|
||||
}
|
||||
@ -6,13 +6,13 @@
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*",
|
||||
"IsInitDatabase": "0",
|
||||
"IsInitDatabase": "1",
|
||||
"Socket": {
|
||||
"ip": "192.168.1.254",
|
||||
"port": "8899"
|
||||
},
|
||||
"ConnectionStrings": {
|
||||
"ConnectionString": "Data Source = NB10920;Initial Catalog=InSituLaboratoryWeb;User ID=sa;Password=zttZTT123;MultipleActiveResultSets=true",
|
||||
"ConnectionString": "server = 127.0.0.1;port = 3306;user = root;password = zttZTT1234;database = 20250418_UIStandardWebApi;Allow User Variables = True;",
|
||||
"DbType": 1
|
||||
},
|
||||
"JWTTokenOptions": {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user