using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Collections; using System.Data.OracleClient; using System.Net; using System.Net.Sockets; using System.IO; namespace CCSTrace.CCS.Object { public class AlarmData { private Hashtable m_TotalData = new Hashtable(); private RecordLog _PLog; private OracleConnection _ConnectionTPC; private OracleTransaction _Transaction; public AlarmData(OracleConnection _Conn,OracleTransaction _Trx, RecordLog _Log) { _ConnectionTPC = _Conn; _Transaction = _Trx; _PLog = _Log; String SqlStmt; SqlStmt = "SELECT DEPTID,IP1,IP2,IP_PATROL FROM CCS.ALARM_IP"; OracleCommand Command = new OracleCommand(SqlStmt, _ConnectionTPC, _Transaction); OracleDataReader reader = Command.ExecuteReader(); try { while (reader.Read()) { ArrayList Tmp = new ArrayList(); Tmp.Add(Convert.ToInt32(reader["DEPTID"].ToString())); Tmp.Add(reader["IP1"].ToString()); Tmp.Add(reader["IP2"].ToString()); Tmp.Add(reader["IP_Patrol"].ToString()); m_TotalData.Add(Convert.ToInt32(reader["DeptID"].ToString()), Tmp); } } catch (Exception e) { _PLog.Error("Problems occur when fetch alarm data: (" + e.Message + ")"); Console.WriteLine(e.StackTrace); } finally { reader.Close(); Command.Dispose(); } } private int getSSCCode(int m_SCCode, int m_Week) { int ssccode = m_SCCode; /* DateTime stime = DateTime.Now; DateTime endtime = DateTime.Now; String SqlStmt = "select g.ssc_code as SSCCODE,t.begintime as BEGINTIME,t.endtime as ENDTIME,t.week as WEEK from ccs.scgroup g,ccs.scgrouptime t where g.groupid=t.groupid and g.sc_code=" + m_SCCode + " and t.week=" + m_Week; try { OracleCommand Command = new OracleCommand(SqlStmt, _ConnectionTPC); OracleDataReader reader = Command.ExecuteReader(); while (reader.Read()) { stime.set(Calendar.DAY_OF_WEEK, Rs.getInt("WEEK")); endtime.set(Calendar.DAY_OF_WEEK, Rs.getInt("WEEK")); stime.set(Calendar.HOUR_OF_DAY, Rs.getInt("BEGINTIME")); endtime.set(Calendar.HOUR_OF_DAY, Rs.getInt("ENDTIME")); if (stime.after(endtime)) endtime.roll(Calendar.DAY_OF_WEEK, 1); if (stime.before(nowtime) && (endtime.after(nowtime))) ssccode = Rs.getInt("SSCCODE"); } reader.Close(); Command.Dispose(); } catch (Exception ex) { _PLog.warn("無法取得SSCCode,以該案件所屬SCCode代替: (" + ex.Message + ")"); }*/ return ssccode; } private ArrayList getIPAddress(int DeptCode) { return (ArrayList) m_TotalData[Convert.ToInt32(DeptCode)]; } public bool alarm(String AcceptNum, int Dept) { _PLog.Info("開始Alarm..."); System.Net.Sockets.Socket m_Client; ArrayList IPAddressSet = this.getIPAddress(this.getSSCCode(Dept, Convert.ToInt32(DateTime.Now.DayOfWeek))); String IP1; String IP2; String IP_Patrol; System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); if (IPAddressSet != null) { _PLog.Info("已取得該部門所需通知的IP Address."); IP1 = IPAddressSet[1].ToString(); IP2 = IPAddressSet[2].ToString(); IP_Patrol = IPAddressSet[3].ToString(); } else { _PLog.Warn("無法取得該部門所需通知的IP Address!"); return false; } try { IPAddress serverIp = IPAddress.Parse(IP1); int serverPort = Convert.ToInt32(CCS.LocalVariable.AlermPort); IPEndPoint iep = new IPEndPoint(serverIp, serverPort); m_Client = new System.Net.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); m_Client.Connect(iep); byte[] byteMessage = encoding.GetBytes(AcceptNum); m_Client.Send(byteMessage); m_Client.Shutdown(SocketShutdown.Both); m_Client.Close(); this.recordAlarmIP(AcceptNum, IP1); _PLog.Info("已成功通知" + IP1 + " !"); return true; } catch (Exception e) { _PLog.Warn("無法通知" + IP1 + ": " + e.Message); } try { IPAddress serverIp = IPAddress.Parse(IP2); int serverPort = Convert.ToInt32(CCS.LocalVariable.AlermPort); IPEndPoint iep = new IPEndPoint(serverIp, serverPort); m_Client = new System.Net.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); m_Client.Connect(iep); byte[] byteMessage = encoding.GetBytes(AcceptNum); m_Client.Send(byteMessage); m_Client.Shutdown(SocketShutdown.Both); m_Client.Close(); this.recordAlarmIP(AcceptNum, IP2); _PLog.Info("已成功通知" + IP2 + " !"); return true; } catch (Exception e) { _PLog.Warn("無法通知" + IP2 + ": " + e.Message); } try { IPAddress serverIp = IPAddress.Parse(IP_Patrol); int serverPort = Convert.ToInt32(CCS.LocalVariable.AlermPort); IPEndPoint iep = new IPEndPoint(serverIp, serverPort); m_Client = new System.Net.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); m_Client.Connect(iep); byte[] byteMessage = encoding.GetBytes(AcceptNum); m_Client.Send(byteMessage); m_Client.Shutdown(SocketShutdown.Both); m_Client.Close(); this.recordAlarmIP(AcceptNum, IP_Patrol); _PLog.Info("已成功通知" + IP_Patrol + " !"); return true; } catch (Exception e) { _PLog.Warn("無法通知" + IP_Patrol + ": " + e.Message); _PLog.Warn("三個IP均無法通知到!"); return false; } } private void recordAlarmIP(String AccNum, String IP) { String SqlStmt = "insert into eos.case_dispatch (acceptnum,alarmip) values('" + AccNum + "','" + IP + "')"; OracleCommand Command = new OracleCommand(SqlStmt, _ConnectionTPC, _Transaction); try { if (Command.ExecuteNonQuery() <= 0) _PLog.Warn("無法紀錄Alarm IP!"); } catch (Exception e) { _PLog.Error("recordAlarmIP Error." + e.Message); return; } finally { Command.Dispose(); } } } }