ulysseskao
2016-04-29 b0c18d369abd06075c83759b0e19823c2a11d716
CCSTrace/CCS/Object/AlarmData.cs
@@ -1,217 +1,212 @@
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;
        private Hashtable _mTotalData = new Hashtable();
        private RecordLog _pLog;
        private OracleConnection _connectionTpc;
        private OracleTransaction _transaction;
        public AlarmData(OracleConnection _Conn,OracleTransaction _Trx, RecordLog _Log)
        public AlarmData(OracleConnection conn, OracleTransaction trx, RecordLog log)
        {
            _ConnectionTPC = _Conn;
            _Transaction = _Trx;
            _PLog = _Log;
            _connectionTpc = conn;
            _transaction = trx;
            _pLog = log;
            String SqlStmt;
            string sqlStmt;
            SqlStmt = "SELECT DEPTID,IP1,IP2,IP_PATROL FROM CCS.ALARM_IP";
            OracleCommand Command = new OracleCommand(SqlStmt, _ConnectionTPC, _Transaction);
            OracleDataReader reader = Command.ExecuteReader();
            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());
                    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);
                    _mTotalData.Add(Convert.ToInt32(reader["DeptID"].ToString()), tmp);
                }
            }
            catch (Exception e)
            {
                _PLog.Error("Problems occur when fetch alarm data: (" + e.Message + ")");
                _pLog.Error("Problems occur when fetch alarm data: (" + e.Message + ")");
                Console.WriteLine(e.StackTrace);
            }
            finally
            {
            {
                reader.Close();
                Command.Dispose();
                command.Dispose();
            }
        }
        private int getSSCCode(int m_SCCode, int m_Week) {
            int ssccode = m_SCCode;
        private int GetSscCode(int mScCode, int mWeek)
        {
            int ssccode = mScCode;
/*            DateTime stime = DateTime.Now;
            /*            DateTime stime = DateTime.Now;
            DateTime endtime = 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"));
                        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;
                    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 + ")");
            }*/
                        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)];
        private ArrayList GetIpAddress(int deptCode)
        {
            return (ArrayList)_mTotalData[Convert.ToInt32(deptCode)];
        }
        public bool alarm(String AcceptNum, int Dept)
        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;
            _pLog.Info("開始Alarm...");
            Socket mClient;
            ArrayList ipAddressSet = GetIpAddress(GetSscCode(dept, Convert.ToInt32(DateTime.Now.DayOfWeek)));
            string ip1;
            string ip2;
            string ipPatrol;
            System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
            if (IPAddressSet != null)
            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!");
                _pLog.Info("已取得該部門所需通知的IP Address.");
                ip1 = ipAddressSet[1].ToString();
                ip2 = ipAddressSet[2].ToString();
                ipPatrol = 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
            {
                IPAddress serverIp = IPAddress.Parse(ip1);
                int serverPort = Convert.ToInt32(GlobalVariable.AlermPort);
                IPEndPoint iep = new IPEndPoint(serverIp, serverPort);
                if (Command.ExecuteNonQuery() <= 0)
                    _PLog.Warn("無法紀錄Alarm IP!");
                mClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                mClient.Connect(iep);
                byte[] byteMessage = encoding.GetBytes(acceptNum);
                mClient.Send(byteMessage);
                mClient.Shutdown(SocketShutdown.Both);
                mClient.Close();
                RecordAlarmIp(acceptNum, ip1);
                _pLog.Info("已成功通知" + ip1 + " !");
                return true;
            }
            catch (Exception e)
            {
                _PLog.Error("recordAlarmIP Error." + e.Message);
                _pLog.Warn("無法通知" + ip1 + ": " + e.Message);
            }
            try
            {
                IPAddress serverIp = IPAddress.Parse(ip2);
                int serverPort = Convert.ToInt32(GlobalVariable.AlermPort);
                IPEndPoint iep = new IPEndPoint(serverIp, serverPort);
                mClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                mClient.Connect(iep);
                byte[] byteMessage = encoding.GetBytes(acceptNum);
                mClient.Send(byteMessage);
                mClient.Shutdown(SocketShutdown.Both);
                mClient.Close();
                RecordAlarmIp(acceptNum, ip2);
                _pLog.Info("已成功通知" + ip2 + " !");
                return true;
            }
            catch (Exception e)
            {
                _pLog.Warn("無法通知" + ip2 + ": " + e.Message);
            }
            try
            {
                IPAddress serverIp = IPAddress.Parse(ipPatrol);
                int serverPort = Convert.ToInt32(GlobalVariable.AlermPort);
                IPEndPoint iep = new IPEndPoint(serverIp, serverPort);
                mClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                mClient.Connect(iep);
                byte[] byteMessage = encoding.GetBytes(acceptNum);
                mClient.Send(byteMessage);
                mClient.Shutdown(SocketShutdown.Both);
                mClient.Close();
                RecordAlarmIp(acceptNum, ipPatrol);
                _pLog.Info("已成功通知" + ipPatrol + " !");
                return true;
            }
            catch (Exception e)
            {
                _pLog.Warn("無法通知" + ipPatrol + ": " + 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();
                command.Dispose();
            }
        }
    }
}
}