ulysseskao
2016-05-01 8f5c8891aba521570fa63c4baf616e3a3c8d9526
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
using System;
 
using System.Collections;
using System.Data.OracleClient;
using System.Net;
using System.Net.Sockets;
using NLog;
 
namespace CCSTrace.CCS.Object
{
    public class AlarmData
    {
        private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
        private Hashtable _mTotalData = new Hashtable();
        private OracleConnection _connectionTpc;
        private OracleTransaction _transaction;
 
        public AlarmData(OracleConnection conn, OracleTransaction trx)
        {
            _connectionTpc = conn;
            _transaction = trx;
 
            var 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());
 
                    _mTotalData.Add(Convert.ToInt32(reader["DeptID"].ToString()), tmp);
                }
            }
            catch (Exception e)
            {
                Logger.Error(e, "Problems occur when fetch alarm data: (" + e.Message + ")");
                Console.WriteLine(e.StackTrace);
            }
            finally
            {
                reader.Close();
                command.Dispose();
            }
        }
 
        private int GetSscCode(int mScCode, int mWeek)
        {
            int ssccode = mScCode;
 
            /*            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)_mTotalData[Convert.ToInt32(deptCode)];
        }
 
        public bool Alarm(string acceptNum, int dept)
        {
            Logger.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)
            {
                Logger.Info("已取得該部門所需通知的IP Address.");
                ip1 = ipAddressSet[1].ToString();
                ip2 = ipAddressSet[2].ToString();
                ipPatrol = ipAddressSet[3].ToString();
            }
            else
            {
                Logger.Warn("無法取得該部門所需通知的IP Address!");
                return false;
            }
 
            try
            {
                IPAddress serverIp = IPAddress.Parse(ip1);
                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, ip1);
                Logger.Info("已成功通知" + ip1 + " !");
                return true;
            }
            catch (Exception e)
            {
                Logger.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);
                Logger.Info("已成功通知" + ip2 + " !");
                return true;
            }
            catch (Exception e)
            {
                Logger.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);
                Logger.Info("已成功通知" + ipPatrol + " !");
                return true;
            }
            catch (Exception e)
            {
                Logger.Warn("無法通知" + ipPatrol + ": " + e.Message);
                Logger.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)
                    Logger.Warn("無法紀錄Alarm IP!");
            }
            catch (Exception e)
            {
                Logger.Error("recordAlarmIP Error." + e.Message);
                return;
            }
            finally
            {
                command.Dispose();
            }
        }
    }
}