Exemplo n.º 1
0
        public static void QUEUE_PacketSent(C_Packet instructionPacket)
        {
            int rotMot = (int)instructionPacket.rotMotor;

            if (C_Packet.IS_statusPacketFollowing(instructionPacket) == true)
            {
                lock (queueSent_locker)
                {
                    instructionPacket.sentTime = DateTime.Now;
                    queueSent[rotMot].Enqueue(instructionPacket);
                    queueSent_Count[rotMot] = queueSent[rotMot].Count;
                    C_MotorControl.ACTUALIZE_queueCounts(queueSent);
                }
            }
        }
Exemplo n.º 2
0
        public static bool FIND_bestPairInQueue(C_Packet received, ref C_Packet pairedPacket, ref List <C_Packet> listLastSent)
        {
            // go through whole queue
            // find the best suitable pair for this packet in this queue: (id, read bytes wanted, timeSpan freshness)
            // remove it from his queue and return it

            // if you found echo -> remove it from queue, log it, and continue with search

            List <int>      suitableIndexes = new List <int>();
            List <TimeSpan> age             = new List <TimeSpan>();

            for (int q = 0; q < listLastSent.Count(); q++)
            {
                if (received == listLastSent[q]) // echo - possibly from uart2usb converter
                {
                    C_Packet.LOG_statusPacket(string.Format(
                                                  "Processed echo of : [{0}] which was sent at [{1}]",
                                                  C_Packet.GET_packetInfo(listLastSent[q]),
                                                  listLastSent[q].sentTime.ToString("HH:mm:ss.fff")
                                                  ));
                    if (C_Packet.IS_statusPacketFollowing(listLastSent[q]) == false)
                    {
                        // if there is not supposed to be more packets comming (not mentioning this echo)
                        // for this lastSent packet - remove it from the listLastSent
                        // (why it was there in the first place then?) - to be removed here
                        listLastSent.RemoveAt(q);
                    }
                    return(false); // found that this is echo of last sent - best pair,
                    // but pairing is just deleting from listLastSent! - for now
                }
                if (listLastSent[q].IS_fresh(received.statusReceivedTime) == true) // suitability
                {
                    if (received.IS_answerOf(listLastSent[q]) == true)
                    {
                        // this is probably the one as the not fresh ones are already dead
                        suitableIndexes.Add(q);
                        age.Add(listLastSent[q].GET_freshness(received.statusReceivedTime));
                    }
                    else
                    {
                        // just not suitable for this status, but maybe will be for another
                    }
                }
                else
                {
                    listLastSent.RemoveAt(q); // it is too old - remove it
                    q--;                      // and analyse new lastSent on this index
                    continue;
                    // possibly kills correct ones (if they are too old)
                }
            }

            // find the freshest from the suitable
            if (age.Count > 0)
            {
                // from the suitable ones get the most fresh one -
                // - return it as through ref as Paired and leave the other one in the listSent (which is also ref)
                int minimumValueIndex = age.IndexOf(age.Min());
                pairedPacket = listLastSent[minimumValueIndex];
                listLastSent.RemoveAt(minimumValueIndex);

                if (age.Count > 1)
                {
                    LOG_debug(string.Format(
                                  "[{0}] packets suitable pairs in the motor.listLastSent for this status, but only one was selceted. (age={1}ms)",
                                  suitableIndexes.Count - 1, age[minimumValueIndex]
                                  ));
                }
                return(true);
            }
            else
            {
                return(false);
            }
        }