using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Runtime.InteropServices; using System.Threading.Tasks; using ConsoleHttp; using ConsoleHttp.Model; using MqttMsgServer.Dao; using MqttMsgServer.HttpService.dto; using MqttMsgServer.Model; using MqttMsgServer.Redis; using MqttMsgServer.Service.Client.Dto; using MqttMsgServer.Tools; using MQTTnet.Protocol; using StackExchange.Redis; namespace MqttMsgServer.Service.Client { public interface IClientInfoService { ResponseResult AddOrUpdateClient(ClientDto input); ResponseResult RegisterClient(ClientDto input); ResponseResult UpdateClients(ClientDto input); void RefreshClientCache(ClientInfos input); ResponseResult ConnectServer(ConnectDto input); } public class ClientInfoService : IClientInfoService { //private readonly IRepository ClientsRepository; //private readonly IRepository ReceiveMessageRecordRepository; protected RedisHelpers redisCache; public ClientInfoService()//IRepository clientsRepository, IRepository receiveMessageRecordRepository { //ClientsRepository = clientsRepository; //ReceiveMessageRecordRepository = receiveMessageRecordRepository; this.redisCache = new RedisHelpers(); } public ResponseResult ConnectServer(ConnectDto input) { ResponseResult result = new ResponseResult(); if (string.IsNullOrEmpty(input.ClientName) || string.IsNullOrEmpty(input.ClientId) || string.IsNullOrEmpty(input.Password)) { return StringHelper.CheckError("请求字段为空!"); } //var rs = RedisHelper.Instance.GetStringKey(CacheName.ClientCachePrefix + input.ClientId); var rs = redisCache.StringGet(CacheName.ClientCachePrefix + input.ClientId); if (rs == null) { string lcSql = $"select * from Clients where Id='{input.ClientId}';"; DataTable dt = SqlDbHelper.ExecuteDataTable(lcSql); if (dt != null && dt.Rows.Count > 0) { rs= StringHelper.ToList(dt).FirstOrDefault(); } } if (rs == null) { return StringHelper.CheckError("用户不存在!"); } if (rs.ClientState == DefineConfig.ClientStateClosed) { return StringHelper.CheckError("账户已被锁定!"); } var pwd = StringHelper.GenerateMD5(input.Password); if (pwd != rs.Password) { return StringHelper.CheckError("密码不正确!"); } // RedisHelper.Instance.SetStringKey(CacheName.ClientCachePrefix + input.ClientId, rs, TimeSpan.FromHours(24)); redisCache.StringSet(CacheName.ClientCachePrefix + input.ClientId, rs, TimeSpan.FromHours(24)); return result; } public void RefreshClientCache(ClientInfos input) { //RedisHelper.Instance.SetStringKey(CacheName.ClientCachePrefix + input.Id, input, TimeSpan.FromHours(input.Hours)); redisCache.StringSet(CacheName.ClientCachePrefix + input.Id, input, TimeSpan.FromHours(input.Hours)); } public ResponseResult AddOrUpdateClient(ClientDto input) { ResponseResult result = new ResponseResult(); if (string.IsNullOrEmpty(input.ClientName) || string.IsNullOrEmpty(input.Id) || string.IsNullOrEmpty(input.Password)) { return StringHelper.CheckError("编号,名称,密码请求字段为空!"); } var check = GetClientInfo(input.Id); //typeof(SqlDbHelper).LogError("1.--查询对象"); if (check == null) { ClientInfos c = new ClientInfos() { Id = input.Id, ClientName = input.ClientName, ClientState = input.ClientState, CreatorDate = DateTime.Now, CreatorUserId = 1, Password = StringHelper.GenerateMD5(input.Password), SystemDesc = input.SystemDesc, Hours = input.Hours }; SqlDbHelper.ExecuteNonQuery(c.InsertSql()); RefreshClientCache(c); } else { //typeof(SqlDbHelper).LogError("2.--查询到对象"); ClientInfos c = new ClientInfos() { Id = input.Id, ClientName = string.IsNullOrEmpty(input.ClientName) ? check.ClientName : input.ClientName, ClientState = input.ClientState, Password = string.IsNullOrEmpty(input.Password) ? check.Password : StringHelper.GenerateMD5(input.Password), SystemDesc = string.IsNullOrEmpty(input.SystemDesc) ? check.SystemDesc : input.SystemDesc, Hours = input.Hours, CreatorDate = check.CreatorDate, CreatorUserId = check.CreatorUserId, }; //typeof(SqlDbHelper).LogError("3.--创建到对象"); SqlDbHelper.ExecuteNonQuery(c.UpdateSql()); //typeof(SqlDbHelper).LogError("4.--更新数据库对象"); RefreshClientCache(c); //typeof(SqlDbHelper).LogError("5.--更新缓存"); } return result; } private ClientInfos GetClientInfo(string id) { ClientInfos check = null; string lcSql = $"select * from Clients where Id='{id}';"; DataTable dt = SqlDbHelper.ExecuteDataTable(lcSql); if (dt != null && dt.Rows.Count > 0) { check = StringHelper.ToList(dt).FirstOrDefault(); } return check; } public ResponseResult RegisterClient(ClientDto input) { ResponseResult result = new ResponseResult(); if (string.IsNullOrEmpty(input.ClientName) || string.IsNullOrEmpty(input.Id) || string.IsNullOrEmpty(input.Password)) { return StringHelper.CheckError("编号,名称,密码请求字段为空!"); } var check = GetClientInfo(input.Id); if (check != null) { return StringHelper.CheckError("clientId已经存在!"); } ClientInfos c = new ClientInfos() { Id = input.Id, ClientName = input.ClientName, ClientState = input.ClientState, CreatorDate = DateTime.Now, CreatorUserId = 1, Password = StringHelper.GenerateMD5(input.Password), SystemDesc = input.SystemDesc, Hours = input.Hours }; SqlDbHelper.ExecuteNonQuery(c.InsertSql()); RefreshClientCache(c); return result; } public ResponseResult UpdateClients(ClientDto input) { ResponseResult result = new ResponseResult(); if ( string.IsNullOrEmpty(input.Id) ) { return StringHelper.CheckError("编码字段为空!"); } var check = GetClientInfo(input.Id); if (check == null) { return StringHelper.CheckError("clientId不存在!"); } ClientInfos c = new ClientInfos() { Id = input.Id, ClientName = string.IsNullOrEmpty(input.ClientName)?check.ClientName:input.ClientName, ClientState = input.ClientState, Password = string.IsNullOrEmpty(input.Password)?check.Password:StringHelper.GenerateMD5(input.Password), SystemDesc = string.IsNullOrEmpty(input.SystemDesc)?check.SystemDesc:input.SystemDesc, Hours = input.Hours, CreatorDate = check.CreatorDate, CreatorUserId = check.CreatorUserId, }; SqlDbHelper.ExecuteNonQuery(c.UpdateSql()); RefreshClientCache(c); return result; } public void WriteCache(string clientId, string topic, string msg) { var entity = new ReceiveMessageRecord() { Id = Guid.NewGuid().ToString("N"), ClientId = clientId, Topic = topic, Payload = msg, QualityOfServiceLevel= MqttQualityOfServiceLevel.AtLeastOnce, CreatorDate = DateTime.Now, CreatorUserId = 1 }; WriteData(entity); } public void WriteData(ReceiveMessageRecord pReceiveMessageRecord) { WriteCache(pReceiveMessageRecord); Action ac = () => { WriteRecordToDb(pReceiveMessageRecord); }; ac.Invoke(); } public void WriteCache(ReceiveMessageRecord pReceiveMessageRecord) { //List rs = redisCache.StringGet>(CacheName.TopicMsgCachePrefix + pReceiveMessageRecord.Topic) ?? new List(); //if (rs.Any()) //{ // DateTime currentDay = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 00:00:00")); // rs = rs.Where(i => i.CreatorDate > currentDay).Take(100).ToList(); //} //rs.Add(pReceiveMessageRecord); //redisCache.StringSet(CacheName.TopicMsgCachePrefix + pReceiveMessageRecord.Topic, rs, TimeSpan.FromDays(1)); int retainMinute = AppSetting.GetInt("redisMsgTTL"); redisCache.ListRightPush(CacheName.TopicMsgCachePrefix + pReceiveMessageRecord.Topic, pReceiveMessageRecord, TimeSpan.FromMinutes(retainMinute)); //List rs2 = redisCache.StringGet>(CacheName.ClientMsgCachePrefix + pReceiveMessageRecord.ClientId) ?? new List(); //if (rs2.Any()) //{ // DateTime currentDay = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 00:00:00")); // rs2 = rs2.Where(i => i.CreatorDate > currentDay).Take(100).ToList(); //} //rs2.Add(pReceiveMessageRecord); //redisCache.StringSet(CacheName.ClientMsgCachePrefix + pReceiveMessageRecord.ClientId, rs2, TimeSpan.FromDays(1)); redisCache.ListRightPush(CacheName.ClientMsgCachePrefix + pReceiveMessageRecord.ClientId, pReceiveMessageRecord, TimeSpan.FromMinutes(retainMinute)); } public void WriteRecordToDb(ReceiveMessageRecord pReceiveMessageRecord) { try { SqlDbHelper.ExecuteNonQuery(pReceiveMessageRecord.InsertSql()); } catch (Exception e) { Console.WriteLine(e); } } } }