private async Task SendModelUpdateCommands() { var scadaCommandingClient = ScadaCommandingClient.CreateClient(); var enumerableAddressToGidMapResult = await CurrentAddressToGidMap.GetEnumerableDictionaryAsync(); var tasks = new List <Task>() { Task.Run(async() => { var key = (short)PointType.ANALOG_OUTPUT; if (!enumerableAddressToGidMapResult.ContainsKey(key)) { return; } var analogItemsAddressToGidMap = enumerableAddressToGidMapResult[key]; var analogCommandingValues = new Dictionary <long, float>(analogItemsAddressToGidMap.Count); foreach (long gid in analogItemsAddressToGidMap.Values) { var result = await CurrentGidToPointItemMap.TryGetValueAsync(gid); var analogPointItem = result.Value as IAnalogPointItem; analogCommandingValues.Add(gid, analogPointItem.CurrentEguValue); } if (analogCommandingValues.Count > 0) { await scadaCommandingClient.SendMultipleAnalogCommand(analogCommandingValues, CommandOriginType.MODEL_UPDATE_COMMAND); } }), Task.Run(async() => { var key = (short)PointType.DIGITAL_OUTPUT; if (!enumerableAddressToGidMapResult.ContainsKey(key)) { return; } var discreteItemsAddressToGidMap = enumerableAddressToGidMapResult[key]; var discreteCommandingValues = new Dictionary <long, ushort>(discreteItemsAddressToGidMap.Count); foreach (long gid in discreteItemsAddressToGidMap.Values) { var result = await CurrentGidToPointItemMap.TryGetValueAsync(gid); var discretePointItem = result.Value as IDiscretePointItem; discreteCommandingValues.Add(gid, discretePointItem.CurrentValue); } if (discreteCommandingValues.Count > 0) { await scadaCommandingClient.SendMultipleDiscreteCommand(discreteCommandingValues, CommandOriginType.MODEL_UPDATE_COMMAND); } }), }; Task.WaitAll(tasks.ToArray()); }
private async Task InitializeIncomingScadaModel() { var enumerableCurrentGidToPointItemMap = await CurrentGidToPointItemMap.GetEnumerableDictionaryAsync(); foreach (long gid in enumerableCurrentGidToPointItemMap.Keys) { //NODO: to tasks, await all (performanse) IScadaModelPointItem pointItem = enumerableCurrentGidToPointItemMap[gid].Clone(); await IncomingGidToPointItemMap.SetAsync(gid, pointItem); short registerType = (short)pointItem.RegisterType; if (!(await IncomingAddressToGidMap.ContainsKeyAsync(registerType))) { await IncomingAddressToGidMap.SetAsync(registerType, new Dictionary <ushort, long>()); } var addressToGidDictionaryResult = await IncomingAddressToGidMap.TryGetValueAsync(registerType); if (!addressToGidDictionaryResult.HasValue) { string message = $"{baseLogString} InitializeIncomingScadaModel => reliable collection '{ReliableDictionaryNames.IncomingAddressToGidMap}' is not initialized properly."; Logger.LogError(message); throw new InternalSCADAServiceException(message); } var addressToGidDictionary = addressToGidDictionaryResult.Value; if (addressToGidDictionary.ContainsKey(pointItem.Address)) { string message = $"{baseLogString} InitializeIncomingScadaModel => SCADA model is invalid => Address: {pointItem.Address} (RegType: {registerType}) belongs to more than one entity."; Logger.LogError(message); throw new InternalSCADAServiceException(message); } addressToGidDictionary.Add(pointItem.Address, pointItem.Gid); await IncomingAddressToGidMap.SetAsync(registerType, addressToGidDictionary); string debugMessage = $"{baseLogString} InitializeIncomingScadaModel => measurement added to Incoming SCADA model [Gid: 0x{gid:X16}, Address: {pointItem.Address}]"; Logger.LogDebug(debugMessage); } }
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}"); }