using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; using System.Web; namespace HardWareInteface.Common { public class SHZQueueTask { public class dataInfo { public string data { get; set; } public string docPath { get; set; } } const string PICPATH = "shzxfzd"; const string LOGPATH = "shzxfzd"; private static FangYar.BLL.TBL_PERSONACCESS bll = new FangYar.BLL.TBL_PERSONACCESS(); public static ConcurrentQueue infoQueue = new ConcurrentQueue(); private static CancellationTokenSource m_DealToken = new CancellationTokenSource(); private static Task m_DealTask = Task.Factory.StartNew(DealInfoList, m_DealToken.Token); private static int i = 0; public static void infoQueueFun(string data, string PICPATH) { try { infoQueue.Enqueue(new dataInfo { data = data, docPath = PICPATH }); //添加 } catch (Exception e) { WriteLog(e.Message, LOGPATH); } WriteLog("队列长度:" + infoQueue.Count.ToString(), LOGPATH); } private static void DealInfoList() { while (!m_DealToken.IsCancellationRequested) { dataInfo dataInfo = null; if (infoQueue.TryDequeue(out dataInfo)) { try { DealInfo(dataInfo); } catch (Exception ex) { WriteLog(ex.Source + ex.StackTrace + ex.Message + "DealInfoList_Error", dataInfo.docPath); } } else { Thread.Sleep(100); } } } private static void DealInfo(dataInfo dataInfo) { bool flag = false; try { WriteLog("JSON字符串转换前输出:" + dataInfo.data, dataInfo.docPath); JObject jsonStr = JsonConvert.DeserializeObject(dataInfo.data); flag = true; } catch (Exception e) { flag = false; } if (flag) { FangYar.Model.SB_DATA rz_data = JsonConvert.DeserializeObject(dataInfo.data); if (rz_data.personType == "0") { WriteLog("false:EmployeeTypeStr为0,验证无效!", dataInfo.docPath); } else { string avatar = rz_data.path.Replace("\u003d", "="); byte[] avatar_byte = Convert.FromBase64String(avatar); string ImagePath = GetFilePath(avatar_byte, dataInfo.docPath); rz_data.path = ImagePath; string isadd = bll.HikASCData_Add(rz_data); WriteLog("结果:" + isadd + "_" + rz_data.name + "_" + rz_data.time, dataInfo.docPath); } } else { WriteLog("\r\n 数据有误: 字符串不是json格式", dataInfo.docPath); } } //private static void DealInfo(string data) //{ // bool flag = false; // try // { // WriteLog("JSON字符串转换前输出:" + data); // JObject jsonStr = JsonConvert.DeserializeObject(data); // flag = true; // } // catch (Exception e) // { // flag = false; // } // if (flag) // { // FangYar.Model.SB_DATA rz_data = JsonConvert.DeserializeObject(data); // if (rz_data.personType == "0") // { // WriteLog("false:EmployeeTypeStr为0,验证无效!"); // } // else // { // string avatar = rz_data.path.Replace("\u003d", "="); // byte[] avatar_byte = Convert.FromBase64String(avatar); // string ImagePath = GetFilePath(avatar_byte); // rz_data.path = ImagePath; // string isadd = bll.HikASCData_Add(rz_data); // WriteLog("结果:" +isadd+"_"+ rz_data.name+"_"+ rz_data.time); // } // } // else // { // WriteLog("\r\n 数据有误: 字符串不是json格式"); // } //} /// /// base转图片保存到服务器 /// /// private static string GetFilePath(byte[] bytes, string picPath) { string fileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString();//年月 string ImageFilePath = "/Attachment" + "/Person/hik/" + picPath + "/" + DateTime.Now.Year.ToString() + "/" + DateTime.Now.Month.ToString(); //图片保存地址 string imgSavePath = System.Configuration.ConfigurationManager.AppSettings["imgSavePath"] + ""; if (HttpContext.Current != null) //web应用程序 单线程 { if (System.IO.Directory.Exists(HttpContext.Current.Server.MapPath(ImageFilePath)) == false)//如果不存在就创建文件夹 { System.IO.Directory.CreateDirectory(HttpContext.Current.Server.MapPath(ImageFilePath)); } } else //非web程序引用 多线程 { //ImageFilePath = ImageFilePath.Replace("/", "\\"); string filePath = imgSavePath + ImageFilePath; if (System.IO.Directory.Exists(filePath) == false)//如果不存在就创建文件夹 { System.IO.Directory.CreateDirectory(filePath); } } string ImagePath; string str = System.DateTime.Now.ToString("yyyyHHddHHmmssfff") + Guid.NewGuid().ToString(); ImagePath = imgSavePath + ImageFilePath + "/" + str;//定义图片名称 File.WriteAllBytes(ImagePath + ".png", bytes); //保存图片到服务器,然后获取路径 return ImageFilePath + "/" + str + ".png";//获取保存后的路径 } static ReaderWriterLockSlim LogWriteLock = new ReaderWriterLockSlim(); private static void WriteLog(string msg, string docPath) { FangYar.Common.MyLogHelper.WriteMsg(new FangYar.Common.LogInfoMo() { message = msg, msgType = FangYar.Common.EnumLogMsgTypeEnum.Error, path = "HIKperson\\" + docPath }); //try //{ // LogWriteLock.EnterWriteLock(); // string path = AppDomain.CurrentDomain.BaseDirectory + "log\\HIKperson\\"+ docPath + "\\"; // string filename = DateTime.Now.ToString("yyyyMMdd") + ".txt"; // if (Directory.Exists(path) == false)//如果不存在就创建文件夹 // { // Directory.CreateDirectory(path); // } // FileInfo file = new FileInfo(path + filename); // if (!file.Exists) // { // try // { // FileStream fs; // fs = File.Create(path + filename); // fs.Close(); // } // catch // { // } // } // using (FileStream fs = new FileStream(path + filename, FileMode.Append, FileAccess.Write)) // { // using (StreamWriter sw = new StreamWriter(fs)) // { // sw.WriteLine("时间:" + DateTime.Now.ToString() + " 信息:" + msg); // } // } //} //catch (Exception e) //{ //} //finally //{ // LogWriteLock.ExitWriteLock(); //} } } }