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); }