Exemplo n.º 1
0
        private async ValueTask Read(IReadOnlyList <S7Tag> tagList)
        {
            await ReadSemaphore.WaitAsync();

            Task <List <DataItem> > result = null;

            try
            {
                result =
                    ClientRead.ReadMultipleVarsAsync(tagList.Select(t => t.DataItemTag).ToList());

                await result.ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                Logger.LogError("Read: " + ex.Message + " - " + ex.StackTrace);
            }
            finally
            {
                ReadSemaphore.Release();
            }

            if (result != null)
            {
                var currentTime = DateTime.Now;
                for (int i = 0; i < tagList.Count; i++)
                {
                    await tagList[i].SetReadValue(result.Result[i].Value.ToString(), currentTime)
                    .ConfigureAwait(false);     //ToDo
                }
            }
        }//Read
Exemplo n.º 2
0
        private async ValueTask Read(IReadOnlyList <S7Tag> tagList)
        {
            //Logger?.LogInformation("Read: " + tagList.Count + " " + ConnectionStatusRead());
            ;
            Task <List <DataItem> > result = ClientRead.ReadMultipleVarsAsync(tagList.Select(t => t.DataItemTag).ToList());

            await result.ConfigureAwait(false);

            //Logger?.LogInformation("Read: " + result.Result.Count);
            var currentTime = DateTime.Now;

            for (int i = 0; i < tagList.Count; i++)
            {
                await tagList[i].SetReadValue(result.Result[i].Value.ToString(), currentTime).ConfigureAwait(false); //ToDo
            }
        }//Read
Exemplo n.º 3
0
        //public void Initiate(Common.Connection connection)
        //{
        //    //StdConnection = connection;
        //    //Logger.Trace("Domain.Entity.S7.Connection Initiate: " + StdConnection.Name);
        //    //ClientRead = new Plc((Std.Com.Driver.S7.CpuType)CpuTypeId, StdConnection.Ip, Rack, Slot);

        //    //if (StdConnection.WriteEnable)
        //    //{
        //    //    ClientWrite = new Plc((Std.Com.Driver.S7.CpuType)CpuTypeId, StdConnection.Ip, Rack, Slot);
        //    //}

        //    TryConnectRead();
        //}

        public async void TryConnectRead()
        {
            var policy = Policy
                         .Handle <Exception>()
                         //.OrResult<Plc>(r => r.IsConnected == false)
                         .WaitAndRetryForeverAsync(retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
                                                   (ex, time) =>
            {
                //Logger.Warn("TryConnectRead: " + ex.Exception.Message + " - " + ex.Exception.StackTrace);
                Logger.LogWarning("TryConnectRead: " + ex.Message + " - " + ex.StackTrace);
            }
                                                   );

            //await policy.Execute(async () => await ClientRead.OpenAsync());

            await policy.ExecuteAsync(() => ClientRead.OpenAsync()).ConfigureAwait(false);
        }