/// <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> /// 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> /// 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> /// Reads the item values and stores the results in the request object. /// </summary> /// <param name="requests">The requests.</param> private void Da20ReadItemValues(List<ReadRequest> requests) { // lock (m_groupLock) { // if (m_group == null) //{ ComDaGroup m_group = new ComDaGroup(this, false); //} try { int count1 = 0; GroupItem[] items = new GroupItem[requests.Count]; ReadRequest[] addItemRequests = new ReadRequest[requests.Count]; // create the items in the temporary group. for (int ii = 0; ii < requests.Count; ii++) { ReadRequest request = requests[ii]; if (request == null) { continue; } if (!request.ValueRequired) { continue; } // add the item. items[count1] = m_group.CreateItem(request.ItemId, 0, 0, true); addItemRequests[count1] = request; count1++; } // create the items on the server. m_group.ApplyChanges(); // build the list of values to write. int count2 = 0; int[] serverHandles = new int[count1]; ReadRequest[] readRequests = new ReadRequest[count1]; for (int ii = 0; ii < count1; ii++) { // check for error on create. GroupItem item = items[ii]; ReadRequest request = addItemRequests[ii]; if (item.ErrorId < 0) { request.Value = new DaValue(); request.Value.Error = item.ErrorId; continue; } serverHandles[count2] = item.ServerHandle; readRequests[count2] = request; count2++; } if (count2 > 0) { // write values to the server. DaValue[] values = m_group.SyncRead(serverHandles, count2); // read the values. for (int ii = 0; ii < count2; ii++) { if (values != null && values.Length > ii) { readRequests[ii].Value = values[ii]; } else { readRequests[ii].Value = new DaValue() { Error = ResultIds.E_FAIL, Timestamp = DateTime.UtcNow }; } } // delete the items. for (int ii = 0; ii < count1; ii++) { GroupItem item = items[ii]; if (item.ErrorId >= 0) { m_group.RemoveItem(item); } } m_group.ApplyChanges(); } } finally { // delete the group and items. m_group.Delete(); } } }
/// <summary> /// Writes the item values to servers. /// </summary> /// <param name="requests">The requests.</param> private void Da20WriteItemValues(List<WriteRequest> requests) { //lock (m_groupLock) { //if (m_group == null) //{ ComDaGroup m_group = new ComDaGroup(this, false); //} try { int count1 = 0; GroupItem[] items = new GroupItem[requests.Count]; WriteRequest[] addItemRequests = new WriteRequest[requests.Count]; object[] convertedValues = new object[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.ServerTimestamp != DateTime.MinValue) { request.Error = ResultIds.E_NOTSUPPORTED; continue; } // timestamp writes not supported. if (request.Value.SourceTimestamp != DateTime.MinValue) { request.Error = ResultIds.E_NOTSUPPORTED; continue; } // convert to a DA compatible type. object convertedValue = null; request.Error = ComDaClientNodeManager.LocalToRemoteValue(request.Value.WrappedValue, out convertedValue); if (request.Error < 0) { continue; } // add the item. items[count1] = m_group.CreateItem(request.ItemId, 0, 0, true); addItemRequests[count1] = request; convertedValues[count1] = convertedValue; count1++; } // create the items on the server. m_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] = convertedValues[ii]; writeRequests[count2] = request; count2++; } if (count2 > 0) { // write values to the server. int[] errors = m_group.SyncWrite(serverHandles, values, count2); // read the errors. for (int ii = 0; ii < count2; ii++) { if (errors != null && errors.Length > ii) { writeRequests[ii].Error = errors[ii]; } else { writeRequests[ii].Error = ResultIds.E_FAIL; } } // delete the items. for (int ii = 0; ii < count1; ii++) { GroupItem item = items[ii]; if (item.ErrorId >= 0) { m_group.RemoveItem(item); } } m_group.ApplyChanges(); } } finally { // delete the group and items. m_group.Delete(); } } }