Эх сурвалжийг харах

Merge pull request #18 from bartreedijk/feature/Char_(Daniel)

Feature/char (daniel)
bartreedijk 10 жил өмнө
parent
commit
8ac9166145

+ 3 - 2
Proftaak Remote Healthcare/FietsClientV2/DoctorModel.cs

@@ -55,11 +55,12 @@ namespace FietsClient
             if (onlinePatients.Count != doctorSessions.Count || true)
             {
                 // ruim eerst alle doctorSessions op die niet van toepassing zijn
-                foreach (string str in doctorSessions.Keys)
+                List<string> temp = doctorSessions.Keys.ToList();
+                foreach (string str in temp)
                 {
                     if (!(onlinePatients.Any(s => str.Contains(s))))
                     {
-                        doctorform.RemoveSessionFromTabcontrol(str);
+                        doctorform.Invoke(new Action(() => doctorform.RemoveSessionFromTabcontrol(str)));
                         doctorform.Invoke(new Action(() => doctorSessions.Remove(str)));
                     }
                     //onlinePatients.Find(username => username.Equals(str));

+ 13 - 5
Proftaak Remote Healthcare/FietsClientV2/Forms/DoctorForm.cs

@@ -23,7 +23,8 @@ namespace FietsClient
             doctorModel.tcpConnection = connection;
             this.summaryUserControl = doctorSummaryUC1;
             DataHandler.IncomingErrorEvent += HandleError;
-            
+            connection.IncomingChatmessageEvent += new TcpConnection.ChatmassegeDelegate(printMessage);
+
         }
 
         private void HandleError(string error)
@@ -54,14 +55,21 @@ namespace FietsClient
             {
                 String[] data = new String[2];
                 data[0] = messageBox.Text;
-                data[1] = doctorModel.tcpConnection.currentData.GetUserID();
+                //current patient:
+                data[1] = doctorTabControl.SelectedTab.Name;
                 messageBox.Clear();
 
                 doctorModel.tcpConnection.SendChatMessage(data);
-            }
+            }  
+        }
 
-            // selecteer active patient:
-            string activePatient = doctorTabControl.SelectedTab.Name;
+        private void printMessage(string[] data)
+        {
+            string finalMessage = "\r\n" + data[0] + ":\t" + data[2];
+            chatBox.Invoke((MethodInvoker) delegate ()
+            {
+                chatBox.AppendText(finalMessage);
+            });
         }
 
         public void AddSessionToTabcontrol(string patientID)

+ 34 - 30
Proftaak Remote Healthcare/FietsClientV2/Forms/PatientForm.cs

