Opc.Ua.Com.Server.ComDaGroupManager.AddGroup C# (CSharp) Method

AddGroup() public method

Adds the group.
public AddGroup ( string groupName, bool active, int updateRate, int clientHandle, int timeBias, float deadband, int lcid ) : ComDaGroup
groupName string The group name.
active bool if set to true the group is active.
updateRate int The update rate.
clientHandle int The client handle.
timeBias int The time bias.
deadband float The deadband.
lcid int The lcid.
return ComDaGroup
        public ComDaGroup AddGroup(
            string groupName,
            bool active,
            int updateRate,
            int clientHandle,
            int timeBias,
            float deadband,
            int lcid)
        {
            TraceState("AddGroup", groupName, active, updateRate, clientHandle, timeBias, deadband, lcid);

            // check for valid session.
            Session session = m_session;

            if (session == null)
            {
                throw ComUtils.CreateComException(ResultIds.E_FAIL);
            }

            ComDaGroup group = null;

            // check for duplicate name.
            lock (m_lock)
            {
                // ensure the name is unique.
                if (!String.IsNullOrEmpty(groupName))
                {
                    if (GetGroupByName(groupName) != null)
                    {
                        throw ComUtils.CreateComException(ResultIds.E_DUPLICATENAME);
                    }
                }

                // assign a unique name.
                else
                {
                    groupName = Utils.Format("Group{0}", m_groupCounter+1);
                }

                // validate the deadband.
                if (deadband < 0 || deadband > 100)
                {
                    throw ComUtils.CreateComException(ResultIds.E_INVALIDARG);
                }

                // create the group.
                group = new ComDaGroup(this, groupName, ++m_groupCounter);
                m_groups.Add(group);

                group.ClientHandle = clientHandle;
                group.Active = active;
                group.UpdateRate = updateRate;
                group.TimeBias = timeBias;
                group.Deadband = deadband;
                group.Lcid = lcid;

                if (updateRate < 100)
                {
                    updateRate = 100;
                }

                // create a new subscription.
                Subscription subscription = new Subscription();
                subscription.DisplayName = groupName;
                subscription.PublishingInterval = updateRate/2;
                subscription.KeepAliveCount = 30;
                subscription.LifetimeCount = 600;
                subscription.MaxNotificationsPerPublish = 10000;
                subscription.Priority = 1;
                subscription.PublishingEnabled = active;
                subscription.DisableMonitoredItemCache = true;
                
                // create the subscription on the server.
                session.AddSubscription(subscription);

                try
                {
                    // create the initial subscription.
                    subscription.Create();

                    // set the keep alive interval to 30 seconds and the the lifetime interval to 5 minutes.
                    subscription.KeepAliveCount = (uint)((30000/(int)subscription.CurrentPublishingInterval)+1);
                    subscription.LifetimeCount = (uint)((600000/(int)subscription.CurrentPublishingInterval)+1);

                    // update the subscription.
                    subscription.Modify();
                }
                catch (Exception e)
                {
                    session.RemoveSubscription(subscription);
                    Utils.Trace((int)Utils.TraceMasks.Error, "Create subscription failed: {0}", e.Message);
                    throw ComUtils.CreateComException(e, ResultIds.E_FAIL);
                }

                // update the group.
                group.ActualUpdateRate = (int)(subscription.CurrentPublishingInterval*2);
                group.Subscription = subscription;
            }

            return group;
        }