Bläddra i källkod

Merge branch 'feature/prepare' into feature/chatten

Conflicts:
	Proftaak Remote Healthcare/FietsClientV2/TCPConnection.cs
Bart Reedijk 10 år sedan
förälder
incheckning
7a107d99b4

+ 1 - 0
.gitignore

@@ -210,3 +210,4 @@ FakesAssemblies/
 GeneratedArtifacts/
 _Pvt_Extensions/
 ModelManifest.xml
+Proftaak Remote Healthcare/FietsClientV2/TCPConnection.cs

+ 6 - 1
Proftaak Remote Healthcare/FietsClientV2/CurrentData.cs

@@ -7,7 +7,7 @@ using System.Threading.Tasks;
 
 namespace FietsClient
 {
-    class CurrentData
+    public class CurrentData
     {
         private string userID;
         private List<Session> testResult;
@@ -33,5 +33,10 @@ namespace FietsClient
         {
             testResult.Add(s);
         }
+
+        public void SetMeasurment(Measurement measurment)
+        {
+            testResult.Last().AddMeasurement(measurment);
+        }
     }
 }

+ 95 - 0
Proftaak Remote Healthcare/FietsClientV2/DoctorModel.cs

@@ -0,0 +1,95 @@
+using System;
+using System.Collections.Generic;
+using System.ComponentModel;
+using System.Linq;
+using System.Text;
+using System.Threading;
+using System.Threading.Tasks;
+using System.Windows.Forms.DataVisualization.Charting;
+
+namespace FietsClient
+{
+    class DoctorModel
+    {
+
+        private static DoctorModel _doctorModel;
+        public DoctorForm doctorform { get; set; }
+
+        public static DoctorModel doctorModel { get { return _doctorModel ?? (_doctorModel = new DoctorModel()); } }
+        public TcpConnection tcpConnection { private get; set; }
+        private Thread receiveDataLoop;
+
+        public DoctorModel()
+        {
+
+        }
+
+        public void startAskingData()
+        {
+            receiveDataLoop = new Thread(() => receiveDataThreadLoop());
+            receiveDataLoop.Start();
+        }
+
+        private void receiveDataThreadLoop()
+        {
+            while (true)
+            {
+                Thread.Sleep(1000);
+                //receive data and display in through handle bike data
+                HandleBikeData(tcpConnection.currentData.GetSessions().Last().GetLastMeasurement());
+            }
+        }
+
+        //display values
+        private List<DataPoint> speedPoints = new List<DataPoint>();
+        private List<DataPoint> bpmPoints = new List<DataPoint>();
+        private List<DataPoint> rpmPoints = new List<DataPoint>();
+        private void HandleBikeData(Measurement data)
+        {
+            if (doctorform.InvokeRequired)
+            {
+                doctorform.Invoke((new Action(() => HandleBikeData(data))));
+            }
+            else
+            {
+                //fill fields
+                doctorform.pulseBox.Text = data.pulse.ToString();
+                doctorform.rpmInfoBox.Text = data.rpm.ToString();
+                doctorform.speedInfoBox.Text = data.speed.ToString();
+                doctorform.distanceInfoBox.Text = data.distance.ToString();
+                doctorform.requestedBox.Text = data.requestedPower.ToString();
+                doctorform.energyInfoBox.Text = data.energy.ToString();
+                doctorform.timeBox.Text = data.time.ToString() ;
+                doctorform.actualBox.Text = data.actualPower.ToString();
+
+                //fill graph speed
+                speedPoints.Add(new DataPoint(Convert.ToDateTime(data.time.ToString()).ToOADate(), Convert.ToDouble(data.speed.ToString())));
+                doctorform.speedChart.Series[0].Points.Clear();
+                for (int i = 0; i < speedPoints.Count; i++)
+                    doctorform.speedChart.Series[0].Points.Add(speedPoints[i]);
+                if (speedPoints.Count > 25)
+                    speedPoints.RemoveAt(0);
+                doctorform.speedChart.Update();
+
+                //fill graph pulse
+                bpmPoints.Add(new DataPoint(Convert.ToDateTime(data.time.ToString()).ToOADate(), Convert.ToDouble(data.pulse.ToString())));
+                doctorform.bpmChart.Series[0].Points.Clear();
+                for (int i = 0; i < bpmPoints.Count; i++)
+                    doctorform.bpmChart.Series[0].Points.Add(bpmPoints[i]);
+                if (bpmPoints.Count > 25)
+                    bpmPoints.RemoveAt(0);
+                doctorform.speedChart.Update();
+
+                //fill graph rpm
+                rpmPoints.Add(new DataPoint(Convert.ToDateTime(data.time.ToString()).ToOADate(), Convert.ToDouble(data.rpm.ToString())));
+                doctorform.rpmChart.Series[0].Points.Clear();
+                for (int i = 0; i < rpmPoints.Count; i++)
+                    doctorform.rpmChart.Series[0].Points.Add(rpmPoints[i]);
+                if (rpmPoints.Count > 25)
+                    rpmPoints.RemoveAt(0);
+                doctorform.rpmChart.Update();
+            }
+
+        }
+    }
+}

