예제 #1
0
        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);
        }
예제 #2
0
        public async Task SetDelayedActionAsExecuted(string uuid)
        {
            StorageFolder folder = await GetFolder(ForegroundActionsFolder, true);

            StorageFile file = await folder.CreateFileAsync(DelayedActionsFileName, CreationCollisionOption.OpenIfExists);

            List <DelayedActionHelper> delayedActionHelpers = FileStorageHelper.DelayedActionsFromStrings(await FileIO.ReadLinesAsync(file));

            bool          needed  = false;
            List <string> strings = new List <string>();

            foreach (DelayedActionHelper delayedActionHelper in delayedActionHelpers)
            {
                if (delayedActionHelper.Id == uuid)
                {
                    delayedActionHelper.Executed = true;
                    needed = true;
                }
                strings.Add(FileStorageHelper.DelayedActionToString(delayedActionHelper));
            }
            if (needed)
            {
                await FileIO.WriteLinesAsync(file, strings);
            }
        }
예제 #3
0
        public async Task <bool> SaveHistoryAction(HistoryAction action)
        {
            try
            {
                action.Background = Background;
                string actionToString = FileStorageHelper.ActionToString(action);
                if (Background)
                {
                    StorageFolder folder = await GetFolder(Background?BackgroundActionsFolder : ForegroundActionsFolder);

                    StorageFile file = await folder.CreateFileAsync(ActionsFileName, CreationCollisionOption.OpenIfExists);

                    return(await RetryAppending(file, actionToString));
                }
                else
                {
                    await ForegroundHistoryActionWriter.WriteLine(actionToString);

                    return(true);
                }
            }
            catch (Exception e)
            {
                Logger.Error("Error writing HistoryAction", e);
            }
            return(false);
        }
예제 #4
0
        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);
                    }
                }
            });
        }
예제 #5
0
        public async Task <bool> SaveBeaconEventState(string pid, BeaconEventType type)
        {
            StorageFolder folder = await GetFolder(BackgroundSettingsFolder, true);

            StorageFile file = await folder.CreateFileAsync(pid, CreationCollisionOption.OpenIfExists);

            return(await RetryWriting(file, FileStorageHelper.BeaconEventStateToString(pid, type, DateTimeOffset.Now)));
        }
예제 #6
0
        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);
        }
예제 #7
0
        public async Task <bool> SaveDelayedAction(ResolvedAction action, DateTimeOffset dueTime, string beaconPid, BeaconEventType eventType, string location)
        {
            StorageFolder folder = await GetFolder(Background?BackgroundActionsFolder : ForegroundActionsFolder, true);

            StorageFile file = await folder.CreateFileAsync(DelayedActionsFileName, CreationCollisionOption.OpenIfExists);

            string actionToString = FileStorageHelper.DelayedActionToString(action, dueTime, beaconPid, eventType, location);

            return(await RetryAppending(file, actionToString));
        }
예제 #8
0
        public async Task SetActionsAsDelivered(IList <HistoryAction> sendActions)
        {
            StorageFolder folder = await GetFolder(BackgroundActionsFolder, true);

            StorageFile deliveredActionsFile = await folder.CreateFileAsync(ActionsFileName, CreationCollisionOption.OpenIfExists);

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

                StorageFile actionFile = files.FirstOrDefault(f => f.Name == ActionsFileName);
                if (actionFile != null)
                {
                    List <HistoryAction> actions = FileStorageHelper.ActionsFromStrings(await FileIO.ReadLinesAsync(actionFile));

                    List <string> stringActions = new List <string>();
                    foreach (HistoryAction historyAction in actions)
                    {
                        historyAction.Delivered = true;
                        stringActions.Add(FileStorageHelper.ActionToString(historyAction));
                    }
                    await FileIO.AppendLinesAsync(deliveredActionsFile, stringActions);
                }
                await storageFolder.DeleteAsync();
            }

            if (ForegroundHistoryActionWriter != null)
            {
                await ForegroundHistoryActionWriter.RewriteFile((l, l2) =>
                {
                    foreach (string s in l)
                    {
                        HistoryAction action = FileStorageHelper.ActionFromString(s);
                        if (!sendActions.Contains(action))
                        {
                            l2.Add(s);
                        }
                    }
                });
            }
        }
