using System;
|
using System.Collections;
|
using System.Collections.Generic;
|
using System.Data.OracleClient;
|
using System.Net;
|
using System.Net.Sockets;
|
using CCSTrace.TPower.DMMS.Model.CCS;
|
using NLog;
|
|
namespace CCSTrace.CCS.Object
|
{
|
public class AlarmData
|
{
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
|
public static AlarmData Instance { get; } = new AlarmData();
|
|
private readonly Dictionary<int, ALARMIP> _totalAddrData = new Dictionary<int, ALARMIP>();
|
|
private AlarmData()
|
{
|
}
|
|
public void Initialize(OracleConnection conn)
|
{
|
var sqlStmt = "SELECT DEPTID,IP1,IP2,IP_PATROL FROM CCS.ALARM_IP";
|
OracleCommand command = new OracleCommand(sqlStmt, conn);
|
OracleDataReader reader = command.ExecuteReader();
|
|
try
|
{
|
while (reader.Read())
|
{
|
ALARMIP tmp = new ALARMIP
|
{
|
DEPTID = Convert.ToInt32(reader["DEPTID"].ToString()),
|
IP1 = reader["IP1"].ToString(),
|
IP2 = reader["IP2"].ToString(),
|
IPPATROL = reader["IP_Patrol"].ToString()
|
};
|
|
_totalAddrData.Add(tmp.DEPTID, tmp);
|
}
|
}
|
catch (Exception e)
|
{
|
Logger.Error(e, "Problems occur when fetch alarm data: (" + e.Message + ")");
|
}
|
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 ALARMIP GetIpAddress(int deptCode)
|
{
|
return _totalAddrData[deptCode];
|
}
|
|
public bool Alarm(OracleConnection conn, OracleTransaction trx, string acceptNum, int dept)
|
{
|
Logger.Info("開始Alarm...");
|
Socket clientSocket;
|
ALARMIP alarmip = GetIpAddress(GetSscCode(dept, Convert.ToInt32(DateTime.Now.DayOfWeek)));
|
string ip1;
|
string ip2;
|
string ipPatrol;
|
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
|
|
if (alarmip != null)
|
{
|
Logger.Info("已取得該部門所需通知的IP Address.");
|
ip1 = alarmip.IP1;
|
ip2 = alarmip.IP2;
|
ipPatrol = alarmip.IPPATROL;
|
}
|
else
|
{
|
Logger.Warn("無法取得該部門所需通知的IP Address!");
|
return false;
|
}
|
|
try
|
{
|
IPAddress serverIp = IPAddress.Parse(ip1);
|
var serverPort = Convert.ToInt32(GlobalVariable.AlermPort);
|
IPEndPoint iep = new IPEndPoint(serverIp, serverPort);
|
|
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
clientSocket.Connect(iep);
|
|
byte[] byteMessage = encoding.GetBytes(acceptNum);
|
|
clientSocket.Send(byteMessage);
|
clientSocket.Shutdown(SocketShutdown.Both);
|
clientSocket.Close();
|
RecordAlarmIp(conn, trx, acceptNum, ip1);
|
Logger.Info("已成功通知" + ip1 + " !");
|
return true;
|
}
|
catch (Exception e)
|
{
|
Logger.Warn("無法通知" + ip1 + ": " + e.Message);
|
}
|
|
try
|
{
|
IPAddress serverIp = IPAddress.Parse(ip2);
|
var serverPort = Convert.ToInt32(GlobalVariable.AlermPort);
|
IPEndPoint iep = new IPEndPoint(serverIp, serverPort);
|
|
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
clientSocket.Connect(iep);
|
|
byte[] byteMessage = encoding.GetBytes(acceptNum);
|
|
clientSocket.Send(byteMessage);
|
clientSocket.Shutdown(SocketShutdown.Both);
|
clientSocket.Close();
|
RecordAlarmIp(conn, trx, 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);
|
|
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
clientSocket.Connect(iep);
|
|
byte[] byteMessage = encoding.GetBytes(acceptNum);
|
|
clientSocket.Send(byteMessage);
|
clientSocket.Shutdown(SocketShutdown.Both);
|
clientSocket.Close();
|
RecordAlarmIp(conn, trx, acceptNum, ipPatrol);
|
Logger.Info("已成功通知" + ipPatrol + " !");
|
return true;
|
}
|
catch (Exception e)
|
{
|
Logger.Warn("無法通知" + ipPatrol + ": " + e.Message);
|
Logger.Warn("三個IP均無法通知到!");
|
return false;
|
}
|
}
|
|
private void RecordAlarmIp(OracleConnection conn, OracleTransaction trx, string accNum, string ip)
|
{
|
string sqlStmt = "INSERT INTO EOS.CASE_DISPATCH (ACCEPTNUM,ALARMIP) VALUES('" + accNum + "','" + ip + "')";
|
|
OracleCommand command = new OracleCommand(sqlStmt, conn, trx);
|
|
try
|
{
|
if (command.ExecuteNonQuery() <= 0)
|
Logger.Warn("無法紀錄Alarm IP!");
|
}
|
catch (Exception e)
|
{
|
Logger.Error("recordAlarmIP Error." + e.Message);
|
}
|
finally
|
{
|
command.Dispose();
|
}
|
}
|
}
|
}
|