+ 1 - 0
Proftaak Remote Healthcare/FietsClientV2/FietsClient.csproj

@@ -89,6 +89,7 @@
       <DependentUpon>PatientForm.cs</DependentUpon>
     </Compile>
     <Compile Include="lib\JsonConverter.cs" />
+    <Compile Include="DoctorModel.cs" />
     <Compile Include="PatientModel.cs" />
     <Compile Include="Program.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />

Filskillnaden har hållts tillbaka eftersom den är för stor
+ 410 - 419
Proftaak Remote Healthcare/FietsClientV2/Forms/DoctorForm.Designer.cs


+ 40 - 1
Proftaak Remote Healthcare/FietsClientV2/Forms/DoctorForm.cs

@@ -12,14 +12,53 @@ namespace FietsClient
 {
     public partial class DoctorForm : Form
     {
-        public DoctorForm()
+        private TcpConnection connection;
+        private DoctorModel doctorModel;
+
+        public DoctorForm(TcpConnection connection)
         {
+            this.connection = connection;
             InitializeComponent();
+            doctorModel = DoctorModel.doctorModel;
+            doctorModel.doctorform = this;
+            doctorModel.tcpConnection = connection;
+            DataHandler.IncomingErrorEvent += HandleError;
+        }
+
+        private void HandleError(string error)
+        {
+            switch (error)
+            {
+                default:
+                    break;
+            }
         }
 
         private void Form1_Load(object sender, EventArgs e)
         {
 
         }
+
+        private void setDistanceButton_Click(object sender, EventArgs e)
+        {
+            int distance;
+            Int32.TryParse(setDistanceBox.Text,out distance);
+            connection.SendDistance(distance);
+        }
+
+        private void setTimeButton_Click(object sender, EventArgs e)
+        {
+            int minutes,seconds;
+            Int32.TryParse(setTimeMinutesBox.Text, out minutes);
+            Int32.TryParse(setTimeSecondsBox.Text, out seconds);
+            connection.SendTime(minutes, seconds);
+        }
+
+        private void setPowerButton_Click(object sender, EventArgs e)
+        {
+            int power; 
+            Int32.TryParse(setPowerBox.Text, out power);
+            connection.SendPower(power);
+        }
     }
 }

+ 10 - 15
Proftaak Remote Healthcare/FietsClientV2/Forms/Login.cs

@@ -21,27 +21,26 @@ namespace FietsClient
         [DllImport("User32.dll")]
         public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
 
-        public Login(TcpConnection conn)
+        public Login(TcpConnection connection)
         {
-            connection = conn;
+            this.connection = connection;
             InitializeComponent();
         }
 
-        public Login(String message)
+        public Login(string message)
         {
             InitializeComponent();
             errorLBL.Text = message;
         }
 
