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();
|
}
|
}
|
}
|
}
|