private int identity; //ID /* * public void Job_Timeout(object sender, EventArgs e) * { * bool IsConnected = (bool)sender; * database.APServer_Timeout(IsConnected); * Log.write("APMsgHandler-Job_APServer_Timeout"); * } */ public APMsgHandler(UDPSocket udpSocket, DB_APMsgHandler database, List <APParser> apOrderList, int identity) { this.udpSocket = udpSocket; this.database = database; this.apOrderList = apOrderList; this.identity = identity; isWaiting = true; apEncapsulator = new APEncapsulator(); apHandlerThread = new Thread(new ThreadStart(APHandleFunc)); }
private void Job_Timer(object source, System.Timers.ElapsedEventArgs e) { if (!bInit) { bInit = true; Init(); return; } if (db_Base != null) { if (bRunningNormal && db_Base.Status() == ConnectionState.Open) { return; } } if (configManager == null || dbOrderMonitor == null || db_Base == null) { return; } bool bRestart = true; string server_address = configManager.dbConfig.db.address; // "192.168.0.200,2433"; string server_name = configManager.dbConfig.users.userName; //"sa"; string server_password = configManager.dbConfig.users.password; // "witop_402507"; string table = configManager.dbConfig.db.table; //"FilesDB"; if (dbOrderMonitor != null) { dbOrderMonitor.Close(); if (!dbOrderMonitor.Open(server_address, server_name, server_password, table)) { Log.write("Open databse failed!", 1); bRunningNormal = false; bRestart = false; return; } } if (db_Base != null) { db_Base.Close(); if (!db_Base.Open(server_address, server_name, server_password, table)) { Log.write("Open database failed!", 1); bRunningNormal = false; bRestart = false; return; } } if (db_DBMsgHandler != null) { db_DBMsgHandler.Close(); if (!db_DBMsgHandler.Open(server_address, server_name, server_password, table)) { Log.write("Open database failed!", 1); bRunningNormal = false; bRestart = false; return; } } if (db_AlarmHandler != null) { db_AlarmHandler.Close(); if (!db_AlarmHandler.Open(server_address, server_name, server_password, table)) { Log.write("Open database failed!", 1); bRunningNormal = false; bRestart = false; return; } } if (db_APLotHandler != null) { db_APLotHandler.Close(); if (!db_APLotHandler.Open(server_address, server_name, server_password, table)) { Log.write("Open database failed!", 1); bRunningNormal = false; bRestart = false; return; } } for (int i = 0; i < DB_ApMsgHandlerList.Count; i++) { DB_APMsgHandler db_apMsgHandler = DB_ApMsgHandlerList[i]; if (db_apMsgHandler != null) { db_apMsgHandler.Close(); if (!db_apMsgHandler.Open(server_address, server_name, server_password, table)) { Log.write("Open database failed!", 1); bRunningNormal = false; bRestart = false; return; } } } if (bRestart) { bRunningNormal = true; UpdateAllStatus(); Log.write("Update manager server status", 1); if (!db_Base.ManagerServer_Status(true)) { bRunningNormal = false; } } }
public bool Init() { //List创建 try { Log.write("Launching service"); apOrderList = new List <APParser>(); dbResultList = new List <APParser>(); dbOrderList = new List <APParser>(); alarmList = new List <APParser>(); apLotList = new List <APParser>(); apMsgHandlerList = new List <APMsgHandler>(); DB_ApMsgHandlerList = new List <DB_APMsgHandler>(); apEncapsulator = new APEncapsulator(); Log.write("Initilizing Scheduler"); scheduler = new Scheduler(apMsgHandlerList, apOrderList); //本地數據 Log.write("Get Local data", 1); configManager = new ConfigManager(); string server_address = configManager.dbConfig.db.address; // "192.168.0.200,2433"; string server_name = configManager.dbConfig.users.userName; //"sa"; string server_password = configManager.dbConfig.users.password; // "witop_402507"; string table = configManager.dbConfig.db.table; //"FilesDB"; int cmd_enable = Convert.ToInt32(configManager.serviceConfig.record.cmd_enable); count_Handler = Convert.ToInt32(configManager.serviceConfig.APthread.count); int bufferSize = Convert.ToInt32(configManager.serviceConfig.record.Buffer); Log.level = Convert.ToInt32(configManager.serviceConfig.log.level); string APPath = configManager.serviceConfig.AP.path; //Launch AP //RunBat(APPath); /*DateTime time = new DateTime(2017, 04, 19, 19,30, 00); * string resultFile = ""; * if (videoFilesManager.PathExist(time, 1)) * { * videoFilesManager.GetFile(time, 1, ref resultFile); * }//get the files*/ //对象创建 Log.write("Initialize HikVision", 1); hikvision = new HIKVISION(); if (hikvision.Init()) { Log.write("HikVision Initialize succeed", 1); if (hikvision.login_IP("192.168.0.92", 8000, "admin", "admin123")) { Log.write("HikVision login succeed!", 1); } else { Log.write("HikVision login failed!", 1); } } else { Log.write("HikVision Initialize failed!", 1); } Log.write("Initialize UDP Socket", 1); udpsocket = new UDPSocket(apOrderList, dbResultList, alarmList, apLotList, bufferSize); string strLog = "Connect to database-server_address:" + server_address + " server_name:" + server_name + " table:" + table; Log.write(strLog, 2); //AP多线程处理 for (int i = 0; i < count_Handler; i++) { Log.write("Initialize DB_APMsgHandler" + i.ToString(), 1); DB_APMsgHandler db_APMsgHandler = new DB_APMsgHandler(cmd_enable, i); Log.write("Initialize APMsgHandler" + i.ToString(), 1); APMsgHandler apMsgHandler = new APMsgHandler(udpsocket, db_APMsgHandler, apOrderList, i); if (!db_APMsgHandler.Open(server_address, server_name, server_password, table)) { Log.write("Open database " + i.ToString() + " failed!", 1); bRunningNormal = false; } DB_ApMsgHandlerList.Add(db_APMsgHandler); apMsgHandlerList.Add(apMsgHandler); } //数据库DB_Base Log.write("Initialize DB_Base", 1); db_Base = new DB_Core(cmd_enable); if (!db_Base.Open(server_address, server_name, server_password, table)) { Log.write("Open database failed!", 1); bRunningNormal = false; } //数据库db_DBMsgHandler Log.write("Initialize db_DBMsgHandler", 1); db_DBMsgHandler = new DB_DBMsgHandler(cmd_enable); if (!db_DBMsgHandler.Open(server_address, server_name, server_password, table)) { Log.write("Open database failed!", 1); bRunningNormal = false; } //数据库db_DBMsgHandler Log.write("Initialize db_AlarmHandler", 1); db_AlarmHandler = new DB_AlarmHandler(cmd_enable, 1); if (!db_AlarmHandler.Open(server_address, server_name, server_password, table)) { Log.write("Open database failed!", 1); bRunningNormal = false; } //数据库dbOrderMonitor Log.write("Initialize dbOrderMonitor", 1); dbOrderMonitor = new DBOrderMonitor(dbOrderList); if (!dbOrderMonitor.Open(server_address, server_name, server_password, table)) { Log.write("Open database failed!", 1); bRunningNormal = false; } //数据库DB_APLotHandler Log.write("Initialize db_DB_APLotHandler", 1); db_APLotHandler = new DB_APLotHandler(cmd_enable, 1); if (!db_APLotHandler.Open(server_address, server_name, server_password, table)) { Log.write("Open database failed!", 1); bRunningNormal = false; } // Log.write("Initialize APMsgHandler", 1); //apMsgHandler = new APMsgHandler(udpsocket, database, apOrderList); //处理线程---------------- //Video operator videoFilesManager = new VideoFilesManager(db_Base, configManager.serviceConfig.Path.path_video, configManager.serviceConfig.Path.path_dest, configManager.serviceConfig.Path.IP, Convert.ToInt32(configManager.serviceConfig.Path.Port)); Log.write("Initialize DBMsgHandler", 1); dbMsgHandler = new DBMsgHandler(udpsocket, db_DBMsgHandler, dbOrderList, dbResultList); Log.write("Initialize alarmHandler", 1); alarmHandler = new AlarmHandler(udpsocket, alarmList, db_AlarmHandler, videoFilesManager); Log.write("Initialize alarmHandler", 1); apLotHandler = new APLotHandler(udpsocket, db_APLotHandler, apLotList, 1); return(true); } catch (Exception ex) { Log.write("Init:ERROR-" + ex.Message, 0); bRunningNormal = false; return(false); } finally { int listenPort = Convert.ToInt32(configManager.apConfig.local.localPort); //6668; string RemoteIP = configManager.apConfig.remote.remoteIP; // "192.168.0.27"; int RemotePort = Convert.ToInt32(configManager.apConfig.remote.remotePort); // 5555; string strLog = "Open UDP Port-Listen Port:" + listenPort.ToString() + " Remote IP:" + RemoteIP + " Remote Port:" + RemotePort; Log.write(strLog, 2); udpsocket.Init(listenPort, RemoteIP, RemotePort); udpsocket.Event_ResumeAPMsgHandler += scheduler.onWorking; //事件通知关联 udpsocket.Event_Timeout += this.Job_Timeout; //事件通知关联 udpsocket.Event_ResumeAPLotHandler += apLotHandler.Resume; //事件通知关联 udpsocket.Event_ResumeAlarmHandler += alarmHandler.Resume; dbOrderMonitor.Event_ResumeDBHandler += dbMsgHandler.Resume; //事件通知关联 Log.write("Starting thread", 1); udpsocket.Start(); for (int i = 0; i < apMsgHandlerList.Count; i++) { APMsgHandler apMsgHandler = apMsgHandlerList[i]; apMsgHandler.Start(); } //apMsgHandler.Start(); dbMsgHandler.Start(); dbOrderMonitor.Start(); alarmHandler.Start(); apLotHandler.Start(); videoFilesManager.Start(); if (bRunningNormal) { UpdateAllStatus(); Log.write("Update manager server status", 1); if (!db_Base.ManagerServer_Status(true)) { bRunningNormal = false; } } bInit = true; } }
public void Close() { timeout.Enabled = false; if (videoFilesManager != null) { videoFilesManager.Stop(); }//stop video File Manager if (db_Base != null) { db_Base.ManagerServer_Status(false); }//update manager server status if (apMsgHandlerList != null) { for (int i = 0; i < apMsgHandlerList.Count; i++) { APMsgHandler apMsgHandler = apMsgHandlerList[i]; if (apMsgHandler != null) { apMsgHandler.Stop(); } } apMsgHandlerList.Clear(); }//Stop Threads apMsgHandler if (dbMsgHandler != null) { dbMsgHandler.Stop(); }//Stop Thread dbMsgHandler if (dbOrderMonitor != null) { dbOrderMonitor.Stop(); dbOrderMonitor.Close(); }//Stop Thread dbOrderMonitor if (alarmHandler != null) { alarmHandler.Stop(); }//Stop Thread alarmHandler if (apLotHandler != null) { apLotHandler.Stop(); }//stop Thread apLotHandler if (udpsocket != null) { udpsocket.Close(); }//stop Thread udpSocket if (db_Base != null) { db_Base.Close(); }//stop databse if (db_DBMsgHandler != null) { db_DBMsgHandler.Close(); }//stop databse DBMsgHandler if (db_AlarmHandler != null) { db_AlarmHandler.Close(); }//stop database AlarmHandler if (DB_ApMsgHandlerList != null) { for (int i = 0; i < DB_ApMsgHandlerList.Count; i++) { DB_APMsgHandler db_apMsgHandler = DB_ApMsgHandlerList[i]; if (db_apMsgHandler != null) { db_apMsgHandler.Close(); } } DB_ApMsgHandlerList.Clear(); }//stop databse ApMsgHandler if (db_APLotHandler != null) { db_APLotHandler.Close(); } if (hikvision != null) { hikvision.Close(); } bRunningNormal = false; }