@@ -17,21 +17,21 @@ namespace FietsClient
     public partial class PatientForm : Form
     {
         private TcpConnection _connection;
-        private PatientModel patienModel;
+        private PatientModel patientModel;
 
         public PatientForm(TcpConnection connection)
         {
             this._connection = connection;
             InitializeComponent();
-            patienModel = PatientModel.patientModel;
-            patienModel.patientform = this;
+            patientModel = PatientModel.patientModel;
+            patientModel.patientform = this;
             DataHandler.IncomingErrorEvent += HandleError; //initialize event
 
             _connection.IncomingChatmessageEvent += new TcpConnection.ChatmassegeDelegate(printMessage);
 
             //TIJDELIJK STUK CODE OM MESSAGE TE TESTEN
-            _connection.SendString("6|TOM|TOM|Je bent een homo");
-            Console.WriteLine("Bericht versturen");
+            //_connection.SendString("6|TOM|TOM|Je bent een homo");
+            //Console.WriteLine("Bericht versturen");
             //EINDE TESTCODE
         }
 
@@ -60,17 +60,17 @@ namespace FietsClient
 
         private void requestDataToolStripMenuItem_Click(object sender, EventArgs e)
         {
-            patienModel.startAskingData();
+            patientModel.startAskingData();
         }
 
         private void closePortToolStripMenuItem_Click(object sender, EventArgs e)
         {
-            patienModel.closeComPort();
+            patientModel.closeComPort();
         }
 
         private void openPortToolStripMenuItem_Click(object sender, EventArgs e)
         {
-            patienModel.startComPort(toolStripComboBox1.SelectedItem.ToString());
+            patientModel.startComPort(toolStripComboBox1.SelectedItem.ToString());
             requestDataToolStripMenuItem.Enabled = true;
             closePortToolStripMenuItem.Enabled = true;
         }
@@ -80,7 +80,7 @@ namespace FietsClient
             int n;
             if (int.TryParse(distanceBox.Text, out n))
             {
-                patienModel.setDistanceMode(distanceBox.Text);
+                patientModel.setDistanceMode(distanceBox.Text);
             }
             else
             {
@@ -97,7 +97,7 @@ namespace FietsClient
             if (isNumericM)
             {
                 if (isNumericS)
-                    patienModel.setTimeMode(minutes + ":" + seconds);
+                    patientModel.setTimeMode(minutes + ":" + seconds);
                 else MessageBox.Show("Minutes is not a valid number.");
             }
             else MessageBox.Show("Seconds is not a valid number.");
@@ -105,27 +105,27 @@ namespace FietsClient
 
         private void stopTrainingToolStripMenuItem_Click(object sender, EventArgs e)
         {
-            patienModel.reset();
+            patientModel.reset();
         }
 
         private void setPower_Click(object sender, EventArgs e)
         {
             int n;
             if (int.TryParse(powerBox.Text, out n))
-                patienModel.setPower(powerBox.Text);
+                patientModel.setPower(powerBox.Text);
             else
                 MessageBox.Show("Power is not a valid number.");
         }
 
         private void sendButton_Click(object sender, EventArgs e)
         {
-            if (messageBox.Text != null)
+            if (messageBox.Text != null && patientModel.CurrentDoctorID != "")
             {
                 String[] data = new String[2];
                 data[0] = messageBox.Text;
-                data[1] = _connection.currentData.GetUserID();
+                //receiver ID of doctor
+                data[1] = patientModel.CurrentDoctorID;
                 messageBox.Clear();
-
                 _connection.SendChatMessage(data);
             }
         }
@@ -163,39 +163,39 @@ namespace FietsClient
                         this.actualBox.Text = measurments[measurments.Count - 1].actualPower.ToString();
 
                         //fill speedpoints
-                        patienModel.speedPoints = new List<DataPoint>();
+                        patientModel.speedPoints = new List<DataPoint>();
                         for (int i = 0; i < measurments.Count; i++)
                         {
-                            patienModel.speedPoints.Add(new DataPoint(measurments[i].time, measurments[i].speed));
+                            patientModel.speedPoints.Add(new DataPoint(measurments[i].time, measurments[i].speed));
                         }
                         //fill speedgraph
                         this.speedChart.Series[0].Points.Clear();
-                        for (int i = 0; i < patienModel.speedPoints.Count; i++)
-                            this.speedChart.Series[0].Points.Add(patienModel.speedPoints[i]);
+                        for (int i = 0; i < patientModel.speedPoints.Count; i++)
+                            this.speedChart.Series[0].Points.Add(patientModel.speedPoints[i]);
                         this.speedChart.Update();
 
                         //fill bpm
-                        patienModel.bpmPoints = new List<DataPoint>();
+                        patientModel.bpmPoints = new List<DataPoint>();
                         for (int i = 0; i < measurments.Count; i++)
                         {
-                            patienModel.bpmPoints.Add(new DataPoint(measurments[i].time, measurments[i].bpm));
+                            patientModel.bpmPoints.Add(new DataPoint(measurments[i].time, measurments[i].bpm));
                         }
                         //fill bpmgraph
                         this.bpmChart.Series[0].Points.Clear();
-                        for (int i = 0; i < patienModel.bpmPoints.Count; i++)
-                            this.bpmChart.Series[0].Points.Add(patienModel.bpmPoints[i]);
+                        for (int i = 0; i < patientModel.bpmPoints.Count; i++)
+                            this.bpmChart.Series[0].Points.Add(patientModel.bpmPoints[i]);
                         this.bpmChart.Update();
 
                         //fill rpm
-                        patienModel.rpmPoints = new List<DataPoint>();
+                        patientModel.rpmPoints = new List<DataPoint>();
                         for (int i = 0; i < measurments.Count; i++)
                         {
-                            patienModel.rpmPoints.Add(new DataPoint(measurments[i].time, measurments[i].rpm));
+                            patientModel.rpmPoints.Add(new DataPoint(measurments[i].time, measurments[i].rpm));
                         }
                         //fill rpmgraph
                         this.rpmChart.Series[0].Points.Clear();
-                        for (int i = 0; i < patienModel.rpmPoints.Count; i++)
-                            this.rpmChart.Series[0].Points.Add(patienModel.rpmPoints[i]);
+                        for (int i = 0; i < patientModel.rpmPoints.Count; i++)
+                            this.rpmChart.Series[0].Points.Add(patientModel.rpmPoints[i]);
                         this.rpmChart.Update();
                     })
                 );
@@ -204,9 +204,13 @@ namespace FietsClient
         }
         private void printMessage(string[] data)
         {
-
-            string finalMessage = data[1] + ":\t\t" + data[3] + "\r\n";
-            chatBox.AppendText(finalMessage);
+            if (data[0] != _connection.userID)
+                patientModel.CurrentDoctorID = data[0];
+            string finalMessage = "\r\n" + data[0] + ":\t" + data[2];
+            chatBox.Invoke((MethodInvoker)delegate ()
+            {
+                chatBox.AppendText(finalMessage);
+            });
         }
 
         private void PatientForm_FormClosing(object sender, FormClosingEventArgs e)

