/// <summary> /// Removes the item from the group. /// </summary> /// <param name="item">The item.</param> /// <returns>True if the item exists.</returns> public bool RemoveItem(GroupItem item) { // add the item if it does not exist. if (item == null) { return false; } // flag the item as deleted. item.Deleted = true; return true; }
/// <summary> /// Modifies the item. /// </summary> /// <param name="item">The item.</param> /// <param name="samplingInterval">The sampling interval.</param> /// <param name="deadband">The deadband.</param> /// <param name="active">if set to <c>true</c> [active].</param> /// <returns>True if the item is in the group.</returns> public bool ModifyItem(GroupItem item, int samplingInterval, float deadband, bool active) { // check if the item can be added to the group. if (m_samplingInterval != samplingInterval || m_deadband != deadband) { // check if the item needs to be removed from the group. if (m_items.Count > 1) { item.Deleted = true; return false; } // update active state. item.ActiveChanged = active != item.Active; item.Active = active; // update the group parameters. m_samplingInterval = samplingInterval; m_deadband = deadband; m_updateRequired = true; return true; } // undelete the item. item.Deleted = false; // update active state. item.ActiveChanged = active != item.Active; item.Active = active; // nothing to do - the group matches the item. return true; }
/// <summary> /// Sets the monitored items associated with the item. /// </summary> /// <param name="item">The item.</param> /// <param name="monitoredItems">The monitored items.</param> public void SetMonitoredItems(GroupItem item, MonitoredItem[] monitoredItems) { // check if callbacks are enabled. if (item == null || m_monitoredItems == null) { return; } // save the monitored items. lock (m_monitoredItems) { DataChangeInfo info = null; if (!m_monitoredItems.TryGetValue(item.ClientHandle, out info)) { m_monitoredItems[item.ClientHandle] = info = new DataChangeInfo(); } info.MonitoredItems = monitoredItems; // resend the last cached value. if (info.LastError != null || info.LastValue != null) { for (int ii = 0; ii < monitoredItems.Length; ii++) { monitoredItems[ii].QueueValue(info.LastValue, info.LastError); } } } }
/// <summary> /// Creates the item. /// </summary> /// <param name="itemId">The item id.</param> /// <param name="samplingInterval">The sampling interval.</param> /// <param name="deadband">The deadband.</param> /// <param name="active">if set to <c>true</c> [active].</param> /// <returns> /// The item that was added to the group. Null if the item could not be added. /// </returns> public GroupItem CreateItem(string itemId, int samplingInterval, float deadband, bool active) { // set the group parameters if this is the first item. if (m_items.Count == 0) { m_samplingInterval = samplingInterval; m_deadband = deadband; } // check if the item can be added to the group. if (m_samplingInterval != samplingInterval || m_deadband != deadband) { return null; } // create the item. GroupItem item = new GroupItem(); item.ItemId = itemId; item.ClientHandle = Utils.IncrementIdentifier(ref m_itemCounter); item.ServerHandle = 0; item.Active = active; item.ActiveChanged = false; item.Deleted = false; item.Created = false; item.ErrorId = 0; lock (Lock) { m_items.Add(item); } return item; }
/// <summary> /// Writes the item values to servers. /// </summary> /// <param name="requests">The requests.</param> private void WriteItemValues(List <WriteRequest> requests) { ComDaGroup group = new ComDaGroup(this, false); try { int count1 = 0; GroupItem[] items = new GroupItem[requests.Count]; WriteRequest[] addItemRequests = new WriteRequest[requests.Count]; // create the items in the temporary group. for (int ii = 0; ii < requests.Count; ii++) { WriteRequest request = requests[ii]; if (request == null) { continue; } // status code writes not supported. if (request.Value.StatusCode != StatusCodes.Good) { request.Error = ResultIds.E_NOTSUPPORTED; continue; } // timestamp writes not supported. if (request.Value.SourceTimestamp != DateTime.MinValue) { request.Error = ResultIds.E_NOTSUPPORTED; continue; } // add the item. items[count1] = group.CreateItem(request.ItemId, 0, 0, true); addItemRequests[count1] = request; count1++; } // create the items on the server. group.ApplyChanges(); // build the list of values to write. int count2 = 0; int[] serverHandles = new int[count1]; object[] values = new object[count1]; WriteRequest[] writeRequests = new WriteRequest[count1]; for (int ii = 0; ii < count1; ii++) { // check for error on create. GroupItem item = items[ii]; WriteRequest request = addItemRequests[ii]; if (item.ErrorId < 0) { request.Error = item.ErrorId; continue; } serverHandles[count2] = item.ServerHandle; values[count2] = ComUtils.GetVARIANT(request.Value.Value); writeRequests[count2] = request; count2++; } // write values to the server. int[] errors = group.SyncWrite(serverHandles, values, count2); // read the errors. for (int ii = 0; ii < count2; ii++) { writeRequests[ii].Error = errors[ii]; } } finally { // delete the group and items. group.Delete(); } }