示例#1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="txt">Each field from data packet should be in the order RS+Name+Value</param>
        /// <returns></returns>
        public bool ParseText(string txt)
        {
            // Note STX and ETX, ENQ, ACK, NAK should not enter here as a rule.

            packetIsHeader = false;

            // If debugging (causing a ide delay) this can be commented out

            if (prevEpochTime != DateTime.MinValue)
            {
                // this check is here to prevent two epochs seprated by a min time interval being stitched together
                TimeSpan minSpanAllowed = TimeSpan.FromSeconds(TagConstants.MIN_EPOCH_INTERVAL_SECS);
                TimeSpan currentSpan    = DateTime.Now - prevEpochTime;
                if (currentSpan > minSpanAllowed)
                {
                    if (Prev_EpochRec != null)
                    {
                        Prev_EpochRec = null; // stops the previous epoch being associated to this epoch
                    }
                    if (_PrevTagFile_EpochRec != null)
                    {
                        _PrevTagFile_EpochRec = null;
                    }
                }
            }


            prevEpochTime = DateTime.Now;

            try
            {
                byte[] bArray = Encoding.UTF8.GetBytes(txt);
                if (!ValidateText(ref bArray))
                {
                    return(false);
                }

                HaveName  = false;
                HaveValue = false;

                // construct records(tagfile fields) byte by byte
                for (int i = 0; i < bArray.Length; i++)
                {
                    switch (bArray[i])
                    {
                    case TagConstants.STX:
                        continue;

                    case TagConstants.ETX:
                        continue;

                    case TagConstants.ENQ:
                        continue;

                    case TagConstants.ACK:
                        continue;

                    case TagConstants.NAK:
                        continue;

                    case TagConstants.RS:
                        if (HaveName && TagValue != String.Empty)
                        {
                            ProcessField();
                        }
                        HaveName  = false;
                        HaveValue = false;
                        TagValue  = String.Empty;
                        TagName   = String.Empty;
                        continue;

                    default:
                        if (!HaveName)
                        {
                            TagName  = TagName + (char)bArray[i];
                            HaveName = TagName.Length == TagConstants.TAG_NAME_LENGHT;
                        }
                        else
                        {
                            TagValue = TagValue + (char)bArray[i];
                            if (!HaveValue)
                            {
                                HaveValue = true;
                            }
                            if (HaveName && i == bArray.Length - 1) // endoftext
                            {
                                if (HaveName && TagValue != String.Empty)
                                {
                                    var res = ProcessField();
                                    if (res == false)
                                    {
                                        return(false);
                                    }
                                }
                            }
                        }
                        continue;
                    }
                }

                // finally process all fields picked out of the datapacket
                var newHeader = false;

                LastStateEpochRecord.EpochCopyLatestValues(ref EpochRec); // Remeber last valid values for each tag

                if (Prev_EpochRec != null)                                // test needed
                {
                    // Check if its the same position and elevation
                    if (packetIsHeader) // if header record
                    {
                        UpdateTagContentList(ref EpochRec, ref newHeader, TagConstants.UpdateReason.NewHeader);
                    }
                    else if (EpochRec.MachineStateDifferent(ref Prev_EpochRec))
                    {
                        UpdateTagContentList(ref EpochRec, ref newHeader, TagConstants.UpdateReason.ChangeRecord);
                        if (NotSeenNewPosition && updateCount > 1)
                        {
                            NotSeenNewPosition = false;
                        }
                    }
                    else
                    {
                        Log.LogError($"** Same Position ***");
                    }
                }
                else
                if (packetIsHeader)
                {
                    UpdateTagContentList(ref EpochRec, ref newHeader, TagConstants.UpdateReason.NewHeader);
                }
                else
                {
                    UpdateTagContentList(ref EpochRec, ref newHeader, TagConstants.UpdateReason.ChangeRecord);
                }

                if (newHeader)                                           // Is this the start of a new tagfile
                {
                    if (_PrevTagFile_EpochRec != null)                   // epoch from last tagfile
                    {
                        if (_PrevTagFile_EpochRec.IsFullPositionEpoch()) // if it is a new tagfile we use last known epoch to start new tagfile
                        {
                            UpdateTagContentList(ref _PrevTagFile_EpochRec, ref tmpNR, TagConstants.UpdateReason.LastTagFileEpoch);
                        }
                        _PrevTagFile_EpochRec = null;
                        LastStateEpochRecord.ClearEpoch();
                    }
                }
                else
                {
                    if (Prev_EpochRec == null)
                    {
                        Prev_EpochRec = new EpochRecord();
                    }
                    Prev_EpochRec.EpochCopy(ref LastStateEpochRecord);
                }

                return(true);
            }
            catch (Exception e)
            {
                Log.LogError($"Unexpected error occured in ParseText. Error:{e.Message}, {e.StackTrace}");
                return(false);
            }
        }
示例#2
0
 /// <summary>
 /// Make copy of last epoch to be reused in new tagfile
 /// </summary>
 public void CloneLastEpoch()
 {
     _PrevTagFile_EpochRec = new EpochRecord();
     _PrevTagFile_EpochRec.EpochCopy(ref EpochRec);
 }