示例#1
0
        private static async Task <int> ReadAlarms(ReadAlarmsOptions readOptions, ILoggerFactory loggerFactory)
        {
            var client = new Dacs7Client(readOptions.Address, PlcConnectionType.Pg, 5000, loggerFactory)
            {
                MaxAmQCalled  = (ushort)readOptions.MaxJobs,
                MaxAmQCalling = (ushort)readOptions.MaxJobs
            };
            var logger = loggerFactory?.CreateLogger("Dacs7Cli.ReadAlarms");

            try
            {
                long msTotal = 0;
                await client.ConnectAsync();

                try
                {
                    var sw = new Stopwatch();
                    sw.Start();
                    var results = await client.ReadPendingAlarmsAsync();

                    foreach (var alarm in results)
                    {
                        Console.WriteLine($"Alarm update: ID: {alarm.Id}   MsgNumber: {alarm.MsgNumber}  IsAck: {alarm.IsAck} ", alarm);
                    }
                    sw.Stop();
                    msTotal += sw.ElapsedMilliseconds;
                    logger?.LogDebug($"ReadAlarmsTime: {sw.Elapsed}");
                }
                catch (Exception ex)
                {
                    logger?.LogError($"Exception in read alarms {ex.Message}.");
                }
            }
            catch (Exception ex)
            {
                logger?.LogError($"An error occured in ReadAlarms: {ex.Message} - {ex.InnerException?.Message}");
                return(1);
            }
            finally
            {
                await client.DisconnectAsync();
            }

            return(0);
        }
示例#2
0
        internal static void Register(CommandLineApplication app)
        {
            app.Command("readalarms", cmd =>
            {
                cmd.Description = "Read alarms from the plc.";

                var addressOption = cmd.Option("-a | --address", "The IPAddress of the plc", CommandOptionType.SingleValue);
                var debugOption   = cmd.Option("-d | --debug", "Activate debug output", CommandOptionType.NoValue);
                var traceOption   = cmd.Option("-t | --trace", "Trace also dacs7 internals", CommandOptionType.NoValue);
                var maxJobsOption = cmd.Option("-j | --jobs", "Maximum number of concurrent jobs.", CommandOptionType.SingleValue);

                cmd.OnExecute(async() =>
                {
                    ReadAlarmsOptions readOptions = null;;
                    try
                    {
                        readOptions = new ReadAlarmsOptions
                        {
                            Debug   = debugOption.HasValue(),
                            Trace   = traceOption.HasValue(),
                            Address = addressOption.HasValue() ? addressOption.Value() : "localhost",
                            MaxJobs = maxJobsOption.HasValue() ? int.Parse(maxJobsOption.Value()) : 10,
                        }.Configure();
                        var result = await ReadAlarms(readOptions, readOptions.LoggerFactory);

                        await Task.Delay(500);

                        return(result);
                    }
                    finally
                    {
                        readOptions?.LoggerFactory?.Dispose();
                    }
                });
            });
        }
示例#3
0
        private static async Task <int> ReadAlarms(ReadAlarmsOptions readOptions, ILoggerFactory loggerFactory)
        {
            var client = new Dacs7Client(readOptions.Address, PlcConnectionType.Pg, 5000, loggerFactory)
            {
                MaxAmQCalled  = (ushort)readOptions.MaxJobs,
                MaxAmQCalling = (ushort)readOptions.MaxJobs
            };
            var logger = loggerFactory?.CreateLogger("Dacs7Cli.ReadAlarms");

            try
            {
                long msTotal = 0;
                await client.ConnectAsync();

                try
                {
                    var sw = new Stopwatch();
                    sw.Start();
                    var c = new CancellationTokenSource();
                    _ = Task.Factory.StartNew(() =>
                    {
                        Console.ReadKey();
                        c.Cancel();
                    });



                    using (var subscription = client.CreateAlarmSubscription())
                    {
                        var pendingResults = await client.ReadPendingAlarmsAsync();

                        foreach (var alarm in pendingResults)
                        {
                            Console.WriteLine($"Pending Alarm: ID: {alarm.Id}   MsgNumber: {alarm.MsgNumber} Id: {alarm.Id} IsAck: {alarm.IsAck} IsComing: {alarm.IsComing} IsGoing: {alarm.IsGoing} State: {alarm.State} EventState: {alarm.EventState} AckStateComing: {alarm.AckStateComing}  AckStateGoing: {alarm.AckStateGoing} ", alarm);
                        }
                        while (true)
                        {
                            var results = await subscription.ReceiveAlarmUpdatesAsync(c.Token);

                            if (results.HasAlarms)
                            {
                                foreach (var alarm in results.Alarms)
                                {
                                    Console.WriteLine($"Alarm update: ID: {alarm.Id}   MsgNumber: {alarm.MsgNumber} Id: {alarm.Id} IsAck: {alarm.IsAck} IsComing: {alarm.IsComing} IsGoing: {alarm.IsGoing} State: {alarm.State} EventState: {alarm.EventState} AckStateComing: {alarm.AckStateComing}  AckStateGoing: {alarm.AckStateGoing} ", alarm);
                                }
                            }
                            else if (!results.ChannelClosed)
                            {
                                break;
                            }
                            else
                            {
                                break;
                            }
                        }
                    }


                    sw.Stop();
                    msTotal += sw.ElapsedMilliseconds;
                    logger?.LogDebug($"ReadAlarmsTime: {sw.Elapsed}");
                }
                catch (Exception ex)
                {
                    logger?.LogError($"Exception in read alarms {ex.Message}.");
                }
            }
            catch (Exception ex)
            {
                logger?.LogError($"An error occured in ReadAlarms: {ex.Message} - {ex.InnerException?.Message}");
                return(1);
            }
            finally
            {
                await client.DisconnectAsync();
            }

            return(0);
        }