public IHttpActionResult Post(AddNewNotificationRuleModel addNewNotificationRuleModel) { var userId = User.Identity.GetUserId(); NotificationRuleService notificationRuleService = new NotificationRuleService(); UserDeviceService userDeviceService = new UserDeviceService(); var sourceDevice = userDeviceService.Collection.Find( x => x.AppUserId == userId && x.Name == addNewNotificationRuleModel.SourceDeviceName).FirstOrDefault(); var targetDevice = userDeviceService.Collection.Find( x => x.AppUserId == userId && x.Name == addNewNotificationRuleModel.TargetDeviceName).FirstOrDefault(); if (sourceDevice != null && targetDevice != null) { var newRule = new NotificationRule() { AppUserId = userId, Id = ObjectId.GenerateNewId().ToString(), SourceDeviceId = sourceDevice.Id, SourceDeviceName = sourceDevice.Name, TargetDeviceId = targetDevice.Id, TargetDeviceName = targetDevice.Name, SourceMessage = addNewNotificationRuleModel.SourceMessage, TargetMessage = addNewNotificationRuleModel.TargetMessage }; notificationRuleService.Create(newRule); return(Ok(newRule)); } return(BadRequest("Devices with such name do not exists")); }
public IHttpActionResult Post(AddNewNotificationRuleModel addNewNotificationRuleModel) { var userId = User.Identity.GetUserId(); NotificationRuleService notificationRuleService = new NotificationRuleService(); UserDeviceService userDeviceService = new UserDeviceService(); var sourceDevice = userDeviceService.Collection.Find( x => x.AppUserId == userId && x.Name == addNewNotificationRuleModel.SourceDeviceName).FirstOrDefault(); var targetDevice = userDeviceService.Collection.Find( x => x.AppUserId == userId && x.Name == addNewNotificationRuleModel.TargetDeviceName).FirstOrDefault(); if (sourceDevice != null && targetDevice != null) { var newRule = new NotificationRule() { AppUserId = userId, Id = ObjectId.GenerateNewId().ToString(), SourceDeviceId = sourceDevice.Id, SourceDeviceName = sourceDevice.Name, TargetDeviceId = targetDevice.Id, TargetDeviceName = targetDevice.Name, SourceMessage = addNewNotificationRuleModel.SourceMessage, TargetMessage = addNewNotificationRuleModel.TargetMessage }; notificationRuleService.Create(newRule); return Ok(newRule); } return BadRequest("Devices with such name do not exists"); }
public IHttpActionResult Get() { var userId = User.Identity.GetUserId(); NotificationRuleService notificationRuleService = new NotificationRuleService(); var rules = notificationRuleService.Collection.Find(x => x.AppUserId == userId).ToList(); return Ok(rules); }
public IHttpActionResult Delete(string id) { var userId = User.Identity.GetUserId(); NotificationRuleService notificationRuleService = new NotificationRuleService(); notificationRuleService.Delete(id); return(Ok()); }
public IHttpActionResult Get() { var userId = User.Identity.GetUserId(); NotificationRuleService notificationRuleService = new NotificationRuleService(); var rules = notificationRuleService.Collection.Find(x => x.AppUserId == userId).ToList(); return(Ok(rules)); }
public IHttpActionResult GetNotificationRuleDetails(string notificationId) { var userId = User.Identity.GetUserId(); NotificationRuleService notificationRuleService = new NotificationRuleService(); var rule = notificationRuleService.Collection.Find(x => x.AppUserId == userId && x.Id == notificationId).FirstOrDefault(); if (rule != null) { return Ok(rule); } return BadRequest("Notification rule does not exist"); }
public IHttpActionResult GetNotificationRuleDetails(string notificationId) { var userId = User.Identity.GetUserId(); NotificationRuleService notificationRuleService = new NotificationRuleService(); var rule = notificationRuleService.Collection.Find(x => x.AppUserId == userId && x.Id == notificationId).FirstOrDefault(); if (rule != null) { return(Ok(rule)); } return(BadRequest("Notification rule does not exist")); }
public static void OnUserDeviceMessageReceived(DeviceMessageEvent deviceMessageEvent) { var notificationRulesService = new NotificationRuleService(); var rules = notificationRulesService.Collection.Find( x => x.AppUserId == deviceMessageEvent.AppUserId && x.SourceDeviceId == deviceMessageEvent.DeviceId).ToList(); if (rules.Any()) { var deviceService = new UserDeviceService(); foreach (var notificationRule in rules) { if (notificationRule.SourceMessage == deviceMessageEvent.Message) { var device = deviceService.GetById(notificationRule.TargetDeviceId); if (device.ConnectionState == DeviveConnectionState.FullDuplex && !String.IsNullOrEmpty(device.ConnectionId)) { var devicesHub = GlobalHost.ConnectionManager.GetHubContext<DevicesHub>(); devicesHub.Clients.Client(device.ConnectionId).NotificationRule(notificationRule.TargetMessage); } } } } }
public static void OnUserDeviceMessageReceived(DeviceMessageEvent deviceMessageEvent) { var notificationRulesService = new NotificationRuleService(); var rules = notificationRulesService.Collection.Find( x => x.AppUserId == deviceMessageEvent.AppUserId && x.SourceDeviceId == deviceMessageEvent.DeviceId).ToList(); if (rules.Any()) { var deviceService = new UserDeviceService(); foreach (var notificationRule in rules) { if (notificationRule.SourceMessage == deviceMessageEvent.Message) { var device = deviceService.GetById(notificationRule.TargetDeviceId); if (device.ConnectionState == DeviveConnectionState.FullDuplex && !String.IsNullOrEmpty(device.ConnectionId)) { var devicesHub = GlobalHost.ConnectionManager.GetHubContext <DevicesHub>(); devicesHub.Clients.Client(device.ConnectionId).NotificationRule(notificationRule.TargetMessage); } } } } }
public IHttpActionResult Delete(string id) { var userId = User.Identity.GetUserId(); NotificationRuleService notificationRuleService = new NotificationRuleService(); notificationRuleService.Delete(id); return Ok(); }