예제 #1
0
파일: Program.cs 프로젝트: lanicon/dacs7
        //private static async Task<int> Watch(WatchOptions watchOptions)
        //{
        //    var client = new Dacs7Client(_factory);
        //    try
        //    {
        //        await client.ConnectAsync(watchOptions.ConnectionString);

        //        client.Subscribe(1000, (Subscription subscription, DataChangeNotification notification, IList<string> stringTable) =>
        //        {
        //            foreach (var item in notification.MonitoredItems)
        //            {
        //                var clientItem = subscription.FindItemByClientHandle(item.ClientHandle);
        //                _logger.LogInformation($"DataChanged: {clientItem.DisplayName}={item.Value}");
        //            }

        //        }, watchOptions.Tags.Select(x => MonitorItem.Create(x, 1)));

        //        Console.WriteLine("Press any key to stop!");
        //        Console.ReadKey();
        //    }
        //    catch (Exception ex)
        //    {
        //        _logger.LogError($"An error occured in Watch: {ex.Message}");
        //        return 1;
        //    }
        //    finally
        //    {
        //        await client.DisconnectAsync();
        //    }

        //    return 0;
        //}

        //private static async Task<int> WatchAlarms(WatchAlarmsOptions watchOptions)
        //{
        //    var client = new Dacs7Client(_factory);
        //    try
        //    {
        //        await client.ConnectAsync(watchOptions.ConnectionString);

        //        var filter = new EventFilter
        //        {
        //            SelectClauses = Events.DefaultEventAttributes
        //        };
        //        filter.CreateDefaultFilter(1, 1000, null);

        //        await client.Subscribe(100, (Subscription subscription, EventNotificationList notification, IList<string> stringTable) =>
        //        {
        //            foreach (var item in notification.Events)
        //            {
        //                _logger.LogInformation($"Event: {item.EventFields.Aggregate((x, y) => $"{x.ToString()};{y.ToString()}") }");
        //            }

        //        }, new[] { MonitorItem.Create(Objects.Server, 100, filter, Attributes.EventNotifier) })
        //        .Refresh();


        //        Console.WriteLine("Press any key to stop!");
        //        Console.ReadKey();
        //    }
        //    catch (Exception ex)
        //    {
        //        _logger.LogError($"An error occured in Watch: {ex.Message}");
        //        return 1;
        //    }
        //    finally
        //    {
        //        await client.DisconnectAsync();
        //    }

        //    return 0;
        //}


        private static async Task <int> Write(WriteOptions writeOptions)
        {
            var client = new Dacs7Client(writeOptions.Address);

            try
            {
                await client.ConnectAsync();

                var write = writeOptions.Tags.Select(x =>
                {
                    var s = x.Split('=');
                    return(KeyValuePair.Create <string, object>(s[0], s[1]));
                }
                                                     ).ToList();

                await client.WriteAsync(write);

                foreach (var item in write)
                {
                    _logger?.LogInformation($"Write: {item.Key}={item.Value}");
                }
            }
            catch (Exception ex)
            {
                _logger?.LogError($"An error occured in Write: {ex.Message}");
                return(1);
            }
            finally
            {
                await client.DisconnectAsync();
            }

            return(0);
        }
예제 #2
0
        private async Task Papper_OnWrite(IEnumerable <DataPack> writes)
        {
            try
            {
                var result  = writes.ToList();
                var results = await _client.WriteAsync(writes.SelectMany(BuildWritePackages)).ConfigureAwait(false);



                writes.AsParallel().Select((item, index) =>
                {
                    if (results != null)
                    {
                        item.ExecutionResult = results.ElementAt(index) == ItemResponseRetValue.Success ? ExecutionResult.Ok : ExecutionResult.Error;
                    }
                    else
                    {
                        item.ExecutionResult = ExecutionResult.Error;
                    }
                    return(true);
                }).ToList();
            }
            catch (Exception ex)
            {
                writes.AsParallel().Select((item, index) =>
                {
                    item.ExecutionResult = ExecutionResult.Error;
                    return(true);
                }).ToList();
            }
        }
