/// <summary> /// Toggles the active/inactive state for a subscription. /// </summary> private void ActiveMI_Click(object sender, System.EventArgs e) { try { ListViewItem item = subscriptionsLv_.SelectedItems[0]; TsCAeSubscription subscription = (TsCAeSubscription)item.Tag; TsCAeSubscriptionState state = new TsCAeSubscriptionState { Active = !activeMi_.Checked }; subscription.ModifyState((int)TsCAeStateMask.Active, state); // toggle checkbox. activeMi_.Checked = !activeMi_.Checked; // update list. Update(item); // receive notifications. if (MSubscriptionAction != null) { MSubscriptionAction(subscription, !subscription.Active); } // adjust columns. AdjustColumns(subscriptionsLv_); } catch (Exception exception) { MessageBox.Show(exception.Message); } }
//====================================================================== // State Management /// <summary> /// Returns the current state of the subscription. /// </summary> /// <returns>The current state of the subscription.</returns> public TsCAeSubscriptionState GetState() { lock (this) { // verify state and arguments. if (m_subscription == null) { throw new NotConnectedException(); } // initialize arguments. int pbActive = 0; int pdwBufferTime = 0; int pdwMaxSize = 0; int phClientSubscription = 0; int pdwKeepAliveTime = 0; // invoke COM method. try { ((IOPCEventSubscriptionMgt)m_subscription).GetState( out pbActive, out pdwBufferTime, out pdwMaxSize, out phClientSubscription); } catch (Exception e) { throw Technosoftware.DaAeHdaClient.Com.Interop.CreateException("IOPCEventSubscriptionMgt.GetState", e); } // get keep alive. if (m_supportsAE11) { try { ((IOPCEventSubscriptionMgt2)m_subscription).GetKeepAlive(out pdwKeepAliveTime); } catch (Exception e) { throw Technosoftware.DaAeHdaClient.Com.Interop.CreateException("IOPCEventSubscriptionMgt2.GetKeepAlive", e); } } // build results TsCAeSubscriptionState state = new TsCAeSubscriptionState(); state.Active = pbActive != 0; state.ClientHandle = m_clientHandle; state.BufferTime = pdwBufferTime; state.MaxSize = pdwMaxSize; state.KeepAlive = pdwKeepAliveTime; // return results. return(state); } }
/// <summary> /// Creates a new object. /// </summary> public object Create() { TsCAeSubscriptionState state = new TsCAeSubscriptionState { BufferTime = 1000 }; return(state); }
/// <summary> /// Copy values from control into object - throw exceptions on error. /// </summary> public object Get() { TsCAeSubscriptionState state = new TsCAeSubscriptionState { Name = nameTb_.Text, Active = activeCb_.Checked, BufferTime = (int)bufferTimeCtrl_.Value, KeepAlive = (int)keepAliveCtrl_.Value, MaxSize = (int)maxSizeCtrl_.Value }; return(state); }
/// <summary> /// Prompts a user to create a new subscription with a modal dialog. /// </summary> public TsCAeSubscription ShowDialog(TsCAeServer server, TsCAeSubscription subscription) { if (server == null) { throw new ArgumentNullException("server"); } mServer_ = server; mSubscription_ = subscription; // go to the initial stage. mStage_ = 0; ChangeStage(0); // initialize controls. stateCtrl_.SetDefaults(); filtersCtrl_.SetDefaults(); categoriesCtrl_.ShowCategories(mServer_); attributesCtrl_.ShowAttributes(mServer_); browseCtrl_.ShowAreas(mServer_); if (mSubscription_ != null) { mState_ = mSubscription_.GetState(); mFilters_ = mSubscription_.GetFilters(); mAttributes_ = mSubscription_.GetAttributes(); mAreas_ = mSubscription_.Areas.ToArray(); mSources_ = mSubscription_.Sources.ToArray(); } else { mState_.Name = String.Format("Subscription{0,3:000}", ++mCount_); } // set current values. stateCtrl_.Set(mState_); filtersCtrl_.Set(mFilters_); categoriesCtrl_.SetSelectedCategories(mFilters_.Categories.ToArray()); attributesCtrl_.SetSelectedAttributes(mAttributes_); areaSourcesListCtrl_.AddAreas(mAreas_); areaSourcesListCtrl_.AddSources(mSources_); // show dialog. if (ShowDialog() != DialogResult.OK) { return(null); } // return updated/created subscription. return(mSubscription_); }
/// <summary> /// Called when the next button is clicked. /// </summary> private void NextBTN_Click(object sender, System.EventArgs e) { if (mStage_ == 0) { mState_ = (TsCAeSubscriptionState)stateCtrl_.Get(); mFilters_ = (TsCAeSubscriptionFilters)filtersCtrl_.Get(); mFilters_.Categories.Clear(); mFilters_.Categories.AddRange(categoriesCtrl_.GetSelectedCategories()); mAttributes_ = attributesCtrl_.GetSelectedAttributes(); } ChangeStage(++mStage_); }
/// <summary> /// Copy object values into controls. /// </summary> public void Set(object value) { // check for null value. if (value == null) { SetDefaults(); return; } TsCAeSubscriptionState state = (TsCAeSubscriptionState)value; nameTb_.Text = state.Name; activeCb_.Checked = state.Active; bufferTimeCtrl_.Value = (decimal)state.BufferTime; keepAliveCtrl_.Value = (decimal)state.KeepAlive; maxSizeCtrl_.Value = (decimal)state.MaxSize; }
/// <summary> /// Initializes the object with the specified URL and COM server. /// </summary> internal Subscription(TsCAeSubscriptionState state, object subscription) { m_subscription = subscription; m_clientHandle = Technosoftware.DaAeHdaClient.OpcConvert.Clone(state.ClientHandle); m_supportsAE11 = true; m_callback = new Callback(state.ClientHandle); // check if the V1.1 interfaces are supported. try { IOPCEventSubscriptionMgt2 server = (IOPCEventSubscriptionMgt2)m_subscription; } catch { m_supportsAE11 = false; } }
/// <summary> /// Initializes the object with the specified URL and COM server. /// </summary> internal Subscription(TsCAeSubscriptionState state, object subscription) { subscription_ = subscription; clientHandle_ = OpcConvert.Clone(state.ClientHandle); supportsAe11_ = true; callback_ = new Callback(state.ClientHandle); // check if the V1.1 interfaces are supported. try { IOPCEventSubscriptionMgt2 server = (IOPCEventSubscriptionMgt2)subscription_; } catch { supportsAe11_ = false; } }
public void Run() { try { const string serverUrl = "opcae://localhost/Technosoftware.AeSample"; Console.WriteLine(); Console.WriteLine("Simple OPC AE Client based on the OPC AE Client SDK .NET Standard"); Console.WriteLine("-----------------------------------------------------------------"); Console.Write(" Press <Enter> to connect to "); Console.WriteLine(serverUrl); Console.ReadLine(); Console.WriteLine(" Please wait..."); TsCAeServer myAeServer = new TsCAeServer(); // Connect to the server myAeServer.Connect(serverUrl); Console.WriteLine(" Connected, press <Enter> to create an active subscription and press <Enter>"); Console.WriteLine(" again to deactivate the subscription. This stops the reception of new events."); Console.ReadLine(); TsCAeSubscriptionState state = new TsCAeSubscriptionState { Active = true, BufferTime = 0, MaxSize = 0, ClientHandle = 100, Name = "Simple Event Subscription" }; TsCAeCategory[] categories = myAeServer.QueryEventCategories((int)TsCAeEventType.Condition); TsCAeAttribute[] attributes = null; attributes = myAeServer.QueryEventAttributes(categories[0].ID); int[] attributeIDs = new int[2]; attributeIDs[0] = attributes[0].ID; attributeIDs[1] = attributes[1].ID; TsCAeSubscription subscription; subscription = (TsCAeSubscription)myAeServer.CreateSubscription(state); subscription.DataChangedEvent += OnDataChangedEvent; subscription.SelectReturnedAttributes(categories[0].ID, attributeIDs); Console.ReadLine(); subscription.DataChangedEvent -= OnDataChangedEvent; Console.WriteLine(" Subscription deactivated, press <Enter> to remove the subscription and disconnect from the server."); subscription.Dispose(); // Remove subscription myAeServer.Disconnect(); // Disconnect from server myAeServer.Dispose(); // Dispose server object Console.ReadLine(); Console.WriteLine(" Disconnected from the server."); Console.WriteLine(); } catch (OpcResultException e) { Console.WriteLine(String.Format(" {0}", e.Message)); Console.ReadLine(); return; } catch (Exception e) { Console.WriteLine(String.Format(" {0}", e.Message)); Console.ReadLine(); return; } }
/// <summary> /// Changes the state of a subscription. /// </summary> /// <param name="masks">A bit mask that indicates which elements of the subscription state are changing.</param> /// <param name="state">The new subscription state.</param> /// <returns>The actual subscption state after applying the changes.</returns> public TsCAeSubscriptionState ModifyState(int masks, TsCAeSubscriptionState state) { lock (this) { // verify state and arguments. if (m_subscription == null) { throw new NotConnectedException(); } // initialize arguments. int active = (state.Active)?1:0; GCHandle hActive = GCHandle.Alloc(active, GCHandleType.Pinned); GCHandle hBufferTime = GCHandle.Alloc(state.BufferTime, GCHandleType.Pinned); GCHandle hMaxSize = GCHandle.Alloc(state.MaxSize, GCHandleType.Pinned); IntPtr pbActive = ((masks & (int)TsCAeStateMask.Active) != 0)?hActive.AddrOfPinnedObject():IntPtr.Zero; IntPtr pdwBufferTime = ((masks & (int)TsCAeStateMask.BufferTime) != 0)?hBufferTime.AddrOfPinnedObject():IntPtr.Zero; IntPtr pdwMaxSize = ((masks & (int)TsCAeStateMask.MaxSize) != 0)?hMaxSize.AddrOfPinnedObject():IntPtr.Zero; int phClientSubscription = 0; int pdwRevisedBufferTime = 0; int pdwRevisedMaxSize = 0; // invoke COM method. try { ((IOPCEventSubscriptionMgt)m_subscription).SetState( pbActive, pdwBufferTime, pdwMaxSize, phClientSubscription, out pdwRevisedBufferTime, out pdwRevisedMaxSize); } catch (Exception e) { throw Technosoftware.DaAeHdaClient.Com.Interop.CreateException("IOPCEventSubscriptionMgt.SetState", e); } finally { if (hActive.IsAllocated) { hActive.Free(); } if (hBufferTime.IsAllocated) { hBufferTime.Free(); } if (hMaxSize.IsAllocated) { hMaxSize.Free(); } } // update keep alive. if (((masks & (int)TsCAeStateMask.KeepAlive) != 0) && m_supportsAE11) { int pdwRevisedKeepAliveTime = 0; try { ((IOPCEventSubscriptionMgt2)m_subscription).SetKeepAlive( state.KeepAlive, out pdwRevisedKeepAliveTime); } catch (Exception e) { throw Technosoftware.DaAeHdaClient.Com.Interop.CreateException("IOPCEventSubscriptionMgt2.SetKeepAlive", e); } } // return current state. return(GetState()); } }