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