using Newtonsoft.Json; using Newtonsoft.Json.Linq; using RestHttp; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Configuration; using System.Data; using System.IO; using System.Linq; using System.Net; using System.Threading; using System.Threading.Tasks; using System.Web; using System.Web.Caching; namespace HardWareInteface.Common { public class hikAlarmQueueTask { private static FangYar.BLL.TBL_ALARMDATA aBll = new FangYar.BLL.TBL_ALARMDATA(); // 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 model) { try { infoQueue.Enqueue(model); //添加 } catch (Exception e) { WriteLog(e.Message); } WriteLog("新疆队列长度:" + infoQueue.Count.ToString()); } private static void DealInfoList() { while (!m_DealToken.IsCancellationRequested) { string dataInfo = null; if (infoQueue.TryDequeue(out dataInfo)) { try { WriteLog("开始处理..."); DealInfo(dataInfo); WriteLog("结束处理..."); } catch (Exception ex) { WriteLog(ex.Source + ex.StackTrace + ex.Message + "DealInfoList_Error"); } } else { WriteLog("新疆队列长度:" + infoQueue.Count.ToString() + ",正在等待..."); Thread.Sleep(100); } } } private static string DealInfo(string data) { try { //data = "{\"alarmTime\":1627919606000,\"channel\":\"11\",\"channelType\":\"10300\",\"devSerial\":\"G99356847\",\"alarmId\":\"20210802235327-E35064652-11-10014\",\"alarmType\":\"10014\",\"channelName\":\"东南1号向西\",\"pictureUrl\":[\"https://i.ys7.com/streamer/alarm/url/get?fileId=20210802235327-E35064652-11-10014-2-1&deviceSerialNo=E35064652&cn=11&isEncrypted=0&isCloudStored=0&ct=1&lc=7&bn=1_hikalarm&isDevVideo=0\"]}"; //data = "{\"alarmTime\":1627919606000,\"channel\":\"8\",\"channelType\":\"10300\",\"devSerial\":\"G16833582\",\"alarmId\":\"20210802235327-E35064652-11-10014\",\"alarmType\":\"10014\",\"channelName\":\"东南1号向西\",\"pictureUrl\":[\"https://dn-od4ir4su9.qbox.me/7/G16833582_8_20210822020009-G16833582-8-10014-2-1?e=1630395821&token=5JNHy55nNlr5InRTUltuUGUbGkZQb9Vpzf_Ko-XH:0Bjgn_WFVsYQA0nTkVpy_3V4XN8=\"]}"; data = System.Web.HttpUtility.UrlDecode(data, System.Text.Encoding.UTF8); #region 用于传递数据的输出 WriteLog(data.ToString()); #endregion //FangYar.FYMQTT.MQTT2 mq = new FangYar.FYMQTT.MQTT2(); //mq.messagePublished(data, "video-http-hk/abc"); string redata_string = data.Replace(Environment.NewLine, ""); string redata_string_json = "["; if (redata_string.Contains("} {")) { redata_string = redata_string.Replace("} {", "},{"); } else if (redata_string.Contains("}{")) { redata_string = redata_string.Replace("}{", "},{"); } redata_string_json += redata_string; redata_string_json += "]"; try { JArray jsonStr = JsonConvert.DeserializeObject(redata_string_json); } catch (Exception e) { WriteLog("\r\n 数据有误: 字符串不是json格式"); } List ar_data = JsonConvert.DeserializeObject>(redata_string_json); FangYar.BLL.TBL_CAMERA bll_cam = new FangYar.BLL.TBL_CAMERA(); for (int i = 0; i < ar_data.Count; i++) { //判断是否该条报警为重复上报,已存在于缓存中 object cameraAlarmTime = FangYar.Common.CacheHelper.GetCache(ar_data[i].devSerial + "_" + ar_data[i].channel + "_" + ar_data[i].alarmTime + "_" + ar_data[i].alarmType); if (cameraAlarmTime != null) { return ""; // } else { FangYar.Common.CacheHelper.SetCache(ar_data[i].devSerial + "_" + ar_data[i].channel + "_" + ar_data[i].alarmTime + "_" + ar_data[i].alarmType, true, Cache.NoAbsoluteExpiration, new TimeSpan(7, 0, 0, 0, 0)); #region 根据设备序列号获取设备所属机构;判断是否为西藏设备,如果是择转发西藏ashx处理程序 //将设备的序列号和所属西藏、新疆类型 存入缓存 object orgAreaType = FangYar.Common.CacheHelper.GetCache(ar_data[i].devSerial); if (orgAreaType == null) { DataTable orgDt = FangYar.Common.MySqlHelper.QueryTable("select ORG_ID,ORG_NAME,TYPE from tbl_device_org where devSerial = '" + ar_data[i].devSerial + "'"); if (orgDt.Rows.Count > 0) { FangYar.Common.CacheHelper.SetCache(ar_data[i].devSerial, orgDt.Rows[0]["TYPE"].ToString(), Cache.NoAbsoluteExpiration, new TimeSpan(7, 0, 0, 0, 0)); //将时间间隔初始化为7天0小时0分0秒0毫秒 if (orgDt.Rows[0]["TYPE"].ToString() == "1") { try { string xzIp = ConfigurationManager.AppSettings["XZHardWareIntefaceIP"]; var xzRsp = RestHttpRequest.HttpPost("http://" + xzIp + ":8310/Handle/hikAlarmReceptionXZ.ashx", data); } catch (Exception e) { WriteLog(e.Message); } } else { DataWarehousing(ar_data[i]); } } else { DataWarehousing(ar_data[i]); } } else { if (orgAreaType.ToString() == "1") { try { string xzIp = ConfigurationManager.AppSettings["XZHardWareIntefaceIP"]; var xzRsp = RestHttpRequest.HttpPost("http://" + xzIp + ":8310/Handle/hikAlarmReceptionXZ.ashx", data); } catch (Exception e) { WriteLog(e.Message); } } else { DataWarehousing(ar_data[i]); } } } #endregion } } catch (Exception e) { WriteLog(e.Message); } return ""; } public static void DataWarehousing(FangYar.Model.AlarmReception_DATA dataInfo) { FangYar.Model.OA.TBL_ALARMDATA amodel = new FangYar.Model.OA.TBL_ALARMDATA(); amodel.ID = Guid.NewGuid().ToString("N"); amodel.DVC_ID = dataInfo.devSerial; amodel.TYPE = "0"; System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区 DateTime dt = startTime.AddTicks(dataInfo.alarmTime * 10000); amodel.TIME = dt; amodel.ALARM_TYPE = "0"; amodel.CHANNEL = dataInfo.channel; amodel.CHANNELTYPE = dataInfo.channelType; amodel.CUSTOMINFO = dataInfo.customInfo; amodel.DEVSERIAL = dataInfo.devSerial; amodel.ALARMID = dataInfo.alarmId; amodel.ALARMTYPE = dataInfo.alarmType; amodel.CHANNELNAME = dataInfo.channelName; string picsrcs = ""; for (int y = 0; y < dataInfo.pictureUrl.Length; y++) { WebClient client = new WebClient(); byte[] byteArray = client.DownloadData(dataInfo.pictureUrl[y]); if (y == 0) { picsrcs = GetFilePath(byteArray, dataInfo.devSerial); } else { picsrcs += "," + GetFilePath(byteArray, dataInfo.devSerial); } } amodel.PICSRC = picsrcs; #region 老版固定设备 switch (dataInfo.devSerial) { case "E35064652": amodel.ORG_ID = "362AD769D3B74C8D9BC20A1DC0E9CC92"; amodel.ADDR = "哈密消防支队"; break; case "G16833582": amodel.ORG_ID = "021933cc9b11441c920a431eeffe60c2"; amodel.ADDR = "特勤大队一站"; break; case "G46238993": amodel.ORG_ID = "01c56fc1145a48e1b3690460a287de12"; amodel.ADDR = "西一路消防救援站"; break; case "J04022168": amodel.ORG_ID = "CEC6CC9DB57E4F43A61A07A9DCD1BF39"; amodel.ADDR = "南子午路消防站"; break; case "G46239003": amodel.ORG_ID = "f9a14b93970644fa8b6d8bfca3418b9b"; amodel.ADDR = "北八路特勤站"; break; case "F24995053": amodel.ORG_ID = "9d567172979c4e77a2ab30344943106b"; amodel.ADDR = "天山路消防救援站"; break; } #endregion bool isadd = aBll.Add(amodel); WriteLog("结果:" + isadd + "_" + amodel.DVC_ID + "_" + amodel.TIME); } /// /// base转图片保存到服务器 /// /// private static string GetFilePath(byte[] bytes, string devSerial) { string fileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString();//年月 string ImageFilePath = "/Attachment" + "/hikAlarmReception/" + devSerial + "/" + 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) { FangYar.Common.MyLogHelper.WriteMsg(new FangYar.Common.LogInfoMo() { message = msg, msgType = FangYar.Common.EnumLogMsgTypeEnum.Error, path = "hikAlarmReceptionXJ" }); //try //{ // LogWriteLock.EnterWriteLock(); // string path = AppDomain.CurrentDomain.BaseDirectory + "log\\hikAlarmReceptionXJ\\"; // 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(); //} } } }