Example #1
0
        public override string GetValue(string itemName)
        {
            if (!ServiceManager.DesignMode && _opcDaServer.IsConnected)
            {
                string     val  = null;
                OpcAddress addr = Find(itemName);
                if (addr != null)
                {
                    val = addr.Value;
                }
                else if (addr == null && AutoCreateAddr)
                {
                    Add(itemName);
                    addr = Find(itemName);
                    val  = addr.GetValue();
                }

                if (val == null)
                {
                    val = addr.GetValue();
                    if (val != null)
                    {
                        addr.SetValue(val);
                    }
                    else
                    {
                        val = string.Empty;
                    }
                }
                return(val);
            }
            else
            {
                return(string.Empty);
            }
        }
Example #2
0
        public override void Add(string itemName)
        {
            lock (_thisLock)
            {
                try
                {
                    OpcAddress addr = Find(itemName);

                    if (addr == null)
                    {
                        string[] itemIdSplited = itemName.Split(']');
                        string[] addressSplited;
                        string   topic = "";

                        //Encontra o nome do topico
                        if (itemName.Contains(']'))
                        {
                            topic = itemName.Split(']')[0].Substring(1);
                        }

                        if (itemIdSplited.Length > 1)
                        {
                            addressSplited = itemIdSplited[1].Split(':');
                        }
                        else
                        {
                            itemIdSplited  = itemName.Split('.');
                            addressSplited = itemIdSplited[2].Split(':');
                        }

                        Opc.Da.Subscription opcDaGroup = null;
                        foreach (Opc.Da.Subscription opcDaGrp in _opcDaServer.Subscriptions)
                        {
                            if (opcDaGrp.Name == topic + addressSplited[0])
                            {
                                opcDaGroup = opcDaGrp;
                            }
                        }

                        if (opcDaGroup == null)
                        {
                            Opc.Da.SubscriptionState groupState = new Opc.Da.SubscriptionState();
                            groupState.ClientHandle = Guid.NewGuid().ToString();
                            groupState.Name         = topic + addressSplited[0];
//							groupState.UpdateRate = 250;

                            opcDaGroup = (Opc.Da.Subscription)_opcDaServer.CreateSubscription(groupState);
                            //opcDaGroup.DataChanged += new Opc.Da.DataChangedEventHandler(OpcDataChanged);
                            opcDaGroup.DataChanged += delegate(object subscriptionHandle, object requestHandle, ItemValueResult[] values)
                            {
//								BackgroundWorker back = new BackgroundWorker();
//								back.DoWork += delegate(object sender, DoWorkEventArgs e)
//								{
//									foreach (Opc.Da.ItemValueResult ivr in values)
//									{
//										OpcAddress adr = Find(ivr.ItemName);
//										adr.SetValue(ivr.Value.ToString());
//										OnDataChanged(new DataChangedEventArgs(ivr.ItemName, ivr.Value));
//									}
//								};
//								back.RunWorkerAsync();
                                foreach (Opc.Da.ItemValueResult ivr in values)
                                {
                                    OpcAddress adr = Find(ivr.ItemName);
                                    adr.SetValue(ivr.Value.ToString());
                                    OnDataChanged(new DataChangedEventArgs(ivr.ItemName, ivr.Value));
                                }
                            };
                        }

                        Opc.Da.Item opcDaItem = new Opc.Da.Item();
                        opcDaItem.ClientHandle          = Guid.NewGuid().ToString();
                        opcDaItem.ItemName              = itemName;
                        opcDaItem.MaxAgeSpecified       = true;
                        opcDaItem.MaxAge                = 100;
                        opcDaItem.SamplingRateSpecified = true;
                        opcDaItem.SamplingRate          = 10;

                        opcDaGroup.AddItems(new Opc.Da.Item[] { opcDaItem });

                        addr = new OpcAddress(opcDaItem, opcDaGroup);
                        _addrList.Add(addr);
                    }

                    return;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
            }
        }