Ejemplo n.º 1
0
        public void ProcessOther(CommandContext cmdCxt, BaseMessage categorizedCommand)
        {
            //if (AppConfiguration.useSequenceNumber)
            //{
            //    int sn = commandBeingProcessed.SeqNum.Value;

            //    if (CommandState == DeviceState.None || commandBeingProcessed.CommandName.Equals("INIT"))//first time or reinit
            //    {
            //        SeqNum = sn;
            //        //CommandState = DeviceState.Ready;
            //    }
            //    else
            //    {
            //        if (sn == SeqNum + 1)
            //        {
            //            SeqNum = sn;
            //        }
            //        else if (sn == SeqNum)
            //        {
            //            CommandState = DeviceState.ErrorSent;
            //            cmdCxt.ResponseQCallback($"Duplicate Sequence number. Received {sn}");
            //            return;
            //        }
            //        else
            //        {
            //            CommandState = DeviceState.ErrorSent;
            //            cmdCxt.ResponseQCallback($"Sequence number out of order. Received {sn}");
            //            return;
            //        }
            //    }

            //}
            commandBeingProcessed = categorizedCommand;

            if (IsError && !commandBeingProcessed.CommandName.Equals("INIT"))
            {
                cmdCxt.ResponseQCallback(ReceptionError.Generate("2001"));
                CommandState = DeviceState.ErrorSent;
                return;
            }

            if (commandBeingProcessed.Type == MessageType.Ack)
            {
                if (CommandState == DeviceState.Ready)
                {
                    return;//Duplicate ACK
                }
                while (CommandState != DeviceState.EOESent)
                {
                    Thread.Sleep(0);
                }

                RetryTimer.Stop();
                LastCtxtForWhichSentEoE = null;
                retryCount   = 0;
                CommandState = DeviceState.Ready;
                IsReady      = true;
                return;
            }

            if ((CommandState == DeviceState.EOESent || CommandState == DeviceState.CommandResponseSent) &&
                commandBeingProcessed.CommandName.Equals("INIT"))
            {
                RetryTimer.Stop();
                LastCtxtForWhichSentEoE = null;
                retryCount   = 0;
                CommandState = DeviceState.Ready;
                IsReady      = true;
            }
            else if (!(CommandState == DeviceState.None ||
                       CommandState == DeviceState.Ready ||
                       CommandState == DeviceState.CommandResponseSent ||
                       CommandState == DeviceState.ErrorSent ||
                       CommandState == DeviceState.EventSent))
            {
                cmdCxt.ResponseQCallback(ReceptionError.Generate("9001"));
                CommandState = DeviceState.ErrorSent;
                return;
            }

            commandBeingProcessed.PreProcess(cmdCxt, this);

            if (commandBeingProcessed.Type == MessageType.Action || commandBeingProcessed.Type == MessageType.Control)
            {
                commandBeingProcessed.Process(cmdCxt, this);
            }
        }
Ejemplo n.º 2
0
        public void Start()
        {
            Task.Run(() =>
            {
                foreach (var cmdctxt in IncomingQ.GetConsumingEnumerable())
                {
                    var cmdstr = cmdctxt.CommandMessage;

                    if (AppConfiguration.checkSumCheck)
                    {
                        var checkSum       = cmdstr.Substring(cmdstr.Length - 1 - 2, 2);
                        string strippedCmd = cmdstr.Substring(1, cmdstr.Length - 1 - 3);

                        if (!CheckSum.IsValid(strippedCmd, checkSum))
                        {
                            cmdctxt.ResponseQCallback(ReceptionError.Generate("2000"));
                            continue;
                        }
                    }

                    if (!cmdctxt.CommandMessage.Substring(2, 1).Equals(UnitNumber.ToString()))
                    {
                        cmdctxt.ResponseQCallback(ReceptionError.Generate("5000"));
                        continue;;
                    }

                    var commandBeingCategorized = BaseMessage.Create(UnitNumber, cmdctxt.CommandMessage);

                    if (commandBeingCategorized == null)
                    {
                        cmdctxt.ResponseQCallback(ReceptionError.Generate("6000"));
                        continue;;
                    }

                    commandBeingCategorized.Parse();//Process(cmdctxt);

                    if (commandBeingCategorized.Type == MessageType.Reference)
                    {
                        IncomingReferenceQ.Add(new Tuple <CommandContext, BaseMessage>(cmdctxt, commandBeingCategorized));
                    }
                    else
                    {
                        IncomingOtherQ.Add(new Tuple <CommandContext, BaseMessage>(cmdctxt, commandBeingCategorized));
                    }

                    if (Stop)
                    {
                        break;
                    }
                }
            });

            Task.Run(() =>
            {
                foreach (var tuple in IncomingOtherQ.GetConsumingEnumerable())
                {
                    ProcessOther(tuple.Item1, tuple.Item2);
                    if (Stop)
                    {
                        break;
                    }
                }
            });

            Task.Run(() =>
            {
                foreach (var tuple in IncomingReferenceQ.GetConsumingEnumerable())
                {
                    ProcessReference(tuple.Item1, tuple.Item2);
                    if (Stop)
                    {
                        break;
                    }
                }
            });
        }