private void Group_DataChange(object p_Sender, DataChangeEventArgs p_EvArgs) { m_ItemAccessMutex.WaitOne(); for (int i = 0; i < p_EvArgs.Status.Length; ++i) { try { Dictionary <string, string> l_ItemProps = new Dictionary <string, string>(); IOPCItem l_Item = m_ServerModel.GetItemFromClientHandle(p_EvArgs.Status[i].HandleClient); l_ItemProps.Add(ServerModel.ITEM_PROP_VALUE_KEY, OPCUtility.ValueToString(l_Item.Type, p_EvArgs.Status[i].DataValue)); l_ItemProps.Add(ServerModel.ITEM_PROP_QUALITY_KEY, OPCUtility.QualityToString(p_EvArgs.Status[i].Quality)); l_ItemProps.Add(ServerModel.ITEM_PROP_TIMESTAMP_KEY, OPCUtility.TimeStampToString(p_EvArgs.Status[i].Timestamp)); m_ServerModel.UpdateItem(p_EvArgs.Status[i].HandleClient, l_ItemProps); if (this.VeriteqOPCServer) { WriteToDatabase(l_Item); } } catch (Exception l_Ex) { ErrorLog l_ErrorLog = ErrorLog.GetInstance(); l_ErrorLog.WriteToErrorLog(l_Ex.Message, l_Ex.StackTrace, "Error during OPC group update"); } } m_ItemAccessMutex.ReleaseMutex(); }
public void WriteItems(List <string> p_ItemsId, string p_Value) { m_ItemAccessMutex.WaitOne(); try { int l_ItemsToWriteCount = p_ItemsId.Count; int[] l_ServerHandleArray = new int[l_ItemsToWriteCount]; object[] l_ValueArray = new object[l_ItemsToWriteCount]; int[] l_ErrorArray; for (int i = 0; i < l_ItemsToWriteCount; ++i) { l_ServerHandleArray[i] = m_ServerModel.GetServerHandleFromItemId(p_ItemsId[i]); int l_ClientHandle = m_ServerModel.GetClientHandleFromServerHandle(l_ServerHandleArray[i]); IOPCItem l_Item = m_ServerModel.GetItemFromClientHandle(l_ClientHandle); l_ValueArray[i] = OPCUtility.StringToValue(l_Item.Type, p_Value); } m_Group.Write(l_ServerHandleArray, l_ValueArray, out l_ErrorArray); // TODO Check the error } catch (Exception l_Ex) { throw l_Ex; } finally { m_ItemAccessMutex.ReleaseMutex(); } }
public void SetItems(IOPCItem items) { //ctrl.SetItems(new string[] { ITEM_1, ITEM_2, ITEM_3, ITEM_4, ITEM_5 }); // ctrl.SetItems(new string[] { ITEM_1,ITEM_2,ITEM_3,ITEM_4}); ctrl.SetItems(items.GetItemList().ToArray()); ctrl.OPCDataChange += new OPCControl.OPCDataChangeEventHandler(DataChange); ctrl.OPCReadComplete += new OPCControl.OPCReadCompleteEventHandler(RingItemReadComplete); }
public void ReadItems() { m_ItemAccessMutex.WaitOne(); try { int l_Index = 0; OPCItemState[] l_ItemStateArray; List <int> l_ServerHandleList = m_ServerModel.GetServerHandleList(); m_Group.Read(OPCDataSource.OPC_DS_DEVICE, l_ServerHandleList.ToArray(), out l_ItemStateArray); foreach (int l_ServerHandle in l_ServerHandleList) { if (l_ItemStateArray[l_Index].Error == HResults.S_OK) { int l_ClientHandle = m_ServerModel.GetClientHandleFromServerHandle(l_ServerHandle); Dictionary <string, string> l_ItemProps = new Dictionary <string, string>(); IOPCItem l_Item = m_ServerModel.GetItemFromClientHandle(l_ClientHandle); l_ItemProps.Add(ServerModel.ITEM_PROP_VALUE_KEY, OPCUtility.ValueToString(l_Item.Type, l_ItemStateArray[l_Index].DataValue)); l_ItemProps.Add(ServerModel.ITEM_PROP_QUALITY_KEY, OPCUtility.QualityToString(l_ItemStateArray[l_Index].Quality)); l_ItemProps.Add(ServerModel.ITEM_PROP_TIMESTAMP_KEY, OPCUtility.TimeStampToString(l_ItemStateArray[l_Index].Timestamp)); m_ServerModel.UpdateItem(l_ClientHandle, l_ItemProps); if (this.VeriteqOPCServer) { WriteToDatabase(l_Item); } } l_Index++; } } catch (Exception l_Ex) { Disconnect(); throw l_Ex; } finally { m_ItemAccessMutex.ReleaseMutex(); } }
public void ReadItems() { _itemAccessMutex.WaitOne(); try { int l_Index = 0; OpcItemState[] l_ItemStateArray; List <int> l_ServerHandleList = _serverModel.GetServerHandleList(); _group.Read(OpcDataSource.OPC_DS_DEVICE, l_ServerHandleList.ToArray(), out l_ItemStateArray); foreach (int l_ServerHandle in l_ServerHandleList) { if (l_ItemStateArray[l_Index].Error == HResults.S_OK) { int l_ClientHandle = _serverModel.GetClientHandleFromServerHandle(l_ServerHandle); Dictionary <string, string> l_ItemProps = new Dictionary <string, string>(); IOPCItem l_Item = _serverModel.GetItemFromClientHandle(l_ClientHandle); l_ItemProps.Add(CModel.ServerModel.ItemPropValueKey, OpcUtility.ValueToString(l_Item.Type, l_ItemStateArray[l_Index].DataValue)); l_ItemProps.Add(CModel.ServerModel.ItemPropQualityKey, OpcUtility.QualityToString(l_ItemStateArray[l_Index].Quality)); l_ItemProps.Add(CModel.ServerModel.ItemPropTimestampKey, OpcUtility.TimeStampToString(l_ItemStateArray[l_Index].Timestamp)); _serverModel.UpdateItem(l_ClientHandle, l_ItemProps); } l_Index++; } } catch (Exception l_Ex) { Disconnect(); throw l_Ex; } finally { _itemAccessMutex.ReleaseMutex(); } }
public void WriteToDatabase(IOPCItem p_Items) { string l_Id, l_Value, l_Quality; l_Id = p_Items.ID; l_Value = p_Items.Value; l_Quality = p_Items.Quality; string[] ID_Spilts = l_Id.Split(new char[] { '.' }); if (ID_Spilts.Length < 3) { /* We will not record this message to the Database*/ return; } /* Update the <ID, Description> Dictionary if the data is Px.Cx.Description */ if (ID_Spilts[2] != null && ID_Spilts[2] == "Description") { string Temp_ID = ID_Spilts[0] + ID_Spilts[1]; if (!m_DescriptionDictionary.ContainsKey(Temp_ID)) { m_DescriptionDictionary.Add(Temp_ID, l_Value); } else { m_DescriptionDictionary.Remove(Temp_ID); m_DescriptionDictionary.Add(Temp_ID, l_Value); } } /* Update the <ID, Unit> Dictionary if the data is Px.Cx.Units */ if (ID_Spilts[2] != null && ID_Spilts[2] == "Units") { string Temp_ID = ID_Spilts[0] + ID_Spilts[1]; if (!m_UnitDictionary.ContainsKey(Temp_ID)) { m_UnitDictionary.Add(Temp_ID, l_Value); } else { m_UnitDictionary.Remove(Temp_ID); m_UnitDictionary.Add(Temp_ID, l_Value); } } if (l_Value.Length == 0 || l_Value == "" || l_Value == "0" || l_Quality == "BAD" || l_Quality == "bad") { System.Console.WriteLine("The Value is not valid to wirte into database!!!!"); return; } /* Write Data into Database if the data is Px.Cx.Value */ if (ID_Spilts[2] != null && ID_Spilts[2] == "Value") { string Value_ID, Value_Descrition, Value, Value_Unit, Value_TimeStamp, Rec_TimeStamp; Value_ID = ID_Spilts[0] + ID_Spilts[1]; m_DescriptionDictionary.TryGetValue(Value_ID, out Value_Descrition); m_UnitDictionary.TryGetValue(Value_ID, out Value_Unit); Value_TimeStamp = p_Items.Timestamp; Value = p_Items.Value; DateTime recordtime = DateTime.Now; Rec_TimeStamp = recordtime.ToLocalTime().ToString(); /* Get a statement to write data into SQL Server Database */ if (Database_con != null) { /* Schema * T_OPCData( probeindex varchar(10), * probename varchar(36), * value float, * unit varchar(10), * capturetime dateTime, * recordtime dateTime * ) */ string CommandText; CommandText = "insert into T_OPCData values("; CommandText += "'" + ID_Spilts[0] + "', "; CommandText += "'" + ID_Spilts[1] + "', "; CommandText += "'" + Value_Descrition + "', "; switch (Value_Unit.Substring(Value_Unit.Length - 1, 1)) { case "C": CommandText += "Convert(decimal(18,2)," + Value + "), "; break; case "H": CommandText += "Convert(decimal(18,1)," + Value + "), "; break; default: CommandText += Value + ", "; break; } CommandText += "'" + Value_Unit + "', "; CommandText += "'" + Value_TimeStamp + "', "; CommandText += "'" + Rec_TimeStamp + "')"; System.Console.WriteLine("gary debug: SQL" + CommandText); try { SqlCommand command = new SqlCommand(CommandText, Database_con); command.ExecuteNonQuery(); command.Dispose(); } catch (Exception Ex) { throw Ex; } } } }