コード例 #1
0
ファイル: Fota.cs プロジェクト: jesumarquez/lt
        private static String PeekPrivate(IFoteable device)
        {
            if (device.LastSent.IsOnTheFly())
            {
                return(device.LastSent.Text);
            }
            var t      = new TimeElapsed();
            var fileR  = GetFilePath(device, ".txR");
            var fileT  = GetFilePath(device, ".txt");
            var fileR2 = GetFilePath(device, ".txR2");

            if (t.getTimeElapsed().TotalSeconds > 1)
            {
                STrace.Debug("ParserLock", device.Id, String.Format("Peek Private GetFilePath ({0} secs)", t.getTimeElapsed().TotalSeconds.ToString()));
            }

            //txR --- txt --- txR2 --- tx1 --- tx2
            //process
            //        reverse
            //                process
            //                        reverse
            //                                 reverse

            t.Restart();
            if ((!IOUtils.FileExists(fileR)) && (IOUtils.FileExists(fileT) || !IOUtils.FileExists(fileR2)))
            {
                if (t.getTimeElapsed().TotalSeconds > 1)
                {
                    STrace.Debug("ParserLock", device.Id, String.Format("Peek Private FileExists ({0} secs)", t.getTimeElapsed().TotalSeconds.ToString()));
                }
                t.Restart();
                ProccessAnyPendingFileInternal(device);
                if (t.getTimeElapsed().TotalSeconds > 1)
                {
                    STrace.Debug("ParserLock", device.Id, String.Format("Peek Private ProcessAnyPending ({0} secs)", t.getTimeElapsed().TotalSeconds.ToString()));
                }
/*				ThreadPool.QueueUserWorkItem(ProccessAnyPendingFile, device); return null;*/
            }

            //process
            t.Restart();
            var file = ObtainFirstExistantReversedFile(device);

            if (t.getTimeElapsed().TotalSeconds > 1)
            {
                STrace.Debug("ParserLock", device.Id, String.Format("Peek Private ObtainFirstExistantReversedFile ({0} secs)", t.getTimeElapsed().TotalSeconds.ToString()));
            }
            if (file == null)
            {
                return(null);
            }
            String line;
            var    validLines = 0;

            t.Restart();
            var lineNumber = 0;

            using (var sr = new BackwardReader(file))
            {
                if (t.getTimeElapsed().TotalSeconds > 1)
                {
                    STrace.Debug("ParserLock", device.Id, String.Format("Peek Private using BackwardReader ({0} secs)", t.getTimeElapsed().TotalSeconds.ToString()));
                }
                while ((line = sr.ReadLine()) != null)
                {
                    lineNumber++;
                    if (t.getTimeElapsed().TotalSeconds > 1)
                    {
                        STrace.Debug("ParserLock", device.Id, String.Format("Peek Private Readline {1} ({0} secs)", t.getTimeElapsed().TotalSeconds.ToString(), lineNumber));
                    }
                    line = line.Trim();
                    if (line.Contains("/*"))
                    {
                        line = line.Substring(0, line.IndexOf("/*"));
                    }

                    if (IsLastSequenceMessage(line))
                    {
                        continue;
                    }
                    var t2 = new TimeElapsed();
                    if (IsVirtualMessage(line) || (line = GetNextInLineNotSentCommand(line)) != null)
                    {
                        validLines++;
                        break;
                    }
                    if (t2.getTimeElapsed().TotalSeconds > 1)
                    {
                        STrace.Debug("ParserLock", device.Id, String.Format("Peek Private IsVirtual+GetNextInLine {1} ({0} secs)", t.getTimeElapsed().TotalSeconds.ToString(), lineNumber));
                    }
                }
            }
            if (t.getTimeElapsed().TotalSeconds > 1)
            {
                STrace.Debug("ParserLock", device.Id, String.Format("Peek Private end of BackwardReader ({0} secs)", t.getTimeElapsed().TotalSeconds.ToString()));
            }
            if (validLines == 0)
            {
                t.Restart();
                File.Delete(file);
                if (t.getTimeElapsed().TotalSeconds > 1)
                {
                    STrace.Debug("ParserLock", device.Id, String.Format("Peek Private Delete File ({0} secs)", t.getTimeElapsed().TotalSeconds.ToString()));
                }
            }
            return(line);
        }
