コード例 #1
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);
        }
コード例 #2
0
        /// <summary>
        /// Parse the given string to a HistoryAction.
        /// </summary>
        /// <param name="s">Comma separated string representing a HistoryAction.</param>
        /// <returns></returns>
        public static HistoryAction ActionFromString(string s)
        {
            if (string.IsNullOrEmpty(s))
            {
                return(null);
            }

            string[] ss = s.Split(new char[] { ',' });
            if (ss.Length < 5)
            {
                return(null);
            }

            HistoryAction ha = new HistoryAction();

            ha.EventId  = ss[0];
            ha.BeaconId = ss[1];

            try
            {
                ha.ActionTime = DateTimeOffset.FromUnixTimeMilliseconds(long.Parse(ss[2])).ToString(History.Timeformat);
            }
            catch (FormatException)
            {
                Debug.WriteLine("ERROR: parsing action: " + s);
                return(null);
            }

            ha.Trigger = int.Parse(ss[3]);
            try
            {
                if (ss.Length > 5)
                {
                    ha.Delivered = bool.Parse(ss[4]);
                }
            }
            catch (FormatException)
            {
                Debug.WriteLine("ERROR: parsing action: " + s);
            }
            try
            {
                if (ss.Length > 6)
                {
                    ha.Background = bool.Parse(ss[5]);
                }
            }
            catch (FormatException)
            {
                Debug.WriteLine("ERROR: parsing action: " + s);
            }

            if (ss.Length > 7)
            {
                ha.Location = ss[6];
            }

            return(ha);
        }
コード例 #3
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);
                        }
                    }
                });
            }
        }
コード例 #4
0
        public static List <HistoryAction> ActionsFromStrings(IList <string> strings)
        {
            if (strings == null || strings.Count == 0)
            {
                return(new List <HistoryAction>());
            }
            List <HistoryAction> actions = new List <HistoryAction>();

            foreach (string s in strings)
            {
                HistoryAction ha = ActionFromString(s);
                if (ha != null)
                {
                    actions.Add(ha);
                }
            }
            return(actions);
        }
コード例 #5
0
 protected bool Equals(HistoryAction other)
 {
     return(string.Equals(EventId, other.EventId) && string.Equals(BeaconId, other.BeaconId) && string.Equals(ActionTime, other.ActionTime) && string.Equals(Location, other.Location) && Trigger == other.Trigger && Delivered == other.Delivered && Background == other.Background);
 }
コード例 #6
0
 public static string ActionToString(HistoryAction historyAction)
 {
     return(ActionToString(historyAction.EventId, historyAction.BeaconId, DateTimeOffset.Parse(historyAction.ActionTime), historyAction.Trigger, historyAction.Delivered, historyAction.Background, historyAction.Location));
 }
コード例 #7
0
 protected bool Equals(HistoryAction other)
 {
     return string.Equals(EventId, other.EventId) && string.Equals(BeaconId, other.BeaconId) && string.Equals(ActionTime, other.ActionTime) && string.Equals(Location, other.Location) && Trigger == other.Trigger && Delivered == other.Delivered && Background == other.Background;
 }
コード例 #8
0
        /// <summary>
        /// Parse the given string to a HistoryAction.
        /// </summary>
        /// <param name="s">Comma separated string representing a HistoryAction.</param>
        /// <returns></returns>
        public static HistoryAction ActionFromString(string s)
        {
            if (string.IsNullOrEmpty(s))
            {
                return null;
            }

            string[] ss = s.Split(new char[] {','});
            if (ss.Length < 5)
            {
                return null;
            }

            HistoryAction ha = new HistoryAction();
            ha.EventId = ss[0];
            ha.BeaconId = ss[1];

            try
            {
                ha.ActionTime = DateTimeOffset.FromUnixTimeMilliseconds(long.Parse(ss[2])).ToString(History.Timeformat);
            }
            catch (FormatException)
            {
                Debug.WriteLine("ERROR: parsing action: " + s);
                return null;
            }

            ha.Trigger = int.Parse(ss[3]);
            try
            {
                if (ss.Length > 5)
                {
                    ha.Delivered = bool.Parse(ss[4]);
                }
            }
            catch (FormatException)
            {
                Debug.WriteLine("ERROR: parsing action: " + s);
            }
            try
            {
                if (ss.Length > 6)
                {
                    ha.Background = bool.Parse(ss[5]);
                }
            }
            catch (FormatException)
            {
                Debug.WriteLine("ERROR: parsing action: " + s);
            }

            if (ss.Length > 7)
            {
                ha.Location = ss[6];
            }

            return ha;
        }
コード例 #9
0
 public static string ActionToString(HistoryAction historyAction)
 {
     return ActionToString(historyAction.EventId, historyAction.BeaconId, DateTimeOffset.Parse(historyAction.ActionTime), historyAction.Trigger, historyAction.Delivered, historyAction.Background, historyAction.Location);
 }
コード例 #10
0
 public async Task<bool> SaveHistoryAction(HistoryAction action)
 {
     UndeliveredActions.Add(action);
     return true;
 }