private void AddItems(List <string> p_ItemIds)
        {
            int l_ItemClientHandle = 0;

            OpcItemDef[]    l_ItemDef = new OpcItemDef[p_ItemIds.Count];
            OpcItemResult[] l_AddRes;

            foreach (string l_ItemName in p_ItemIds)
            {
                l_ItemDef[l_ItemClientHandle] = new OpcItemDef(l_ItemName, false, l_ItemClientHandle, VarEnum.VT_BSTR);
                l_ItemClientHandle++;
            }

            _group.AddItems(l_ItemDef, out l_AddRes);

            l_ItemClientHandle = 0;
            foreach (string l_ItemName in p_ItemIds)
            {
                if (l_AddRes[l_ItemClientHandle].Error == HResults.S_OK)
                {
                    Dictionary <string, string> l_ItemProps = new Dictionary <string, string>();
                    l_ItemProps.Add(CModel.ServerModel.ItemPropIdKey, _server.GetItemID(l_ItemName));
                    l_ItemProps.Add(CModel.ServerModel.ItemPropTypeKey, OpcUtility.TypeToString((int)l_AddRes[l_ItemClientHandle].CanonicalDataType));

                    _serverModel.AddItem(l_ItemClientHandle, l_AddRes[l_ItemClientHandle].HandleServer, l_ItemProps);
                }

                l_ItemClientHandle++;
            }
        }
        public void WriteItems(List <string> p_ItemsId, string p_Value)
        {
            _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] = _serverModel.GetServerHandleFromItemId(p_ItemsId[i]);

                    int      l_ClientHandle = _serverModel.GetClientHandleFromServerHandle(l_ServerHandleArray[i]);
                    IOPCItem l_Item         = _serverModel.GetItemFromClientHandle(l_ClientHandle);
                    l_ValueArray[i] = OpcUtility.StringToValue(l_Item.Type, p_Value);
                }
                _group.Write(l_ServerHandleArray, l_ValueArray, out l_ErrorArray);

                // TODO Check the error
            }
            catch (Exception l_Ex)
            {
                throw l_Ex;
            }
            finally
            {
                _itemAccessMutex.ReleaseMutex();
            }
        }
        private void Group_DataChange(object sender, DataChangeEventArgs eventArgs)
        {
            _itemAccessMutex.WaitOne();

            for (int i = 0; i < eventArgs.Status.Length; ++i)
            {
                try
                {
                    var itemProps = new Dictionary <string, string>();
                    var item      = _serverModel.GetItemFromClientHandle(eventArgs.Status[i].HandleClient);

                    itemProps.Add(ServerModel.ItemPropValueKey, OpcUtility.ValueToString(item.Type, eventArgs.Status[i].DataValue));
                    itemProps.Add(ServerModel.ItemPropQualityKey, OpcUtility.QualityToString(eventArgs.Status[i].Quality));
                    itemProps.Add(ServerModel.ItemPropTimestampKey, OpcUtility.TimeStampToString(eventArgs.Status[i].Timestamp));

                    _serverModel.UpdateItem(eventArgs.Status[i].HandleClient, itemProps);
                }
                catch (Exception ex)
                {
                    ErrorLog errorLog = ErrorLog.GetInstance();
                    errorLog.WriteToErrorLog(ex.Message, ex.StackTrace, "Error during OPC group update");
                }
            }

            _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();
            }
        }