田工版本初次提交
This commit is contained in:
15
CRICS_SSL/CRICS_SSL.csproj
Normal file
15
CRICS_SSL/CRICS_SSL.csproj
Normal file
@@ -0,0 +1,15 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="CSRedisCore" Version="3.8.807" />
|
||||
<PackageReference Include="RestSharp" Version="113.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
296
CRICS_SSL/CSRedisCacheHelper.cs
Normal file
296
CRICS_SSL/CSRedisCacheHelper.cs
Normal file
@@ -0,0 +1,296 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Configuration;
|
||||
using CSRedis;
|
||||
|
||||
namespace Common
|
||||
{
|
||||
/// <summary>
|
||||
/// Redis缓存辅助类
|
||||
/// </summary>
|
||||
public class CSRedisCacheHelper
|
||||
{
|
||||
public static CSRedisClient redis;
|
||||
public static CSRedisClient redis1;
|
||||
public static CSRedisClient redis2;
|
||||
public static CSRedisClient redis3;
|
||||
public static CSRedisClient redis4;
|
||||
public static CSRedisClient redis5;
|
||||
private static int SessionExpireMinutes = 6;
|
||||
private static int MonitorLogExpireMinutes = 30;
|
||||
//过期时间60个小时
|
||||
private static int DeviceStatusExpireMinutes = 60;
|
||||
|
||||
public static string HeartBeatPrefix = "HeartBeatMonitor";
|
||||
static CSRedisCacheHelper()
|
||||
{
|
||||
var redisHostStr = "127.0.0.1:6379";
|
||||
if (!string.IsNullOrEmpty(redisHostStr))
|
||||
{
|
||||
redis = new CSRedisClient(redisHostStr);//+ ",password=,defaultDatabase=0,poolsize=500,ssl=false,writeBuffer=10240,prefix=");
|
||||
redis1 = new CSRedisClient(redisHostStr + ",password=,defaultDatabase=1");
|
||||
redis2 = new CSRedisClient(redisHostStr + ",password=,defaultDatabase=2");
|
||||
redis3 = new CSRedisClient(redisHostStr + ",password=,defaultDatabase=3");
|
||||
redis4 = new CSRedisClient(redisHostStr + ",password=,defaultDatabase=4");
|
||||
redis5 = new CSRedisClient(redisHostStr + ",password=,defaultDatabase=5");
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 添加缓存
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="value"></param>
|
||||
public static void Set<T>(string key, T value)
|
||||
{
|
||||
redis.Set(key, value, SessionExpireMinutes * 60);
|
||||
}
|
||||
|
||||
public static T Get<T>(string key)
|
||||
{
|
||||
return redis.Get<T>(key);
|
||||
}
|
||||
public static void Del(string[] key)
|
||||
{
|
||||
redis.Del(key.ToArray());
|
||||
}
|
||||
public static void Del_Partition(string key, int SliceNo = 2)
|
||||
{
|
||||
CSRedisClient client = WhitchRedisSlice(SliceNo);
|
||||
client.Del(key);
|
||||
}
|
||||
|
||||
public static T ForeverGet<T>(string key)
|
||||
{
|
||||
return redis1.Get<T>(key);
|
||||
}
|
||||
|
||||
public static void Forever<T>(string key, T value)
|
||||
{
|
||||
redis1.Set(key, value, MonitorLogExpireMinutes * 24 * 60 * 60);
|
||||
}
|
||||
|
||||
public static T Get_Partition<T>(string key, int SliceNo = 2)
|
||||
{
|
||||
CSRedisClient client = WhitchRedisSlice(SliceNo);
|
||||
return client.Get<T>(key);
|
||||
}
|
||||
public static void Set_Partition<T>(string key, T value, int SliceNo = 2)
|
||||
{
|
||||
CSRedisClient client = WhitchRedisSlice(SliceNo);
|
||||
client.Set(key, value, DeviceStatusExpireMinutes * 60 * 60);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置过期时间
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="ExpireTime_Minutes"></param>
|
||||
/// <param name="SliceNo"></param>
|
||||
public static void Set_PartitionWithTime<T>(string key, T value, int ExpireTime_Minutes, int SliceNo = 2)
|
||||
{
|
||||
CSRedisClient client = WhitchRedisSlice(SliceNo);
|
||||
client.Set(key, value, ExpireTime_Minutes * 60);
|
||||
}
|
||||
public static void Set_PartitionWithForever<T>(string key, T value, int SliceNo = 2)
|
||||
{
|
||||
CSRedisClient client = WhitchRedisSlice(SliceNo);
|
||||
client.Set(key, value, -1);
|
||||
}
|
||||
public static void Del_Partition(string[] key, int SliceNo = 2)
|
||||
{
|
||||
CSRedisClient client = WhitchRedisSlice(SliceNo);
|
||||
client.Del(key);
|
||||
}
|
||||
public static bool Contains_Partition(string key, int SliceNo = 2)
|
||||
{
|
||||
CSRedisClient client = WhitchRedisSlice(SliceNo);
|
||||
bool result = client.Exists(key);
|
||||
return result;
|
||||
}
|
||||
private static CSRedisClient WhitchRedisSlice(int SliceNo)
|
||||
{
|
||||
CSRedisClient client = null;
|
||||
if (SliceNo == 1)
|
||||
{
|
||||
client = redis1;
|
||||
}
|
||||
else if (SliceNo == 2)
|
||||
{
|
||||
client = redis2;
|
||||
}
|
||||
else if (SliceNo == 3)
|
||||
{
|
||||
client = redis3;
|
||||
}
|
||||
else if (SliceNo == 4)
|
||||
{
|
||||
client = redis4;
|
||||
}
|
||||
else if (SliceNo == 5)
|
||||
{
|
||||
client = redis5;
|
||||
}
|
||||
else
|
||||
{
|
||||
client = redis;
|
||||
}
|
||||
return client;
|
||||
}
|
||||
|
||||
public static long GetForever_ExpireMinutes<T>(string key)
|
||||
{
|
||||
return redis.Ttl(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
public static T Get<T>(string key, string mac)
|
||||
{
|
||||
T obj = redis.Get<T>(mac);
|
||||
if (obj == null)
|
||||
{
|
||||
return redis.Get<T>(key);
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
/// <summary>
|
||||
/// 判断是否存在
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="mac"></param>
|
||||
/// <returns></returns>
|
||||
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)
|
||||
{
|
||||
bool result = redis.Exists(key);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static void Publish(string Topic, string Payload)
|
||||
{
|
||||
CSRedisCacheHelper.redis3.Publish(Topic, Payload);
|
||||
}
|
||||
/// <summary>
|
||||
/// 添加Hash缓存
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="value"></param>
|
||||
public static void HMSet(int SliceNo, string key, params object[] value)
|
||||
{
|
||||
CSRedisClient client = WhitchRedisSlice(SliceNo);
|
||||
client.HMSet(key, value);
|
||||
}
|
||||
|
||||
public static void HMSet(int SliceNo, int ExpireTime_M, string key, params object[] value)
|
||||
{
|
||||
CSRedisClient client = WhitchRedisSlice(SliceNo);
|
||||
client.HMSet(key, value);
|
||||
client.Expire(key, new TimeSpan(0, ExpireTime_M, 0));
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取Hash缓存
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
public static T[] HMGet<T>(int SliceNo, string key, string KeyV)
|
||||
{
|
||||
CSRedisClient client = WhitchRedisSlice(SliceNo);
|
||||
return client.HMGet<T>(key, KeyV);
|
||||
}
|
||||
public static Dictionary<string, string> HMGetAll(int SliceNo, string key)
|
||||
{
|
||||
CSRedisClient client = WhitchRedisSlice(SliceNo);
|
||||
return client.HGetAll(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取 某个hash值的数量
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
public static int GetHMCount(string key)
|
||||
{
|
||||
return redis5.HGetAll(key).Count;
|
||||
}
|
||||
/// <summary>
|
||||
/// 删除Hash缓存
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
public static long HDelAll<T>(int SliceNo, string key)
|
||||
{
|
||||
CSRedisClient client = WhitchRedisSlice(SliceNo);
|
||||
return client.HDel(key);
|
||||
}
|
||||
public static long HDel(int SliceNo, string key, string key1)
|
||||
{
|
||||
CSRedisClient client = WhitchRedisSlice(SliceNo);
|
||||
return client.HDel(key, key1);
|
||||
}
|
||||
|
||||
public static void ListAdd(int SliceNo, string key, params object[] obj)
|
||||
{
|
||||
CSRedisClient client = WhitchRedisSlice(SliceNo);
|
||||
client.LPush(key, obj);
|
||||
}
|
||||
public static int MaxLen = 500000;
|
||||
public static void StreamAdd(int SliceNo, string key, string Value)
|
||||
{
|
||||
try
|
||||
{
|
||||
CSRedisClient client = WhitchRedisSlice(SliceNo);
|
||||
// 添加简单消息
|
||||
client.XAdd(key, MaxLen, "*", new ValueTuple<string, string>("__data", Value));
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
public static void StreamConsume(int SliceNo, string key, string group = "UDPData", string consumer = "Crics1")
|
||||
{
|
||||
CSRedisClient client = WhitchRedisSlice(SliceNo);
|
||||
|
||||
var data = redis.XReadGroup(group, consumer, 100, 10, new ValueTuple<string, string>(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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
133
CRICS_SSL/Program.cs
Normal file
133
CRICS_SSL/Program.cs
Normal file
@@ -0,0 +1,133 @@
|
||||
using Common;
|
||||
using Newtonsoft.Json;
|
||||
using RestSharp;
|
||||
using System.Net;
|
||||
|
||||
namespace CRICS_SSL
|
||||
{
|
||||
public class XuanZhuResponse
|
||||
{
|
||||
/// <summary>
|
||||
/// 酒店编码
|
||||
/// </summary>
|
||||
public string code { get; set; }
|
||||
/// <summary>
|
||||
/// 房号
|
||||
/// </summary>
|
||||
public string roomNumber { get; set; }
|
||||
/// <summary>
|
||||
/// 回路地址
|
||||
/// </summary>
|
||||
public string address { get; set; }
|
||||
/// <summary>
|
||||
/// 回路名称
|
||||
/// </summary>
|
||||
public string name { get; set; }
|
||||
/// <summary>
|
||||
/// 状态
|
||||
/// </summary>
|
||||
public int status { get; set; }
|
||||
/// <summary>
|
||||
/// 异常类型
|
||||
/// </summary>
|
||||
public int faultType { get; set; }
|
||||
/// <summary>
|
||||
/// 异常值
|
||||
/// </summary>
|
||||
public int faultData { get; set; }
|
||||
/// <summary>
|
||||
/// 亮度
|
||||
/// </summary>
|
||||
public int brightness { get; set; }
|
||||
/// <summary>
|
||||
/// 当前温度
|
||||
/// </summary>
|
||||
public int currentTemp { get; set; }
|
||||
/// <summary>
|
||||
/// 设定温度
|
||||
/// </summary>
|
||||
public int settingTemp { get; set; }
|
||||
/// <summary>
|
||||
/// 风速
|
||||
/// </summary>
|
||||
public int fanSpeed { get; set; }
|
||||
/// <summary>
|
||||
/// 模式
|
||||
/// </summary>
|
||||
public int mode { get; set; }
|
||||
/// <summary>
|
||||
/// 阀门
|
||||
/// </summary>
|
||||
public int valve { get; set; }
|
||||
}
|
||||
|
||||
public class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Task.Factory.StartNew(() =>
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
StreamConsume("UDPData", "Crics1", "task1");
|
||||
}
|
||||
}, TaskCreationOptions.LongRunning);
|
||||
SendHttpData<string>("", "");
|
||||
Console.WriteLine("Hello, World!");
|
||||
Console.ReadLine();
|
||||
}
|
||||
|
||||
public static string SendHttpData<T>(string Url, T obj)
|
||||
{
|
||||
var client1 = new RestClient("https://f-xcx.blv-oa.com/rcu/report");
|
||||
var request1 = new RestRequest("", Method.Post);
|
||||
|
||||
string jjj = """
|
||||
{"code":"1085","roomNumber":"8099","address":"009001004","name":"PM2.5浓度","status":1,"faultType":0,"faultData":0,"brightness":45,"currentTemp":0,"settingTemp":0,"fanSpeed":0,"mode":0,"valve":0}
|
||||
""";
|
||||
request1.AddJsonBody(jjj);
|
||||
var QQQ = client1.Execute(request1);
|
||||
HttpStatusCode HHH = QQQ.StatusCode;
|
||||
if (HHH == HttpStatusCode.OK)
|
||||
{
|
||||
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
public static string UDPAllDataKey = "BaoYi_ReportData";
|
||||
public static void StreamConsume(string group = "BaoYiData", string consumer = "BaoYiCRICS1", string task_key = "task1")
|
||||
{
|
||||
try
|
||||
{
|
||||
var redis = CSRedisCacheHelper.redis1;
|
||||
var data = redis.XReadGroup(group, consumer, 500, 10, new ValueTuple<string, string>(UDPAllDataKey, ">"));
|
||||
|
||||
if (data != null)
|
||||
{
|
||||
//var pending = redis.XPending(UDPAllDataKey, group);
|
||||
foreach (var item in data)
|
||||
{
|
||||
var idarray = item.Item2;
|
||||
string nsa = string.Concat(task_key, "#", idarray.Count().ToString());
|
||||
CSRedisCacheHelper.Publish("udp_package_consumer", nsa);
|
||||
foreach (var SerializeNo in idarray)
|
||||
{
|
||||
var id1 = SerializeNo.Item1;
|
||||
var str = SerializeNo.Item2[1];
|
||||
var GGG = JsonConvert.DeserializeObject<XuanZhuResponse>(str);
|
||||
redis.XAck(UDPAllDataKey, group, id1);
|
||||
redis.XDel(UDPAllDataKey, id1);
|
||||
//ProcessData(GGG);
|
||||
SendHttpData("", "");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user