public async Task RunAsync() { listener = new TcpListener(new IPEndPoint(GetIPAddress(System.Net.Dns.GetHostName()), 502)); listener.ExclusiveAddressUse = false; listener.Start(); while (true) { try { TcpClient tcpClient = await listener.AcceptTcpClientAsync(); tcpClient.LingerState = new LingerOption(true, 0); tcpClient.NoDelay = true; tcpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); CancellationTokenSource cts = new CancellationTokenSource(); IChannel channel = ChannelFactory.Create(false, tcpClient, 1024, 1024 * 100, cts.Token); RtuMap map = await RtuMap.LoadAsync(config.RtuMapSasUri); //load RTU map so always up to date. VirtualRtuAdapter adapter = new VirtualRtuAdapter(map, channel); adapters.Add(adapter.Id, adapter); sources.Add(adapter.Id, cts); adapter.OnError += Adapter_OnError; adapter.OnClose += Adapter_OnClose; await adapter.StartAsync(); } catch (Exception ex) { OnError?.Invoke(this, new ListenerErrorEventArgs(ex)); } } }
public async Task RunAsync() { try { map = await RtuMap.LoadAsync(config.StorageConnectionString, config.Container, config.Filename); if (map == null) { logger?.LogWarning("RTU map is null."); throw new InvalidOperationException("RTU map was not found."); } logger?.LogInformation("SCADA client adapter loaded RTU map."); channel.OnOpen += Channel_OnOpen; channel.OnReceive += Channel_OnReceive; channel.OnError += Channel_OnError; channel.OnClose += Channel_OnClose; await channel.OpenAsync(); } catch (Exception ex) { logger?.LogError(ex.Message); OnError?.Invoke(this, new AdapterErrorEventArgs(Id, ex)); } }
private async Task UpdateRtuMapAsync(ContainerEntity entity, string connectionString, string container, string filename) { RtuMap map = await RtuMap.LoadAsync(connectionString, container, filename); map = map ?? new RtuMap(); map.Name = map.Name ?? entity.VirtualRtuId; foreach (var slave in entity.Slaves) { if (map.HasItem(slave.UnitId)) { map.Remove(slave.UnitId); } string requestUriString = UriGenerator.GetRtuPiSystem(entity.Hostname, entity.VirtualRtuId, entity.DeviceId, slave.UnitId, true); string responseUriString = UriGenerator.GetRtuPiSystem(entity.Hostname, entity.VirtualRtuId, entity.DeviceId, slave.UnitId, false); if (slave.Constraints != null && slave.Constraints.Count == 0) { slave.Constraints = null; } map.Add(slave.UnitId, requestUriString, responseUriString, slave.Constraints); } await map.UpdateAsync(connectionString, container, filename); }
public VirtualRtuPipeline(VrtuConfig config, IChannel input, IChannel output, List <IFilter> inputFiters, List <IFilter> outputFilters, ILogger logger = null) { Id = Guid.NewGuid().ToString(); this.config = config; this.logger = logger; InputChannel = input; OutputChannel = output; InputFilters = inputFiters; OutputFilters = outputFilters; map = RtuMap.LoadAsync(config.StorageConnectionString, config.Container, config.Filename).GetAwaiter().GetResult(); }
public VirtualRtuChannel(VrtuConfig config, ILogger logger = null) { this.config = config; map = RtuMap.LoadAsync(config.StorageConnectionString, config.Container, config.Filename).GetAwaiter().GetResult(); this.logger = logger; subscriptions = new HashSet <byte>(); name = Guid.NewGuid().ToString(); cache = new LocalCache(name); cache.OnExpired += Cache_OnExpired; securityToken = GetSecurityToken(config); endpointUrl = new Uri($"wss://{config.Hostname}/ws/api/connect"); }