public async Task CleanDatabase() { try { if (ForegroundHistoryActionWriter != null) { await ForegroundHistoryActionWriter.Clear(); } if (ForegroundHistoryEventWriter != null) { await ForegroundHistoryEventWriter.Clear(); } StorageFolder folder = ApplicationData.Current.LocalFolder; StorageFolder root = await folder.CreateFolderAsync(RootFolder, CreationCollisionOption.OpenIfExists); await root.DeleteAsync(); } catch (SEHException) { } catch (FileNotFoundException) { } await InitStorage(); }
public async Task <bool> SaveHistoryEvents(HistoryEvent he) { try { string eventToString = FileStorageHelper.EventToString(he); Logger.Trace("SaveHistoryEvents " + he.BeaconId); if (Background) { StorageFolder folder = await GetFolder(Background?BackgroundEventsFolder : ForegroundEventsFolder); StorageFile file = await folder.CreateFileAsync(he.BeaconId, CreationCollisionOption.OpenIfExists); return(await RetryAppending(file, eventToString)); } else { await ForegroundHistoryEventWriter.WriteLine(eventToString); return(true); } } catch (Exception e) { Logger.Error("Error writing HistoryEvent", e); } return(false); }
public async Task SetEventsAsDelivered(IList <HistoryEvent> sendEvents) { { StorageFolder folder = await GetFolder(BackgroundEventsFolder, true); IReadOnlyList <StorageFolder> folders = await folder.GetFoldersAsync(); foreach (StorageFolder storageFolder in folders) { IReadOnlyList <StorageFile> files = await storageFolder.GetFilesAsync(); //ignore unlocked folders if (files.FirstOrDefault(f => f.Name == FolderLockFile) == null) { continue; } await storageFolder.DeleteAsync(); } } await ForegroundHistoryEventWriter.RewriteFile((l, l2) => { foreach (string s in l) { HistoryEvent e = FileStorageHelper.EventFromString(s); if (!sendEvents.Contains(e)) { l2.Add(s); } } }); }
private async Task <IList <HistoryEvent> > GetUndeliveredEvents(bool lockFolder) { IList <HistoryEvent> events = new List <HistoryEvent>(); { StorageFolder folder = await GetFolder(BackgroundEventsFolder); if (lockFolder) { await CreateEventMarker(folder); } IReadOnlyList <StorageFolder> folders = await(await folder.GetParentAsync()).GetFoldersAsync(); foreach (StorageFolder storageFolder in folders) { IReadOnlyList <StorageFile> files = await storageFolder.GetFilesAsync(); //when no lock ignore unlocked folders if (!lockFolder && files.FirstOrDefault(f => f.Name == FolderLockFile) == null) { continue; } foreach (StorageFile file in files) { if (file.Name == FolderLockFile) { continue; } List <HistoryEvent> fileEvents = FileStorageHelper.EventsFromStrings(await FileIO.ReadLinesAsync(file)); if (fileEvents != null) { foreach (HistoryEvent historyEvent in fileEvents) { if (!historyEvent.Delivered) { events.Add(historyEvent); } } } } } } if (ForegroundHistoryEventWriter != null) { List <string> list = await ForegroundHistoryEventWriter.ReadLines(); foreach (string s in list) { HistoryEvent historyEvent = FileStorageHelper.EventFromString(s); if (historyEvent != null && !historyEvent.Delivered) { events.Add(historyEvent); } } } return(events); }