Ejemplo n.º 1
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
                try
                {
                    BinaryExecuter executer = new BinaryExecuter(
                        _appSettings.ExePath,
                        _appSettings.ExeArgs,
                        System.Guid.NewGuid().ToString(),
                        System.Guid.NewGuid().ToString(),
                        (int exitCode, string stdout, string stderr, string transactionId, string invocationId) =>
                    {     //Caller should be in closure https://stackoverflow.com/questions/595482/what-are-closures-in-c
                        _logger.LogInformation("Call to: "
                                               + _appSettings.ExePath
                                               + " completed without exceptions with exitcode: " + exitCode.ToString()
                                               + " and stdout: " + stdout
                                               + " and stderr: " + stderr
                                               );
                    });


                    _logger.LogInformation("Calling: " + _appSettings.ExePath + " with args: " + _appSettings.ExeArgs);

                    await executer.Execute();
                }
                catch (Exception ex)
                {
                    _logger.LogError("Call to: " + _appSettings.ExePath + " resulted in exception: " + ex.Message);
                }

                await Task.Delay(100000, stoppingToken);
            }
        }
Ejemplo n.º 2
0
        protected async Task InvokeBinary(string binaryPath, string binaryArgs, string transactionId, Boolean isSynchronous)
        {
            string connectionId = Context.ConnectionId;

            try
            {
                Func <IHubCallerClients, string, Action <int, string, string, string, string> > finishedHandlerClosure = (IHubCallerClients clients, string connectionId) =>
                                                                                                                         async(int exitCode, string stdout, string stderr, string transactionId, string connectionId) =>
                {
                    _logger.LogInformation("{0} InvokeBinary: {1} {2} completed without exceptions returning exitCode: {3} and stdout: {4} and stderr: {5}",
                                           DateTimeOffset.Now,
                                           transactionId,
                                           binaryPath,
                                           exitCode.ToString(),
                                           stdout,
                                           stderr);

                    await clients.Client(connectionId).SendAsync("InvokeBinarySucceeded", exitCode, stdout, stderr, transactionId);
                };

                BinaryExecuter executer = new BinaryExecuter(
                    binaryPath,
                    binaryArgs,
                    transactionId,
                    connectionId,
                    new BinaryExecuter.FinishedHandler(finishedHandlerClosure(Clients, connectionId))
                    );

                _logger.LogInformation("{0} InvokeBinary {1}: Calling: {2}: with args: {3}.",
                                       DateTimeOffset.Now,
                                       transactionId,
                                       binaryPath,
                                       binaryArgs);

                if (isSynchronous)
                {
                    await executer.Execute();
                }
                else
                {
                    _ = executer.Execute(); // see https://docs.microsoft.com/en-us/dotnet/csharp/discards
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("{0} InvokeBinary {1}: {2} threw exception {3}",
                                 DateTimeOffset.Now,
                                 transactionId,
                                 binaryPath,
                                 ex.Message);
                await Clients.Caller.SendAsync("InvokeBinaryExceptioned", ex.Message, transactionId);
            }
        }
        internal void ReadWrite(ref ReadWriteRequest[] values, bool suppressEthernetHeader)
        {
            lock (objectLocker)
            {
                m_BreakFlagCount++;
            }

            System.Diagnostics.Debug.Print("Entering Read Write. Count: " + m_BreakFlagCount.ToString());
            Guid parentID = Guid.NewGuid();

            List <ReadWriteRequest> requestsList = new List <ReadWriteRequest>();

            try
            {
                ComDriverLogger.LogReadWriteRequest(DateTime.Now, m_channel.GetLoggerChannelText(), values,
                                                    MessageDirection.Sent, parentID.ToString());

                CheckReadWriteRequests(values);

                for (int i = 0; i < values.Length; i++)
                {
                    ReadWriteRequest rw = values[i];

                    if ((rw is ReadOperands) || (rw is WriteOperands))
                    {
                        requestsList.Add(rw);

                        if (i == values.Length - 1)
                        {
                            if (requestsList.Count > 0)
                            {
                                ReadWriteRequest[] requestsArray = requestsList.ToArray();
                                if (OperandsExecuter != null)
                                {
                                    OperandsExecuter.PerformReadWrite(ref requestsArray, parentID.ToString(),
                                                                      suppressEthernetHeader);
                                }
                                else
                                {
                                    throw new ComDriveExceptions(
                                              "The PLC or the state the PLC is in does not support Read/Write Operands",
                                              ComDriveExceptions.ComDriveException.UnsupportedCommand);
                                }

                                requestsList.Clear();
                            }
                        }
                    }

                    else if ((rw is ReadDataTables) || (rw is WriteDataTables))
                    {
                        ReadWriteRequest[] requestsArray;

                        if (requestsList.Count > 0)
                        {
                            requestsArray = requestsList.ToArray();
                            if (OperandsExecuter != null)
                            {
                                OperandsExecuter.PerformReadWrite(ref requestsArray, parentID.ToString(),
                                                                  suppressEthernetHeader);
                            }
                            else
                            {
                                throw new ComDriveExceptions(
                                          "The PLC or the state the PLC is in does not support Read/Write Operands",
                                          ComDriveExceptions.ComDriveException.UnsupportedCommand);
                            }

                            requestsList.Clear();
                        }

                        requestsArray = new ReadWriteRequest[] { rw };

                        if (DataTablesExecuter != null)
                        {
                            DataTablesExecuter.PerformReadWrite(ref requestsArray, parentID.ToString(),
                                                                suppressEthernetHeader);
                        }
                        else
                        {
                            throw new ComDriveExceptions(
                                      "The PLC or the state the PLC is in does not support Read/Write Data Tables",
                                      ComDriveExceptions.ComDriveException.UnsupportedCommand);
                        }
                    }

                    else if (rw is BinaryRequest)
                    {
                        ReadWriteRequest[] requestsArray;
                        if (requestsList.Count > 0)
                        {
                            requestsArray = requestsList.ToArray();
                            if (OperandsExecuter != null)
                            {
                                OperandsExecuter.PerformReadWrite(ref requestsArray, parentID.ToString(),
                                                                  suppressEthernetHeader);
                            }
                            else
                            {
                                throw new ComDriveExceptions(
                                          "The PLC or the state the PLC is in does not support Read/Write Operands",
                                          ComDriveExceptions.ComDriveException.UnsupportedCommand);
                            }

                            requestsList.Clear();
                        }

                        requestsArray = new ReadWriteRequest[] { rw };

                        BinaryRequest br = rw as BinaryRequest;

                        switch (br.CommandCode)
                        {
                        case 0x1:    //Read Ram/Flash
                        case 0x41:   //Write Ram/Flash
                        case 62:     //Set Password
                        case 0x2:    //verify password
                        case 0x9:    //Download Start
                        case 0x45:   //Download End
                        case 0xA:    //Erase Flash
                        case 0x7:    //Wait for flash idle
                        case 0xB:    //Blind mode
                        case 0x13:   //UnBlind mode
                        case 0xF:    //Put PLC in state (Preebot, boot, OS Stop, OS Run)
                        case 0xC:    //Get PLC Name

                            if (BasicBinaryExecuter != null)
                            {
                                BasicBinaryExecuter.PerformReadWrite(ref requestsArray, parentID.ToString(),
                                                                     suppressEthernetHeader);
                            }
                            else
                            {
                                throw new ComDriveExceptions(
                                          "The PLC or the state the PLC is in does not support Basic Binary commands",
                                          ComDriveExceptions.ComDriveException.UnsupportedCommand);
                            }

                            break;

                        default:

                            if (BinaryExecuter != null)
                            {
                                BinaryExecuter.PerformReadWrite(ref requestsArray, parentID.ToString(),
                                                                suppressEthernetHeader);
                            }
                            else
                            {
                                throw new ComDriveExceptions(
                                          "The PLC or the state the PLC is in does not support Binary Commands",
                                          ComDriveExceptions.ComDriveException.UnsupportedCommand);
                            }

                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ComDriveExceptions comDriveException = ex as ComDriveExceptions;
                if (comDriveException != null)
                {
                    if ((comDriveException.ErrorCode == ComDriveExceptions.ComDriveException.AbortedByUser) ||
                        m_BreakFlag)
                    {
                        throw;
                    }
                    else if (m_BreakFlag)
                    {
                        throw new ComDriveExceptions("Request aborted by user",
                                                     ComDriveExceptions.ComDriveException.AbortedByUser);
                    }
                    else
                    {
                        throw;
                    }
                }
                else if (m_BreakFlag)
                {
                    throw new ComDriveExceptions("Request aborted by user",
                                                 ComDriveExceptions.ComDriveException.AbortedByUser);
                }
                else
                {
                    throw;
                }
            }
            finally
            {
                lock (objectLocker)
                {
                    m_BreakFlagCount--;
                }

                System.Diagnostics.Debug.Print("Exiting Read Write. Count: " + m_BreakFlagCount.ToString());
                ComDriverLogger.LogReadWriteRequest(DateTime.Now, m_channel.GetLoggerChannelText(), values,
                                                    MessageDirection.Received, parentID.ToString());
            }
        }