/// <summary> /// 向web 上报事件 /// </summary> public void sendMsgToWebSocketClients(string strEventTheOne, string msg) { //发送数据Json格式 string strSendMSG = jscontrol.EventJson(strEventTheOne); Mainform.form1.SendMsgToWebSocketClients(strSendMSG); //更新UI界面 StaticUtils.ShowEventMsg(msg); //计数加一 StaticData.g_inAlarmNum++; }
/// <summary> /// 轮询获取 DB 中App端上报的event /// </summary> public void PollForAppEvent() { /// Design:服务器维护一个关于Event_assemble的DataTable /// 每次轮询更新DataTable ,find a new record and then send a message to clients by websocket; /// Point : if there are many new records found, consist them and the split is signal ','. InitEventTable(); MyJsonCtrl jscontrol = new MyJsonCtrl(); while (true) { refreshEventTable(); string theones = ""; List <string> warning_msg = new List <string>(); //StaticUtils.ShowEventMsg(Convert.ToString(last_PollForAppEvent_time) +" "+ StaticData.g_dtEventAssemble.Rows.Count+"\n"); if (StaticData.g_dtEventAssemble.Rows.Count > 0) { for (int i = 0; i < StaticData.g_dtEventAssemble.Rows.Count; i++) { if (i == StaticData.g_dtEventAssemble.Rows.Count - 1) { theones += StaticData.g_dtEventAssemble.Rows[i]["事件theone"]; } else { theones += StaticData.g_dtEventAssemble.Rows[i]["事件theone"] + ","; } warning_msg.Add("报警时间:" + StaticData.g_dtEventAssemble.Rows[i]["时间"] + " 事件类型:App故障事件上报 事件名称:" + StaticData.g_dtEventAssemble.Rows[i]["事件名称"] + "\n"); } if (!"".Equals(theones)) { //发送数据Json格式 string strSendMSG = jscontrol.EventJson(theones); Mainform.form1.SendMsgToWebSocketClients(strSendMSG); foreach (var item in warning_msg) { //更新UI界面 StaticUtils.ShowEventMsg(item); //计数加一 StaticData.g_inAlarmNum++; } } } Thread.Sleep(5 * 1000); // APP轮询 } }
//读取事件数据库,判断数据库是否存在该事件,且时间是否2分钟以上才再次触发,存在且是的话插入新事件 //不是的话更新endtime字段为当前时间 //不存在的话直接插入数据 private void CheckEvent(string strDevice, string strTarget_theone, string strEvent, string strTime, string strPosition, string srtCameraName, string[] paras, string channelName) { //事件比较特殊,可有可能同时都会调用,所以这里每次调用的时候,都新建一个数据库的conn DBConnect dbCheck = new DBConnect(); DBConnect dbInsORUp = new DBConnect(); string[] strReadDB = new string[] { }; try { //判断数据库状态并打开数据库连接 if (dbCheck.msConn.State == System.Data.ConnectionState.Closed) { dbCheck.msConn.Open(); } if (dbInsORUp.msConn.State == System.Data.ConnectionState.Closed) { dbInsORUp.msConn.Open(); } // string strCMD = "select theone,event,endtime from event;"; string strCMD = "select end_time from event_assemble where target_theone='" + strTarget_theone + "' ORDER BY id DESC LIMIT 1;"; MySqlCommand cmd = new MySqlCommand(strCMD, dbCheck.msConn); MySqlDataReader sqlReader = null; string currentMethodName = System.Reflection.MethodBase.GetCurrentMethod().Name; if (StaticUtils.DBReaderOperationException(ref cmd, ref sqlReader, currentClassName, currentMethodName)) { while (sqlReader.Read()) { strReadDB = new string[] { sqlReader.GetString(0) }; //逐条数据判断 //判断数据库中是否存在数据 if (strReadDB != null) { //比较endtime的时间和当前时间 //传入时间 DateTime data_create = Convert.ToDateTime(strTime); DateTime data_db = Convert.ToDateTime(strReadDB[0]); // 相同报警间隔time unit: s long time_interval = StaticUtils.GetCurrentTimeUnix(data_create) - StaticUtils.GetCurrentTimeUnix(data_db); if (time_interval < 120) { string strUpdate = "UPDATE event_assemble SET end_time='" + strTime + "' WHERE target_theone='" + strTarget_theone + "' AND end_time='" + strReadDB[0] + "';"; MySqlCommand cmdUpdate = new MySqlCommand(strUpdate, dbInsORUp.msConn); StaticUtils.DBNonQueryOperationException(ref cmd, currentClassName, currentMethodName); //跳出 goto here; } } } } string event_name = ""; //故障二级名称 //事件等级判断 string strLevel = ""; if (strEvent.Contains("信号丢失")) { strLevel = "一般情况"; event_name = "监控设备掉线"; } else if (strEvent.Contains("移动侦测")) { strLevel = "一般情况"; event_name = "监控移动侦测"; } else if (strEvent.Contains("穿越警戒面")) { strLevel = "重要情况"; event_name = "监控越界侦测"; } //生成eventtheon string strEventTheOne = Guid.NewGuid().ToString(); //插入数据 //往总表跟字表里面都插入数据 DataRow[] dr_abnormal_super = StaticData.g_dtAbnormalInfor.Select("故障父类名称='event_camera'"); DataRow[] dr_abnormal_sub = StaticData.g_dtAbnormalInfor.Select("父ID='" + dr_abnormal_super[0]["ID"] + "'"); string strInsert1 = "INSERT INTO event_assemble (event_name,event_theone,event_type,start_time,end_time,event_status,position,event_level,target_theone) values" + "('" + event_name + "','" + strEventTheOne + "','event_camera','" + DateTime.Now.ToString().Replace('/', '-') + "','" + DateTime.Now.ToString().Replace('/', '-') + "','未读','" + strPosition + "','" + strLevel + "','" + strTarget_theone + "');"; string strInsert2 = "INSERT INTO event_camera (event_theone,device_event_type,device_theone) values" + "('" + strEventTheOne + "','" + event_name + "','" + strTarget_theone + "');"; MySqlCommand cmdInsert = new MySqlCommand(strInsert1, dbInsORUp.msConn); StaticUtils.DBNonQueryOperationException(ref cmdInsert, currentClassName, currentMethodName); MySqlCommand cmdInsert2 = new MySqlCommand(strInsert2, dbInsORUp.msConn); StaticUtils.DBNonQueryOperationException(ref cmdInsert2, currentClassName, currentMethodName); //向web上报事件 string strSendMSG = jscontrol.EventJson(strEventTheOne); Mainform.form1.SendMsgToWebSocketClients(strSendMSG); //更新UI界面 StaticUtils.ShowEventMsg("报警时间:" + paras[0] + " NVRIP:" + paras[1] + " 事件类型:" + event_name + " 通道号:" + channelName + " 摄像机名称:" + strDevice + "\n"); //计数加一 StaticData.g_inAlarmNum++; here: dbInsORUp.msConn.Close(); sqlReader.Close(); dbCheck.msConn.Close(); } catch (Exception e) { StaticUtils.ShowEventMsg("HikvisionAlarmCallBackFun.class-CheckEvent : 插入摄像头报警事件出现异常!!\n"); } }