using FangYar.BLL; using FangYar.BLL.TBL; using FangYar.Model.TBL; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using RestHttp; using RoadFlow.Data.Model.WorkFlowExecute; using RoadFlow.Platform; using System; using System.Collections; using System.Collections.Generic; using System.Configuration; using System.Data; using System.IO; using System.Linq; using System.Net; using System.Reflection; using System.Web; namespace FangYar.WebUI.Handle { /// /// 大华云睿 设备报警消息回调服务地址 /// public class dhyrAlarmTest : IHttpHandler { string UpFileAddr = System.Configuration.ConfigurationManager.AppSettings["SqlConnectionStr"]; public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain;charset=UTF-8"; string returnStr = ""; returnStr = ReceiveAlarmInfoPlate(context); context.Response.Write(returnStr); } private string ReceiveAlarmInfoPlate(HttpContext context) { WriteLog("进入!"); HttpRequest request = context.Request; Stream stream = request.InputStream; string data = string.Empty; string response = string.Empty; bool isadd = false; if (stream.Length != 0) { try { StreamReader streamReader = new StreamReader(stream); data = streamReader.ReadToEnd(); data = System.Web.HttpUtility.UrlDecode(data, System.Text.Encoding.UTF8); #region 用于传递数据的输出 if (stream.Length != 0) { WriteLog(data.ToString()); } #endregion else { WriteLog("\r\n 数据有误: 字符串不是json格式"); } } catch (Exception e) { WriteLog(e.Message); } } else { WriteLog("输入流的长度为0!"); } if (isadd == true) { return "true"; } else { return "false"; } } #region 自定义返回结果 public class ReutrnResult { public bool result { get; set; } public string msg { get; set; } } #endregion #region base转图片保存到服务器 /// /// base转图片保存到服务器 /// /// private 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(); if (Directory.Exists(HttpContext.Current.Server.MapPath(ImageFilePath)) == false)//如果不存在就创建文件夹 { Directory.CreateDirectory(HttpContext.Current.Server.MapPath(ImageFilePath)); } string ImagePath, DbImagePath; string str = DateTime.Now.ToString("yyyyHHddHHmmss"); DbImagePath = ImageFilePath + "/" + str;//定义图片名称 ImagePath = HttpContext.Current.Server.MapPath(ImageFilePath) + "/" + str;//定义图片名称 File.WriteAllBytes(ImagePath + ".png", bytes); //保存图片到服务器,然后获取路径 return DbImagePath + ".png";//获取保存后的路径 } #endregion #region 记录日志 /// /// 记录日志 /// /// private void WriteLog(string msg) { string path = AppDomain.CurrentDomain.BaseDirectory + "log\\hikAlarmReception\\"; string filename = DateTime.Now.ToString("yyyyMMdd") + ".txt"; if (Directory.Exists(path) == false)//如果不存在就创建文件夹 { Directory.CreateDirectory(path); } FileInfo file = new FileInfo(path + filename); if (!file.Exists) { FileStream fs; fs = File.Create(path + filename); fs.Close(); } using (FileStream fs = new FileStream(path + filename, FileMode.Append, FileAccess.Write)) { using (StreamWriter sw = new StreamWriter(fs)) { sw.WriteLine("时间:" + DateTime.Now.ToString() + " 信息:" + msg); } } } public bool IsReusable { get { return false; } } #endregion } }