예제 #9
0
        public async Task <BackgroundEvent> GetLastEventStateForBeacon(string pid)
        {
            StorageFolder folder = await GetFolder(BackgroundSettingsFolder, true);

            StorageFile file = await folder.CreateFileAsync(pid, CreationCollisionOption.OpenIfExists);

            try
            {
                return(FileStorageHelper.BeaconEventStateFromString(await FileIO.ReadTextAsync(file)));
            }
            catch (SEHException)
            {
                return(null);
            }
            catch (FileNotFoundException)
            {
                return(null);
            }
        }
예제 #10
0
        public async Task <IList <DelayedActionData> > GetDelayedActions()
        {
            List <DelayedActionData> actions = new List <DelayedActionData>();

            StorageFolder folder = await GetFolder(ForegroundActionsFolder, true);

            StorageFile file = await folder.CreateFileAsync(DelayedActionsFileName, CreationCollisionOption.OpenIfExists);

            List <DelayedActionHelper> delayedActionHelpers = FileStorageHelper.DelayedActionsFromStrings(await FileIO.ReadLinesAsync(file));

            foreach (DelayedActionHelper delayedActionHelper in delayedActionHelpers)
            {
                if (!delayedActionHelper.Executed)
                {
                    DelayedActionData data = FileStorageHelper.DelayedActionFromHelper(delayedActionHelper);
                    actions.Add(data);
                }
            }

            return(actions);
        }
예제 #11
0
        private async Task <IList <HistoryAction> > GetActions(bool lockFolder)
        {
            IList <HistoryAction> actions = new List <HistoryAction>();

            StorageFolder folder = await GetFolder(BackgroundActionsFolder);

            if (lockFolder)
            {
                await CreateEventMarker(folder);
            }
            StorageFolder parentFolder = await folder.GetParentAsync();

            IReadOnlyList <StorageFolder> folders = await parentFolder.GetFoldersAsync();

            foreach (StorageFolder storageFolder in folders)
            {
                try
                {
                    IReadOnlyList <StorageFile> files = await storageFolder.GetFilesAsync();

                    StorageFile first = null;
                    foreach (var f in files)
                    {
                        if (f.Name == ActionsFileName)
                        {
                            first = f;
                            break;
                        }
                    }
                    if (first != null)
                    {
                        List <HistoryAction> fileActions = FileStorageHelper.ActionsFromStrings(await FileIO.ReadLinesAsync(first));
                        if (fileActions != null)
                        {
                            foreach (HistoryAction historyAction in fileActions)
                            {
                                actions.Add(historyAction);
                            }
                        }
                    }
                }
                catch (SEHException)
                {
                }
                catch (FileNotFoundException)
                {
                }
            }
            if (ForegroundHistoryActionWriter != null)
            {
                List <HistoryAction> foreGroundfileActions = FileStorageHelper.ActionsFromStrings(await ForegroundHistoryActionWriter.ReadLines());
                if (foreGroundfileActions != null)
                {
                    foreach (HistoryAction historyAction in foreGroundfileActions)
                    {
                        actions.Add(historyAction);
                    }
                }
            }

            return(actions);
        }