예제 #3
0
        public async Task <List <WriteResultItem> > WriteAsync(List <WriteRequestItem> writeItems)
        {
            var writes      = writeItems.Select(ri => new KeyValuePair <string, object>(ri.ToTag(), ri.Data));
            var writeResult = await _client.WriteAsync(writes).ConfigureAwait(false);

            var enumerator = writeItems.GetEnumerator();
            var result     = new List <WriteResultItem>();

            foreach (var item in writeResult)
            {
                if (!enumerator.MoveNext())
                {
                    break;
                }
                result.Add(new WriteResultItem(enumerator.Current, item));
            }
            return(result);
        }
예제 #4
0
        internal static async Task <int> Write(WriteOptions writeOptions, ILoggerFactory loggerFactory)
        {
            var client = new Dacs7Client(writeOptions.Address, PlcConnectionType.Pg, 5000, loggerFactory)
            {
                MaxAmQCalled  = (ushort)writeOptions.MaxJobs,
                MaxAmQCalling = (ushort)writeOptions.MaxJobs
            };
            var logger = loggerFactory?.CreateLogger("Dacs7Cli.Write");

            try
            {
                await client.ConnectAsync();

                var write = writeOptions.Tags.Select(x =>
                {
                    var s = x.Split('=');
                    return(KeyValuePair.Create <string, object>(s[0], s[1]));
                }
                                                     ).ToList();

                var results = await client.WriteAsync(write);

                var resultEnumerator = results.GetEnumerator();
                foreach (var item in write)
                {
                    if (resultEnumerator.MoveNext())
                    {
                        logger?.LogInformation($"Write: {item.Key}={item.Value}  result: {resultEnumerator.Current}");
                    }
                }
            }
            catch (Exception ex)
            {
                logger?.LogError($"An error occured in Write: {ex.Message} - {ex.InnerException?.Message}");
                return(1);
            }
            finally
            {
                await client.DisconnectAsync();
            }

            return(0);
        }
예제 #5
0
파일: ReadTests.cs 프로젝트: lanicon/dacs7
        public async Task ReadWriteBigDBData()
        {
            var client = new Dacs7Client(Address);

            try
            {
                await client.ConnectAsync();

                var results0 = new Memory <byte>(Enumerable.Repeat((byte)0x25, 1000).ToArray());
                var results1 = await client.WriteAsync(WriteItemSpecification.Create("DB1114", 0, results0));

                var results2 = (await client.ReadAsync(ReadItemSpecification.Create <byte[]>("DB1114", 0, 1000)));
                Assert.True(results0.Span.SequenceEqual(results2.FirstOrDefault().Data.Span));
            }
            finally
            {
                await client.DisconnectAsync();
            }
        }
 /// <summary>
 /// Takes a list of <see cref="KeyValuePair{string, object}"/> an tries to write them to the plc.
 /// Where the string is the variable name and the object is the data to write
 /// </summary>
 /// <param name="values">a list of tags with the following syntax Area.Offset,DataType[,length]</param>
 /// <returns>returns a enumerable with the write result, 0xFF = Success</returns>
 public static Task <IEnumerable <ItemResponseRetValue> > WriteAsync(this Dacs7Client client, IEnumerable <KeyValuePair <string, object> > values) => client.WriteAsync(client.CreateWriteNodeIdCollection(values));
 /// <summary>
 /// Takes a list of <see cref="WriteItem"/> an tries to write them to the plc.
 /// </summary>
 /// <param name="values">a list of <see cref="WriteItem"/>.</param>
 /// <returns>returns a enumerable with the write result, 0xFF = Success</returns>
 public static Task <IEnumerable <ItemResponseRetValue> > WriteAsync(this Dacs7Client client, params WriteItem[] values) => client.WriteAsync(values as IEnumerable <WriteItem>);
 /// <summary>
 /// Takes a list of <see cref="KeyValuePair{string, object}"/> an tries to write them to the plc.
 /// Where the string is the variable name and the object is the data to write
 /// </summary>
 /// <param name="values">a list of tags with the following syntax Area.Offset,DataType[,length]</param>
 /// <returns>returns a enumerable with the write result, 0xFF = Success</returns>
 public static Task <IEnumerable <ItemResponseRetValue> > WriteAsync(this Dacs7Client client, params KeyValuePair <string, object>[] values) => client.WriteAsync(values as IEnumerable <KeyValuePair <string, object> >);