+ 2 - 0
Proftaak Remote Healthcare/FietsClientV2/PatientModel.cs

@@ -22,6 +22,8 @@ namespace FietsClient
 
         private string powerLog;
 
+        public string CurrentDoctorID { get; set; }
+
         public PatientModel()
         {
             dataHandler = new DataHandler();

+ 24 - 22
Proftaak Remote Healthcare/FietsClientV2/TCPConnection.cs

@@ -46,7 +46,7 @@ namespace FietsClient
         {
                 try
                 {
-                    client.Connect("brdk.nl", 1288);
+                    client.Connect("127.0.0.1", 1288);
 
                     // create streams
                     serverStream = client.GetStream();
@@ -75,7 +75,7 @@ namespace FietsClient
             while (true)
             {
                 byte[] bytesFrom = new byte[(int)client.ReceiveBufferSize];
-                serverStream.Read(bytesFrom, 0, (int)client.ReceiveBufferSize);
+                serverStream.Read(bytesFrom, 0, client.ReceiveBufferSize);
                 string response = Encoding.ASCII.GetString(bytesFrom);
                 string[] response_parts = response.Split('|');
 
@@ -109,22 +109,25 @@ namespace FietsClient
                             if (currentData.isDoctor == true)
                             {
                                 Form activeForm = Form.ActiveForm;
-                                activeForm.Invoke((MethodInvoker) delegate()
-                                {
-                                    DoctorForm doctorForm = new DoctorForm(this);
-                                    activeForm.Hide();
-                                    doctorForm.Show();
-                                });
+                                activeForm.Invoke((MethodInvoker)delegate ()
+                               {
+                                   DoctorForm doctorForm = new DoctorForm(this);
+                                   activeForm.Hide();
+                                   doctorForm.Show();
+                               });
                             }
                             else
                             {
                                 Form activeForm = Form.ActiveForm;
-                                activeForm.Invoke((MethodInvoker)delegate ()
+                                if (activeForm != null)
                                 {
-                                    PatientForm patientForm = new PatientForm(this);
-                                    activeForm.Hide();
-                                    patientForm.Show();
-                                });
+                                    activeForm.Invoke((MethodInvoker)delegate ()
+                                    {
+                                        PatientForm patientForm = new PatientForm(this);
+                                        activeForm.Hide();
+                                        patientForm.Show();
+                                    });
+                                }
                             }
 
                             break;
@@ -132,14 +135,16 @@ namespace FietsClient
                             currentData.GetSessions().Last().AddMeasurement(JsonConvert.DeserializeObject<Measurement>(response_parts[1]));
                             break;
                         case "7":
+                            //                  sender              receiver            message
                             string[] data = { response_parts[1], response_parts[2], response_parts[3] };
-                            SendChatMessage(data);
+                            
+                            onIncomingChatMessage(data);
                             break;
                         case "8":
                             if (response_parts[1].TrimEnd('\0') != "-1")
                             {
                                 DoctorModel.doctorModel.onlinePatients = response_parts[1].TrimEnd('\0').Split('\t').ToList();
-                            } else if (response_parts[1] == "-1")
+                            } else if (response_parts[1].TrimEnd('\0') == "-1")
                             {
                                 DoctorModel.doctorModel.onlinePatients = new List<String>();
                             }
@@ -177,18 +182,15 @@ namespace FietsClient
 	
 	    public void SendChatMessage(string[] data)
         {
-            String receiverID = data[0];
+            String receiverID = data[1];
 
             if (currentData != null)
             {
-                if (currentData.GetUserID() == receiverID)
-                {
-                    String message = data[1];
+                    String message = data[0];
 
-                    // send command ( cmdID | username sender | username patient | message )
-                    string protocol = "6 | " + this.userID + " | " + receiverID + " | " + message;
+                    // send command ( cmdID | username sender | username receiverID | message )
+                    string protocol = "6|" + this.userID + "|" + receiverID + "|" + message;
                     SendString(protocol);
-                }
             }
         }
         public void SendGetActivePatients()

+ 6 - 2
Proftaak Remote Healthcare/Server/Client.cs

@@ -106,8 +106,12 @@ namespace Server
                                 String receiver = response_parts[2];
                                 String sender = response_parts[1];
 
-                                //bericht doorsturen naar alle actieve gebruikers (de Fietsclient zorgt ervoor dat alleen de geadresseerde het bericht kan zien)
-                                sendString("7|" + sender + "|" + receiver + "|" + message);      
+                                sendString("7|" + sender + "|" + receiver + "|" + message);
+                                foreach (var client in Program.Clients)
+                                {
+                                    if (client.username == receiver)
+                                        client.sendString("7|" + sender + "|" + receiver + "|" + message);
+                                }
                             }
                             break;
                         case "8": //alle online Patients sturen naar Doctorclient