-
-        public void setError(String message)
+        public void setError(string message)
         {
             errorLBL.Text = message;
         }
 
         private void SubmitButton_Click(object sender, EventArgs e)
         {
-            if(string.IsNullOrWhiteSpace(UsernameBox.Text))
+            if (string.IsNullOrWhiteSpace(UsernameBox.Text))
             {
                 errorLBL.Text = "Username is incorrect";
             }
@@ -77,26 +76,22 @@ namespace FietsClient
 
         private void checkConnection()
         {
-            if (!connection.isConnected())
-            {
+            if (!connection.isConnectedFlag)
                 connLBL.Text = "No Connection established";
-            }
             else
-            {
                 connLBL.Text = "";
-            }
         }
 
         private void reconnectBTN_Click(object sender, EventArgs e)
         {
-            if(!connection.isConnected())
+            if (!connection.isConnectedFlag)
             {
                 connection.connect();
                 checkConnection();
                 this.Refresh();
             }
-            
+
         }
     }
-    
-}
+
+}

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

@@ -20,7 +20,7 @@ namespace FietsClient
         private DataHandler dataHandler;
         private Thread workerThread;
 
-        private String powerLog;
+        private string powerLog;
 
         public PatientModel()
         {
@@ -54,7 +54,7 @@ namespace FietsClient
                 {
                     dataHandler.sendData(DataHandler.STATUS);
                 }
-                catch (Exception e)
+                catch (Exception)
                 {
                     dataHandler.closeComm();
                 }

+ 2 - 5
Proftaak Remote Healthcare/FietsClientV2/Program.cs

@@ -12,13 +12,10 @@ namespace FietsClient
         [STAThread]
         static void Main()
         {
-
-            
-            
             Application.EnableVisualStyles();
             Application.SetCompatibleTextRenderingDefault(false);
-            Application.Run(new Login(new TcpConnection()));
-            //Application.Run(new PatientForm());
+            TcpConnection tcpConnection = new TcpConnection();
+            Application.Run(new Login(tcpConnection));
         }
     }
 }

+ 2 - 2
Proftaak Remote Healthcare/FietsClientV2/Resources/PatientForm.Designer.cs

@@ -348,7 +348,7 @@ namespace FietsClient
             // bpmBox
             // 
             this.bpmBox.Controls.Add(this.bpmChart);
-            this.bpmBox.Location = new System.Drawing.Point(16, 422);
+            this.bpmBox.Location = new System.Drawing.Point(16, 421);
             this.bpmBox.Margin = new System.Windows.Forms.Padding(4);
             this.bpmBox.Name = "bpmBox";
             this.bpmBox.Padding = new System.Windows.Forms.Padding(4);
@@ -688,7 +688,7 @@ namespace FietsClient
             // rpmBox
             // 
             this.rpmBox.Controls.Add(this.rpmChart);
-            this.rpmBox.Location = new System.Drawing.Point(557, 422);
+            this.rpmBox.Location = new System.Drawing.Point(557, 421);
             this.rpmBox.Margin = new System.Windows.Forms.Padding(4);
             this.rpmBox.Name = "rpmBox";
             this.rpmBox.Padding = new System.Windows.Forms.Padding(4);

+ 48 - 33
Proftaak Remote Healthcare/FietsClientV2/TCPConnection.cs

@@ -13,23 +13,22 @@ namespace FietsClient
     public class TcpConnection
     {
         public TcpClient client;
+        public bool isConnectedFlag { private set; get; }
         private NetworkStream serverStream;
-        private CurrentData currentData;
+        public CurrentData currentData { private set; get; }
         private string userID;
-        private bool isConnectedFlag;
+        private Thread receiveThread;
 
         public delegate void ChatmassegeDelegate(string[] data);
         public event ChatmassegeDelegate IncomingChatmessageEvent;
 
         public TcpConnection()
         {
-            // create a connection
             client = new TcpClient();
-
             connect();
         }
 
-        private void onIncomingChatMessage(string[] data)
+	private void onIncomingChatMessage(string[] data)
         {
             ChatmassegeDelegate cMD = IncomingChatmessageEvent;
             if (cMD != null)
@@ -51,8 +50,8 @@ namespace FietsClient
 
                     // create streams
                     serverStream = client.GetStream();
-                    Thread t = new Thread(recieve);
-                    t.Start();
+                    receiveThread = new Thread(receive);
+                    receiveThread.Start();
                     isConnectedFlag = true;
                 }
                 catch (Exception ex)
@@ -63,46 +62,48 @@ namespace FietsClient
                 }
         }
 
