async private Task LoadMessagesFromServer() //Function to create a Json object and send to server using a lambda function { try { UserInfoItem message = new UserInfoItem { Item = new MessageLst { ToFrom = Helpers.Settings.ToFromArn, Time = Constants.date.ToString("yyyy-MM-dd HH:mm:ss:ffff") } }; //Helpers.Settings.EndpointArnSetting UserInfoJson messageJson = new UserInfoJson { operation = "read", tableName = "Message", payload = message }; string args = JsonConvert.SerializeObject(messageJson); var ir = new InvokeRequest() { FunctionName = Constants.AWSLambdaID, PayloadStream = AWSSDKUtils.GenerateMemoryStreamFromString(args), InvocationType = InvocationType.RequestResponse }; InvokeResponse resp = await AmazonUtils.LambdaClient.InvokeAsync(ir); resp.Payload.Position = 0; var sr = new StreamReader(resp.Payload); var myStr = sr.ReadToEnd(); try { System.Diagnostics.Debug.WriteLine("Returned Messages : " + myStr); var response = JsonConvert.DeserializeObject<ConversationResponse>(myStr); if (response.Success == "true") { var messages = response.MessageList; messagesFromServer(messages); } } catch (Exception e) { } } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Error:" + e); } }
/** * Generates or updates user account when the page is accessed or information is changed. * Sends request to server which updates the user table. */ async private Task Handshake() { try { string operation; if (!Helpers.Settings.HandShakeDone) { operation = "create"; Helpers.Settings.HandShakeDone = true; } else { operation = "update"; } UserInfoItem message; if (Helpers.Settings.IsVolunteer) { if(Helpers.Settings.ChatActiveSetting){ message = new UserInfoItem { Item = new UserInfo { Latitude = latitude, Longitude = longitude, Nickname = Helpers.Settings.UsernameSetting, Arn = Helpers.Settings.EndpointArnSetting, Available = true } }; }else{ message = new UserInfoItem { Item = new UserInfo { Latitude = latitude, Longitude = longitude, Nickname = Helpers.Settings.UsernameSetting, Arn = Helpers.Settings.EndpointArnSetting, Available = false } }; } } else{ message = new UserInfoItem { Item = new UserInfo { Latitude = latitude, Longitude = longitude, Nickname = this.nameEntry.Text, Arn = Helpers.Settings.EndpointArnSetting, Available = true } }; //Helpers.Settings.EndpointArnSetting } Helpers.Settings.UsernameSetting = this.nameEntry.Text; UserInfoJson messageJson = new UserInfoJson { operation = operation, tableName = "User", payload = message }; string args = JsonConvert.SerializeObject(messageJson); var ir = new InvokeRequest() { FunctionName = Constants.AWSLambdaID, PayloadStream = AWSSDKUtils.GenerateMemoryStreamFromString(args), InvocationType = InvocationType.RequestResponse }; InvokeResponse resp = await AmazonUtils.LambdaClient.InvokeAsync(ir); resp.Payload.Position = 0; var sr = new StreamReader(resp.Payload); var myStr = sr.ReadToEnd(); // System.Diagnostics.Debug.WriteLine("Status code: " + resp.StatusCode); // System.Diagnostics.Debug.WriteLine("Response content: " + myStr); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Error:" + e); } }
/** * Called when the end conversation button is pressed by the user * Generates request to server, which then terminates the conversation * for both users. */ async private Task terminateConversation() { //ID = is from ToFrom Field UserInfoItem message = new UserInfoItem { Item = new UserInfo { ID = Helpers.Settings.ToFromArn } }; //Helpers.Settings.EndpointArnSetting UserInfoJson messageJson = new UserInfoJson { operation = "stopConversation", tableName = "User", payload = message }; string args = JsonConvert.SerializeObject(messageJson); System.Diagnostics.Debug.WriteLine("stopConversation: " + args); var ir = new InvokeRequest() { FunctionName = Constants.AWSLambdaID, PayloadStream = AWSSDKUtils.GenerateMemoryStreamFromString(args), InvocationType = InvocationType.RequestResponse }; InvokeResponse resp = await AmazonUtils.LambdaClient.InvokeAsync(ir); resp.Payload.Position = 0; var sr = new StreamReader(resp.Payload); var myStr = sr.ReadToEnd(); System.Diagnostics.Debug.WriteLine("stopConversation : " + myStr); Helpers.Settings.ToFromArn = ""; Constants.conv.name = ""; Constants.conv.id = ""; }
/** * Queries the server for active conversations, called when the page is loaded * if there exists an active conversation, returns conversation information to * user and activates continue conversation option. */ async private Task queryServerActiveConversation() { UserInfoItem message = new UserInfoItem { Item = new UserInfo {} }; //Helpers.Settings.EndpointArnSetting UserInfoJson messageJson = new UserInfoJson { operation = "currentConversations", tableName = "User", payload = message }; string args = JsonConvert.SerializeObject(messageJson); System.Diagnostics.Debug.WriteLine("currentConversation: " + args); var ir = new InvokeRequest() { FunctionName = Constants.AWSLambdaID, PayloadStream = AWSSDKUtils.GenerateMemoryStreamFromString(args), InvocationType = InvocationType.RequestResponse }; InvokeResponse resp = await AmazonUtils.LambdaClient.InvokeAsync(ir); resp.Payload.Position = 0; var sr = new StreamReader(resp.Payload); var myStr = sr.ReadToEnd(); Boolean failure = false; try { var response = JsonConvert.DeserializeObject<ConversationResponse>(myStr); System.Diagnostics.Debug.WriteLine("currentConversations : " + myStr); if (response.Success == "true") { if (response.Conversations[0].ID != Helpers.Settings.ToFromArn) { Constants.conv.name = response.Conversations[0].Nickname; Constants.conv.id = response.Conversations[0].ID; Helpers.Settings.ToFromArn = response.Conversations[0].ID; Constants.conv.msgs.Clear(); } else { Constants.conv.name = response.Conversations[0].Nickname; Constants.conv.id = response.Conversations[0].ID; } continueConversationPath(); } else if (response.Success == "false") { Helpers.Settings.ToFromArn = ""; startConversationPath(); } else { await DisplayAlert("Error", "No active conversations.", "OK"); } } catch (Exception e) { System.Diagnostics.Debug.WriteLine(e); failure = true; } if (failure) { await DisplayAlert("Error", "No active conversations", "OK"); } }
/** * Called when the start button is tapped, generates request to server * checks response and either actives enter conversation mode or prompts * user with failed to start message. */ async private Task initiateConversation(){ UserInfoItem message = new UserInfoItem { Item = new UserInfo { } }; //Helpers.Settings.EndpointArnSetting UserInfoJson messageJson = new UserInfoJson { operation = "newConversation", tableName = "User", payload = message }; string args = JsonConvert.SerializeObject(messageJson); System.Diagnostics.Debug.WriteLine("newConversation: " + args); var ir = new InvokeRequest() { FunctionName = Constants.AWSLambdaID, PayloadStream = AWSSDKUtils.GenerateMemoryStreamFromString(args), InvocationType = InvocationType.RequestResponse }; InvokeResponse resp = await AmazonUtils.LambdaClient.InvokeAsync(ir); resp.Payload.Position = 0; var sr = new StreamReader(resp.Payload); var myStr = sr.ReadToEnd(); Boolean failure = false; try { System.Diagnostics.Debug.WriteLine("newConversations : " + myStr); var response = JsonConvert.DeserializeObject<ConversationResponse>(myStr); if (response.Success == "true") { Constants.conv.name = response.Nickname; Constants.conv.id = response.ID; Constants.conv.msgs.Clear(); Helpers.Settings.ToFromArn = response.ID; continueConversationPath(); var stack = Navigation.NavigationStack; if (stack[stack.Count - 1].GetType() != typeof(ChatPage)) await Navigation.PushAsync(new ChatPage(), false); } else { await DisplayAlert("Error", "No available volunteers, please try again later.", "OK"); } } catch(Exception e) { failure = true; } if (failure) { await DisplayAlert("Error", "No available volunteers, please try again later.", "OK"); } }