785 lines
36 KiB
C#
785 lines
36 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.ComponentModel;
|
||
using System.Data;
|
||
using System.Data.OracleClient;
|
||
using System.Diagnostics;
|
||
using System.Drawing;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Net;
|
||
using System.Runtime.InteropServices;
|
||
using System.Text;
|
||
using System.Threading;
|
||
using System.Threading.Tasks;
|
||
using System.Windows.Forms;
|
||
|
||
namespace GetData_PLC
|
||
{
|
||
public partial class frmMain : Form
|
||
{
|
||
[DllImport("Iphlpapi.dll")]
|
||
|
||
|
||
private static extern int SendARP(Int32 dest, Int32 host, ref Int64 mac, ref Int32 length);
|
||
//[DllImport("Ws2_32.dll")]
|
||
private static string strErr = "";
|
||
|
||
public static string strAppPath = Environment.CurrentDirectory ; //采集程序路径
|
||
public static string strAPPDataPath = Environment.CurrentDirectory + @"\Data\"; //采集程序文件存储路径
|
||
public static string strParPath = strAPPDataPath + @"par\"; //计量参数文件存储路径
|
||
public static string strConfigPath = strAPPDataPath + @"Config\"; //采集程序参数保存路径
|
||
public static string strDataPath = strAPPDataPath + @"RealTime\"; //实时数据存储路径
|
||
|
||
public static string strDataPath_JlData = strDataPath + @"JlData\"; //分站计量实时数据存储路径
|
||
public static string strDataPath_ScData = strDataPath + @"ScData\"; //输差数据XML存储文件夹
|
||
public static string strDataPath_DataTime = strDataPath + @"DateTime\"; //分站时间xml存储文件夹
|
||
public static string strDataPath_Log = strDataPath + @"Log\"; //日志存储文件夹
|
||
|
||
|
||
|
||
//log 日志存放数据
|
||
public static string strLogPath = strDataPath + @"log\" + DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Day.ToString() + "_log.txt";
|
||
public static string strLogPath_Data = strDataPath + @"log\" + DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Day.ToString() + "_Datalog_";
|
||
|
||
|
||
private DataTable dtZhan = new DataTable();
|
||
static DataTable dtJld = new DataTable();
|
||
//检查控制台程序,如果一分钟之内启动超过3次,则可能有错误,重启计算机。
|
||
private static int StartConselTimes;
|
||
//控制台是否运行的标志
|
||
private static bool ConselRunFlag;
|
||
// 程序启动的时间,用于主程序自动重启
|
||
private static DateTime MainPragramStartTime;
|
||
|
||
Boolean SaveHourFlag; //小时保存数据
|
||
Boolean SaveSSFlag; //实时数据保存数据
|
||
Boolean SaveDayFlag; //保存日报表标志
|
||
Boolean SaveSCDayFlag; //保存日报表标志
|
||
|
||
Boolean UpdateFlag; //更新计量器具的标志
|
||
|
||
|
||
private static int SaveDiff = 300;//实时数据存盘间隔 五分钟
|
||
bool SaveEnable;
|
||
private static DateTime PragramStartTime;// 程序启动的时间,用于主程序自动重启
|
||
private static DateTime LastSaveTime;
|
||
public frmMain()
|
||
{
|
||
InitializeComponent();
|
||
}
|
||
private void frmMain_Load(object sender, EventArgs e)
|
||
{
|
||
//写入程序已经运行的标志 避免服务器程序重复启动
|
||
System.IO.Directory.CreateDirectory(strAPPDataPath); //Data文件夹
|
||
System.IO.Directory.CreateDirectory(strParPath); //Data\par文件夹
|
||
System.IO.Directory.CreateDirectory(strDataPath); //Data\realtime文件夹
|
||
System.IO.Directory.CreateDirectory(strConfigPath); //Data\realtime文件夹
|
||
|
||
System.IO.Directory.CreateDirectory(strDataPath_JlData); //分站xml存储文件夹
|
||
System.IO.Directory.CreateDirectory(strDataPath_ScData); //输差数据XML存储文件夹
|
||
System.IO.Directory.CreateDirectory(strDataPath_DataTime);//分站时间xml存储文件夹
|
||
System.IO.Directory.CreateDirectory(strDataPath_Log); //日志存储文件夹
|
||
|
||
|
||
// 备份数据库
|
||
|
||
DataSet dsBackUp = new DataSet();
|
||
|
||
DataTable dtBackUp = new DataTable();
|
||
dtBackUp = OracleLink.ExecuteDataTable("select * from RV2_BASEINFO", "JLXT", "");
|
||
dtBackUp.TableName = "FlowMeter";
|
||
dsBackUp.Tables.Add(dtBackUp.Copy());
|
||
|
||
dtBackUp = new DataTable();
|
||
dtBackUp = OracleLink.ExecuteDataTable("select * from RV2_TRANS_CONFIG", "JLXT", "");
|
||
dtBackUp.TableName = "SCConfig";
|
||
dsBackUp.Tables.Add(dtBackUp.Copy());
|
||
|
||
dtBackUp = new DataTable();
|
||
dtBackUp = OracleLink.ExecuteDataTable("select * from SYS_ORGANISE", "JLXT", "");
|
||
dtBackUp.TableName = "OrgConfig";
|
||
dsBackUp.Tables.Add(dtBackUp.Copy());
|
||
|
||
|
||
dsBackUp .WriteXml (strParPath + "dsConfig.xml", XmlWriteMode.WriteSchema);
|
||
|
||
|
||
|
||
|
||
|
||
string strParPath1 = strParPath + "runstate.txt";
|
||
if (File.Exists(strParPath))
|
||
{
|
||
if (File.ReadAllText(strParPath1) == "true")
|
||
{
|
||
MessageBox.Show("已有一个采集进程在运行,退出 !", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
Application.Exit();
|
||
}
|
||
}
|
||
else
|
||
{
|
||
File.WriteAllText(strParPath1, "true");
|
||
}
|
||
|
||
System.Net.IPAddress[] addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList;
|
||
toolStripStatusLabel1.Text = "天然气产销厂自动计量采集程序";
|
||
toolStripStatusLabel2.Text = DateTime.Now.ToString();
|
||
|
||
Control.CheckForIllegalCrossThreadCalls = false;//跨线程操作
|
||
string strFileName = "";
|
||
|
||
dtZhan = new DataTable();
|
||
try
|
||
{
|
||
dtZhan = OracleLink.ExecuteDataTable(SQL_Strings.strSQLZhan, "JLXT", strErr);
|
||
}
|
||
catch (Exception)
|
||
{
|
||
strFileName = strDataPath + "dtZhan.xml";
|
||
dtZhan.ReadXml(strFileName);
|
||
}
|
||
dtZhan.TableName = "集输站场";
|
||
DataTable dtStationNew = dtZhan.Copy();
|
||
try
|
||
{
|
||
for (int i = 0; i < dtZhan.Rows.Count; i++)
|
||
{
|
||
//柳屯配气站两台PLC 区分
|
||
string[] tempIp = dtZhan.Rows[i]["IP"].ToString().Split(',');
|
||
string[] tempPORT = dtZhan.Rows[i]["PORT"].ToString().Split(',');
|
||
if (tempIp.Length > 1)
|
||
{
|
||
DataRow drTemp = dtStationNew.NewRow();
|
||
drTemp.ItemArray = dtZhan.Rows[i].ItemArray;
|
||
dtStationNew.Rows[i]["org_id"] = dtZhan.Rows[i]["org_id"].ToString() + "_0";
|
||
dtStationNew.Rows[i]["org_name"] = dtZhan.Rows[i]["org_name"].ToString() + "_0";
|
||
dtStationNew.Rows[i]["IP"] = tempIp[0];
|
||
dtStationNew.Rows[i]["PORT"] = tempPORT[0];
|
||
for (int j = 1; j < tempIp.Length; j++)
|
||
{
|
||
drTemp["org_id"] = dtZhan.Rows[i]["org_id"].ToString() + "_" + j.ToString();
|
||
drTemp["org_name"] = dtZhan.Rows[i]["org_name"].ToString() + "_" + j.ToString();
|
||
drTemp["IP"] = tempIp[j];
|
||
drTemp["PORT"] = tempPORT[j];
|
||
dtStationNew.Rows.Add(drTemp);
|
||
}
|
||
}
|
||
}
|
||
comboBox1.DataSource = dtStationNew;
|
||
comboBox1.DisplayMember = dtZhan.Columns["ORG_NAME"].ToString();
|
||
comboBox1.ValueMember = dtZhan.Columns["ORG_ID"].ToString();
|
||
dtStationNew.WriteXml(strDataPath + "dtZhan.xml", XmlWriteMode.WriteSchema);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
}
|
||
LastSaveTime = DateTime.Now;
|
||
PragramStartTime = DateTime.Now;
|
||
string name = Dns.GetHostName();
|
||
IPAddress[] ipadrlist = Dns.GetHostAddresses(name);
|
||
for (int i = 0; i < ipadrlist.Length; i++)
|
||
{
|
||
if (ipadrlist[i].ToString() == "10.75.167.5" || ipadrlist[i].ToString() == "10.75.166.5")
|
||
{
|
||
checkBox1.Checked = true;
|
||
|
||
}
|
||
}
|
||
}
|
||
|
||
#region "启停采集控制台"
|
||
private void startConsel(string _filePath, string _ProcessesName)
|
||
{
|
||
|
||
foreach (Process p in Process.GetProcesses())
|
||
{
|
||
if (p.ProcessName == _ProcessesName)
|
||
{
|
||
try
|
||
{
|
||
p.Kill();
|
||
}
|
||
catch
|
||
{
|
||
|
||
}
|
||
}
|
||
}
|
||
//实例化一个进程类
|
||
|
||
//Thread.Sleep(5000);
|
||
|
||
Process cmd = new Process();
|
||
cmd.StartInfo.FileName = _filePath + "\\" + _ProcessesName + ".exe";
|
||
//不显示命令行窗口界面true不显示
|
||
cmd.StartInfo.CreateNoWindow = true;
|
||
cmd.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
|
||
cmd.Start(); //启动进程
|
||
textBox1.AppendText("采集程序启动---" + DateTime.Now + "\r\n");
|
||
ConselRunFlag = true;
|
||
StartConselTimes = 0;
|
||
}
|
||
|
||
|
||
private void CloseConsel(string _ProcessesName)
|
||
{
|
||
foreach (Process p in Process.GetProcesses())
|
||
{
|
||
|
||
if (p.ProcessName == _ProcessesName)
|
||
{
|
||
try
|
||
{
|
||
p.Kill();
|
||
}
|
||
catch
|
||
{
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
private void timer3_Tick(object sender, EventArgs e)
|
||
{
|
||
//定时重启 2天重启一次
|
||
TimeSpan stST = DateTime.Now - MainPragramStartTime;
|
||
DateTime SAVETIME = DateTime.Now;
|
||
DateTime oldTime = DateTime.Parse(toolStripStatusLabel2.Text);
|
||
TimeSpan strRuntime = SAVETIME - oldTime;
|
||
toolStripStatusLabel3.Text = "已运行【" + strRuntime.Days.ToString("0#") + "天" + strRuntime.Hours.ToString("0#") + "小时" + strRuntime.Minutes.ToString("0#") + "分钟" + strRuntime.Seconds.ToString("0#") + "秒 】";
|
||
toolStripStatusLabel4.Text = DateTime.Now.ToString();
|
||
|
||
#region //启动停止更新计量器具控制台程序
|
||
//try
|
||
//{
|
||
// if (SAVETIME.Hour >= 2 && SAVETIME.Hour < 3 && UpdateFlag == false) //过了2点启动
|
||
// {
|
||
// UpdateFlag = true;
|
||
// startConsel(strAppPath, "UpdateMeter");
|
||
// try
|
||
// {
|
||
// File.Delete(strAppPath + @"\Data\UpdateMeter\updateFlag.xml");
|
||
// }
|
||
// catch (Exception)
|
||
// {
|
||
|
||
// }
|
||
// }
|
||
|
||
|
||
// if (UpdateFlag) //如果更新成功 则关闭控制台程序
|
||
// {
|
||
// if (File.Exists(strAppPath + @"\Data\UpdateMeter\updateFlag.xml"))
|
||
// {
|
||
// CloseConsel("UpdateMeter");
|
||
// }
|
||
// }
|
||
|
||
|
||
// if (SAVETIME.Hour < 2 || SAVETIME.Hour > 3)
|
||
// {
|
||
// UpdateFlag = false;
|
||
// try
|
||
// {
|
||
// File.Delete(strAppPath + @"\Data\UpdateMeter\updateFlag.xml");
|
||
// }
|
||
// catch (Exception)
|
||
// {
|
||
|
||
|
||
// }
|
||
// }
|
||
//}
|
||
//catch (Exception ex)
|
||
//{
|
||
|
||
//}
|
||
#endregion
|
||
#region //客户端重启control
|
||
try
|
||
{
|
||
string strQuery = "select * from control"; //从数据库中查询出所有站场的名称和站场ID//返回不带重复记录的站场ID表格
|
||
string RestartFlag = "";
|
||
DataTable ReadFlagTable = OracleLink.ExecuteDataTable(strQuery, "DTJK", "");
|
||
RestartFlag = ReadFlagTable.Rows[0]["RESTART"].ToString();
|
||
if (RestartFlag == "1")
|
||
{
|
||
strQuery = "update control set RESTART=0 where id=1 "; //从数据库中查询出所有站场的名称和站场ID//返回不带重复记录的站场ID表格
|
||
OracleLink.ExecuteNonQuery(strQuery, "DTJK", strErr);
|
||
startConsel(strAppPath, "ConsoleGetPLCData");
|
||
}
|
||
|
||
}
|
||
catch (SystemException err)
|
||
{
|
||
|
||
}
|
||
|
||
#endregion
|
||
try
|
||
{
|
||
if (stST.Minutes > 2880)
|
||
{
|
||
System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location);
|
||
Application.Restart();
|
||
}
|
||
toolStripStatusLabel5.Text = "";
|
||
Int64 intPLCPageMemory = 0;
|
||
Int64 intPLCSystemMemory = 0;
|
||
ConselRunFlag = false;
|
||
foreach (Process p in Process.GetProcesses())
|
||
{
|
||
if (p.ProcessName == "ConsoleGetPLCData")
|
||
{
|
||
string PLCMemory = "PLC采集程序【物理内存:" + (p.WorkingSet64 / 1024 / 1024).ToString() + " MB--" + "分页内存:" + (p.PagedMemorySize64 / 1024 / 1024).ToString() + " MB】";
|
||
intPLCPageMemory = p.PagedMemorySize64 / 1024 / 1024;
|
||
intPLCSystemMemory = p.WorkingSet64 / 1024 / 1024;
|
||
toolStripStatusLabel5.Text = PLCMemory;
|
||
ConselRunFlag = true;
|
||
StartConselTimes = 0;
|
||
break;
|
||
}
|
||
}
|
||
|
||
if (ConselRunFlag == false)
|
||
{
|
||
StartConselTimes = StartConselTimes + 1;
|
||
startConsel(strAppPath, "ConsoleGetPLCData");
|
||
textBox1.AppendText("启动采集程序 " + "\r\n");
|
||
|
||
}
|
||
|
||
bool Restart = false;
|
||
if ((System.Math.IEEERemainder(SAVETIME.Minute, 5) > 0 && System.Math.IEEERemainder(SAVETIME.Minute, 5) < 4) || (DateTime.Now.Hour == 23 && DateTime.Now.Minute >= 56))
|
||
{
|
||
Restart = true;
|
||
}
|
||
if (intPLCPageMemory >= 250 && Restart)
|
||
{
|
||
CloseConsel("ConsoleGetPLCData");
|
||
Thread.Sleep(2000);
|
||
startConsel(strAppPath, "ConsoleGetPLCData");
|
||
toolStripStatusLabel7.Text = (int.Parse(toolStripStatusLabel7.Text) + 1).ToString();
|
||
textBox1.AppendText("分页内存超过250MB,重启采集程序----" + DateTime.Now + "\r\n");
|
||
}
|
||
|
||
if (intPLCSystemMemory >= 150 && Restart)
|
||
{
|
||
CloseConsel("ConsoleGetPLCData");
|
||
Thread.Sleep(2000);
|
||
startConsel(strAppPath, "ConsoleGetPLCData");
|
||
toolStripStatusLabel7.Text = (int.Parse(toolStripStatusLabel7.Text) + 1).ToString();
|
||
textBox1.AppendText("物理内存超过150MB,重启采集程序----" + DateTime.Now + "\r\n");
|
||
}
|
||
timer1.Enabled = true;
|
||
|
||
}
|
||
catch (Exception)
|
||
{
|
||
|
||
}
|
||
}
|
||
#endregion
|
||
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
|
||
{
|
||
|
||
}
|
||
|
||
|
||
private void timer1_Tick(object sender, EventArgs e)
|
||
{
|
||
try
|
||
{
|
||
string strFilePath = @"D:\GetPLCData\Data\RealTime\";
|
||
DataTable dtStation = new DataTable();
|
||
dtStation.ReadXml(strFilePath + "JlData\\" + comboBox1.SelectedValue.ToString() + ".xml");
|
||
dataGridView1.DataSource = dtStation;
|
||
|
||
DataSet SsDataDs = new DataSet();//数据集,用于存放全厂所有计量点实时数据
|
||
DataTable dtScRealTime = new DataTable();//数据集,用于存放全厂站场输差、管管线输差和自定义输差的实时显示
|
||
DataTable TotalTable = new DataTable();
|
||
try
|
||
{
|
||
dtScRealTime.ReadXml(strDataPath + "ScData.xml");
|
||
SsDataDs.ReadXml(strDataPath + "SsData.xml", XmlReadMode.ReadSchema);
|
||
TotalTable.ReadXml(strDataPath + "TotalData.xml");
|
||
|
||
}
|
||
catch (SystemException err)
|
||
{
|
||
if (err.Message != null)
|
||
{
|
||
//textBox1.AppendText("注意: 读取合并xml文件出错 " + err.Message + err.StackTrace.Substring(err.StackTrace.LastIndexOf("行号") - 1) + "\r\n");
|
||
//textBox1.AppendText("注意: 读取合并xml文件出错 " + "\r\n");
|
||
}
|
||
}
|
||
|
||
try
|
||
{
|
||
CopyDirectory(strAppPath + "\\Data\\", textBox2 .Text );
|
||
}
|
||
catch
|
||
|
||
{
|
||
|
||
}
|
||
|
||
|
||
|
||
#region 保存到数据库
|
||
if (checkBox1.Checked)
|
||
{
|
||
DateTime SAVETIME = DateTime.Now;
|
||
string strQuery = "";
|
||
#region"保存实时数据"//------------------------------------------------------------------------
|
||
//if (System.Math.Abs(DateDiff.TotalSeconds - SaveDiff) < 1)
|
||
if (System.Math.IEEERemainder(SAVETIME.Minute, 5) == 0 && SaveSSFlag == false)
|
||
{
|
||
strQuery = "select ID,DEPT_ID,STATION_ID,JLD_ID,RTIME,WD,YL,CY,SSL,JRL,JRSJ,YBZT,SSGKLL,CXFLAG from REALTIME_DATA where ID=0";
|
||
DataTable SaveRealTimeData = OracleLink.ExecuteDataTable(strQuery, "DTJK", strErr);
|
||
for (int j = 0; j < TotalTable.Rows.Count; j++)
|
||
{
|
||
DataRow tempRow = SaveRealTimeData.NewRow();
|
||
tempRow["ID"] = TotalTable.Rows[j]["ID"];
|
||
tempRow["RTIME"] = SAVETIME;
|
||
tempRow["WD"] = TotalTable.Rows[j]["WD"];
|
||
tempRow["YL"] = TotalTable.Rows[j]["YL"];
|
||
tempRow["CY"] = TotalTable.Rows[j]["CY"];
|
||
tempRow["SSL"] = TotalTable.Rows[j]["SSL"];
|
||
tempRow["JRL"] = TotalTable.Rows[j]["JRL"];
|
||
tempRow["JRSJ"] = TotalTable.Rows[j]["JRSJ"];
|
||
tempRow["YBZT"] = TotalTable.Rows[j]["YBZT"];
|
||
tempRow["SSGKLL"] = TotalTable.Rows[j]["SSGKLL"];
|
||
tempRow["CXFLAG"] = TotalTable.Rows[j]["CXFLAG"];
|
||
tempRow["DEPT_ID"] = TotalTable.Rows[j]["DEPT_ID"];
|
||
tempRow["STATION_ID"] = TotalTable.Rows[j]["STATION_ID"];
|
||
tempRow["JLD_ID"] = TotalTable.Rows[j]["JLD_ID"];
|
||
SaveRealTimeData.Rows.Add(tempRow);
|
||
}
|
||
try
|
||
{
|
||
strQuery = "select ID,DEPT_ID,STATION_ID,JLD_ID,RTIME,WD,YL,CY,SSL,JRL,JRSJ,YBZT,SSGKLL,CXFLAG from REALTIME_DATA";
|
||
|
||
SaveRealTimeData.WriteXml ("d:\\ccc.xml",XmlWriteMode.WriteSchema);
|
||
OracleLink.SaveDataTable(strQuery, SaveRealTimeData, "DTJK", strErr);
|
||
strErr = "注意:" + DateTime.Now + "保存实时数据成功" + "\r\n";
|
||
textBox1.AppendText(strErr);
|
||
}
|
||
catch (SystemException err)
|
||
{
|
||
if (err.Message != null)
|
||
{
|
||
strErr = "注意:" + DateTime.Now + "保存实时数据库时出错" + err.Message + "\r\n";
|
||
textBox1.AppendText(strErr);
|
||
}
|
||
}
|
||
#endregion
|
||
#region"保存实时输差"//-----------------------------------------------------
|
||
strQuery = "select id, in_p,out_p,rtime, scname, ssintotal, ssouttotal, sssc, ssscbfs, jrlintotal, jrlouttotal, jrlsc, jrlscbfs, station_id, sctype, redept_name from SC_DATA where id=0 ";
|
||
try
|
||
{
|
||
DataTable ScTotalTable = new DataTable();
|
||
ScTotalTable = OracleLink.ExecuteDataTable(strQuery, "DTJK", strErr);
|
||
ScTotalTable.Rows.Clear();
|
||
for (int j = 0; j < dtScRealTime.Rows.Count; j++)
|
||
{
|
||
DataRow tempRow = ScTotalTable.NewRow();
|
||
tempRow["RTIME"] = SAVETIME;
|
||
tempRow["ID"] = dtScRealTime.Rows[j]["SCID"];
|
||
tempRow["IN_P"] = dtScRealTime.Rows[j]["IN_P"];
|
||
tempRow["OUT_P"] = dtScRealTime.Rows[j]["OUT_P"];
|
||
tempRow["scname"] = dtScRealTime.Rows[j]["SCNAME"];
|
||
tempRow["ssintotal"] = dtScRealTime.Rows[j]["SsInTotal"];
|
||
tempRow["ssouttotal"] = dtScRealTime.Rows[j]["SsOutTotal"];
|
||
tempRow["sssc"] = dtScRealTime.Rows[j]["SsSc"];
|
||
tempRow["ssscbfs"] = dtScRealTime.Rows[j]["SsPer"];
|
||
tempRow["jrlintotal"] = dtScRealTime.Rows[j]["JrInTotal"];
|
||
tempRow["jrlouttotal"] = dtScRealTime.Rows[j]["JrOutTotal"];
|
||
tempRow["jrlsc"] = dtScRealTime.Rows[j]["JrSc"];
|
||
tempRow["jrlscbfs"] = dtScRealTime.Rows[j]["JrPer"];
|
||
tempRow["station_id"] = dtScRealTime.Rows[j]["station_id"];
|
||
tempRow["redept_name"] = dtScRealTime.Rows[j]["RE_DEPT_ID"];
|
||
tempRow["sctype"] = dtScRealTime.Rows[j]["SCTYPE"];
|
||
ScTotalTable.Rows.Add(tempRow);
|
||
}
|
||
|
||
strQuery = "select id, rtime,in_p,out_p, scname, ssintotal, ssouttotal, sssc, ssscbfs, jrlintotal, jrlouttotal, jrlsc, jrlscbfs, station_id, sctype, redept_name from SC_DATA ";
|
||
OracleLink.SaveDataTable(strQuery, ScTotalTable, "DTJK", strErr);
|
||
strErr = "注意:" + DateTime.Now + "保存实时输差数据成功" + "\r\n";
|
||
textBox1.AppendText(strErr);
|
||
}
|
||
catch (SystemException err)
|
||
{
|
||
if (err.Message != null)
|
||
{
|
||
strErr = ("注意:" + DateTime.Now + "保存输差数据失败" + err.Message) + "\r\n";
|
||
textBox1.AppendText(strErr);
|
||
}
|
||
}
|
||
|
||
LastSaveTime = SAVETIME;
|
||
SaveSSFlag = true;
|
||
}
|
||
|
||
if (System.Math.IEEERemainder(SAVETIME.Minute, 5) != 0)
|
||
{
|
||
SaveSSFlag = false;
|
||
}
|
||
#endregion
|
||
#region "存小时数据"//-----------------------------------------------------------------
|
||
if (SAVETIME.Minute == 59 && SAVETIME.Second > 40 && SaveHourFlag == false) //到小时整点,如果还没有存储过则开始存储
|
||
{
|
||
strQuery = "select ID,RTIME,WD,YL,CY,SSL,JRL,JRSJ,YBZT,DEPT_ID,STATION_ID,JLD_ID from HOURREPORT where ID=0";
|
||
DataTable SaveHourTable = OracleLink.ExecuteDataTable(strQuery, "DTJK", strErr);
|
||
for (int j = 0; j < TotalTable.Rows.Count; j++)
|
||
{
|
||
DataRow tempRow = SaveHourTable.NewRow();
|
||
tempRow["ID"] = TotalTable.Rows[j]["ID"];
|
||
tempRow["RTIME"] = SAVETIME;
|
||
tempRow["WD"] = TotalTable.Rows[j]["WD"];
|
||
tempRow["YL"] = TotalTable.Rows[j]["YL"];
|
||
tempRow["CY"] = TotalTable.Rows[j]["CY"];
|
||
tempRow["SSL"] = TotalTable.Rows[j]["SSL"];
|
||
tempRow["JRL"] = TotalTable.Rows[j]["JRL"];
|
||
tempRow["JRSJ"] = TotalTable.Rows[j]["JRSJ"];
|
||
tempRow["YBZT"] = TotalTable.Rows[j]["YBZT"];
|
||
tempRow["DEPT_ID"] = TotalTable.Rows[j]["DEPT_ID"];
|
||
tempRow["STATION_ID"] = TotalTable.Rows[j]["STATION_ID"];
|
||
tempRow["JLD_ID"] = TotalTable.Rows[j]["JLD_ID"];
|
||
SaveHourTable.Rows.Add(tempRow);
|
||
}
|
||
|
||
strQuery = "select ID,RTIME,WD,YL,CY,SSL,JRL,JRSJ,YBZT,DEPT_ID,STATION_ID,JLD_ID from HOURREPORT";
|
||
try
|
||
{
|
||
OracleLink.SaveDataTable(strQuery, SaveHourTable, "DTJK", strErr);
|
||
strErr = "注意:" + DateTime.Now + "保存小时数据成功" + "\r\n";
|
||
textBox1.AppendText(strErr);
|
||
|
||
SaveHourFlag = true;
|
||
SaveHourTable.Dispose();
|
||
}
|
||
catch (SystemException err)
|
||
{
|
||
if (err.Message != null)
|
||
{
|
||
strErr = "注意:" + DateTime.Now + "保存小时数据出错" + "\r\n";
|
||
textBox1.AppendText(strErr);
|
||
}
|
||
}
|
||
}
|
||
|
||
if (SAVETIME.Minute != 59) //过了小时准点 存储标志设为否
|
||
{
|
||
SaveHourFlag = false;
|
||
}
|
||
#endregion
|
||
#region "输差每日23:59:55 存盘一次
|
||
if (SAVETIME.Hour == 23 && SAVETIME.Minute.ToString() == "59" && SAVETIME.Second.ToString() == "55" && SaveSCDayFlag == false) //过了零点十分则日报表
|
||
{
|
||
strQuery = "select id, in_p,out_p,rtime, scname, ssintotal, ssouttotal, sssc, ssscbfs, jrlintotal, jrlouttotal, jrlsc, jrlscbfs, station_id, sctype, redept_name from SC_DAYDATA where id=0 ";
|
||
DataTable ScTotalTableH = new DataTable();
|
||
ScTotalTableH = OracleLink.ExecuteDataTable(strQuery, "DTJK", strErr);
|
||
ScTotalTableH.Rows.Clear();
|
||
try
|
||
{
|
||
for (int j = 0; j < dtScRealTime.Rows.Count; j++)
|
||
{
|
||
DataRow tempRow = ScTotalTableH.NewRow();
|
||
tempRow["RTIME"] = SAVETIME;
|
||
tempRow["ID"] = dtScRealTime.Rows[j]["SCID"];
|
||
tempRow["IN_P"] = dtScRealTime.Rows[j]["IN_P"];
|
||
tempRow["OUT_P"] = dtScRealTime.Rows[j]["OUT_P"];
|
||
tempRow["scname"] = dtScRealTime.Rows[j]["SCNAME"];
|
||
tempRow["ssintotal"] = dtScRealTime.Rows[j]["SsInTotal"];
|
||
tempRow["ssouttotal"] = dtScRealTime.Rows[j]["SsOutTotal"];
|
||
tempRow["sssc"] = dtScRealTime.Rows[j]["SsSc"];
|
||
tempRow["ssscbfs"] = dtScRealTime.Rows[j]["SsPer"];
|
||
tempRow["jrlintotal"] = dtScRealTime.Rows[j]["JrInTotal"];
|
||
tempRow["jrlouttotal"] = dtScRealTime.Rows[j]["JrOutTotal"];
|
||
tempRow["jrlsc"] = dtScRealTime.Rows[j]["JrSc"];
|
||
tempRow["jrlscbfs"] = dtScRealTime.Rows[j]["JrPer"];
|
||
tempRow["station_id"] = dtScRealTime.Rows[j]["station_id"];
|
||
tempRow["redept_name"] = dtScRealTime.Rows[j]["RE_DEPT_ID"];
|
||
tempRow["sctype"] = dtScRealTime.Rows[j]["SCTYPE"];
|
||
ScTotalTableH.Rows.Add(tempRow);
|
||
}
|
||
strQuery = "select id, rtime,in_p,out_p, scname, ssintotal, ssouttotal, sssc, ssscbfs, jrlintotal, jrlouttotal, jrlsc, jrlscbfs, station_id, sctype, redept_name from SC_DAYDATA ";
|
||
OracleLink.SaveDataTable(strQuery, ScTotalTableH, "DTJK", strErr);
|
||
strErr = "注意:" + DateTime.Now + "保存实时输差数据成功" + "\r\n";
|
||
SaveSCDayFlag = true;
|
||
textBox1.AppendText(strErr);
|
||
}
|
||
catch (SystemException err)
|
||
{
|
||
if (err.Message != null)
|
||
{
|
||
strErr = ("注意:" + DateTime.Now + "保存输差数据失败" + err.Message) + "\r\n";
|
||
textBox1.AppendText(strErr);
|
||
}
|
||
}
|
||
}
|
||
|
||
if (SAVETIME.Hour != 23 && SAVETIME.Minute.ToString() != "59" && SAVETIME.Second.ToString() != "55") //过了小时准点 存储标志设为否
|
||
{
|
||
SaveSCDayFlag = false;
|
||
}
|
||
#endregion
|
||
#region "存日报表"//-----------------------------------------------------------
|
||
|
||
|
||
if (SAVETIME.Hour == 0 && SAVETIME.Minute.ToString() == "10" && SaveDayFlag == false) //过了零点十分则日报表
|
||
{
|
||
string Queryql = "select ID,RTIME,QL,DEPT_ID,STATION_ID,JLD_ID,JLSJ from DAYREPORT where ID=0";
|
||
DataTable SaveDAYTable = OracleLink.ExecuteDataTable(Queryql, "DTJK", strErr);
|
||
DateTime _Date = DateTime.Now.AddDays(-1);
|
||
_Date = DateTime.Parse(_Date.ToString("yyyy-MM-dd"));
|
||
for (int j = 0; j < TotalTable.Rows.Count; j++)
|
||
{
|
||
DataRow tempRow = SaveDAYTable.NewRow();
|
||
tempRow["ID"] = TotalTable.Rows[j]["ID"];
|
||
tempRow["RTIME"] = _Date;
|
||
tempRow["QL"] = TotalTable.Rows[j]["ZRL"];
|
||
tempRow["DEPT_ID"] = TotalTable.Rows[j]["DEPT_ID"];
|
||
tempRow["STATION_ID"] = TotalTable.Rows[j]["STATION_ID"];
|
||
tempRow["JLD_ID"] = TotalTable.Rows[j]["JLD_ID"];
|
||
tempRow["JLSJ"] = TotalTable.Rows[j]["ZRSJ"];
|
||
SaveDAYTable.Rows.Add(tempRow);
|
||
}
|
||
|
||
Queryql = "select ID,RTIME,QL,DEPT_ID,STATION_ID,JLD_ID,JLSJ from DAYREPORT";
|
||
try
|
||
{
|
||
OracleLink.SaveDataTable(Queryql, SaveDAYTable, "DTJK", strErr);
|
||
//SaveDAYTable.WriteXml(strDataPath + DateTime.Now.ToString() + "SaveTable.xml",XmlWriteMode.WriteSchema);
|
||
//File.Copy(strDataPath + "TotalData.xml", strDataPath + DateTime.Now.ToString() + "TotalData.xml");
|
||
|
||
strErr = "注意:" + DateTime.Now + "保存日报表成功" + "\r\n";
|
||
textBox1.AppendText(strErr);
|
||
|
||
SaveDayFlag = true;
|
||
SaveDAYTable.Dispose();
|
||
}
|
||
catch (SystemException err)
|
||
{
|
||
if (err.Message != null)
|
||
{
|
||
strErr = "注意:" + DateTime.Now + "保存日报表数据出错" + strErr + "\r\n";
|
||
textBox1.AppendText(strErr);
|
||
}
|
||
}
|
||
|
||
|
||
|
||
}
|
||
|
||
if (SAVETIME.Hour != 1 && SAVETIME.Minute.ToString() != "10") //过了小时准点 存储标志设为否
|
||
{
|
||
SaveDayFlag = false;
|
||
}
|
||
#endregion
|
||
}
|
||
#endregion
|
||
}
|
||
catch { }
|
||
}
|
||
private void frmMain_Activated(object sender, EventArgs e)
|
||
{
|
||
|
||
}
|
||
private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
|
||
{
|
||
CloseConsel("ConsoleGetPLCData");
|
||
|
||
}
|
||
private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
|
||
{
|
||
CloseConsel("ConsoleGetPLCData");
|
||
File.WriteAllText(strParPath + "runstate.txt", "false");
|
||
System.Environment.Exit(0);//彻底关闭线程
|
||
|
||
}
|
||
private void timer2_Tick(object sender, EventArgs e)
|
||
{
|
||
|
||
}
|
||
|
||
private void button3_Click(object sender, EventArgs e)
|
||
{
|
||
|
||
string strQuery = "update control set ThreadRestartFlag='1',Thread_ZCID='" + comboBox1.SelectedValue.ToString() + "' where id=1 "; //从数据库中查询出所有站场的名称和站场ID//返回不带重复记录的站场ID表格
|
||
OracleLink.ExecuteNonQuery(strQuery, "DTJK", strErr);
|
||
|
||
}
|
||
|
||
private void button1_Click(object sender, EventArgs e)
|
||
{
|
||
string strQuery = "update control set ThreadRestartFlag='1',Thread_ZCID='" + comboBox1.SelectedValue.ToString() + "' where id=1 "; //从数据库中查询出所有站场的名称和站场ID//返回不带重复记录的站场ID表格
|
||
OracleLink.ExecuteNonQuery(strQuery, "DTJK", strErr);
|
||
|
||
}
|
||
|
||
private void button4_Click(object sender, EventArgs e)
|
||
{
|
||
|
||
FolderBrowserDialog fbd = new FolderBrowserDialog();
|
||
fbd.SelectedPath = @"D:\";
|
||
|
||
if (fbd.ShowDialog() == DialogResult.OK)
|
||
{
|
||
this.textBox2.Text = fbd.SelectedPath;
|
||
}
|
||
|
||
File.WriteAllText(strAppPath + "\\CopyPath.txt", textBox2.Text);
|
||
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
public void CopyDirectory(string sourceDirName, string destDirName)
|
||
{
|
||
try
|
||
{
|
||
if (!Directory.Exists(destDirName))
|
||
{
|
||
Directory.CreateDirectory(destDirName);
|
||
File.SetAttributes(destDirName, File.GetAttributes(sourceDirName));
|
||
|
||
}
|
||
|
||
if (destDirName[destDirName.Length - 1] != Path.DirectorySeparatorChar)
|
||
destDirName = destDirName + Path.DirectorySeparatorChar;
|
||
|
||
string[] files = Directory.GetFiles(sourceDirName);
|
||
foreach (string file in files)
|
||
{
|
||
|
||
File.Copy(file, destDirName + Path.GetFileName(file), true);
|
||
File.SetAttributes(destDirName + Path.GetFileName(file), FileAttributes.Normal);
|
||
//total++;
|
||
}
|
||
|
||
string[] dirs = Directory.GetDirectories(sourceDirName);
|
||
foreach (string dir in dirs)
|
||
{
|
||
CopyDirectory(dir, destDirName + Path.GetFileName(dir));
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
StreamWriter sw = new StreamWriter(Application.StartupPath + "\\log.txt", true);
|
||
sw.Write(ex.Message + " " + DateTime.Now + "\r\n");
|
||
sw.Close();
|
||
}
|
||
}
|
||
|
||
|
||
}
|
||
|
||
|
||
}
|