예제 #12
0
        public async Task <List <HistoryAction> > GetActionsForForeground(bool doNotDelete = false)
        {
            List <HistoryAction> actions = new List <HistoryAction>();

            try
            {
                StorageFolder folder = await GetFolder(BackgroundActionsFolder, true);

                StorageFile deliveredActionsFile = await folder.CreateFileAsync(ActionsFileName, CreationCollisionOption.OpenIfExists);

                List <HistoryAction> fileActions = FileStorageHelper.ActionsFromStrings(await FileIO.ReadLinesAsync(deliveredActionsFile));
                if (fileActions != null)
                {
                    foreach (HistoryAction historyAction in fileActions)
                    {
                        if (historyAction.Background)
                        {
                            actions.Add(historyAction);
                        }
                    }
                    if (!doNotDelete && fileActions.Count != 0)
                    {
                        StringBuilder sb = new StringBuilder();
                        foreach (HistoryAction historyAction in fileActions)
                        {
                            historyAction.Background = false;
                            sb.Append(FileStorageHelper.ActionToString(historyAction));
                            sb.Append('\n');
                        }
                        if (!await RetryWriting(deliveredActionsFile, sb.ToString()))
                        {
                            Logger.Error("GetActionsForForeground#1: Writing failed ");
                        }
                    }
                }
                IReadOnlyList <StorageFolder> folders = await folder.GetFoldersAsync();

                foreach (StorageFolder storageFolder in folders)
                {
                    try
                    {
                        IReadOnlyList <StorageFile> files = await storageFolder.GetFilesAsync();

                        StorageFile first = null;
                        foreach (var f in files)
                        {
                            if (f.Name == ActionsFileName)
                            {
                                first = f;
                                break;
                            }
                        }
                        if (first != null)
                        {
                            fileActions = FileStorageHelper.ActionsFromStrings(await FileIO.ReadLinesAsync(first));
                            if (fileActions != null && fileActions.Count != 0)
                            {
                                foreach (HistoryAction historyAction in fileActions)
                                {
                                    if (historyAction.Background)
                                    {
                                        actions.Add(historyAction);
                                    }
                                }
                                if (!doNotDelete)
                                {
                                    StringBuilder sb = new StringBuilder();
                                    foreach (HistoryAction historyAction in fileActions)
                                    {
                                        historyAction.Background = false;
                                        sb.Append(FileStorageHelper.ActionToString(historyAction));
                                        sb.Append('\n');
                                    }
                                    if (!await RetryWriting(first, sb.ToString()))
                                    {
                                        Logger.Error("GetActionsForForeground#2: Writing failed ");
                                    }
                                }
                            }
                        }
                    }
                    catch (UnauthorizedAccessException)
                    {
                    }
                    catch (SEHException)
                    {
                    }
                    catch (FileNotFoundException)
                    {
                    }
                }
            }
            catch (UnauthorizedAccessException)
            {
            }
            catch (SEHException)
            {
            }
            catch (FileNotFoundException)
            {
            }
            return(actions);
        }
예제 #13
0
        public async Task CleanupDatabase()
        {
            string minDateTime = DateTime.Now.AddDays(-1).ToString(History.Timeformat);
            {
                StorageFolder folder = await GetFolder(BackgroundActionsFolder, true);

                IReadOnlyList <IStorageItem> folders = await folder.GetItemsAsync();

                foreach (IStorageItem storageItem in folders)
                {
                    try
                    {
                        if (storageItem.IsOfType(StorageItemTypes.Folder))
                        {
                            StorageFolder storageFolder       = (StorageFolder)storageItem;
                            IReadOnlyList <StorageFile> files = await storageFolder.GetFilesAsync();

                            foreach (var f in files)
                            {
                                if (f.Name == ActionsFileName)
                                {
                                    List <HistoryAction> fileActions = FileStorageHelper.ActionsFromStrings(await FileIO.ReadLinesAsync(f));
                                    if (fileActions.All(a => a.Delivered && a.ActionTime.CompareTo(minDateTime) < 0))
                                    {
                                        await f.DeleteAsync();

                                        break;
                                    }
                                }
                            }
                            if ((await storageFolder.GetFilesAsync()).Count == 0)
                            {
                                await storageFolder.DeleteAsync();
                            }
                        }
                        else
                        {
                            if (storageItem.Name == ActionsFileName)
                            {
                                StorageFile          storageFile = (StorageFile)storageItem;
                                List <HistoryAction> fileActions = FileStorageHelper.ActionsFromStrings(await FileIO.ReadLinesAsync(storageFile));
                                if (fileActions.RemoveAll(a => a.Delivered && a.ActionTime.CompareTo(minDateTime) < 0) > 0)
                                {
                                    await RetryWriting(storageFile, FileStorageHelper.ActionsToString(fileActions));
                                }
                            }
                        }
                    }
                    catch (SEHException)
                    {
                    }
                    catch (FileNotFoundException)
                    {
                    }
                }
            }
            await ForegroundHistoryActionWriter.RewriteFile((lines, linesToWrite) =>
            {
                List <HistoryAction> fileActions = FileStorageHelper.ActionsFromStrings(lines);
                fileActions.RemoveAll(a => a.Delivered && a.ActionTime.CompareTo(minDateTime) < 0);
                foreach (HistoryAction historyAction in fileActions)
                {
                    linesToWrite.Add(FileStorageHelper.ActionToString(historyAction));
                }
            });

            //Events are deleted when delivered so no cleanup need
        }