/// <summary> /// Creates List, adds/removes items. /// No throw. /// </summary> /// <param name="clientConnectionManager"></param> public void Subscribe(ClientConnectionManager clientConnectionManager) { try { if (!DataGrpcItemsMustBeAddedOrRemoved) { return; } bool firstTimeDataJournalConnection = (DataGrpcList is null); if (firstTimeDataJournalConnection) { try { if (clientConnectionManager.ConnectionExists) { DataGrpcList = clientConnectionManager.NewElementValuesJournalList(null); } } catch (Exception) { } } bool connectionError = SubscribeInitial(); if (!connectionError) { DataGrpcItemsMustBeAddedOrRemoved = false; } } finally { SubscribeFinal(); } }
/// <summary> /// Creates List, adds/removes items. /// No throw. /// </summary> /// <param name="clientConnectionManager"></param> /// <param name="сallbackDispatcher"></param> /// <param name="elementValuesCallbackEventHandler"></param> /// <param name="callbackIsEnabled"></param> /// <param name="ct"></param> public void Subscribe(ClientConnectionManager clientConnectionManager, IDispatcher?сallbackDispatcher, ElementValuesCallbackEventHandler elementValuesCallbackEventHandler, bool callbackIsEnabled, CancellationToken ct) { try { if (ct.IsCancellationRequested) { return; } if (!DataGrpcItemsMustBeAddedOrRemoved) { return; } bool firstTimeDataConnection = (DataGrpcList is null); if (firstTimeDataConnection) { try { if (clientConnectionManager.ConnectionExists) { DataGrpcList = clientConnectionManager.NewElementValueList(null); } } catch (Exception) { } } bool connectionError = SubscribeInitial(); try { if (!connectionError && DataGrpcList is not null && !DataGrpcList.Disposed) { if (firstTimeDataConnection) { DataGrpcList.ElementValuesCallback += (ClientElementValueList dataList, ClientElementValueListItem[] items, ValueStatusTimestamp[] values) => { var changedClientObjs = new List <object>(items.Length); var changedValues = new List <ValueStatusTimestamp>(items.Length); int i = 0; foreach (ClientElementValueListItem dataGrpcElementValueListItem in items) { var o = dataGrpcElementValueListItem.Obj as DataGrpcListItemWrapper; if (o is null) { throw new InvalidOperationException(); } foreach (var modelItem in o.ClientObjectInfosCollection) { modelItem.ForceNotifyClientObj = false; if (modelItem.ClientObj is not null) { changedClientObjs.Add(modelItem.ClientObj); changedValues.Add(values[i]); } } i++; } if (ct.IsCancellationRequested) { return; } Logger.LogDebug("DataGrpcList.ElementValuesCallback"); if (сallbackDispatcher is not null) { try { сallbackDispatcher.BeginInvoke(ct => { Logger.LogDebug("DataGrpcList.ElementValuesCallback dispatched"); elementValuesCallbackEventHandler(changedClientObjs.ToArray(), changedValues.ToArray()); }); } catch (Exception) { } } }; if (callbackIsEnabled) { DataGrpcList.EnableListCallback(true); } } } } catch (Exception ex) { Logger.LogWarning(ex, "ClientElementValueListItemsManager.Subscribe exception"); connectionError = true; } { var utcNow = DateTime.UtcNow; var changedClientObjs = new List <object>(); var changedValues = new List <ValueStatusTimestamp>(); foreach (DataGrpcListItemWrapper dataGrpcListItemWrapper in DataGrpcListItemWrappersDictionary.Values) { foreach (var modelItem in dataGrpcListItemWrapper.ClientObjectInfosCollection) { if (modelItem.ForceNotifyClientObj) { modelItem.ForceNotifyClientObj = false; if (modelItem.ClientObj is not null) { if (dataGrpcListItemWrapper.ItemDoesNotExist) { changedClientObjs.Add(modelItem.ClientObj); changedValues.Add(new ValueStatusTimestamp { ValueStatusCode = ValueStatusCode.ItemDoesNotExist }); } else if (dataGrpcListItemWrapper.DataGrpcListItem is not null) { changedClientObjs.Add(modelItem.ClientObj); changedValues.Add(dataGrpcListItemWrapper.DataGrpcListItem.ValueStatusTimestamp); } else { changedClientObjs.Add(modelItem.ClientObj); changedValues.Add(new ValueStatusTimestamp(new Any(), ValueStatusCode.Unknown, utcNow)); } } } } } if (changedClientObjs.Count > 0) { if (ct.IsCancellationRequested) { return; } if (сallbackDispatcher is not null) { try { сallbackDispatcher.BeginInvoke(ct => elementValuesCallbackEventHandler(changedClientObjs.ToArray(), changedValues.ToArray())); } catch (Exception) { } } } } if (!connectionError) { DataGrpcItemsMustBeAddedOrRemoved = false; } } finally { SubscribeFinal(); } }
/// <summary> /// No throw. /// </summary> /// <param name="clientConnectionManager"></param> /// <param name="сallbackDispatcher"></param> /// <param name="callbackIsEnabled"></param> /// <param name="ct"></param> public void Subscribe(ClientConnectionManager clientConnectionManager, IDispatcher?сallbackDispatcher, bool callbackIsEnabled, CancellationToken ct) { if (ct.IsCancellationRequested) { return; } if (!clientConnectionManager.ConnectionExists) { return; } if (!_dataGrpcEventItemsMustBeAdded) { return; } bool allOk = true; foreach ( var kvp in _eventMessagesCallbackEventHandlers) { if (ct.IsCancellationRequested) { return; } if (kvp.Value.P is not null) { continue; } ClientEventList dataGrpcEventList; try { dataGrpcEventList = clientConnectionManager.NewEventList(null); } catch (Exception) { return; } try { if (dataGrpcEventList.Disposed) { return; } try { Action <Utils.DataAccess.EventMessage[]> eventMessagesCallbackEventHandler = kvp.Key; dataGrpcEventList.EventMessagesCallbackEvent += (ClientEventList eventList, Server.EventMessage[] newEventMessages) => { if (ct.IsCancellationRequested) { return; } if (сallbackDispatcher is not null) { try { сallbackDispatcher.BeginInvoke(ct => eventMessagesCallbackEventHandler( newEventMessages.Select(em => em.ToEventMessage()).ToArray())); } catch (Exception) { } } }; if (callbackIsEnabled) { dataGrpcEventList.EnableListCallback(true); } } catch (Exception) { allOk = false; } } catch (Exception ex) { Logger.LogWarning(ex, "EventMessagesManager.Subscribe exception."); } kvp.Value.P = dataGrpcEventList; if (allOk) { _dataGrpcEventItemsMustBeAdded = false; } } }