public async Task Rollback() { while (!ReliableDictionariesInitialized) { await Task.Delay(1000); } try { await IncomingGidToPointItemMap.ClearAsync(); await IncomingAddressToGidMap.ClearAsync(); await ModelChanges.ClearAsync(); string message = $"{baseLogString} Rollback => Incoming SCADA model is rejected."; Logger.LogInformation(message); await LogAllReliableCollections(); } catch (Exception e) { string errorMessage = $"{baseLogString} Rollback => Exception: {e.Message}"; Logger.LogError(errorMessage, e); } }
public async Task Commit() { while (!ReliableDictionariesInitialized) { await Task.Delay(1000); } try { await reliableDictionaryHelper.TryCopyToReliableDictionary <long, IScadaModelPointItem>(ReliableDictionaryNames.IncomingGidToPointItemMap, ReliableDictionaryNames.GidToPointItemMap, this.stateManager); await reliableDictionaryHelper.TryCopyToReliableDictionary <short, Dictionary <ushort, long> >(ReliableDictionaryNames.IncomingAddressToGidMap, ReliableDictionaryNames.AddressToGidMap, this.stateManager); await IncomingGidToPointItemMap.ClearAsync(); await IncomingAddressToGidMap.ClearAsync(); await ModelChanges.ClearAsync(); await CommandDescriptionCache.ClearAsync(); await MeasurementsCache.ClearAsync(); string message = $"{baseLogString} Commit => Incoming SCADA model is confirmed."; Logger.LogInformation(message); await SendModelUpdateCommands(); await LogAllReliableCollections(); } catch (Exception e) { string errorMessage = $"{baseLogString} Commit => Exception: {e.Message}"; Logger.LogError(errorMessage, e); } }
private ModelChanges GetModelChanges() { ModelChanges mods = new ModelChanges(); HashSet <object> checkd = new HashSet <object>(_present.Keys); foreach (object type in Model) { if (!_present.ContainsKey(type)) { mods.New.Add(type); } else { checkd.Remove(type); } } foreach (object type in checkd) { mods.Removed.Add(type); } return(mods); }
public List <int> AlterModel(ModelChanges modelChanges) { List <int> alteredModel = new List <int>(); switch (modelChanges) { case ModelChanges.Base: alteredModel = baseModel; break; case ModelChanges.Double: for (int i = 0; i < baseModel.Count; i++) { alteredModel.Add(baseModel[i]); alteredModel.Add(baseModel[i]); } break; case ModelChanges.Triple: for (int i = 0; i < baseModel.Count; i++) { alteredModel.Add(baseModel[i]); } break; case ModelChanges.Steep: for (int i = 0; i < baseModel.Count; i = i + 2) { alteredModel.Add(baseModel[i]); } break; /* * case ModelChanges.Giant: * for (int i = 0; i < baseModel.Count; i++) * { * alteredModel.Add(baseModel[i]); * alteredModel.Add(baseModel[i]); * alteredModel.Add(baseModel[i]); * alteredModel.Add(baseModel[i]); * alteredModel.Add(baseModel[i]); * alteredModel.Add(baseModel[i]); * alteredModel.Add(baseModel[i]); * alteredModel.Add(baseModel[i]); * } * break; */ } return(alteredModel); }
public void Update() { if (!IsOrdered) { ModelChanges changes = GetModelChanges(); foreach (object mod in changes.New) { BoundViewContainer view = CreateView(); _list.AddView(view); _present.Add(mod, view); view.InitializeBinding(mod); } foreach (object mod in changes.Removed) { _list.RemoveView(_present[mod]); _present.Remove(mod); } foreach (BoundViewContainer view in _present.Values) { view.Update(); } } else { if (Model.Count == _present.Count) { if (Model.All(x => _present.ContainsKey(x))) { foreach (BoundViewContainer view in _present.Values) { view.Update(); } return; } } _list.RemoveAllViews(); _present.Clear(); foreach (object mod in Model) { BoundViewContainer view = CreateView(); _list.AddView(view); _present.Add(mod, view); view.InitializeBinding(mod); view.Update(); } } }
public void DeleteTable(ModelChanges changes) { }
public void UpdateTable(ModelChanges changes) { }
public void CreateTable(ModelChanges changes) { }
private async Task LogAllReliableCollections() { while (!ReliableDictionariesInitialized) { await Task.Delay(1000); } StringBuilder sb = new StringBuilder(); sb.AppendLine("Reliable Collections"); sb.AppendLine("CurrentGidToPointItemMap =>"); var currentGidToPointItemMap = await CurrentGidToPointItemMap.GetEnumerableDictionaryAsync(); foreach (var element in currentGidToPointItemMap) { sb.AppendLine($"Key => {element.Key}, Value => Gid: 0x{element.Value.Gid:X16}, Address: {element.Value.Address}, Name: {element.Value.Name}, RegisterType: {element.Value.RegisterType}, Alarm: {element.Value.Alarm}, Initialized: {element.Value.Initialized}"); } sb.AppendLine(); sb.AppendLine("IncomingGidToPointItemMap =>"); var incomingGidToPointItemMap = await IncomingGidToPointItemMap.GetEnumerableDictionaryAsync(); foreach (var element in incomingGidToPointItemMap) { sb.AppendLine($"Key => {element.Key}, Value => Gid: 0x{element.Value.Gid:X16}, Address: {element.Value.Address}, Name: {element.Value.Name}, RegisterType: {element.Value.RegisterType}, Alarm: {element.Value.Alarm}, Initialized: {element.Value.Initialized}"); } sb.AppendLine(); sb.AppendLine("CurrentAddressToGidMap =>"); var currentAddressToGidMap = await CurrentAddressToGidMap.GetEnumerableDictionaryAsync(); foreach (var element in currentAddressToGidMap) { sb.AppendLine($"Key => {element.Key}, Value => Dictionary Count: {element.Value.Count}"); } sb.AppendLine(); sb.AppendLine("IncomingAddressToGidMap =>"); var incomingAddressToGidMap = await IncomingAddressToGidMap.GetEnumerableDictionaryAsync(); foreach (var element in incomingAddressToGidMap) { sb.AppendLine($"Key => {element.Key}, Value => Dictionary Count: {element.Value.Count}"); } sb.AppendLine(); sb.AppendLine("InfoCache =>"); var infoCache = await InfoCache.GetEnumerableDictionaryAsync(); foreach (var element in infoCache) { sb.AppendLine($"Key => {element.Key}, Value => {element.Value}"); } sb.AppendLine(); sb.AppendLine("ModelChanges =>"); var modelChanges = await ModelChanges.GetEnumerableDictionaryAsync(); foreach (var element in modelChanges) { sb.AppendLine($"Key => {element.Key}, Value => List Count: {element.Value.Count}"); } sb.AppendLine(); sb.AppendLine("MeasurementsCache =>"); var measurementsCache = await MeasurementsCache.GetEnumerableDictionaryAsync(); foreach (var element in measurementsCache) { sb.AppendLine($"Key => {element.Key}, Value => MeasurementGid: {element.Value.MeasurementGid:X16}, Alarm: {element.Value.Alarm}, CommandOrigin: {element.Value.CommandOrigin}"); } sb.AppendLine(); sb.AppendLine("CommandDescriptionCache =>"); var commandDescriptionCache = await CommandDescriptionCache.GetEnumerableDictionaryAsync(); foreach (var element in commandDescriptionCache) { sb.AppendLine($"Key => {element.Key}, Value => Gid: {element.Value.Gid:X16}, Address: {element.Value.Address}, Value: {element.Value.Value}, CommandOrigin: {element.Value.CommandOrigin}"); } sb.AppendLine(); Logger.LogDebug($"{baseLogString} LogAllReliableCollections => {sb}"); }
public async Task <bool> Prepare() { bool success; while (!ReliableDictionariesInitialized) { await Task.Delay(1000); } try { //INIT INCOMING SCADA MODEL with current model values await InitializeIncomingScadaModel(); //IMPORT ALL measurements from NMS and create PointItems for them var enumerableModelChanges = await ModelChanges.GetEnumerableDictionaryAsync(); Dictionary <long, IScadaModelPointItem> incomingPointItems = await CreatePointItemsFromNetworkModelMeasurements(enumerableModelChanges); //ORDER IS IMPORTANT due to IncomingAddressToGidMap validity: DELETE => UPDATE => INSERT var orderOfOperations = new List <DeltaOpType>() { DeltaOpType.Delete, DeltaOpType.Update, DeltaOpType.Insert }; foreach (var operation in orderOfOperations) { foreach (long gid in enumerableModelChanges[(byte)operation]) { ModelCode type = modelResourceDesc.GetModelCodeFromId(gid); if (type != ModelCode.ANALOG && type != ModelCode.DISCRETE) { continue; } if (operation == DeltaOpType.Delete) { await HandleDeleteOperation(gid); } else if (operation == DeltaOpType.Update) { IScadaModelPointItem incomingPointItem = incomingPointItems[gid]; await HandleUpdateOperation(incomingPointItem, gid); } else if (operation == DeltaOpType.Insert) { IScadaModelPointItem incomingPointItem = incomingPointItems[gid]; await HandleInsertOperation(incomingPointItem, gid); } } } success = await CheckSuccessiveAddressCondition(); ; } catch (Exception e) { string errorMessage = $"{baseLogString} Prepare => Exception: {e.Message}"; Logger.LogError(errorMessage, e); success = false; } return(success); }