コード例 #2
0
ファイル: Fota.cs プロジェクト: jesumarquez/lt
        public static BaseDeviceCommand UpdateFotaCommand(IFoteable device, ulong?msgid, string newStatus, BaseDeviceCommand newCommand, UpdateFotaCommands action)
        {
            var result = (BaseDeviceCommand)null;

            var file = ObtainFirstExistantReversedFile(device);

            if (file == null)
            {
                return(null);
            }

            using (var sr = new BackwardReader(file))
            {
                var buffer     = new StringBuilder();
                var msgidfound = false;

                String line;
                while ((line = sr.ReadLine()) != null)                 // reads the lines from last to first
                {
                    var lineOriginal = line = line.Trim();

                    if (IsLastSequenceMessage(line))
                    {
                        continue;
                    }
                    if (IsVirtualMessage(line))
                    {
                        break;
                    }

                    var newLine = new StringBuilder();

                    var isFirstCommand = true; // at begin of line evaluation, the next command in line is the first

                    MatchCollection commandMatches = getMatchesAllCommandsFrom(line);
                    var             allSent        = commandMatches.Count > 0; // the variable reflects if all commands in line were sent.
                    for (var i = 0; i < commandMatches.Count; i++)
                    {
                        var cM      = commandMatches[i];
                        var command = cM.Groups[0].Value;

                        var dc = BaseDeviceCommand.createFrom(command, device, null);
                        if (!dc.isAlreadySent())
                        {
                            if (msgid == null || dc.MessageId == msgid)
                            {
                                allSent = allSent && (action != UpdateFotaCommands.RollbackTransaction && isFirstCommand && newStatus == BaseDeviceCommand.Attributes.Status_Sent);

                                msgidfound = true;
                                if (isFirstCommand)
                                {
                                    isFirstCommand = false;

                                    if (newCommand != null)
                                    {
                                        dc       = newCommand;
                                        dc.IdNum = device.GetDeviceId();
                                    }

                                    if (action == UpdateFotaCommands.UpdateTries || action == UpdateFotaCommands.RollbackTransaction)
                                    {
                                        dc.Tries   = (dc.Tries == null ? 0 : dc.Tries.Value) + 1;
                                        dc.LastTry = DateTime.Now;
                                    }

                                    if (action == UpdateFotaCommands.RollbackTransaction)
                                    {
                                        dc.setStatus(BaseDeviceCommand.Attributes.Status_Rollbacked);
                                    }
                                    else
                                    {
                                        dc.setStatus(newStatus);
                                    }
                                }
                                else if (action == UpdateFotaCommands.RollbackTransaction)
                                {
                                    dc.Tries   = (dc.Tries == null ? 0 : dc.Tries.Value) + 1;
                                    dc.LastTry = DateTime.Now;
                                    dc.setStatus(BaseDeviceCommand.Attributes.Status_Rollbacked);
                                }



                                if (result == null) // just catch first not sent message
                                {
                                    result = dc;
                                }
                            }
                            else
                            {
                                allSent = false;
                            }
                        }

                        newLine.Append(dc.ToString());
                    }
                    line = (allSent?null:newLine.ToString());
                    if (!String.IsNullOrEmpty(line))
                    {
                        if (lineOriginal != line)
                        {
                            STrace.Debug(typeof(Fota).FullName, device.Id, "FOTA> LINE ORIGINAL: " + lineOriginal);
                            STrace.Debug(typeof(Fota).FullName, device.Id, "FOTA> LINE REPLACED: " + line);
                        }
                        buffer.Insert(0, line + Environment.NewLine);
                    }
                    else
                    {
                        STrace.Debug(typeof(Fota).FullName, device.Id, "FOTA> LINE DELETED : " + lineOriginal);
                    }

                    if (msgidfound)
                    {
                        break;
                    }
                }
                if (buffer.Length != 0)
                {
                    sr.Write(buffer.ToString());
                }
                if (device.Sequence != null)
                {
                    sr.Write(LastSequenceFactory(device));
                }
                sr.TruncateHere();
            }

            if (IOUtils.FileExists(file))
            {
                using (var sr = new BackwardReader(file))
                {
                    var    allLastSequence = true;
                    String line;
                    while (allLastSequence && (line = sr.ReadLine()) != null)
                    {
                        allLastSequence = allLastSequence && IsLastSequenceMessage(line);
                    }
                    sr.Dispose();
                    if (allLastSequence)
                    {
                        File.Delete(file);
                    }
                }
            }
            return(result);
        }