public List<string> SendSosNotifications(ClientData client) { using (EmergencyDBEntities context = new EmergencyDBEntities()) { string notificationStatus = ""; string notificationChannelStatus = ""; string deviceConnectionStatus = ""; var sender = (from c in context.Clients where c.Username == client.Username select c).FirstOrDefault(); var clients = (from f in context.Friendships join c in context.Clients on f.FriendID equals c.ClientID where f.ClientID == sender.ClientID select c); foreach (var cl in clients) { List<string> uriStrings; using (EmergencyDBEntities context2 = new EmergencyDBEntities()) { uriStrings = (from d in context2.Devices where d.ClientID == cl.ClientID select d.DeviceUrl).ToList<string>(); } if (uriStrings.Count != 0) { foreach (var uriString in uriStrings) { Uri uri = new Uri(uriString); byte[] payload = new byte[0]; var request = (HttpWebRequest)WebRequest.Create(uri); request.Method = "POST"; request.ContentType = "text/xml"; payload = GetToastPayload("Emergency", sender.FirstName + " " + sender.LastName); request.Headers.Add("X-WindowsPhone-Target", "toast"); request.Headers.Add("X-NotificationClass", "2"); request.ContentLength = payload.Length; using (Stream requestStream = request.GetRequestStream()) { requestStream.Write(payload, 0, payload.Length); } try { var response = (HttpWebResponse)request.GetResponse(); notificationStatus = response.Headers["X-NotificationStatus"]; notificationChannelStatus = response.Headers["X-SubscriptionStatus"]; deviceConnectionStatus = response.Headers["X-DeviceConnectionStatus"]; } catch (Exception ex) { }//return new List<string> { "Device is not connected!", ex.Message }; } } } } return new List<string> { notificationStatus, notificationChannelStatus, deviceConnectionStatus }; //return new List<string> { "You don't have any friend." }; } }
public void RegisterPhone(Guid phoneId, string channelUri, string username) { using (EmergencyDBEntities context = new EmergencyDBEntities()) { int clientId; var deviceId = phoneId.ToString(); var deviceI = (from c in context.Devices where c.DeviceID == deviceId select c).FirstOrDefault<Device>(); using (EmergencyDBEntities context2 = new EmergencyDBEntities()) { clientId = (from c in context.Clients where c.Username == username select c.ClientID).FirstOrDefault(); } if (context.Devices.Count() == 0 || deviceI == null) { context.Devices.AddObject(new Device { DeviceID = phoneId.ToString(), DeviceUrl = channelUri, ClientID = clientId }); context.SaveChanges(); } foreach (var device in context.Devices) { Debug.WriteLine(device.DeviceID.ToString()); if (device.DeviceID == phoneId.ToString()) if (device.DeviceUrl != channelUri) device.DeviceUrl = channelUri; else if (device.ClientID != clientId) device.ClientID = clientId; } context.SaveChanges(); } }
public List<ClientData> SearchFriends(ClientData client) { List<ClientData> friend; using (EmergencyDBEntities context = new EmergencyDBEntities()) { friend = (from c in context.Clients where c.FirstName.StartsWith(client.FirstName) select new ClientData { Username = c.Username, FirstName = c.FirstName, LastName = c.LastName, Age = c.Age.Value, Email = c.Email }).ToList<ClientData>(); } return friend; }
public List<ClientData> GetFriendsList(string username) { List<ClientData> friends = new List<ClientData>(); using (EmergencyDBEntities context = new EmergencyDBEntities()) { var sender = (from c in context.Clients where c.Username == username select c).FirstOrDefault(); var clients = (from f in context.Friendships join c in context.Clients on f.FriendID equals c.ClientID where f.ClientID == sender.ClientID && f.Status == true select c); try { foreach (var cl in clients) { friends.Add(new ClientData() { Username = cl.Username, FirstName = cl.FirstName, LastName = cl.LastName, Age = cl.Age.Value, Email = cl.Email }); } } catch { return new List<ClientData>(); } } return friends; }
public List<string> PushToSubscribedPhones(NotificationData data) { using (EmergencyDBEntities context = new EmergencyDBEntities()) { foreach (var entry in context.Devices) { if (data.PhoneId.ToString() == entry.DeviceID) { Uri uri = new Uri(entry.DeviceUrl); byte[] payload = new byte[0]; var request = (HttpWebRequest)WebRequest.Create(uri); request.Method = "POST"; request.ContentType = "text/xml"; if (data.PushType == "toast") { payload = GetToastPayload(data.Title, data.PersonName); request.Headers.Add("X-WindowsPhone-Target", "toast"); request.Headers.Add("X-NotificationClass", "2"); } else if (data.PushType == "tile") { payload = GetTilePayload(data); request.Headers.Add("X-WindowsPhone-Target", "token"); request.Headers.Add("X-NotificationClass", "1"); } else if (data.PushType == "raw") { payload = Encoding.UTF8.GetBytes(data.Title); request.Headers.Add("X-NotificationClass", "3"); } request.ContentLength = payload.Length; using (Stream requestStream = request.GetRequestStream()) { requestStream.Write(payload, 0, payload.Length); } try { var response = (HttpWebResponse)request.GetResponse(); data.NotificationStatus = response.Headers["X-NotificationStatus"]; data.NotificationChannelStatus = response.Headers["X-SubscriptionStatus"]; data.DeviceConnectionStatus = response.Headers["X-DeviceConnectionStatus"]; } catch (Exception ex) { return new List<string> { "Device is not connected!", ex.Message }; } } } } return new List<string> { data.NotificationStatus, data.NotificationChannelStatus, data.DeviceConnectionStatus }; }
public string DeleteFriend(string clientUsername, string friendUsername) { string notificationStatusToast = ""; string notificationChannelStatusToast = ""; string deviceConnectionStatusToast = ""; Client friendDB, clientDB; using (EmergencyDBEntities context = new EmergencyDBEntities()) { friendDB = (from c in context.Clients where friendUsername == c.Username select c).FirstOrDefault(); } using (EmergencyDBEntities context = new EmergencyDBEntities()) { clientDB = (from c in context.Clients where clientUsername == c.Username select c).FirstOrDefault(); } using (EmergencyDBEntities context = new EmergencyDBEntities()) { context.Friendships.DeleteObject((from f in context.Friendships where f.ClientID == friendDB.ClientID && f.FriendID == clientDB.ClientID select f).FirstOrDefault()); context.Friendships.DeleteObject((from f in context.Friendships where f.ClientID == clientDB.ClientID && f.FriendID == friendDB.ClientID select f).FirstOrDefault()); context.SaveChanges(); } List<string> uriStrings; using (EmergencyDBEntities context = new EmergencyDBEntities()) { uriStrings = (from d in context.Devices where d.ClientID == friendDB.ClientID select d.DeviceUrl).ToList<string>(); } if (uriStrings.Count != 0) { foreach (var uriString in uriStrings) { Uri uri = new Uri(uriString); byte[] payload = new byte[0]; var requestToast = (HttpWebRequest)WebRequest.Create(uri); requestToast.Method = "POST"; requestToast.ContentType = "text/xml"; payload = GetToastPayload("Friend request:", clientDB.FirstName + " " + clientDB.LastName); requestToast.Headers.Add("X-WindowsPhone-Target", "toast"); requestToast.Headers.Add("X-NotificationClass", "2"); requestToast.ContentLength = payload.Length; using (Stream requestStream = requestToast.GetRequestStream()) { requestStream.Write(payload, 0, payload.Length); } try { var responseToast = (HttpWebResponse)requestToast.GetResponse(); notificationStatusToast = responseToast.Headers["X-NotificationStatus"]; notificationChannelStatusToast = responseToast.Headers["X-SubscriptionStatus"]; deviceConnectionStatusToast = responseToast.Headers["X-DeviceConnectionStatus"]; //return "Friend have been deleted " + friendDB.FirstName + " " + friendDB.LastName + "."; } catch (Exception ex) { }//return "Failed to send delete notification. Exception: " + ex.Message; } } return friendDB.FirstName + " " + friendDB.LastName + " have been deleted."; } else return "Friend doesn't have any device."; }
public void CompleteFriendshipRequest(string clientUsername, string friendUsername, bool status) { string notificationStatus = ""; string notificationChannelStatus = ""; string deviceConnectionStatus = ""; Client clientDB, friendDB; using (EmergencyDBEntities context = new EmergencyDBEntities()) { clientDB = (from c in context.Clients where clientUsername == c.Username select c).FirstOrDefault(); } using (EmergencyDBEntities context = new EmergencyDBEntities()) { friendDB = (from c in context.Clients where friendUsername == c.Username select c).FirstOrDefault(); } using (EmergencyDBEntities context = new EmergencyDBEntities()) { byte[] payload = new byte[0]; if (status) { foreach (var friendToUpdate in context.Friendships) { if (friendToUpdate.ClientID == friendDB.ClientID && friendToUpdate.FriendID == clientDB.ClientID && friendToUpdate.Status == false) friendToUpdate.Status = status; } context.Friendships.AddObject(new Friendship() { ClientID = clientDB.ClientID, FriendID = friendDB.ClientID, Status = true }); context.SaveChanges(); payload = GetToastPayload("Friend request confirmed by:", clientDB.FirstName + " " + clientDB.LastName); } else { context.Friendships.DeleteObject((from f in context.Friendships where f.ClientID == friendDB.ClientID && f.FriendID == clientDB.ClientID && f.Status == status select f).FirstOrDefault()); context.SaveChanges(); payload = GetToastPayload("Friend request disconfirmed by:", clientDB.FirstName + " " + clientDB.LastName); } List<string> uriStrings; using (EmergencyDBEntities context2 = new EmergencyDBEntities()) { uriStrings = (from d in context2.Devices where d.ClientID == friendDB.ClientID select d.DeviceUrl).ToList<string>(); } foreach (var uriString in uriStrings) { Uri uri = new Uri(uriString); var requestToast = (HttpWebRequest)WebRequest.Create(uri); requestToast.Method = "POST"; requestToast.ContentType = "text/xml"; requestToast.Headers.Add("X-WindowsPhone-Target", "toast"); requestToast.Headers.Add("X-NotificationClass", "2"); requestToast.ContentLength = payload.Length; using (Stream requestStream = requestToast.GetRequestStream()) { requestStream.Write(payload, 0, payload.Length); } try { var response = (HttpWebResponse)requestToast.GetResponse(); notificationStatus = response.Headers["X-NotificationStatus"]; notificationChannelStatus = response.Headers["X-SubscriptionStatus"]; deviceConnectionStatus = response.Headers["X-DeviceConnectionStatus"]; } catch (Exception) { } } } }
public string ClientRegistration(ClientData client, string password) { using (EmergencyDBEntities context = new EmergencyDBEntities()) { if ((from c in context.Clients where c.Username == client.Username select c).FirstOrDefault() != null) { return "User with current username is already exist. Please choose another username."; } else if ((from c in context.Clients where c.Email == client.Email select c).FirstOrDefault() != null) { return "User with current email is already exist. Please enter another email address."; } else { context.Clients.AddObject(new Client { Username = client.Username, Password = password, Email = client.Email, FirstName = client.FirstName, LastName = client.LastName, Age = client.Age }); context.SaveChanges(); return "Your account is successfully registered."; } } }
public ClientData ClientLogin(string username, string password) { using (EmergencyDBEntities context = new EmergencyDBEntities()) { var client = (from c in context.Clients where c.Username == username && c.Password == password select new ClientData { Username = c.Username, FirstName = c.FirstName, LastName = c.LastName, Age = c.Age.Value, Email = c.Email }).FirstOrDefault(); if (client != null) return client; } return null; }
public List<ClientData> CheckFriendshipsStatus(string clientUsername) { Client clientDB, friendDB; List<Friendship> isAlreadyFriends; List<ClientData> friendsDB = new List<ClientData>(); using (EmergencyDBEntities context = new EmergencyDBEntities()) { clientDB = (from c in context.Clients where clientUsername == c.Username select c).FirstOrDefault(); } using (EmergencyDBEntities context = new EmergencyDBEntities()) { isAlreadyFriends = (from f in context.Friendships where f.FriendID == clientDB.ClientID && f.Status == false select f).ToList<Friendship>(); } foreach (var isAlreadyFriend in isAlreadyFriends) { using (EmergencyDBEntities context = new EmergencyDBEntities()) { friendDB = (from c in context.Clients where c.ClientID == isAlreadyFriend.ClientID select c).FirstOrDefault(); } friendsDB.Add(new ClientData { Username = friendDB.Username, FirstName = friendDB.FirstName, LastName = friendDB.LastName, Email = friendDB.Email, Age = friendDB.Age.Value }); } return friendsDB; }