public static void InsertAfterLastFotaCommand(IFoteable device, string commands) { var file = ObtainFirstExistantReversedFile(device); if (file == null) { EnqueuePrivate(device, 0, commands, ".txR"); return; } using (var sr = new BackwardReader(file)) { String line; if ((line = sr.ReadLine()) != null) { while (line != null && IsLastSequenceMessage(line)) { line = sr.ReadLine(); } if (line != null) { if (!commands.EndsWith(Environment.NewLine)) { commands += Environment.NewLine; } sr.Write(commands); sr.Write(line + Environment.NewLine); } if (device.Sequence != null) { sr.Write(LastSequenceFactory(device)); } } sr.TruncateHere(); } }
public static UInt32?ObtainLastSequence(INode device) { UInt32?result = null; var file = ObtainFirstExistantReversedFile(device); if (file == null) { return(null); } using (var sr = new BackwardReader(file)) { var line = String.Empty; if ((line = sr.ReadLine()) != null) { result = ExtractLastSequence(line); } } return(result); }
private static void EnqueuePrivate(IFoteable device, ulong messageId, String commands, String ext) { try { if (!commands.EndsWith(Environment.NewLine)) { commands += Environment.NewLine; } var file = GetFilePath(device, ext); var fexists = IOUtils.FileExists(file); if (fexists) { using (var sr = new BackwardReader(file)) { String line; if ((line = sr.ReadLine()) != null) { while (line != null && IsLastSequenceMessage(line)) { line = sr.ReadLine(); } if (line != null) { sr.Write(line + Environment.NewLine); } sr.Write(commands); if (messageId != 0) { sr.Write(VirtualMessageFactory(MessageIdentifier.AckEvent, messageId)); } if (device.Sequence != null) { sr.Write(LastSequenceFactory(device)); } sr.TruncateHere(); } } } else { using (var sw = new StreamWriter(file, true)) { sw.Write(commands); if (messageId != 0) { sw.WriteLine(VirtualMessageFactory(MessageIdentifier.AckEvent, messageId)); } if (device.Sequence != null) { sw.Write(LastSequenceFactory(device)); } sw.Close(); } } } catch (Exception e) { STrace.Exception(typeof(Fota).FullName, e, device.Id); } }
private static void ProccessAnyPendingFileInternal(IFoteable device) { //txR --- txt --- txR2 --- tx1 --- tx2 //process // reverse // process // reverse // reverse var fileR = GetFilePath(device, ".txR"); var fileT = fileR.Replace(".txR", ".txt"); var fileR2 = fileR.Replace(".txR", ".txR2"); var file1 = fileR.Replace(".txR", ".tx1"); var file2 = fileR.Replace(".txR", ".tx2"); var fileBck = fileR.Replace(".txR", ".bck"); if (IOUtils.FileExists(fileR)) { return; } if (!IOUtils.FileExists(fileT) && IOUtils.FileExists(fileR2)) { return; } var fileAct = new List <String> { fileT, file1, file2 } .FirstOrDefault(IOUtils.FileExists); if (fileAct == null) { return; } if (IOUtils.FileExists(fileBck)) { Archivar(device, ".bck"); } using (var sr = new BackwardReader(fileAct)) { using (var sw = new StreamWriter(fileBck)) { String line; while ((line = sr.ReadLine()) != null) { if (!String.IsNullOrEmpty(line) && !IsLastSequenceMessage(line)) { sw.WriteLine(line); } } if (device.Sequence != null) { sw.Write(LastSequenceFactory(device)); } sw.Close(); } } File.Delete(fileAct); File.Move(fileBck, (fileAct == fileT) ? fileR : fileR2); }
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); }
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); }