diff --git a/HighWayIot.Log4net/LogHelper.cs b/HighWayIot.Log4net/LogHelper.cs index 971f5f2..64cd586 100644 --- a/HighWayIot.Log4net/LogHelper.cs +++ b/HighWayIot.Log4net/LogHelper.cs @@ -32,7 +32,7 @@ namespace HighWayIot.Log4net * */ //private readonly string fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log4net.config"); - private readonly string fileName = "Z:\\Desktop\\日常代码\\HighWayIot\\HighWayIot.Log4net\\config\\log4net.config"; + private readonly string fileName = "\\config\\log4net.config"; private LogHelper() { diff --git a/HighWayIot.Repository/HighWayIot.Repository.csproj b/HighWayIot.Repository/HighWayIot.Repository.csproj index 8943229..6a6bc5a 100644 --- a/HighWayIot.Repository/HighWayIot.Repository.csproj +++ b/HighWayIot.Repository/HighWayIot.Repository.csproj @@ -39,8 +39,8 @@ ..\HighWayIot.Library\Oracle.ManagedDataAccess.dll - - Z:\Desktop\日常代码\HighWayIot\HighWayIot.Library\SqlSugar.dll + + ..\packages\SqlSugar.5.1.4.130\lib\SqlSugar.dll @@ -59,17 +59,17 @@ - - - + + - - - - - - + + + + + + + @@ -84,6 +84,7 @@ + \ No newline at end of file diff --git a/HighWayIot.Repository/SqlSugarHelper.cs b/HighWayIot.Repository/SqlSugarHelper.cs index 9bdadd9..899f7da 100644 --- a/HighWayIot.Repository/SqlSugarHelper.cs +++ b/HighWayIot.Repository/SqlSugarHelper.cs @@ -26,7 +26,7 @@ namespace HighWayIot.Repository /** * Mysql */ - private static string mysqlConnStr = "Data Source=127.0.0.1;Port=6000;Initial Catalog=ry-cloud;uid=root;pwd=123456;Charset=utf8mb4;SslMode=none"; + private static string mysqlConnStr = "Data Source=127.0.0.1;Port=3306;Initial Catalog=rfid;uid=root;pwd=123456;Charset=utf8mb4;SslMode=none"; //private static string oracleConnStr = "Data Source=175.27.215.92/helowin;User ID=aucma_mes;Password=aucma"; #endregion diff --git a/HighWayIot.Repository/app.config b/HighWayIot.Repository/app.config index c00df4d..cd04901 100644 --- a/HighWayIot.Repository/app.config +++ b/HighWayIot.Repository/app.config @@ -22,6 +22,10 @@ + + + + \ No newline at end of file diff --git a/HighWayIot.Repository/domain/BaseBomInfo.cs b/HighWayIot.Repository/domain/BaseBomInfo.cs deleted file mode 100644 index da881fb..0000000 --- a/HighWayIot.Repository/domain/BaseBomInfo.cs +++ /dev/null @@ -1,22 +0,0 @@ -using SqlSugar; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace HighWayIot.Repository.domain -{ - [SugarTable("BASE_BOMINFO")] - public class BaseBomInfo - { - /// - /// 主键标识 - /// - [SugarColumn(ColumnName = "OBJID", IsPrimaryKey = true, IsIdentity = true)] - public int ObjId { get; set; } - - [SugarColumn(ColumnName = "BOM_CODE")] - public string bomCode { get; set; } - } -} diff --git a/HighWayIot.Repository/domain/BaseDeviceinfo.cs b/HighWayIot.Repository/domain/BaseDeviceinfo.cs deleted file mode 100644 index 168d352..0000000 --- a/HighWayIot.Repository/domain/BaseDeviceinfo.cs +++ /dev/null @@ -1,59 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using SqlSugar; -namespace HighWayIot.Repository.domain -{ - /// - /// 设备信息 - /// - [SugarTable("base_deviceInfo")] - public class BaseDeviceinfo - { - /// - /// 主键标识 - /// - [SugarColumn(ColumnName="objId" ,IsPrimaryKey = true ,IsIdentity = true )] - public int ObjId { get; set; } - /// - /// 机台编号 - /// - [SugarColumn(ColumnName="process_Id" )] - public int? ProcessId { get; set; } - /// - /// 位置编号 - /// - [SugarColumn(ColumnName="position_Id" )] - public int? PositionId { get; set; } - /// - /// 设备编号 - /// - [SugarColumn(ColumnName="device_Id" )] - public int? DeviceId { get; set; } - /// - /// 设备名称 - /// - [SugarColumn(ColumnName="device_Name" )] - public string DeviceName { get; set; } - /// - /// 设备 IP - /// - [SugarColumn(ColumnName="device_Ip" )] - public string DeviceIp { get; set; } - /// - /// 设备端口 - /// - [SugarColumn(ColumnName="device_Port" )] - public int? DevicePort { get; set; } - /// - /// 设备天线 - /// - [SugarColumn(ColumnName="device_Ant" )] - public int? DeviceAnt { get; set; } - /// - /// 设备类型 - /// - [SugarColumn(ColumnName="device_Type" )] - public string DeviceType { get; set; } - } -} diff --git a/HighWayIot.Repository/domain/RFIDContent.cs b/HighWayIot.Repository/domain/RFIDContent.cs new file mode 100644 index 0000000..9e7ee05 --- /dev/null +++ b/HighWayIot.Repository/domain/RFIDContent.cs @@ -0,0 +1,28 @@ +using SqlSugar; +using System; + +namespace HighWayIot.Repository.domain +{ + + [SugarTable("rfid_content")] + public class RFIDContent + { + + [SugarColumn(ColumnName = "id", IsPrimaryKey = true, IsIdentity = true)] + public int ID { get; set; } + + [SugarColumn(ColumnName = "content")] + public string Content { get; set; } + + [SugarColumn(ColumnName = "deviceno")] + public int DeviceNo { get; set; } + + [SugarColumn(ColumnName = "readkind")] + public string ReadKind { get; set; } + + [SugarColumn(ColumnName = "logtime")] + public DateTime LogTime { get; set; } + + } + +} \ No newline at end of file diff --git a/HighWayIot.Repository/domain/RFIDHeartbeat.cs b/HighWayIot.Repository/domain/RFIDHeartbeat.cs new file mode 100644 index 0000000..97854b6 --- /dev/null +++ b/HighWayIot.Repository/domain/RFIDHeartbeat.cs @@ -0,0 +1,22 @@ +using SqlSugar; +using System; + +namespace HighWayIot.Repository.domain +{ + + [SugarTable("rfid_heartbeat")] + public class RFIDHeartbeat + { + + [SugarColumn(ColumnName = "id", IsPrimaryKey = true, IsIdentity = true)] + public int ID { get; set; } + + [SugarColumn(ColumnName = "deviceno")] + public int DeviceNo { get; set; } + + [SugarColumn(ColumnName = "beattime")] + public DateTime BeatTime { get; set; } + + } + +} \ No newline at end of file diff --git a/HighWayIot.Repository/domain/RFIDState.cs b/HighWayIot.Repository/domain/RFIDState.cs new file mode 100644 index 0000000..33c5326 --- /dev/null +++ b/HighWayIot.Repository/domain/RFIDState.cs @@ -0,0 +1,24 @@ +using SqlSugar; +using System; + +namespace HighWayIot.Repository.domain +{ + + [SugarTable("rfid_state")] + public class RFIDState + { + + [SugarColumn(ColumnName = "id", IsPrimaryKey = true, IsIdentity = true)] + public int ID { get; set; } + + [SugarColumn(ColumnName = "deviceno")] + public int DeviceNo { get; set; } + + [SugarColumn(ColumnName = "devicestate")] + public bool DeviceState { get; set; } + + [SugarColumn(ColumnName = "logtime")] + public DateTime LogTime { get; set; } + } + +} \ No newline at end of file diff --git a/HighWayIot.Repository/domain/SysUserInfo.cs b/HighWayIot.Repository/domain/SysUserInfo.cs deleted file mode 100644 index 9421e5d..0000000 --- a/HighWayIot.Repository/domain/SysUserInfo.cs +++ /dev/null @@ -1,37 +0,0 @@ -using SqlSugar; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Security.Principal; -using System.Text; -using System.Threading.Tasks; - -namespace HighWayIot.Repository.domain -{ - /// - /// 用户信息 - /// - [SugarTable("sys_user")] - public class SysUserInfo - { - /// - /// 用户Id,自增主键 - /// - [SugarColumn(ColumnName = "user_id", IsPrimaryKey = true, IsIdentity = true)] - public int userId { get; set; } - - /// - /// 用户名称 - /// - [SugarColumn(ColumnName = "user_name")] - public string userName { get; set; } - - /// - /// 用户密码 - /// - [SugarColumn(ColumnName = "password")] - public string password { get; set; } - - } -} - diff --git a/HighWayIot.Repository/packages.config b/HighWayIot.Repository/packages.config new file mode 100644 index 0000000..dfa0de5 --- /dev/null +++ b/HighWayIot.Repository/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/HighWayIot.Repository/service/IBaseBomInfoService.cs b/HighWayIot.Repository/service/IBaseBomInfoService.cs deleted file mode 100644 index bb83271..0000000 --- a/HighWayIot.Repository/service/IBaseBomInfoService.cs +++ /dev/null @@ -1,14 +0,0 @@ -using HighWayIot.Repository.domain; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace HighWayIot.Repository.service -{ - public interface IBaseBomInfoService - { - List GetBomInfos(); - } -} diff --git a/HighWayIot.Repository/service/ISysUserInfoService.cs b/HighWayIot.Repository/service/IContentService.cs similarity index 50% rename from HighWayIot.Repository/service/ISysUserInfoService.cs rename to HighWayIot.Repository/service/IContentService.cs index 0540e05..730fbb8 100644 --- a/HighWayIot.Repository/service/ISysUserInfoService.cs +++ b/HighWayIot.Repository/service/IContentService.cs @@ -7,13 +7,19 @@ using System.Threading.Tasks; namespace HighWayIot.Repository.service { - public interface ISysUserInfoService + public interface IContentService { /// - /// 获取用户列表 + /// 获取RFID信息列表 /// /// - List GetUserInfos( ); + List GetContentInfos( ); + + /// + /// 新增一条信息 + /// + /// + void AddContentInfo(RFIDContent content); } } diff --git a/HighWayIot.Repository/service/IHeartbeatService.cs b/HighWayIot.Repository/service/IHeartbeatService.cs new file mode 100644 index 0000000..ae56aef --- /dev/null +++ b/HighWayIot.Repository/service/IHeartbeatService.cs @@ -0,0 +1,33 @@ +using HighWayIot.Repository.domain; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HighWayIot.Repository.service +{ + public interface IHeartbeatService + { + + /// + /// 获取RFID心跳状态列表 + /// + /// + List GetHeartbeatInfos(); + + /// + /// 新增一条心跳 + /// + /// + void AddHeartbeatInfo(RFIDHeartbeat heartbeat); + + /// + /// 更新对应ID的心跳 + /// + /// + /// 影响的行数 + int UpdateHeartbeatInfo(int deviceno); + + } +} diff --git a/HighWayIot.Repository/service/IBaseDeviceinfoService.cs b/HighWayIot.Repository/service/IStateService.cs similarity index 51% rename from HighWayIot.Repository/service/IBaseDeviceinfoService.cs rename to HighWayIot.Repository/service/IStateService.cs index 5d410e9..1b1c6e1 100644 --- a/HighWayIot.Repository/service/IBaseDeviceinfoService.cs +++ b/HighWayIot.Repository/service/IStateService.cs @@ -7,14 +7,19 @@ using System.Threading.Tasks; namespace HighWayIot.Repository.service { - public interface IBaseDeviceinfoService + public interface IStateService { - /// - /// 根据工序编号获取设备集合 - /// - /// - /// - List GetDeviceInfoListByProcessId(int ProcessId); + /// + /// 获取RFID状态列表 + /// + /// + ListGetStateInfos(); + + /// + /// 新增一条状态 + /// + /// + void AddStateInfo(RFIDState state); } } diff --git a/HighWayIot.Repository/service/Impl/BaseBomInfoServiceImpl.cs b/HighWayIot.Repository/service/Impl/BaseBomInfoServiceImpl.cs deleted file mode 100644 index 8764b80..0000000 --- a/HighWayIot.Repository/service/Impl/BaseBomInfoServiceImpl.cs +++ /dev/null @@ -1,30 +0,0 @@ -using HighWayIot.Log4net; -using HighWayIot.Repository.domain; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace HighWayIot.Repository.service.Impl -{ - public class BaseBomInfoServiceImpl:IBaseBomInfoService - { - Repository _bomInfoRepository => new Repository("aucma_mes"); - - private LogHelper logHelper = LogHelper.Instance; - - public List GetBomInfos() - { - try - { - var info = _bomInfoRepository.GetList(); - return info; - }catch(Exception ex) - { - logHelper.Error("获取BOM集合异常", ex); - return null; - } - } - } -} diff --git a/HighWayIot.Repository/service/Impl/BaseContentServiceImpl.cs b/HighWayIot.Repository/service/Impl/BaseContentServiceImpl.cs new file mode 100644 index 0000000..3ce5168 --- /dev/null +++ b/HighWayIot.Repository/service/Impl/BaseContentServiceImpl.cs @@ -0,0 +1,42 @@ +using HighWayIot.Log4net; +using HighWayIot.Repository.domain; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Text; +using System.Threading.Tasks; + +namespace HighWayIot.Repository.service.Impl +{ + public class BaseContentServiceImpl : IContentService + { + private LogHelper log = LogHelper.Instance; + Repository _repository => new Repository("mysql"); + + public List GetContentInfos() + { + try + { + List deviceInfo = _repository.GetList(); + return deviceInfo; + }catch (Exception ex) + { + log.Error("RFID内容信息获取异常", ex); + return null; + } + } + + public void AddContentInfo(RFIDContent content) + { + try + { + _repository.Insert(content); + } + catch (Exception ex) + { + log.Error("RFID内容信息插入异常", ex); + } + } + } +} diff --git a/HighWayIot.Repository/service/Impl/BaseDeviceinfoServiceImpl.cs b/HighWayIot.Repository/service/Impl/BaseDeviceinfoServiceImpl.cs deleted file mode 100644 index 6b76af9..0000000 --- a/HighWayIot.Repository/service/Impl/BaseDeviceinfoServiceImpl.cs +++ /dev/null @@ -1,23 +0,0 @@ -using HighWayIot.Repository.domain; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Linq.Expressions; -using System.Text; -using System.Threading.Tasks; - -namespace HighWayIot.Repository.service.Impl -{ - public class BaseDeviceinfoServiceImpl : IBaseDeviceinfoService - { - Repository _deviceInfoRepository => new Repository("mysql"); - - - public List GetDeviceInfoListByProcessId(int ProcessId) - { - Expression> exp = s1 => s1.ProcessId == ProcessId; - List deviceinfos = _deviceInfoRepository.GetList(exp); - return deviceinfos; - } - } -} diff --git a/HighWayIot.Repository/service/Impl/BaseHeartbeatServiceImpl.cs b/HighWayIot.Repository/service/Impl/BaseHeartbeatServiceImpl.cs new file mode 100644 index 0000000..4da17a9 --- /dev/null +++ b/HighWayIot.Repository/service/Impl/BaseHeartbeatServiceImpl.cs @@ -0,0 +1,55 @@ +using HighWayIot.Log4net; +using HighWayIot.Repository.domain; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Text; +using System.Threading.Tasks; + +namespace HighWayIot.Repository.service.Impl +{ + public class BaseHeartbeatServiceImpl : IHeartbeatService + { + private LogHelper log = LogHelper.Instance; + Repository _repository => new Repository("mysql"); + + public List GetHeartbeatInfos() + { + try + { + List deviceInfo = _repository.GetList(); + return deviceInfo; + }catch (Exception ex) + { + log.Error("RFID心跳信息获取异常", ex); + return null; + } + } + + public void AddHeartbeatInfo(RFIDHeartbeat heartbeat) + { + try + { + _repository.Insert(heartbeat); + } + catch (Exception ex) + { + log.Error("RFID心跳信息插入异常", ex); + } + } + + public int UpdateHeartbeatInfo(int deviceno) + { + try + { + return _repository.AsUpdateable().SetColumns(x => x.BeatTime == DateTime.Now).Where(x => x.DeviceNo == deviceno).ExecuteCommand(); + } + catch(Exception ex) + { + log.Error("RFID心跳信息更新异常", ex); + return -1; + } + } + } +} diff --git a/HighWayIot.Repository/service/Impl/BaseStateServiceImpl.cs b/HighWayIot.Repository/service/Impl/BaseStateServiceImpl.cs new file mode 100644 index 0000000..cbaf529 --- /dev/null +++ b/HighWayIot.Repository/service/Impl/BaseStateServiceImpl.cs @@ -0,0 +1,42 @@ +using HighWayIot.Log4net; +using HighWayIot.Repository.domain; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Text; +using System.Threading.Tasks; + +namespace HighWayIot.Repository.service.Impl +{ + public class BaseStateServiceImpl : IStateService + { + private LogHelper log = LogHelper.Instance; + Repository _repository => new Repository("mysql"); + + public List GetStateInfos() + { + try + { + List deviceInfo = _repository.GetList(); + return deviceInfo; + }catch (Exception ex) + { + log.Error("RFID状态信息获取异常", ex); + return null; + } + } + + public void AddStateInfo(RFIDState state) + { + try + { + _repository.Insert(state); + } + catch (Exception ex) + { + log.Error("RFID状态信息插入异常", ex); + } + } + } +} diff --git a/HighWayIot.Repository/service/Impl/BaseSysUserInfoServiceImpl.cs b/HighWayIot.Repository/service/Impl/BaseSysUserInfoServiceImpl.cs deleted file mode 100644 index f0c84d5..0000000 --- a/HighWayIot.Repository/service/Impl/BaseSysUserInfoServiceImpl.cs +++ /dev/null @@ -1,30 +0,0 @@ -using HighWayIot.Log4net; -using HighWayIot.Repository.domain; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Linq.Expressions; -using System.Text; -using System.Threading.Tasks; - -namespace HighWayIot.Repository.service.Impl -{ - public class BaseSysUserInfoServiceImpl : ISysUserInfoService - { - private LogHelper log = LogHelper.Instance; - Repository _repository => new Repository("mysql"); - - public List GetUserInfos() - { - try - { - List deviceInfo = _repository.GetList(); - return deviceInfo; - }catch (Exception ex) - { - log.Error("用户信息获取异常", ex); - return null; - } - } - } -} diff --git a/HighWayIot.TouchSocket/BufferAnalysis.cs b/HighWayIot.TouchSocket/BufferAnalysis.cs new file mode 100644 index 0000000..51f435a --- /dev/null +++ b/HighWayIot.TouchSocket/BufferAnalysis.cs @@ -0,0 +1,69 @@ +using HighWayIot.Repository.domain; +using HighWayIot.Repository.service.Impl; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HighWayIot.TouchSocket +{ + public class BufferAnalysis + { + /// + /// 心跳报文分析 + /// + /// + public static void HeartbeatSocket(byte[] bytes) + { + string a = Encoding.ASCII.GetString(bytes, 4, 4); + int deviceno; + if(int.TryParse(a, out deviceno)) + { + BaseHeartbeatServiceImpl sql = new BaseHeartbeatServiceImpl(); + if(sql.UpdateHeartbeatInfo(deviceno) == 0) + { + RFIDHeartbeat heartbeat = new RFIDHeartbeat() + { + DeviceNo = deviceno, + BeatTime = DateTime.Now, + }; + sql.AddHeartbeatInfo(heartbeat); + } + } + } + + /// + /// RFID发送设备状态 + /// + /// + public static void RFIDStatusSocket(byte[] bytes) + { + + } + + /// + /// RFID发送条码 + /// + /// + public static void RFIDCodeSocket(byte[] bytes) + { + + } + + /// + /// 将一个数组拆成另外一个数组 + /// + /// 原始数组,被拆分的数组 + /// 从原始数组第几个元素开始 + /// 目标数组的长度 + /// 目标数组开始的元素序号,默认为0 + /// + public static byte[] SplitByteArray(byte[] originbyte, int oringinstartindex, int destbytelength, int destbytestartindex = 0) + { + byte[] result = new byte[destbytelength]; + System.Array.Copy(originbyte, oringinstartindex, result, destbytestartindex, destbytelength); + return result; + } + } +} diff --git a/HighWayIot.TouchSocket/HighWayIot.TouchSocket.csproj b/HighWayIot.TouchSocket/HighWayIot.TouchSocket.csproj index c9b3f8a..f3eedfa 100644 --- a/HighWayIot.TouchSocket/HighWayIot.TouchSocket.csproj +++ b/HighWayIot.TouchSocket/HighWayIot.TouchSocket.csproj @@ -51,7 +51,8 @@ - + + @@ -61,5 +62,15 @@ + + + {DEABC30C-EC6F-472E-BD67-D65702FDAF74} + HighWayIot.Log4net + + + {d0dc3cfb-6748-4d5e-b56a-76fdc72ab4b3} + HighWayIot.Repository + + \ No newline at end of file diff --git a/HighWayIot.TouchSocket/SocketConnect.cs b/HighWayIot.TouchSocket/SocketConnect.cs deleted file mode 100644 index cf6cf3a..0000000 --- a/HighWayIot.TouchSocket/SocketConnect.cs +++ /dev/null @@ -1,44 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using TouchSocket.Core; -using TouchSocket.Sockets; - -namespace HighWayIot.TouchSocket -{ - public class SocketConnect - { - - public void SocketMonitor() - { - var service = new TcpService(); - service.Connecting = (client, e) => { return EasyTask.CompletedTask; };//有客户端正在连接 - service.Connected = (client, e) => { return EasyTask.CompletedTask; };//有客户端成功连接 - service.Disconnecting = (client, e) => { return EasyTask.CompletedTask; };//有客户端正在断开连接,只有当主动断开时才有效。 - service.Disconnected = (client, e) => { return EasyTask.CompletedTask; };//有客户端断开连接 - service.Received = (client, e) => - { - //从客户端收到信息 - var mes = Encoding.UTF8.GetString(e.ByteBlock.Buffer, 0, e.ByteBlock.Len);//注意:数据长度是byteBlock.Len - client.Logger.Info($"已从{client.Id}接收到信息:{mes}"); - - return EasyTask.CompletedTask; - }; - - service.Setup(new TouchSocketConfig()//载入配置 - .SetListenIPHosts("192.168.11.211:6501", "192.168.11.212:6502", "192.168.11.213:6503", "192.168.11.214:6504")//同时监听两个地址 - .ConfigureContainer(a =>//容器的配置顺序应该在最前面 - { - a.AddConsoleLogger();//添加一个控制台日志注入(注意:在maui中控制台日志不可用) - }) - .ConfigurePlugins(a => - { - //a.Add();//此处可以添加插件 - })); - - service.Start();//启动 - } - } -} diff --git a/HighWayIot.TouchSocket/TcpServer.cs b/HighWayIot.TouchSocket/TcpServer.cs new file mode 100644 index 0000000..1ac14da --- /dev/null +++ b/HighWayIot.TouchSocket/TcpServer.cs @@ -0,0 +1,142 @@ +using HighWayIot.Log4net; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TouchSocket.Core; +using TouchSocket.Sockets; + +namespace HighWayIot.TouchSocket +{ + public class TcpServer + { + private static readonly Lazy lazy = new Lazy(() => new TcpServer()); + + public static TcpServer Instance => lazy.Value; + + private static LogHelper logHelper = LogHelper.Instance; + + public ServerState State + { + get => service.ServerState; + private set => State = value; + } + + TcpService service = new TcpService(); + + public bool ServerStart(int port) + { + try + { + service.Connecting = (client, e) => + { + logHelper.Info($"客户端{client.IP}正在连接"); + return EasyTask.CompletedTask; + };//有客户端正在连接 + service.Connected = (client, e) => + { + logHelper.Info($"客户端{client.IP}成功连接"); + return EasyTask.CompletedTask; + };//有客户端成功连接 + service.Disconnecting = (client, e) => + { + logHelper.Info($"客户端{client.IP}正在断开连接"); + return EasyTask.CompletedTask; + };//有客户端正在断开连接,只有当主动断开时才有效。 + service.Disconnected = (client, e) => + { + logHelper.Info($"客户端{client.IP}断开连接"); + return EasyTask.CompletedTask; + };//有客户端断开连接 + service.Received = (client, e) => + { + //从客户端收到信息 + var mes = Encoding.ASCII.GetString(e.ByteBlock.Buffer, 0, e.ByteBlock.Len);//注意:数据长度是byteBlock.Len + logHelper.Info($"已从{client.IP}:{client.Port}接收到信息:{mes}"); + + BufferMemory(BufferAnalysis.SplitByteArray(e.ByteBlock.Buffer, 0, e.ByteBlock.Len)); + + return EasyTask.CompletedTask; + }; + + service.Setup(new TouchSocketConfig()//载入配置 + .SetListenOptions(option => + { + option.Add(new TcpListenOption() + { + IpHost = port, + Name = "Server",//名称用于区分监听 + ServiceSslOption = null,//可以针对当前监听,单独启用ssl加密 + Adapter = () => new NormalDataHandlingAdapter(),//可以单独对当前地址监听,配置适配器 + //还有其他可配置项,都是单独对当前地址有效。 + }); + }) + .ConfigureContainer(a =>//容器的配置顺序应该在最前面 + { + a.AddConsoleLogger();//添加一个控制台日志注入(注意:在maui中控制台日志不可用) + }) + .ConfigurePlugins(a => + { + //a.Add();//此处可以添加插件 + })); + service.Start();//启动 + logHelper.Info("监听服务启动成功"); + return true; + } + catch (Exception ex) + { + logHelper.Error("监听服务启动失败! 错误代码" + ex.ToString()); + return false; + } + } + + public bool ServerStop() + { + try + { + service.Stop(); + logHelper.Info("监听服务关闭成功!"); + return true; + } + catch (Exception ex) + { + logHelper.Error("监听服务关闭失败! 错误代码" + ex.ToString()); + return false; + } + } + + public bool ServerDispose() + { + try + { + service.Dispose(); + logHelper.Info("监听服务释放成功!"); + return true; + } + catch (Exception ex) + { + logHelper.Error("监听服务释放失败! 错误代码" + ex.ToString()); + return false; + } + } + + private void BufferMemory(byte[] bytes) + { + byte[] identify = new byte[2] { bytes[1], bytes[2] }; + if (identify[0] == 0x30 && identify[1] == 0x31) + { + BufferAnalysis.HeartbeatSocket(bytes); + } + else if (identify[0] == 0x30 && identify[1] == 0x34) + { + BufferAnalysis.RFIDStatusSocket(bytes); + } + else if (identify[0] == 0x35 && identify[1] == 0x33) + { + BufferAnalysis.RFIDCodeSocket(bytes); + } + } + + } +} diff --git a/HighWayIot.TouchSocket/app.config b/HighWayIot.TouchSocket/app.config index a0cefc2..bdef95f 100644 --- a/HighWayIot.TouchSocket/app.config +++ b/HighWayIot.TouchSocket/app.config @@ -6,6 +6,18 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/HighWayIot/App.config b/HighWayIot/App.config index d91651e..bee5618 100644 --- a/HighWayIot/App.config +++ b/HighWayIot/App.config @@ -25,6 +25,10 @@ + + + + diff --git a/RFIDSocket/App.config b/RFIDSocket/App.config index 193aecc..5147b5e 100644 --- a/RFIDSocket/App.config +++ b/RFIDSocket/App.config @@ -1,6 +1,22 @@ - + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/RFIDSocket/RFIDSocket.Designer.cs b/RFIDSocket/RFIDSocket.Designer.cs index 0b1c6d4..513b1da 100644 --- a/RFIDSocket/RFIDSocket.Designer.cs +++ b/RFIDSocket/RFIDSocket.Designer.cs @@ -28,20 +28,152 @@ /// private void InitializeComponent() { + this.components = new System.ComponentModel.Container(); + this.dataGridView1 = new System.Windows.Forms.DataGridView(); + this.groupBox1 = new System.Windows.Forms.GroupBox(); + this.SetPort = new System.Windows.Forms.Button(); + this.label1 = new System.Windows.Forms.Label(); + this.Port212Text = new System.Windows.Forms.TextBox(); + this.dataGridView2 = new System.Windows.Forms.DataGridView(); + this.MonitorOnOff = new System.Windows.Forms.Button(); + this.label4 = new System.Windows.Forms.Label(); + this.MonitorState = new System.Windows.Forms.Label(); + this.TableTimer = new System.Windows.Forms.Timer(this.components); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit(); + this.groupBox1.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView2)).BeginInit(); this.SuspendLayout(); // + // dataGridView1 + // + this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.dataGridView1.Location = new System.Drawing.Point(12, 129); + this.dataGridView1.Name = "dataGridView1"; + this.dataGridView1.RowHeadersWidth = 51; + this.dataGridView1.RowTemplate.Height = 27; + this.dataGridView1.Size = new System.Drawing.Size(458, 923); + this.dataGridView1.TabIndex = 0; + // + // groupBox1 + // + this.groupBox1.Controls.Add(this.SetPort); + this.groupBox1.Controls.Add(this.label1); + this.groupBox1.Controls.Add(this.Port212Text); + this.groupBox1.Location = new System.Drawing.Point(12, 12); + this.groupBox1.Name = "groupBox1"; + this.groupBox1.Size = new System.Drawing.Size(260, 111); + this.groupBox1.TabIndex = 1; + this.groupBox1.TabStop = false; + this.groupBox1.Text = "端口设置"; + // + // SetPort + // + this.SetPort.Location = new System.Drawing.Point(63, 55); + this.SetPort.Name = "SetPort"; + this.SetPort.Size = new System.Drawing.Size(130, 50); + this.SetPort.TabIndex = 6; + this.SetPort.Text = "确认端口号"; + this.SetPort.UseVisualStyleBackColor = true; + this.SetPort.Click += new System.EventHandler(this.SetPort_Click); + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(28, 29); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(52, 15); + this.label1.TabIndex = 1; + this.label1.Text = "端口号"; + // + // Port212Text + // + this.Port212Text.Location = new System.Drawing.Point(86, 24); + this.Port212Text.MaxLength = 6; + this.Port212Text.Name = "Port212Text"; + this.Port212Text.Size = new System.Drawing.Size(144, 25); + this.Port212Text.TabIndex = 0; + this.Port212Text.Text = "1234"; + // + // dataGridView2 + // + this.dataGridView2.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.dataGridView2.Location = new System.Drawing.Point(476, 12); + this.dataGridView2.Name = "dataGridView2"; + this.dataGridView2.RowHeadersWidth = 51; + this.dataGridView2.RowTemplate.Height = 27; + this.dataGridView2.Size = new System.Drawing.Size(458, 1040); + this.dataGridView2.TabIndex = 2; + // + // MonitorOnOff + // + this.MonitorOnOff.Location = new System.Drawing.Point(278, 12); + this.MonitorOnOff.Name = "MonitorOnOff"; + this.MonitorOnOff.Size = new System.Drawing.Size(192, 57); + this.MonitorOnOff.TabIndex = 7; + this.MonitorOnOff.Text = "启动监听"; + this.MonitorOnOff.UseVisualStyleBackColor = true; + this.MonitorOnOff.Click += new System.EventHandler(this.MonitorOnOff_Click); + // + // label4 + // + this.label4.AutoSize = true; + this.label4.Location = new System.Drawing.Point(321, 92); + this.label4.Name = "label4"; + this.label4.Size = new System.Drawing.Size(82, 15); + this.label4.TabIndex = 8; + this.label4.Text = "监听状态:"; + // + // MonitorState + // + this.MonitorState.AutoSize = true; + this.MonitorState.BackColor = System.Drawing.Color.Transparent; + this.MonitorState.Location = new System.Drawing.Point(400, 92); + this.MonitorState.Name = "MonitorState"; + this.MonitorState.Size = new System.Drawing.Size(22, 15); + this.MonitorState.TabIndex = 9; + this.MonitorState.Text = "关"; + // + // TableTimer + // + this.TableTimer.Enabled = true; + this.TableTimer.Interval = 1000; + this.TableTimer.Tick += new System.EventHandler(this.TableTimer_Tick); + // // RFIDSocket // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(1643, 1064); + this.ClientSize = new System.Drawing.Size(949, 1064); + this.Controls.Add(this.MonitorState); + this.Controls.Add(this.label4); + this.Controls.Add(this.MonitorOnOff); + this.Controls.Add(this.dataGridView2); + this.Controls.Add(this.groupBox1); + this.Controls.Add(this.dataGridView1); this.Name = "RFIDSocket"; this.Text = "小件日志"; + this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.RFIDSocket_FormClosing); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit(); + this.groupBox1.ResumeLayout(false); + this.groupBox1.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView2)).EndInit(); this.ResumeLayout(false); + this.PerformLayout(); } #endregion + + private System.Windows.Forms.DataGridView dataGridView1; + private System.Windows.Forms.GroupBox groupBox1; + private System.Windows.Forms.Button SetPort; + private System.Windows.Forms.Label label1; + private System.Windows.Forms.TextBox Port212Text; + private System.Windows.Forms.DataGridView dataGridView2; + private System.Windows.Forms.Button MonitorOnOff; + private System.Windows.Forms.Label label4; + private System.Windows.Forms.Label MonitorState; + private System.Windows.Forms.Timer TableTimer; } } diff --git a/RFIDSocket/RFIDSocket.cs b/RFIDSocket/RFIDSocket.cs index 1f42266..76fd447 100644 --- a/RFIDSocket/RFIDSocket.cs +++ b/RFIDSocket/RFIDSocket.cs @@ -1,20 +1,108 @@ -using System; +using HighWayIot.TouchSocket; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; +using System.Runtime.Remoting.Channels; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; +using TouchSocket.Sockets; namespace RFIDSocket { public partial class RFIDSocket : Form { + private static TcpServer Server = TcpServer.Instance; + + int Port; + public RFIDSocket() { InitializeComponent(); + InitAction(); + } + + private void InitAction() + { + if (Server.State != ServerState.Running) + { + MonitorState.Text = "关"; + MonitorState.BackColor = Color.Yellow; + } + else if (Server.State == ServerState.Running) + { + MonitorState.Text = "开"; + MonitorState.BackColor = Color.LightGreen; + } + + Port = Convert.ToInt32(Port212Text.Text); + } + + private void MonitorOnOff_Click(object sender, EventArgs e) + { + if (Server.State != ServerState.Running) + { + if (Server.ServerStart(Port)) + { + MessageBox.Show("监听服务启动成功!"); + } + else + { + MessageBox.Show("监听服务启动失败!"); + } + } + else if (Server.State == ServerState.Running) + { + if (Server.ServerStop()) + { + MessageBox.Show("监听服务关闭成功!"); + } + else + { + MessageBox.Show("监听服务关闭失败!"); + } + } + } + + private void SetPort_Click(object sender, EventArgs e) + { + Port = Convert.ToInt32(Port212Text.Text); + } + + private void TableTimer_Tick(object sender, EventArgs e) + { + if (Server.State != ServerState.Running) + { + MonitorOnOff.Text = "启动监听"; + MonitorState.Text = "关"; + MonitorState.BackColor = Color.Yellow; + } + else if (Server.State == ServerState.Running) + { + MonitorOnOff.Text = "关闭监听"; + MonitorState.Text = "开"; + MonitorState.BackColor = Color.LightGreen; + } + } + + private void RFIDSocket_FormClosing(object sender, FormClosingEventArgs e) + { + if (Server.State == ServerState.Running) + { + if (!Server.ServerStop()) + { + MessageBox.Show("监听服务关闭失败! 请成功关闭后再退出"); + e.Cancel = true; + } + if (!Server.ServerDispose()) + { + MessageBox.Show("监听服务释放失败! 请再次尝试退出"); + e.Cancel = true; + } + } } } } diff --git a/RFIDSocket/RFIDSocket.csproj b/RFIDSocket/RFIDSocket.csproj index 9403162..701c578 100644 --- a/RFIDSocket/RFIDSocket.csproj +++ b/RFIDSocket/RFIDSocket.csproj @@ -33,6 +33,9 @@ 4 + + ..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll + @@ -44,6 +47,12 @@ + + ..\packages\TouchSocket.2.0.0-beta.253\lib\net45\TouchSocket.dll + + + ..\packages\TouchSocket.Core.2.0.0-beta.253\lib\net45\TouchSocket.Core.dll + @@ -66,6 +75,7 @@ RFIDSocket.cs + SettingsSingleFileGenerator Settings.Designer.cs @@ -79,5 +89,14 @@ + + + {DD18A634-1F9C-409A-8C32-C3C81B1B55B5} + HighWayIot.TouchSocket + + + + + \ No newline at end of file diff --git a/RFIDSocket/RFIDSocket.resx b/RFIDSocket/RFIDSocket.resx index 1af7de1..2b3df5e 100644 --- a/RFIDSocket/RFIDSocket.resx +++ b/RFIDSocket/RFIDSocket.resx @@ -117,4 +117,7 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + \ No newline at end of file diff --git a/RFIDSocket/packages.config b/RFIDSocket/packages.config new file mode 100644 index 0000000..daf2c12 --- /dev/null +++ b/RFIDSocket/packages.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file