diff --git a/.gitignore b/.gitignore index d4cc2ef..a471502 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ UI/App_Data/UDPLog/ /UI/Properties/PublishProfiles/*.user /UI/Properties/PublishProfiles/*.user /UI/Properties/PublishProfiles +.vscode diff --git a/Services/Cache/BaseCacheHelpRedis.cs b/Services/Cache/BaseCacheHelpRedis.cs index 7fca252..baef0ae 100644 --- a/Services/Cache/BaseCacheHelpRedis.cs +++ b/Services/Cache/BaseCacheHelpRedis.cs @@ -9,14 +9,14 @@ public abstract class BaseCacheHelpRedis { public static T GetCache(string key, Func setAcrion, TimeSpan? expiry = null) { - if (RedisHelper.CheckKey(key)) + if (StackChangeRedisHelper.CheckKey(key)) { - return RedisHelper.StringGet(key); + return StackChangeRedisHelper.StringGet(key); } T val = setAcrion(); if (val != null) { - RedisHelper.StringSet(key, val, expiry); + StackChangeRedisHelper.StringSet(key, val, expiry); } return val; } @@ -26,62 +26,62 @@ public abstract class BaseCacheHelpRedis T val = setAcrion(); if (val != null) { - RedisHelper.StringSet(key, val, expiry); + StackChangeRedisHelper.StringSet(key, val, expiry); } } public static T GetVaue(string key) { - if (RedisHelper.CheckKey(key)) + if (StackChangeRedisHelper.CheckKey(key)) { - return RedisHelper.StringGet(key); + return StackChangeRedisHelper.StringGet(key); } return default(T); } public static void ClearCache(string key) { - if (RedisHelper.CheckKey(key)) + if (StackChangeRedisHelper.CheckKey(key)) { - RedisHelper.RemoveKey(key); + StackChangeRedisHelper.RemoveKey(key); } } public static void ClearCacheList(List keyList) { - RedisHelper.RemoveKeyList(keyList); + StackChangeRedisHelper.RemoveKeyList(keyList); } public static T GetHashCache(string key, string sid, Func setAcrion) { - if (RedisHelper.HashExists(key, sid)) + if (StackChangeRedisHelper.HashExists(key, sid)) { - return RedisHelper.HashGet(key, sid); + return StackChangeRedisHelper.HashGet(key, sid); } T val = setAcrion(); if (val != null) { - RedisHelper.HashSet(key, sid, val); + StackChangeRedisHelper.HashSet(key, sid, val); } return val; } public static void SetHashCache(string key, string sid, T model) { - RedisHelper.HashSet(key, sid, model); + StackChangeRedisHelper.HashSet(key, sid, model); } public static void SetHashCache(string key, Dictionary dic) { - RedisHelper.HashSet(key, dic); + StackChangeRedisHelper.HashSet(key, dic); } public static Dictionary GetHashAllCache(string key) { - return RedisHelper.HashGetAll(key); + return StackChangeRedisHelper.HashGetAll(key); } public static bool CheckKey(string key) { - return RedisHelper.CheckKey(key); + return StackChangeRedisHelper.CheckKey(key); } } diff --git a/Services/Tool/RedisHelper.cs b/Services/Tool/RedisHelper.cs index 6dd84e5..fb7150c 100644 --- a/Services/Tool/RedisHelper.cs +++ b/Services/Tool/RedisHelper.cs @@ -173,7 +173,7 @@ namespace Services.Tool /// /// redis 数据库操作帮助类 /// - public class RedisHelper + public class StackChangeRedisHelper { //执行顺序---静态字段---静态构造函数---构造函数 diff --git a/UI/App_Data/configs/appSettings.config b/UI/App_Data/configs/appSettings.config index 3dca33e..995dcba 100644 --- a/UI/App_Data/configs/appSettings.config +++ b/UI/App_Data/configs/appSettings.config @@ -8,7 +8,7 @@ - + diff --git a/UI/CSRedisCacheHelper.cs b/UI/CSRedisCacheHelper.cs index 9a33bd2..a456187 100644 --- a/UI/CSRedisCacheHelper.cs +++ b/UI/CSRedisCacheHelper.cs @@ -1,10 +1,10 @@ using CSRedis; +using StackExchange.Redis; using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Text; -using static CSRedis.CSRedisClient; namespace Common { @@ -14,20 +14,19 @@ namespace Common public class CSRedisCacheHelper { public static CSRedisClient redis; - private const string ip = "localhost"; - private const string port = "26379"; + //private static readonly string[] redisHosts = null; + private static int SessionExpireMinutes = 1; + private static int MonitorLogExpireMinutes = 1; + //过期时间60个小时 + private static int DeviceStatusExpireMinutes = 60; + + public static string HeartBeatPrefix = "HeartBeatMonitor"; static CSRedisCacheHelper() { - var redisHostStr = string.Format("{0}:{1}", ip, port); + string redisHostStr = "47.119.147.104:26379"; if (!string.IsNullOrEmpty(redisHostStr)) { redis = new CSRedisClient(redisHostStr + ",password=1001^_^lool,defaultDatabase=0"); - var DingYueMsg = ("CellCorelDRAWUser", new Action(async (args) => - { - string body = args.Body; - })); - - CSRedisCacheHelper.redis.Subscribe(DingYueMsg); } } /// @@ -36,30 +35,103 @@ namespace Common /// /// /// - public static void Set(string key, T value, int ExpireTime) + public static void Set(string key, T value) { - redis?.Set(key, value, ExpireTime * 60); + redis.Set(key, value, SessionExpireMinutes * 60); } public static T Get(string key) { return redis.Get(key); } - - public static void Forever(string key, T value) + public static void Del(string[] key) { - redis.Set(key, value, -1); + redis.Del(key.ToArray()); } - public static void Del(string key) + public static void Del_Partition(string key) { redis.Del(key); } + public static T ForeverGet(string key) + { + return redis.Get(key); + } + + public static void Forever(string key, T value) + { + redis.Set(key, value, MonitorLogExpireMinutes * 24 * 60 * 60); + } + + public static T Get_Partition(string key) + { + return redis.Get(key); + } + public static void Set_Partition(string key, T value) + { + redis.Set(key, value, DeviceStatusExpireMinutes * 60 * 60); + } + + /// + /// 设置过期时间 + /// + /// + /// + /// + /// + public static void Set_PartitionWithTime(string key, T value, int ExpireTime_Minutes) + { + redis.Set(key, value, ExpireTime_Minutes * 60); + } + public static void Set_PartitionWithForever(string key, T value) + { + redis.Set(key, value, -1); + } + public static void Del_Partition(string[] key) + { + redis.Del(key); + } + public static bool Contains_Partition(string key) + { + return redis.Exists(key); + } + + + public static long GetForever_ExpireMinutes(string key) + { + return redis.Ttl(key); + } + + /// + /// 获取 + /// + /// + /// + /// + public static T Get(string key, string mac) + { + T obj = redis.Get(mac); + if (obj == null) + { + return redis.Get(key); + } + return obj; + } /// /// 判断是否存在 /// /// + /// /// + public static bool Contains(string key, string mac) + { + bool result = redis.Exists(mac); + if (!result) + { + result = redis.Exists(key); + } + return result; + } public static bool Contains(string key) { @@ -67,28 +139,10 @@ namespace Common return result; } - /// - /// 发布消息 - /// - /// - /// public static void Publish(string Topic, string Payload) { - CSRedisCacheHelper.redis.PublishNoneMessageId(Topic, Payload); + CSRedisCacheHelper.redis.Publish(Topic, Payload); } - - - public static void LPush(string Key, Dictionary Value) - { - CSRedisCacheHelper.redis.LPush>(Key,Value); - } - - public static T BRPop(string Key) - { - return CSRedisCacheHelper.redis.BRPop(3,Key); - } - - /// /// 添加Hash缓存 /// @@ -115,16 +169,73 @@ namespace Common { return redis.HMGet(key, KeyV); } - - /// - /// Example - /// WXFault_酒店编号_房间号 - /// - /// - /// public static Dictionary HMGetAll(string key) { return redis.HGetAll(key); } + + /// + /// 获取 某个hash值的数量 + /// + /// + /// + public static int GetHMCount(string key) + { + return redis.HGetAll(key).Count; + } + /// + /// 删除Hash缓存 + /// + /// + /// + /// + public static long HDelAll(string key) + { + return redis.HDel(key); + } + public static long HDel(string key, string key1) + { + return redis.HDel(key, key1); + } + + public static void ListAdd(string key, params object[] obj) + { + redis.LPush(key, obj); + } + public static int MaxLen = 500000; + public static void StreamAdd(string key, string Value) + { + try + { + // 添加简单消息 + redis.XAdd(key, MaxLen, "*", new ValueTuple("__data", Value)); + } + catch (Exception) + { + + } + } + public static void StreamConsume(string key, string group = "UDPData", string consumer = "Crics1") + { + var data = redis.XReadGroup(group, consumer, 100, 10, new ValueTuple(key, ">")); + + if (data != null) + { + //检查pending表的长度 + //消费确认前,pending 应该等于2 + var pending = redis.XPending(key, group); + foreach (var item in data) + { + var idarray = item.Item2; + foreach (var SerializeNo in idarray) + { + var id1 = SerializeNo.Item1; + redis.XAck(key, group, id1); + redis.XDel(key, id1); + } + } + } + + } } } diff --git a/UI/Controllers/HomeController.cs b/UI/Controllers/HomeController.cs index bd262f3..7eb6592 100644 --- a/UI/Controllers/HomeController.cs +++ b/UI/Controllers/HomeController.cs @@ -104,8 +104,8 @@ public class HomeController : BaseController if (!string.IsNullOrEmpty(Token)) { string Ip = IPHelper.GetIP(); - var userinfo = RedisHelper.StringGet(Token + Ip); - RedisHelper.RemoveKey(Token + Ip); + var userinfo = StackChangeRedisHelper.StringGet(Token + Ip); + StackChangeRedisHelper.RemoveKey(Token + Ip); } base.ViewBag.Select = -1; base.ViewBag.LogData = SqlSugarBase.Db.Ado.SqlQuery("SELECT * FROM DbLog ORDER BY ID DESC limit 1, 50").ToList(); diff --git a/UI/Controllers/LoginController.cs b/UI/Controllers/LoginController.cs index 2e5eec7..e58c6ee 100644 --- a/UI/Controllers/LoginController.cs +++ b/UI/Controllers/LoginController.cs @@ -30,8 +30,8 @@ public class LoginController : Controller if (!string.IsNullOrEmpty(Token)) { string Ip = IPHelper.GetIP(); - var userinfo = RedisHelper.StringGet(Token); - RedisHelper.RemoveKey(Token); + var userinfo = StackChangeRedisHelper.StringGet(Token); + StackChangeRedisHelper.RemoveKey(Token); if (userinfo != default(UserInfo)) { ReturnResult returnResult = new ReturnResult(); DL(ref returnResult, userinfo.Uid, userinfo.Pwd,1); diff --git a/UI/Controllers/OtherApiController.cs b/UI/Controllers/OtherApiController.cs index 8a24de6..83848d7 100644 --- a/UI/Controllers/OtherApiController.cs +++ b/UI/Controllers/OtherApiController.cs @@ -74,7 +74,7 @@ namespace UI.Controllers token = (BitConverter.ToString(md5.ComputeHash(Encoding.Default.GetBytes(token)), 4, 8)).Replace("-", ""); if (moreLogin == true) { - RedisHelper.StringSet(token, new UserInfo() { Pwd = Pwd, Uid = Uid }, TimeSpan.FromMinutes(20)); + StackChangeRedisHelper.StringSet(token, new UserInfo() { Pwd = Pwd, Uid = Uid }, TimeSpan.FromMinutes(20)); } resdata.Data = new { @@ -161,7 +161,7 @@ namespace UI.Controllers var resdata = new ReturnResult(); try { - var userinfo = RedisHelper.StringGet(Token); + var userinfo = StackChangeRedisHelper.StringGet(Token); if (IsNew) { Ip = Ip == "" ? IPHelper.GetIP() : Ip; @@ -174,11 +174,11 @@ namespace UI.Controllers var token = (userinfo.Uid + userinfo.Pwd + DateTime.Now); var md5 = new MD5CryptoServiceProvider(); token = (BitConverter.ToString(md5.ComputeHash(Encoding.Default.GetBytes(token)), 4, 8)).Replace("-", ""); - RedisHelper.StringSet(token, userinfo); + StackChangeRedisHelper.StringSet(token, userinfo); resdata.Data = new { Token = token }; resdata.Status = 200; } - RedisHelper.StringSet(Token, userinfo, TimeSpan.FromSeconds(10)); + StackChangeRedisHelper.StringSet(Token, userinfo, TimeSpan.FromSeconds(10)); } catch (Exception ex) { @@ -203,8 +203,8 @@ namespace UI.Controllers { Logs.WriteTimingUDPLog($"传入token {Token}"); Ip = Ip == "" ? IPHelper.GetIP() : Ip; - var userinfo = RedisHelper.StringGet(Token); - RedisHelper.RemoveKey(Token); + var userinfo = StackChangeRedisHelper.StringGet(Token); + StackChangeRedisHelper.RemoveKey(Token); if (userinfo == default(UserInfo)) { if (IsData == false) @@ -276,7 +276,7 @@ namespace UI.Controllers try { DbLog log = new DbLog() { Ip = Ip == "" ? IPHelper.GetIP() : Ip, Client = UserAgent == "" ? HttpContext.Request.UserAgent : UserAgent }; - var userinfo = RedisHelper.StringGet(Token); + var userinfo = StackChangeRedisHelper.StringGet(Token); string Uid = userinfo.Uid; if (userinfo == null) { diff --git a/UI/Controllers/UpgradeController.cs b/UI/Controllers/UpgradeController.cs index adbef11..a1309ea 100644 --- a/UI/Controllers/UpgradeController.cs +++ b/UI/Controllers/UpgradeController.cs @@ -13,6 +13,8 @@ using static Services.Manager.SyncAllFromOutterDB; using Newtonsoft.Json; using Models.ApiModei; using NLog; +using Services.Tool; +using Common; namespace UI.Controllers { @@ -448,8 +450,57 @@ namespace UI.Controllers } } - } + /// + /// 获取房间地址状态 + /// + /// + /// + [HttpPost] + public ActionResult GetRoomAddressStatus(int Code, string RoomNum) + { + RoomAddressStatus roomAddressStatus = new RoomAddressStatus + { + Code = Code, + RoomNum = RoomNum + }; + try + { + if (roomAddressStatus == null) + { + Response.StatusCode = (int)HttpStatusCode.BadRequest; + return Json(new { Status = 0, Message = "请求参数不能为空" }, JsonRequestBehavior.DenyGet); + } + string code = roomAddressStatus.Code.ToString(); + string RoomNumVal = roomAddressStatus.RoomNum ?? string.Empty; + string key = $"WXFault_{code}_{RoomNumVal}"; + var result = CSRedisCacheHelper.HMGetAll(key); + + if (result != null && result.Count > 0) + { + return Json(new { Status = 1, Message = "获取成功", Data = result }, JsonRequestBehavior.DenyGet); + } + else + { + Response.StatusCode = (int)HttpStatusCode.NotFound; + return Json(new { Status = 0, Message = "未找到数据", Data = result }, JsonRequestBehavior.DenyGet); + } + } + catch (Exception ex) + { + logger.Error(ex, "GetRoomAddressStatus error"); + Response.StatusCode = (int)HttpStatusCode.InternalServerError; + return Json(new { Status = -1, Message = "服务器错误", Detail = ex.Message }, JsonRequestBehavior.DenyGet); + } + } + + + } + public class RoomAddressStatus + { + public string RoomNum { get; set; } + public int Code { get; set; } + } // 服务控制请求参数类 public class RCUServiceRequest { diff --git a/UI/Controllers/WxController.cs b/UI/Controllers/WxController.cs index a5be5c8..baac96f 100644 --- a/UI/Controllers/WxController.cs +++ b/UI/Controllers/WxController.cs @@ -1,5 +1,4 @@ -using Common; -using Models; +using Models; using Models.ModelItems; using Models.View; using Newtonsoft.Json; @@ -1590,17 +1589,6 @@ namespace UI.Controllers return ri; } - public void A() - { - string code = ""; - string roonumber = ""; - string a = $"WXFault_{code}_{roonumber}"; - CSRedisCacheHelper.HMGetAll("'"); - } - - - - } diff --git a/UI/UI.csproj b/UI/UI.csproj index 7cbe501..ebacdc9 100644 --- a/UI/UI.csproj +++ b/UI/UI.csproj @@ -213,6 +213,7 @@ + Global.asax