public bool EnqueueWriteCommand(IWriteModbusFunction modbusFunction)
        {
            bool success;

            if (!modelUpdateQueue.IsEmpty)
            {
                this.modelUpdateQueueEmptyEvent.WaitOne();
            }

            try
            {
                this.writeCommandQueue.Enqueue(modbusFunction);
                this.readCommandQueue = new ConcurrentQueue <IReadModbusFunction>();
                this.commandEvent.Set();
                success = true;
            }
            catch (Exception e)
            {
                success = false;
                string message = "Exception caught in EnqueueCommand() method.";
                Logger.LogError(message, e);
            }

            return(success);
        }
Ejemplo n.º 2
0
        public async Task <bool> EnqueueWriteCommand(IWriteModbusFunction modbusFunction)
        {
            string verboseMessage = $"{baseLogString} entering EnqueueWriteCommand, FunctionCode: {modbusFunction.FunctionCode}, CommandOrigin: {modbusFunction.CommandOrigin}.";

            Logger.LogVerbose(verboseMessage);

            bool success;

            while (!ReliableQueuesInitialized)
            {
                await Task.Delay(1000);
            }

            try
            {
                if (!(modbusFunction is IWriteModbusFunction writeModbusFunction))
                {
                    string message = $"{baseLogString} EnqueueWriteCommand => trying to enqueue modbus function that does not implement IWriteModbusFunction interface.";
                    Logger.LogError(message);
                    throw new ArgumentException(message);
                }

                if ((await modelUpdateCommandQueue.GetCountAsync()) > 0)
                {
                    verboseMessage = $"{baseLogString} EnqueueWriteCommand => '{CloudStorageQueueNames.ModelUpdateCommandQueue}' queue is not empty.";
                    Logger.LogDebug(verboseMessage);

                    return(false);
                }

                //KEY LOGIC
                if (modbusFunction.CommandOrigin == CommandOriginType.MODEL_UPDATE_COMMAND)
                {
                    await this.modelUpdateCommandQueue.EnqueueAsync((ModbusFunction)modbusFunction);
                }
                else
                {
                    await this.writeCommandQueue.EnqueueAsync((ModbusFunction)modbusFunction);
                }

                success = true;

                if (writeModbusFunction is IWriteSingleFunction writeSingleFunction)
                {
                    string informationMessage = $"{baseLogString} EnqueueWriteCommand => write command SUCCESSFULLY enqueued to '{CloudStorageQueueNames.WriteCommandQueue}' queue. FunctionCode: {writeSingleFunction.FunctionCode}, OutputAddress: {writeSingleFunction.OutputAddress}, CommandValue: {writeSingleFunction.CommandValue}, CommandOrigin: {writeSingleFunction.CommandOrigin},";
                    Logger.LogInformation(informationMessage);
                }
                else if (writeModbusFunction is IWriteMultipleFunction writeMultipleFunction)
                {
                    StringBuilder commandValuesSB = new StringBuilder();
                    commandValuesSB.Append("[ ");
                    foreach (int value in writeMultipleFunction.CommandValues)
                    {
                        commandValuesSB.Append(value);
                        commandValuesSB.Append(" ");
                    }
                    commandValuesSB.Append("]");

                    string informationMessage = $"{baseLogString} EnqueueWriteCommand => write command SUCCESSFULLY enqueued to '{CloudStorageQueueNames.WriteCommandQueue}' queue. FunctionCode: {writeMultipleFunction.FunctionCode}, StartAddress: {writeMultipleFunction.StartAddress}, CommandValue: {commandValuesSB}, CommandOrigin: {writeMultipleFunction.CommandOrigin},";
                    Logger.LogInformation(informationMessage);
                }

                await this.readCommandQueue.ClearAsync();

                string debugMessage = $"{baseLogString} EnqueueModelUpdateCommands => cloud storage queues that were cleared: '{CloudStorageQueueNames.ReadCommandQueue}'";
                Logger.LogDebug(debugMessage);
            }
            catch (Exception e)
            {
                success = false;
                string message = "Exception caught in EnqueueCommand() method.";
                Logger.LogError(message, e);
            }

            return(success);
        }
Ejemplo n.º 3
0
 public Task <bool> EnqueueWriteCommand(IWriteModbusFunction modbusFunctions)
 {
     //return MethodWrapperAsync<bool>("EnqueueWriteCommand", new object[1] { modbusFunctions });
     return(InvokeWithRetryAsync(client => client.Channel.EnqueueWriteCommand(modbusFunctions)));
 }