/// <summary> /// If supressionTime is set for the action, fuction will check from the history if the /// action is already presented during the supression time. /// </summary> /// <param name="resolvedAction"></param> /// <returns>True only if action should be supressed.</returns> public bool ShouldSupressAsync(ResolvedAction resolvedAction) { Logger.Trace("ShouldSupressAsync {0}", resolvedAction.BeaconAction.Id); bool retVal = false; if (resolvedAction.SuppressionTime > 0) { if (lastEvents.Values.ContainsKey(resolvedAction.BeaconAction.Uuid)) { if ((long)lastEvents.Values[resolvedAction.BeaconAction.Uuid] + resolvedAction.SuppressionTime*1000 > DateTimeOffset.Now.ToUnixTimeMilliseconds()) { retVal = true; } } lastEvents.Values[resolvedAction.BeaconAction.Uuid] = DateTimeOffset.Now.ToUnixTimeMilliseconds(); } return retVal; }
/// <summary> /// If sendOnlyOnce is true for resolved action, fuction will check from the history if the /// action is already presented for the user. /// </summary> /// <param name="resolvedAction"></param> /// <returns>True ,if action type is SendOnlyOnce, and it has been shown already. Otherwise false.</returns> public bool CheckSendOnlyOnceAsync(ResolvedAction resolvedAction) { Logger.Trace("CheckSendOnlyOnceAsync {0}", resolvedAction.BeaconAction.Id); if (resolvedAction.SendOnlyOnce) { if (firedActions.Values.ContainsKey(resolvedAction.BeaconAction.Uuid)) { return true; } else { firedActions.Values[resolvedAction.BeaconAction.Uuid] = true; return false; } } return false; }
private bool Equals(ResolvedAction other) { return (!_beaconPids?.Except(other._beaconPids).GetEnumerator().MoveNext()).Value && Equals(BeaconAction.ToString(), other.BeaconAction.ToString()) && EventTypeDetectedByDevice == other.EventTypeDetectedByDevice && Delay == other.Delay && SendOnlyOnce == other.SendOnlyOnce && SuppressionTime == other.SuppressionTime && ReportImmediately == other.ReportImmediately && (!Timeframes?.Except(other.Timeframes).GetEnumerator().MoveNext()).Value; }
public async Task DeleayedActionTest() { ResolvedAction action = new ResolvedAction(); action.BeaconAction = new BeaconAction(); action.BeaconAction.Body = "body"; action.BeaconAction.Id = 1; action.BeaconAction.Payload = JsonObject.Parse("{\"pay\":\"load\"}"); action.BeaconAction.Subject = "Subject"; action.BeaconAction.Type = BeaconActionType.InApp; action.BeaconAction.Url = "http://sensorberg.com"; action.BeaconAction.Uuid = "uuid"; action.Delay = 123; action.BeaconPids = new List<string>() {"1", "2", "3", "4"}; action.EventTypeDetectedByDevice = BeaconEventType.EnterExit; action.ReportImmediately = true; action.SendOnlyOnce = true; action.SuppressionTime = 321; action.Timeframes = new List<Timeframe>() { new Timeframe() {End = DateTimeOffset.Parse("2015-04-16T12:00:00.000+0000"), Start = DateTimeOffset.Parse("2015-04-15T12:00:00.000+0000")} }; await storage.InitStorage(); Assert.IsTrue(await storage.SaveDelayedAction(action, DateTimeOffset.Parse("2015-04-16T12:00:00.000+0000"), "1", BeaconEventType.Enter, "1")); IList<DelayedActionData> delayedActions = await storage.GetDelayedActions(); Assert.AreEqual(1, delayedActions.Count, "to many actions found"); DelayedActionData delayAction = delayedActions[0]; Assert.AreEqual("1", delayAction.BeaconPid, "Not same beacon id"); Assert.AreEqual(DateTimeOffset.Parse("2015-04-16T12:00:00.000+0000"), delayAction.DueTime, "not same delay time"); Assert.AreEqual(BeaconEventType.Enter, delayAction.EventTypeDetectedByDevice, "not same event type"); Assert.AreEqual(action.Delay, delayAction.ResolvedAction.Delay, "not same action delay"); Assert.AreEqual(action.EventTypeDetectedByDevice, delayAction.ResolvedAction.EventTypeDetectedByDevice, "not same action event type"); Assert.AreEqual(action.ReportImmediately, delayAction.ResolvedAction.ReportImmediately, "not same ReportImmediately"); Assert.AreEqual(action.SendOnlyOnce, delayAction.ResolvedAction.SendOnlyOnce, "not same SendOnlyOnce"); Assert.AreEqual(action.SuppressionTime, delayAction.ResolvedAction.SuppressionTime, "not same SendOnlyOnce"); Assert.AreEqual(action.Timeframes.Count, delayAction.ResolvedAction.Timeframes.Count, "not same Timeframes count"); Assert.AreEqual(action.Timeframes[0].Start, delayAction.ResolvedAction.Timeframes[0].Start, "not same Timeframes count"); Assert.AreEqual(action.Timeframes[0].End, delayAction.ResolvedAction.Timeframes[0].End, "not same Timeframes count"); Assert.AreEqual(action.BeaconPids.Count, delayAction.ResolvedAction.BeaconPids.Count, "not same beacon count"); Assert.AreEqual(action.BeaconAction.Body, delayAction.ResolvedAction.BeaconAction.Body, "not same beacon action body"); Assert.AreEqual(action.BeaconAction.Subject, delayAction.ResolvedAction.BeaconAction.Subject, "not same beacon action Subject"); Assert.AreEqual(action.BeaconAction.Id, delayAction.ResolvedAction.BeaconAction.Id, "not same beacon action Id"); Assert.AreEqual(action.BeaconAction.Type, delayAction.ResolvedAction.BeaconAction.Type, "not same beacon action Type"); Assert.AreEqual(action.BeaconAction.Uuid, delayAction.ResolvedAction.BeaconAction.Uuid, "not same beacon action Uuid"); Assert.AreEqual(action.BeaconAction.Payload.ToString(), delayAction.ResolvedAction.BeaconAction.Payload.ToString(), "not same beacon action Payload"); Assert.AreEqual(action, delayAction.ResolvedAction, "not same action"); ResolvedAction action2 = new ResolvedAction(); action2.BeaconAction = new BeaconAction(); action2.BeaconAction.Body = "body2"; action2.BeaconAction.Id = 2; action2.BeaconAction.Payload = JsonObject.Parse("{\"pay\":\"load2\"}"); action2.BeaconAction.Subject = "Subject2"; action2.BeaconAction.Type = BeaconActionType.UrlMessage; action2.BeaconAction.Url = "http://sensorberg.com"; action2.BeaconAction.Uuid = "uuid2"; action2.Delay = 1234; action2.BeaconPids = new List<string>() {"1", "2", "3", "4", "5"}; action2.EventTypeDetectedByDevice = BeaconEventType.EnterExit; action2.ReportImmediately = false; action2.SendOnlyOnce = false; action2.SuppressionTime = 3210; action2.Timeframes = new List<Timeframe>() { new Timeframe() {End = DateTimeOffset.Parse("2016-04-16T12:00:00.000+0000"), Start = DateTimeOffset.Parse("2014-04-15T12:00:00.000+0000")} }; Assert.IsTrue(await storage.SaveDelayedAction(action, DateTimeOffset.Parse("2015-05-16T12:00:00.000+0000"), "2", BeaconEventType.EnterExit, null)); delayedActions = await storage.GetDelayedActions(); Assert.AreEqual(2, delayedActions.Count, "to many actions found"); delayAction = delayedActions.FirstOrDefault(d => d.BeaconPid == "1"); string idToDelete = delayAction.Id; Assert.AreEqual("1", delayAction.BeaconPid, "Not same beacon id"); Assert.AreEqual(DateTimeOffset.Parse("2015-04-16T12:00:00.000+0000"), delayAction.DueTime, "not same delay time"); Assert.AreEqual(BeaconEventType.Enter, delayAction.EventTypeDetectedByDevice, "not same event type"); Assert.AreEqual(action.Delay, delayAction.ResolvedAction.Delay, "not same action delay"); Assert.AreEqual(action.EventTypeDetectedByDevice, delayAction.ResolvedAction.EventTypeDetectedByDevice, "not same action event type"); Assert.AreEqual(action.ReportImmediately, delayAction.ResolvedAction.ReportImmediately, "not same ReportImmediately"); Assert.AreEqual(action.SendOnlyOnce, delayAction.ResolvedAction.SendOnlyOnce, "not same SendOnlyOnce"); Assert.AreEqual(action.SuppressionTime, delayAction.ResolvedAction.SuppressionTime, "not same SendOnlyOnce"); Assert.AreEqual(action.Timeframes.Count, delayAction.ResolvedAction.Timeframes.Count, "not same Timeframes count"); Assert.AreEqual(action.Timeframes[0].Start, delayAction.ResolvedAction.Timeframes[0].Start, "not same Timeframes count"); Assert.AreEqual(action.Timeframes[0].End, delayAction.ResolvedAction.Timeframes[0].End, "not same Timeframes count"); Assert.AreEqual(action.BeaconPids.Count, delayAction.ResolvedAction.BeaconPids.Count, "not same beacon count"); Assert.AreEqual(action.BeaconAction.Body, delayAction.ResolvedAction.BeaconAction.Body, "not same beacon action body"); Assert.AreEqual(action.BeaconAction.Subject, delayAction.ResolvedAction.BeaconAction.Subject, "not same beacon action Subject"); Assert.AreEqual(action.BeaconAction.Id, delayAction.ResolvedAction.BeaconAction.Id, "not same beacon action Id"); Assert.AreEqual(action.BeaconAction.Type, delayAction.ResolvedAction.BeaconAction.Type, "not same beacon action Type"); Assert.AreEqual(action.BeaconAction.Uuid, delayAction.ResolvedAction.BeaconAction.Uuid, "not same beacon action Uuid"); Assert.AreEqual(action.BeaconAction.Payload.ToString(), delayAction.ResolvedAction.BeaconAction.Payload.ToString(), "not same beacon action Payload"); Assert.AreEqual(action, delayAction.ResolvedAction, "not same action"); delayAction = delayedActions.FirstOrDefault(d => d.BeaconPid == "2"); Assert.AreEqual("2", delayAction.BeaconPid, "Not same beacon id"); Assert.AreEqual(DateTimeOffset.Parse("2015-05-16T12:00:00.000+0000"), delayAction.DueTime, "not same delay time"); Assert.AreEqual(BeaconEventType.EnterExit, delayAction.EventTypeDetectedByDevice, "not same event type"); Assert.AreEqual(action.Delay, delayAction.ResolvedAction.Delay, "not same action delay"); Assert.AreEqual(action.EventTypeDetectedByDevice, delayAction.ResolvedAction.EventTypeDetectedByDevice, "not same action event type"); Assert.AreEqual(action.ReportImmediately, delayAction.ResolvedAction.ReportImmediately, "not same ReportImmediately"); Assert.AreEqual(action.SendOnlyOnce, delayAction.ResolvedAction.SendOnlyOnce, "not same SendOnlyOnce"); Assert.AreEqual(action.SuppressionTime, delayAction.ResolvedAction.SuppressionTime, "not same SendOnlyOnce"); Assert.AreEqual(action.Timeframes.Count, delayAction.ResolvedAction.Timeframes.Count, "not same Timeframes count"); Assert.AreEqual(action.Timeframes[0].Start, delayAction.ResolvedAction.Timeframes[0].Start, "not same Timeframes count"); Assert.AreEqual(action.Timeframes[0].End, delayAction.ResolvedAction.Timeframes[0].End, "not same Timeframes count"); Assert.AreEqual(action.BeaconPids.Count, delayAction.ResolvedAction.BeaconPids.Count, "not same beacon count"); Assert.AreEqual(action.BeaconAction.Body, delayAction.ResolvedAction.BeaconAction.Body, "not same beacon action body"); Assert.AreEqual(action.BeaconAction.Subject, delayAction.ResolvedAction.BeaconAction.Subject, "not same beacon action Subject"); Assert.AreEqual(action.BeaconAction.Id, delayAction.ResolvedAction.BeaconAction.Id, "not same beacon action Id"); Assert.AreEqual(action.BeaconAction.Type, delayAction.ResolvedAction.BeaconAction.Type, "not same beacon action Type"); Assert.AreEqual(action.BeaconAction.Uuid, delayAction.ResolvedAction.BeaconAction.Uuid, "not same beacon action Uuid"); Assert.AreEqual(action.BeaconAction.Payload.ToString(), delayAction.ResolvedAction.BeaconAction.Payload.ToString(), "not same beacon action Payload"); Assert.AreEqual(action, delayAction.ResolvedAction, "not same action"); await storage.SetDelayedActionAsExecuted(idToDelete); delayedActions = await storage.GetDelayedActions(); Assert.AreEqual(1, delayedActions.Count, "to many actions found after executing action"); Assert.AreEqual("2", delayedActions[0].BeaconPid, "Not same beacon id"); }
public async Task<bool> SaveDelayedAction(ResolvedAction action, DateTimeOffset dueTime, string beaconPid, BeaconEventType eventType, string location) { return await SaveDelayedActionsRetry(action, dueTime, beaconPid, eventType, location, MaxRetries); }
private async Task<bool> SaveDelayedActionsRetry(ResolvedAction action, DateTimeOffset dueTime, string beaconPid, BeaconEventType eventTypeDetectedByDevice, string location, int retry) { if (retry < 0) { return false; } try { if (await Storage.SaveDelayedAction(action, dueTime, beaconPid, eventTypeDetectedByDevice, location)) { return true; } return await SaveDelayedActionsRetry(action, dueTime, beaconPid, eventTypeDetectedByDevice, location, --retry); } catch (UnauthorizedAccessException) { return await SaveDelayedActionsRetry(action, dueTime, beaconPid, eventTypeDetectedByDevice, location, --retry); } catch (FileNotFoundException) { return await SaveDelayedActionsRetry(action, dueTime, beaconPid, eventTypeDetectedByDevice, location, --retry); } }
public static string DelayedActionToString(ResolvedAction action, DateTimeOffset dueTime, string beaconPid, BeaconEventType beaconEventType, Guid guid, string location) { string serializeObject = JsonConvert.SerializeObject(new SerializedAction() {Action = action, Time = dueTime, Beacon = beaconPid, Event = beaconEventType}); return DelayedActionToString(Convert.ToBase64String(Encoding.UTF8.GetBytes(serializeObject)), dueTime, false, guid.ToString(), location); }
public static string DelayedActionToString(ResolvedAction action, DateTimeOffset dueTime, string beaconPid, BeaconEventType beaconEventType, string location) { return DelayedActionToString(action, dueTime, beaconPid, beaconEventType, Guid.NewGuid(), location); }
public async Task<bool> SaveDelayedAction(ResolvedAction action, DateTimeOffset dueTime, string beaconPid, BeaconEventType eventType, string location) { DelayedActions.Add(new DelayedActionData() {BeaconPid = beaconPid,DueTime = dueTime, EventTypeDetectedByDevice = eventType, Id = Guid.NewGuid().ToString(), ResolvedAction = action, Location = location}); return true; }
public void TestDelayedActionFromString() { Guid guid = Guid.NewGuid(); DelayedActionHelper simpleDelayedActionFromString = FileStorageHelper.SimpleDelayedActionFromString(guid+ ",1429192800000,False,eyJBY3Rpb24iOnsiQmVhY29uQWN0aW9uIjp7IklkIjoxLCJUeXBlIjozLCJlaWQiOiJ1dWlkIiwiU3ViamVjdCI6IlN1YmplY3QiLCJCb2R5IjoiYm9keSIsIlVybCI6Imh0dHA6Ly9zZW5zb3JiZXJnLmNvbSIsIlBheWxvYWRTdHJpbmciOiJ7XCJwYXlcIjpcImxvYWRcIn0ifSwiYmVhY29ucyI6WyIxIiwiMiIsIjMiLCI0Il0sInRyaWdnZXIiOjMsIkRlbGF5IjoxMjMsIlNlbmRPbmx5T25jZSI6dHJ1ZSwic3VwcHJlc3Npb25UaW1lIjozMjEsIlJlcG9ydEltbWVkaWF0ZWx5Ijp0cnVlLCJUaW1lZnJhbWVzIjpbeyJTdGFydCI6IjIwMTUtMDQtMTVUMTI6MDA6MDArMDA6MDAiLCJFbmQiOiIyMDE1LTA0LTE2VDEyOjAwOjAwKzAwOjAwIn1dfSwiVGltZSI6IjIwMTUtMDQtMTZUMTQ6MDA6MDArMDA6MDAiLCJCZWFjb24iOiIxMjMiLCJFdmVudCI6MX0="); Assert.AreEqual(DateTimeOffset.Parse("2015-04-16T14:00:00.000+0000"), simpleDelayedActionFromString.Offset, "Wrong offset"); Assert.IsFalse(simpleDelayedActionFromString.Executed, "Is executed"); string s = "eyJBY3Rpb24iOnsiQmVhY29uQWN0aW9uIjp7IklkIjoxLCJUeXBlIjozLCJlaWQiOiJ1dWlkIiwiU3ViamVjdCI6IlN1YmplY3QiLCJCb2R5IjoiYm9keSIsIlVybCI6Imh0dHA6Ly9zZW5zb3JiZXJnLmNvbSIsIlBheWxvYWRTdHJpbmciOiJ7XCJwYXlcIjpcImxvYWRcIn0ifSwiYmVhY29ucyI6WyIxIiwiMiIsIjMiLCI0Il0sInRyaWdnZXIiOjMsIkRlbGF5IjoxMjMsIlNlbmRPbmx5T25jZSI6dHJ1ZSwic3VwcHJlc3Npb25UaW1lIjozMjEsIlJlcG9ydEltbWVkaWF0ZWx5Ijp0cnVlLCJUaW1lZnJhbWVzIjpbeyJTdGFydCI6IjIwMTUtMDQtMTVUMTI6MDA6MDArMDA6MDAiLCJFbmQiOiIyMDE1LTA0LTE2VDEyOjAwOjAwKzAwOjAwIn1dfSwiVGltZSI6IjIwMTUtMDQtMTZUMTQ6MDA6MDArMDA6MDAiLCJCZWFjb24iOiIxMjMiLCJFdmVudCI6MX0="; Assert.AreEqual(s, simpleDelayedActionFromString.Content, "Wrong content"); ResolvedAction action = new ResolvedAction(); action.BeaconAction = new BeaconAction(); action.BeaconAction.Body = "body"; action.BeaconAction.Id = 1; action.BeaconAction.Payload = JsonObject.Parse("{\"pay\":\"load\"}"); action.BeaconAction.Subject = "Subject"; action.BeaconAction.Type = BeaconActionType.InApp; action.BeaconAction.Url = "http://sensorberg.com"; action.BeaconAction.Uuid = "uuid"; action.Delay = 123; action.BeaconPids = new List<string>() { "1", "2", "3", "4" }; action.EventTypeDetectedByDevice = BeaconEventType.EnterExit; action.ReportImmediately = true; action.SendOnlyOnce = true; action.SuppressionTime = 321; action.Timeframes = new List<Timeframe>() { new Timeframe() {End = DateTimeOffset.Parse("2015-04-16T12:00:00.000+0000"), Start = DateTimeOffset.Parse("2015-04-15T12:00:00.000+0000")} }; DelayedActionData data = FileStorageHelper.DelayedActionFromHelper(simpleDelayedActionFromString); Assert.AreEqual("123", data.BeaconPid, "Wrong beacon pid"); Assert.AreEqual(BeaconEventType.Enter, data.EventTypeDetectedByDevice, "Wrong event type"); Assert.AreEqual(DateTimeOffset.Parse("2015-04-16T14:00:00.000+0000"), data.DueTime, "Wrong time"); Assert.AreEqual(guid.ToString(), data.Id, "ID isnt set"); Assert.AreEqual(action, data.ResolvedAction); Assert.IsNull(FileStorageHelper.SimpleDelayedActionFromString("")); Assert.IsNull(FileStorageHelper.SimpleDelayedActionFromString(null)); Assert.IsNull(FileStorageHelper.DelayedActionFromHelper(null)); }
private bool Equals(ResolvedAction other) { return((!_beaconPids?.Except(other._beaconPids).GetEnumerator().MoveNext()).Value && Equals(BeaconAction.ToString(), other.BeaconAction.ToString()) && EventTypeDetectedByDevice == other.EventTypeDetectedByDevice && Delay == other.Delay && SendOnlyOnce == other.SendOnlyOnce && SuppressionTime == other.SuppressionTime && ReportImmediately == other.ReportImmediately && (!Timeframes?.Except(other.Timeframes).GetEnumerator().MoveNext()).Value); }