You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
180 lines
6.9 KiB
180 lines
6.9 KiB
9 months ago
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using System.IO;
|
||
|
using System.Linq;
|
||
|
using System.Text;
|
||
|
using System.Threading.Tasks;
|
||
|
using System.Web;
|
||
|
|
||
|
namespace FangYar.Common
|
||
|
{
|
||
|
|
||
|
/// <summary>
|
||
|
/// 队列处理硬件数据信息
|
||
|
/// </summary>
|
||
|
public class QueueDeviceUtil
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// 配置文件中线程数
|
||
|
/// </summary>
|
||
|
private static string QueueDeviceThreadNumStr = System.Configuration.ConfigurationManager.AppSettings["QueueDeviceThreadNum"] + "";
|
||
|
/// <summary>
|
||
|
/// 开启线程数
|
||
|
/// </summary>
|
||
|
private static int QueueDeviceThreadNumNum = 0;
|
||
|
/// <summary>
|
||
|
/// 正常消息队列
|
||
|
/// </summary>
|
||
|
public static System.Collections.Concurrent.ConcurrentQueue<QueueDataMo> infoMsgQueue = new System.Collections.Concurrent.ConcurrentQueue<QueueDataMo>();
|
||
|
/// <summary>
|
||
|
/// 控制消息处理线程
|
||
|
/// </summary>
|
||
|
private static System.Threading.Tasks.Task task;
|
||
|
/// <summary>
|
||
|
/// 将待处理消息添加至队列中
|
||
|
/// </summary>
|
||
|
/// <param name="mo"></param>
|
||
|
public static void AddDeviceDataToQueue(QueueDataMo mo)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
if (mo != null)
|
||
|
{
|
||
|
//将待处理信息添加到队列
|
||
|
infoMsgQueue.Enqueue(mo);
|
||
|
|
||
|
}
|
||
|
|
||
|
//判断线程是否创建
|
||
|
if (task == null)
|
||
|
{
|
||
|
//判断开启队列数是否小于1
|
||
|
if (QueueDeviceThreadNumNum < 1)
|
||
|
{
|
||
|
//验证配置文件中队列数是否小于1
|
||
|
int.TryParse(QueueDeviceThreadNumStr, out QueueDeviceThreadNumNum);
|
||
|
QueueDeviceThreadNumNum = QueueDeviceThreadNumNum < 1 ? 1 : QueueDeviceThreadNumNum;
|
||
|
}
|
||
|
//通过循环开启线程
|
||
|
for (int i = 0; i < QueueDeviceThreadNumNum; i++)
|
||
|
{
|
||
|
task = System.Threading.Tasks.Task.Factory.StartNew(async () =>
|
||
|
{
|
||
|
//线程一直循环等待处理数据
|
||
|
while (true)
|
||
|
{
|
||
|
//判断是否退出线程
|
||
|
if (FangYar.Common.MyTaskControlHelper.TokenSource.IsCancellationRequested)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
// 初始化为true时执行WaitOne不阻塞
|
||
|
FangYar.Common.MyTaskControlHelper.ResetEvent.WaitOne();
|
||
|
//判断队列数据长度是否小于1
|
||
|
if (infoMsgQueue.Count < 1)
|
||
|
{
|
||
|
// 队列内无待处理数据,等待1秒后执行
|
||
|
await System.Threading.Tasks.Task.Delay(1000);
|
||
|
}
|
||
|
|
||
|
//处理数据
|
||
|
try
|
||
|
{
|
||
|
//从队列取出一个数并进行处理
|
||
|
if (infoMsgQueue.TryDequeue(out QueueDataMo dataMo))
|
||
|
{
|
||
|
|
||
|
//判断待处理数据对象是否为空
|
||
|
if (dataMo != null)
|
||
|
{
|
||
|
//判断待处理对象执行处理方法是否为空
|
||
|
if (dataMo.dataFun != null)
|
||
|
{
|
||
|
//判断待处理数据是否为空
|
||
|
if (!string.IsNullOrWhiteSpace(dataMo.dataStr))
|
||
|
{
|
||
|
//调用数据处理方法
|
||
|
dataMo.dataFun(dataMo.dataStr);
|
||
|
//释放对象
|
||
|
dataMo.Dispose();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
string str = "队列处理数据异常:" + ex;
|
||
|
MyLogHelper.WriteMsg(new FangYar.Common.LogInfoMo() { message = str, msgType = FangYar.Common.EnumLogMsgTypeEnum.Error, path = "QueueDevice" });
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
string str = "队列添加数据异常:" + ex;
|
||
|
MyLogHelper.WriteMsg(new FangYar.Common.LogInfoMo() { message = str, msgType = FangYar.Common.EnumLogMsgTypeEnum.Error, path = "QueueDevice" });
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 获取硬件接口上传数据字符串
|
||
|
/// </summary>
|
||
|
/// <param name="context"></param>
|
||
|
/// <returns></returns>
|
||
|
public static string GetHttpContextDataStr(HttpContext context)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
HttpRequest request = context.Request;
|
||
|
Stream stream = request.InputStream;
|
||
|
string data = string.Empty;
|
||
|
StreamReader streamReader = new StreamReader(stream);
|
||
|
data = streamReader.ReadToEnd();
|
||
|
return data;
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
string str = "队列获取数据字符串异常:" + ex;
|
||
|
MyLogHelper.WriteMsg(new FangYar.Common.LogInfoMo() { message = str, msgType = FangYar.Common.EnumLogMsgTypeEnum.Error, path = "QueueDevice" });
|
||
|
}
|
||
|
return "";
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 待处理数据模型
|
||
|
/// </summary>
|
||
|
public class QueueDataMo : IDisposable
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// 待处理字符串
|
||
|
/// </summary>
|
||
|
public string dataStr { get; set; }
|
||
|
/// <summary>
|
||
|
/// 数据处理方法
|
||
|
/// </summary>
|
||
|
public DataFun dataFun { get; set; }
|
||
|
/// <summary>
|
||
|
/// 释放资源
|
||
|
/// </summary>
|
||
|
public void Dispose()
|
||
|
{
|
||
|
dataStr = null;
|
||
|
dataFun = null;
|
||
|
GC.Collect();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 代理处理数据方法
|
||
|
/// </summary>
|
||
|
/// <param name="dataStr"></param>
|
||
|
public delegate string DataFun(string dataStr);
|
||
|
|
||
|
}
|