-
-        public void recieve ()
+        public void receive()
         {
             while (true)
             {
                 byte[] bytesFrom = new byte[(int)client.ReceiveBufferSize];
                 serverStream.Read(bytesFrom, 0, (int)client.ReceiveBufferSize);
-                String response = Encoding.ASCII.GetString(bytesFrom);
-                String[] response_parts = response.Split('|');
+                string response = Encoding.ASCII.GetString(bytesFrom);
+                string[] response_parts = response.Split('|');
 
                 if (response_parts.Length > 0)
                 {
                     switch (response_parts[0])
                     {
-                        case "0":   //login
+                        case "0":   //login and display correct window after login
                             if (response_parts.Length == 4)
                             {
                                 if (response_parts[1] == "1" && response_parts[2] == "1")
                                 {
-                                    new DoctorForm().Show();
+                                    
+                                    Form activeForm = Form.ActiveForm;
+                                    activeForm.Invoke((MethodInvoker)delegate ()
+                                    {
+                                        DoctorForm doctorForm = new DoctorForm(this);
+					activeForm.Hide();
+                                        doctorForm.Show();
+                                    });
                                     currentData = new CurrentData(userID);
                                 }
-                                else if(response_parts[2] == "0" && response_parts[1] == "1")
+                                else if (response_parts[2] == "0" && response_parts[1] == "1")
                                 {
-                                    PatientForm form = new PatientForm(this);
+                                    
                                     Form activeForm = Form.ActiveForm;
-
-                                    activeForm.Invoke((MethodInvoker)delegate () {
+                                    activeForm.Invoke((MethodInvoker)delegate ()
+                                    {
+                                        PatientForm patientForm = new PatientForm(this);
                                         activeForm.Hide();
-                                        form.Show();
+                                        patientForm.Show();
                                     });
-                                    
-
-
                                     currentData = new CurrentData(userID);
                                 }
                                 else
-                                {
                                     new Login("Geen gebruiker gevonden");
-                                }
                             }
                             break;
                         case "1":
@@ -124,14 +125,12 @@ namespace FietsClient
             // send command ( cmdID | username | password )
             this.userID = username;
             SendString("0|" + username + "|" + password + "|");
-
         }
 
         public void SendGet(int GetWhat)
         {
             // send command ( cmdID | username )
-            SendString( GetWhat + "|" + userID );
-
+            SendString(GetWhat + "|" + userID);
         }
 
         public void SendNewSession()
@@ -145,19 +144,35 @@ namespace FietsClient
             // send command ( cmdID | username )
             SendString("5|" + userID + lib.JsonConverter.SerializeLastMeasurement(currentData.GetSessions().Last().GetLastMeasurement()));
         }
+	
+	public void SendChatMessage(string message)
+        {
+            // send command ( cmdID | username sender | username patient | message )
+            string protocol = "6 | " + this.userID +" | "/* + idPatient */ + " | " + message;
+            SendString(protocol);
+        }
+
+	public void SendDistance(int distance)
+        {
+            SendString("10|" + userID + "|" + distance);
+        }
+	
+	public void SendTime(int Minutes, int seconds)
+        {
+            SendString("11|" + userID + "|" + Minutes + ":" + seconds);
+        }
 
-        public void SendString(string s)
+        public void SendPower(int power)
+        {
+            SendString("12|" + userID + "|" + power);
+        }
+	
+	public void SendString(string s)
         {
             byte[] b = Encoding.ASCII.GetBytes(s);
             serverStream.Write(b, 0, b.Length);
             serverStream.Flush();
         }
 
-        public void SendChatMessage(string message)
-        {
-            // send command ( cmdID | username sender | username patient | message )
-            string protocol = "6 | " + this.userID +" | "/* + idPatient */ + " | " + message;
-            SendString(protocol);
-        }
     }
 }

+ 196 - 0
Proftaak Remote Healthcare/FietsClientV2/TCPConnection.cs.orig

@@ -0,0 +1,196 @@
+using FietsClient.JSONObjecten;
+using Newtonsoft.Json;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Net.Sockets;
+using System.Text;
+using System.Threading;
+using System.Windows.Forms;
+
+namespace FietsClient
+{
+    public class TcpConnection
+    {
+        public TcpClient client;
+        public bool isConnectedFlag { private set; get; }
+        private NetworkStream serverStream;
+        public CurrentData currentData { private set; get; }
+        private string userID;
+        private Thread receiveThread;
+
+        public delegate void ChatmassegeDelegate(string[] data);
+        public event ChatmassegeDelegate IncomingChatmessageEvent;
+
+        public TcpConnection()
+        {
+            client = new TcpClient();
+            connect();
+        }
+
+<<<<<<< HEAD
+        private void onIncomingChatMessage(string[] data)
+        {
+            ChatmassegeDelegate cMD = IncomingChatmessageEvent;
+            if (cMD != null)
+            {
+                cMD(data);
+            }
+        }
+
+        public bool isConnected()
+        {
+            return isConnectedFlag;
+        }
+
+        public void connect()
+        {
+                try
+                {
+                    client.Connect("127.0.0.1", 1288);
+
+                    // create streams
+                    serverStream = client.GetStream();
+                    Thread t = new Thread(recieve);
+                    t.Start();
+                    isConnectedFlag = true;
+                }
+                catch (Exception ex)
+                {
+                    Console.WriteLine(ex);
+                    Thread.Sleep(1000);
+                    isConnectedFlag = false;
+                }
+=======
+        public void connect()
+        {
+            try
+            {
+                client.Connect("127.0.0.1", 1288);
+                // create streams
+                serverStream = client.GetStream();
+                receiveThread = new Thread(receive);
+                receiveThread.Start();
+                isConnectedFlag = true;
+            }
+            catch (Exception)
+            {
+                Thread.Sleep(1000);
+                isConnectedFlag = false;
+            }
+>>>>>>> feature/prepare
+        }
+
+        public void receive()
+        {
+            while (true)
+            {
+                byte[] bytesFrom = new byte[(int)client.ReceiveBufferSize];
+                serverStream.Read(bytesFrom, 0, (int)client.ReceiveBufferSize);
+                string response = Encoding.ASCII.GetString(bytesFrom);
+                string[] response_parts = response.Split('|');
+
+                if (response_parts.Length > 0)
+                {
+                    switch (response_parts[0])
+                    {
+                        case "0":   //login and display correct window after login
+                            if (response_parts.Length == 4)
+                            {
+                                if (response_parts[1] == "1" && response_parts[2] == "1")
+                                {
+                                    DoctorForm doctorForm = new DoctorForm(this);
+                                    Form activeForm = Form.ActiveForm;
+                                    activeForm.Invoke((MethodInvoker)delegate ()
+                                    {
+                                        activeForm.Hide();
+                                        doctorForm.Show();
+                                    });
+                                    currentData = new CurrentData(userID);
+                                }
+                                else if (response_parts[2] == "0" && response_parts[1] == "1")
+                                {
+                                    
+                                    Form activeForm = Form.ActiveForm;
+                                    activeForm.Invoke((MethodInvoker)delegate ()
+                                    {
+                                        PatientForm patientForm = new PatientForm(this);
+                                        activeForm.Hide();
+                                        patientForm.Show();
+                                    });
+                                    currentData = new CurrentData(userID);
+                                }
+                                else
+                                    new Login("Geen gebruiker gevonden");
+                            }
+                            break;
+                        case "1":
+                            currentData.setSessionList(JsonConvert.DeserializeObject<List<Session>>(response_parts[1]));
+                            break;
+                        case "2":
+                            currentData.GetSessions().Last().AddMeasurement(JsonConvert.DeserializeObject<Measurement>(response_parts[1]));
+                            break;
+                        case "7":
+                            string[] data = { response_parts[1], response_parts[2], response_parts[3] };
+                            break;
+                    }
+                }
+            }
+        }
+
+        public void SendLogin(string username, string password)
+        {
+            // send command ( cmdID | username | password )
+            this.userID = username;
+            SendString("0|" + username + "|" + password + "|");
+        }
+
+        public void SendGet(int GetWhat)
+        {
+            // send command ( cmdID | username )
+            SendString(GetWhat + "|" + userID);
+        }
+
+        public void SendNewSession()
+        {
+            // send command ( cmdID | username )
+            SendString("3|" + userID + lib.JsonConverter.SerializeSession(currentData.GetSessions().Last()));
+        }
+
+        public void SendNewMeasurement()
+        {
+            // send command ( cmdID | username )
+            SendString("5|" + userID + lib.JsonConverter.SerializeLastMeasurement(currentData.GetSessions().Last().GetLastMeasurement()));
+        }
+
+        public void SendString(string s)
+        {
+            byte[] b = Encoding.ASCII.GetBytes(s);
+            serverStream.Write(b, 0, b.Length);
+            serverStream.Flush();
+        }
+
+<<<<<<< HEAD
+        public void SendChatMessage(string message)
+        {
+            // send command ( cmdID | username sender | username patient | message )
+            string protocol = "6 | " + this.userID +" | "/* + idPatient */ + " | " + message;
+            SendString(protocol);
+=======
+        public void SendDistance(int distance)
+        {
+            SendString("10|" + userID + "|" + distance);
+        }
+
+        public void SendTime(int Minutes, int seconds)
+        {
+            SendString("11|" + userID + "|" + Minutes + ":" + seconds);
+        }
+
+        public void SendPower(int power)
+        {
+            SendString("12|" + userID + "|" + power);
+>>>>>>> feature/prepare
+        }
+    }
+}

+ 2 - 0
Proftaak Remote Healthcare/Server/AppGlobal.cs

@@ -13,6 +13,7 @@ namespace Server
         private static AppGlobal _instance;
 
         private List<User> users;
+        public List<Client> Clients;
 
         public static AppGlobal Instance
         {
@@ -22,6 +23,7 @@ namespace Server
         public AppGlobal() 
         {
             users = new List<User>();
+            Clients = new List<Client>();
             users.Add(new User("no", "no", 0, false, 0));
             users.Add(new User("JK123", "jancoow", 5, true, 100));
             users.Add(new User("TOM", "tommie", 80, false, 77, true));

+ 4 - 3
Proftaak Remote Healthcare/Server/Client.cs

@@ -17,11 +17,11 @@ namespace Server
         private readonly AppGlobal _global;
         private int iduser;
 
-        public Client(TcpClient socket, AppGlobal global)
+        public Client(TcpClient socket)
         {
             client = socket;
             networkStream = client.GetStream();
-            _global = global;
+            _global = AppGlobal.Instance;
             iduser = -1;
             Console.WriteLine("New client connected");
             Thread t = new Thread(recieve);
@@ -48,7 +48,8 @@ namespace Server
                                 _global.CheckLogin(response_parts[1], response_parts[2], out admin, out id);
                                 if (id > -1)
                                 {
-                                    if(_global.GetUsers().First(item => item.id == response_parts[1]).isDoctor)
+                                    this.iduser = id;
+                                    if (_global.GetUsers().First(item => item.id == response_parts[1]).isDoctor)
                                     {
                                         sendString("0|1|1|");   // Doctor
                                     }

+ 7 - 2
Proftaak Remote Healthcare/Server/Program.cs

@@ -5,27 +5,32 @@ using System.Text;
 using System.IO;
 using System.Net;
 using Server;
+using System.Collections.Generic;
 
 namespace Server
 {
     class Program
     {
+
+
         static void Main(string[] args)
         {
             Console.WriteLine("Server gestart");
 
+            //zorg dat AppGlobal bestaat...
+            AppGlobal.Instance.ToString();
             TcpListener serverSocket = new TcpListener(1288);
             serverSocket.Start();
 
             while (true)
             {
                 Console.WriteLine("Waiting for clients..");
-               new Client(serverSocket.AcceptTcpClient(), AppGlobal.Instance);
+                AppGlobal.Instance.Clients.Add(new Client(serverSocket.AcceptTcpClient()));
             }
 
             serverSocket.Stop();
             Console.WriteLine("Server afsluiten");
         }
     }
-  
+
 }

Vissa filer visades inte eftersom för många filer har ändrats