using System; using System.Collections.Generic; using System.Text; using System.Data; using DBFactory; using CommonLib; using System.Threading; namespace WcfControlMonitorLib { public static class CUpdateIoControl { static Thread mythread; static bool exitThread = false;//20091107 static string _CUpdateIoControlError = "";//监控调度类错误说明 public static string CUpdateIoControlError { get { return _CUpdateIoControlError; } set { _CUpdateIoControlError = value; } } public static event RefreshMonitorEventHandler RefreshMonitor; public static void OnRefreshMonitor(RefreshMonitorEventArgs e) { if (RefreshMonitor != null) { RefreshMonitor(e); } } static StringBuilder temp = new StringBuilder(); static DBOperator dbo = new DBOperator();//20130926 static DBOperator dboM1 = CStaticClass.dboM1;//20130926 static DBOperator dbo1 = CStaticClass.dbo1;//20130926 static object lockThis = new object(); public static List sql = new List(); private static void BeginListen() {//20091107 while (!exitThread) { try { lock (lockThis) {//20130226richard.liu RunDark(); RunSql(); } } catch (Exception ex) { _CUpdateIoControlError = ex.StackTrace + ex.Message; } finally { Thread.Sleep(200); } } //if (mythread != null && exitThread == true) //{ // mythread.Abort(); // mythread = null; //} } public static void EndListen() {//20091107 if (sql.Count == 0) { exitThread = true; if (mythread != null) { mythread.Abort(); mythread = null; } } else { if (mythread == null) { mythread = new Thread(new ThreadStart(BeginListen)); mythread.IsBackground = true; mythread.Start(); while (true) { if (sql.Count == 0) { exitThread = true; mythread.Abort(); mythread = null; break; } } } else { while (true) { if (sql.Count == 0) { exitThread = true; mythread.Abort(); mythread = null; break; } } } } //if (mythread != null) //{ // mythread.Abort(); // mythread = null; //} } public static void StartListen() { //20091107 exitThread = false; mythread = new Thread(new ThreadStart(BeginListen)); mythread.IsBackground = true; mythread.Start(); } public static void RunDark() { if (CommonClassLib.CCarryConvert.dark.Count > 0) { for (int i = 0; i < CommonClassLib.CCarryConvert.dark.Count; i++) { try { if (CommonClassLib.CCarryConvert.dark[i] == null) break; //dark中数量近万时,偶尔会出现 dark[0] 值为null string[] aaa = CommonClassLib.CCarryConvert.dark[i].ToString().Split('#'); CommonClassLib.CCarryConvert.kkk(aaa[0], aaa[1], aaa[2], aaa[3], aaa[4]); //将 dark[i] 的记录写入文件中 //dbo1.ExecuteSql("insert into test(T1) values('" + sql[i].ToString() + "')"); } catch (Exception exa) { _CUpdateIoControlError = string.Format("解析PLC反馈数据的记录设备状态时发生错误:{0}", exa.StackTrace + exa.Message); RefreshMonitorEventArgs rmea = new RefreshMonitorEventArgs("tsStatus", _CUpdateIoControlError); OnRefreshMonitor(rmea); } finally { CommonClassLib.CCarryConvert.dark.RemoveAt(i);//移除 dark[i] i--; } } } } public static void RunSql() { if (sql.Count > 0) { for (int i = 0; i < sql.Count; i++) { try { if (sql[i].IndexOf("CONTROL_STATUS=10") >= 0) { temp.Remove(0, temp.Length); temp.Append("select CONTROL_STATUS from io_control where CONTROL_ID=").Append(sql[i].Split('|')[1]); int a = Convert.ToInt32(dboM1.GetSingle(temp.ToString())); if (a != 10 && a != 999) { dboM1.ExecuteSql(sql[i].Split('|')[0]); } } else if (sql[i].IndexOf("CONTROL_STATUS=3") >= 0) { temp.Remove(0, temp.Length); temp.Append("select CONTROL_STATUS from io_control where CONTROL_ID=").Append(sql[i].Split('|')[1]); int c = Convert.ToInt32(dboM1.GetSingle(temp.ToString())); if (c != 3 && c != 999) { dboM1.ExecuteSql(sql[i].Split('|')[0]); } } else if (sql[i].IndexOf("SET ERROR_TEXT =''") >= 0) { temp.Remove(0, temp.Length); temp.Append("select ERROR_TEXT from io_control where CONTROL_ID=").Append(sql[i].Split('|')[1]); object b = Convert.ToInt32(dboM1.GetSingle(temp.ToString())); if (b != "" && b != null && b != string.Empty) { dboM1.ExecuteSql(sql[i].Split('|')[0]); } } else { dboM1.ExecuteSql(sql[i].Split('|')[0]); } //dbo1.ExecuteSql("insert into test(T1) values('" + sql[i].ToString() + "')"); } catch (Exception exa) { _CUpdateIoControlError = string.Format("解析PLC反馈数据的记录设备状态时发生错误:{0}", exa.StackTrace + exa.Message); RefreshMonitorEventArgs rmea = new RefreshMonitorEventArgs("tsStatus", _CUpdateIoControlError); OnRefreshMonitor(rmea); } finally { sql.RemoveAt(i); i--; } } } } } }