/// <summary> /// This method is responisble for saving logs into database. /// </summary> public void Run() { string Function_Name = "Run"; LogHelper.Trace(CLASS_NAME, Function_Name, "Function_Entered"); //store current thread instance m_thread = Thread.CurrentThread; while (true) { //wait for new item to be added to quene/ termination of thread m_addItemSignal.WaitOne(); if (IsTerminated()) { break; } //if there is any item in queue to process if (!IsQueneEmpty()) { //copy to local quene Queue <EtyTrendLog> tempQuene = null; lock (m_ObjectLock) { tempQuene = new Queue <EtyTrendLog>(m_writeQuene.ToArray()); m_writeQuene.Clear(); } while (tempQuene != null && tempQuene.Count != 0) { if (CheckDatabaseConnection()) { EtyTrendLog etyTrendLog = tempQuene.Dequeue(); //save into database if (!TrendLogDAO.GetInstance().InsertTrendViewerLog(etyTrendLog)) { m_dbDisconnected = true; //check whether insert SQL failed due to database Connection failure if (!CheckDatabaseConnection()) { //insert back to quene var Items = tempQuene.ToArray(); tempQuene.Clear(); tempQuene.Enqueue(etyTrendLog); foreach (var item in Items) { tempQuene.Enqueue(item); } } //due to some other error, ignore this item } } } } } // time to end the thread Thread.CurrentThread.Abort(); LogHelper.Trace(CLASS_NAME, Function_Name, "Function_Exited"); }
public void PushToWriteQuene(EtyTrendLog etyTrendLog) { lock (m_sharedThreadObjLock) { m_writeQuene.PushToQuene(etyTrendLog); } }
public void InsertTrendViewerLog() { EtyTrendLog etyTrendLog = new EtyTrendLog(); etyTrendLog.Data_PT_Host = TestDBInit.HOST_NAME; etyTrendLog.Data_PT_Server = TestDBInit.OPCSERVER_NAME; //"TransActiveDataSource" etyTrendLog.Data_PT_Name = "unitTest_Log1"; etyTrendLog.Data_PT_Value = 2; etyTrendLog.Data_PT_Time = DateTime.Now; TrendLogDAO.GetInstance().InsertTrendViewerLog(etyTrendLog); }
/// <summary> /// insert a record into TRENDVIEWER_LOG /// </summary> /// <param name="etyTrendLog">a record of trending log</param> /// <returns>true/false(successful/failed)</returns> public bool InsertTrendViewerLog(EtyTrendLog etyTrendLog) { string Function_Name = "InsertTrendViewerLog"; LogHelper.Trace(CLASS_NAME, Function_Name, "Function_Entered"); bool result = false; /*string localSQL = " INSERT INTO TRENDVIEWER_LOG(DATA_PT_HOST," + * "DATA_PT_SERVER,DATA_PT_NAME,DATA_PT_VALUE,DATA_PT_DATE) " + * " VALUES( '" + etyTrendLog.Data_PT_Host + "'" + * " ,'" + etyTrendLog.Data_PT_Server + "'" + * " ,'" + etyTrendLog.Data_PT_Name + "'" + * " ,'" + etyTrendLog.Data_PT_Value + "'" + * " , to_date('" + etyTrendLog.Data_PT_Time.ToString("yyyyMMddHHmmss") + "','YYYYMMDDHH24MISS') " + * " )"; * result = SimpleDatabase.getInstance().ExecuteAndReturnNonQuery(localSQL);*/ //SimpleDatabase.GetInstance().BeginTransaction(); string localSQL = " INSERT INTO TRENDVIEWER_LOG(DATA_PT_HOST," + "DATA_PT_SERVER,DATA_PT_NAME,DATA_PT_VALUE,DATA_PT_DATE) " + " VALUES( '" + etyTrendLog.Data_PT_Host + "'" + " ,'" + etyTrendLog.Data_PT_Server + "'" + " ,'" + etyTrendLog.Data_PT_Name + "'" + " ,'" + etyTrendLog.Data_PT_Value + "'" + " ,@DateValue)"; SqlParameter parameter = new SqlParameter(); parameter.ParameterName = "DateValue"; parameter.DbType = DbType.DateTime; parameter.Value = etyTrendLog.Data_PT_Time; parameter.Direction = System.Data.ParameterDirection.Input; List <SqlParameter> parameters = new List <SqlParameter>(); parameters.Add(parameter); result = SimpleDatabase.GetInstance().ExecuteNonQueryWithParams(localSQL, parameters); /* if (result) * result = SimpleDatabase.GetInstance().ExecuteNonQuery("INSERT INTO TRENDVIEWR_LOG(DATA_PT_HOST,DATA_PT_SERVER,DATA_PT_NAME,DATA_PT_VALUE,DATA_PT_DATE) VALUES('xxx','ddd','dpw','1','20130506190000')"); * if (result) * SimpleDatabase.GetInstance().CommitTransaction(); * else * SimpleDatabase.GetInstance().RollbackTransaction();*/ LogHelper.Trace(CLASS_NAME, Function_Name, "Function_Exited"); return(result); }
/// <summary> /// This method is reponsible for inserting new item into quene and /// signalling the thread to process the item. /// </summary> /// <param name="etyTrendLog"></param> public void PushToQuene(EtyTrendLog etyTrendLog) { string Function_Name = "PushToQuene"; LogHelper.Trace(CLASS_NAME, Function_Name, "Function_Entered"); lock (m_ObjectLock) { if (m_writeQuene.Count > m_maxCapacity) { LogHelper.Debug(CLASS_NAME, Function_Name, "Maximum capacity of the queue is reached so removing the first item in queue"); //this case is very rare.. loss of logs m_writeQuene.Dequeue(); } m_writeQuene.Enqueue(etyTrendLog); //m_containsDataInQuene = true; m_addItemSignal.Set(); } LogHelper.Trace(CLASS_NAME, Function_Name, "Function_Exited"); }
public void TestEtyTrendLog01() { EtyTrendLog etyTrendLog = new EtyTrendLog(); string s = etyTrendLog.Data_PT_Host; etyTrendLog.Data_PT_Host = s; s = etyTrendLog.Data_PT_Name; etyTrendLog.Data_PT_Name = s; s = etyTrendLog.Data_PT_Server; etyTrendLog.Data_PT_Server = s; DateTime dt = etyTrendLog.Data_PT_Time; etyTrendLog.Data_PT_Time = dt; double d = etyTrendLog.Data_PT_Value; etyTrendLog.Data_PT_Value = d; }
/// <summary> /// get dp log list for a historical dp to plot /// </summary> /// <param name="histDP">the given historical dp for charting</param> /// <param name="startTime">start time</param> /// <param name="endTime">end time</param> /// <returns>trend log list</returns> public List <EtyTrendLog> GetHistDPLogList(EtyHistDataPoint histDP, DateTime startTime, DateTime endTime) { string Function_Name = "GetHistDPLogList"; List <EtyTrendLog> res = new List <EtyTrendLog>(); string localSQL; //changed by luxiangmei, to make the logic for historical mode the same as mixed mode. // if (usedInMixMode) //used in mixed mode // { localSQL = " SELECT DATA_PT_DATE,DATA_PT_VALUE " + " FROM TRENDVIEWER_LOG WHERE DATA_PT_NAME = '" + DAOHelper.convertEscapeStringAndGB2312To8859P1(histDP.DPName) + "' " + " AND DATA_PT_SERVER = '" + DAOHelper.convertEscapeStringAndGB2312To8859P1(histDP.DPServer) + "' " + " AND DATA_PT_HOST = '" + DAOHelper.convertEscapeStringAndGB2312To8859P1(histDP.DPHost) + "' " + " AND DATA_PT_DATE >= @StartDateValue" + " AND DATA_PT_DATE <= @EndDateValue" + " ORDER BY DATA_PT_DATE"; // } // else // used in history mode, will ignore "startTime" and "endTime" // { // localSQL = " SELECT DATA_PT_DATE,DATA_PT_VALUE " + // " FROM TRENDVIEWER_LOG WHERE DATA_PT_NAME = '" + DAOHelper.convertGB2312To8859P1(histDP.DPName) + "' " + // " AND DATA_PT_SERVER = '" + DAOHelper.convertGB2312To8859P1(histDP.DPServer) + "' " + // " AND DATA_PT_HOST = '" + DAOHelper.convertGB2312To8859P1(histDP.DPHost) + "' " + // " AND DATA_PT_DATE >= to_date(" + histDP.DPStartDateTime.ToString("yyyyMMdd") + // "000000,'YYYYMMDDHH24MISS') " + // " AND DATA_PT_DATE <= to_date(" + histDP.DPEndDateTime.ToString("yyyyMMdd") + // "235959,'YYYYMMDDHH24MISS') " + // " ORDER BY DATA_PT_DATE"; // } SqlParameter parameter1 = new SqlParameter(); parameter1.ParameterName = "StartDateValue"; parameter1.DbType = DbType.DateTime; DateTime dtStart = new DateTime(histDP.DPStartDateTime.Year, histDP.DPStartDateTime.Month, histDP.DPStartDateTime.Day, startTime.Hour, startTime.Minute, startTime.Second); parameter1.Value = dtStart; parameter1.Direction = System.Data.ParameterDirection.Input; List <SqlParameter> parameters = new List <SqlParameter>(); parameters.Add(parameter1); SqlParameter parameter2 = new SqlParameter(); parameter2.ParameterName = "EndDateValue"; parameter2.DbType = DbType.DateTime; DateTime dtEnd = new DateTime(histDP.DPEndDateTime.Year, histDP.DPEndDateTime.Month, histDP.DPEndDateTime.Day, endTime.Hour, endTime.Minute, endTime.Second); parameter2.Value = dtEnd; parameter2.Direction = System.Data.ParameterDirection.Input; parameters.Add(parameter2); System.Data.IDataReader drReader = SimpleDatabase.GetInstance().ExecuteQueryWithParams(localSQL, parameters); try { while (drReader != null && drReader.Read()) { EtyTrendLog entity = new EtyTrendLog(); if (!drReader.IsDBNull(drReader.GetOrdinal("DATA_PT_DATE"))) { DateTime dt; if (DateTime.TryParse(drReader["DATA_PT_DATE"].ToString(), out dt)) { entity.Data_PT_Time = dt; } } if (!drReader.IsDBNull(drReader.GetOrdinal("DATA_PT_VALUE"))) { entity.Data_PT_Value = Convert.ToDouble(drReader["DATA_PT_VALUE"]); } res.Add(entity); } } catch (System.Exception ex) { LogHelper.Error(CLASS_NAME, Function_Name, ex.ToString()); } if (drReader != null) { drReader.Close(); drReader.Dispose(); } return(res); }
/// <summary> /// The main function for logging the datapoints value from OPC Server in database. /// </summary> public void Run() { string Function_Name = "Run"; LogHelper.Trace(CLASS_NAME, Function_Name, "Function_Entered"); //store current thread instance m_thread = Thread.CurrentThread; //Initiliase the Datalogger here to avoid different thread COM Object issue. OPCDataPointManager.GetInstance().InitializeDataLogger(false); //till the thread is running while (m_serviceStarted) { try { // Start ID 0001170 if (TestOracleConnection() && CheckOPCConnection()) { //LogFaileDataPTQuene(); // Add Datapoints which failed to be added to OPCGroup since OPCServer didnot intialize at that time properly. OPCServerProxy.GetInstance().AddFailedOPCItems(); // End ID 0001170 CheckSampleGroupFlag(); OPCDPGrpDetails opcDetails = null; Dictionary <string, OPCDPGrpDetails> opcDataPTsDic = OPCDataPointManager.GetInstance().GetOPCLoggerDataPoints(); foreach (KeyValuePair <string, OPCDPGrpDetails> pair in opcDataPTsDic) { try { opcDetails = pair.Value; //bOPCItemQualityNotGood = false; if (CanLogDataPT(opcDetails)) { DateTime plannedLogTime = System.Convert.ToDateTime(opcDetails.NextSampleTime); DateTime currenttime = DateTime.Now; opcDetails.NextSampleTime = OPCDataPointManager.GetInstance().GetNextSampleTime(plannedLogTime, opcDetails.Interval, false); string opcvalue = GetOPCValue(opcDetails.DT_PT_Name); if (opcvalue.Trim() != "") { opcDetails.Value = opcvalue; } else { LogHelper.Debug(CLASS_NAME, Function_Name, string.Format("DataPoint: {0}- DataPoint value: invalid", opcDetails.DT_PT_Name)); //bOPCItemQualityNotGood = true; continue; } string value = ""; double delta = 0; bool writeFlag = false; EtyTrendLog etyTrendLog = new EtyTrendLog(); etyTrendLog.Data_PT_Name = opcDetails.DT_PT_Name; etyTrendLog.Data_PT_Host = m_opcSvrHost; etyTrendLog.Data_PT_Server = m_opcSrvName; etyTrendLog.Data_PT_Time = currenttime; //first time logging if (opcDetails.OldValue.ToString() == "null") { opcDetails.OldValue = value = opcDetails.Value; etyTrendLog.Data_PT_Value = Convert.ToDouble(value); writeFlag = true; } else { // check delta delta = Math.Abs(Convert.ToDouble(opcDetails.Value.ToString()) - Convert.ToDouble(opcDetails.OldValue.ToString())); //log only if the value is >= delta value. if (delta >= Convert.ToDouble(opcDetails.Delta.ToString())) { opcDetails.OldValue = value = opcDetails.Value; etyTrendLog.Data_PT_Value = Convert.ToDouble(value); writeFlag = true; } } if (writeFlag) { OPCDataPointManager.GetInstance().PushToWriteQuene(etyTrendLog); } } } catch (Exception localException) { LogHelper.Error(opcDetails.DT_PT_Name.ToString() + " Fail"); CheckOracleConnection(localException.ToString()); } } } } catch (Exception localException) { LogHelper.Error(CLASS_NAME, Function_Name, localException.ToString()); CheckOracleConnection(localException.ToString()); } if (m_serviceStarted) { Thread.Sleep(new TimeSpan(0, 0, 0, 0, OPCDataPointManager.GetInstance().GetLoggerInterval())); } } // time to end the thread Thread.CurrentThread.Abort(); LogHelper.Trace(CLASS_NAME, Function_Name, "Function_Exited"); }