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")
                                {
                                    
                                    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")
                                {
                                    
                                    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] };
                            SendChatMessage(data);
                            break;
                    }
                }
            }
        }
Beispiel #2
0
        public void receive()
        {
            while (client.Connected)
            {
                byte[] bytesFrom = new byte[(int)client.ReceiveBufferSize];
                try
                {
                    sslStream.Read(bytesFrom, 0, client.ReceiveBufferSize);
                }
                catch (IOException e)
                {
                    Console.WriteLine(e.StackTrace);
                    break;
                }

                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")
                            {
                                currentData          = new CurrentData(userID);
                                currentData.isDoctor = true;
                                SendGet(1);
                            }
                            else if (response_parts[2] == "0" && response_parts[1] == "1")
                            {
                                currentData          = new CurrentData(userID);
                                currentData.isDoctor = false;
                                SendGet(1);
                            }
                            else
                            {
                                new Login("Geen gebruiker gevonden");
                            }
                        }
                        break;

                    case "1":
                        response_parts[1] = response_parts[1].TrimEnd('\0');
                        currentData.setSessionList(JsonConvert.DeserializeObject <List <Session> >(response_parts[1]));

                        if (currentData.isDoctor == true)
                        {
                            Form activeForm = Form.ActiveForm;
                            activeForm.Invoke((MethodInvoker) delegate()
                            {
                                DoctorForm doctorForm = new DoctorForm(this);
                                activeForm.Hide();
                                doctorForm.Show();
                            });
                        }
                        else
                        {
                            Form activeForm = Form.ActiveForm;
                            if (activeForm != null)
                            {
                                activeForm.Invoke((MethodInvoker) delegate()
                                {
                                    PatientForm patientForm = new PatientForm(this);
                                    activeForm.Hide();
                                    patientForm.Show();
                                });
                            }
                        }

                        break;

                    case "2":
                        dynamic     DynMeasurement    = JsonConvert.DeserializeObject <dynamic>(response_parts[2].TrimEnd('\0'));
                        Measurement outputMeasurement = new Measurement((int)DynMeasurement.pulse, (int)DynMeasurement.rpm, (int)DynMeasurement.speed, (int)DynMeasurement.distance, (int)DynMeasurement.requestedPower, (int)DynMeasurement.energy, (int)DynMeasurement.actualPower, (int)DynMeasurement.time);
                        currentData.GetSessions().Last().AddMeasurement(outputMeasurement);
                        Forms.DoctorSessionUC sessionUC = null;
                        bool b = DoctorModel.doctorModel.doctorSessions.TryGetValue(response_parts[1], out sessionUC);
                        if (b)
                        {
                            sessionUC.HandleSessionBikeData(outputMeasurement);
                        }
                        break;

                    case "7":
                        //                                        sender              receiver          message
                        onIncomingChatMessage(new string[] { response_parts[1], response_parts[2], response_parts[3].TrimEnd('\0') });
                        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].TrimEnd('\0') == "-1")
                        {
                            DoctorModel.doctorModel.onlinePatients = new List <String>();
                        }
                        break;

                    case "9":
                        dynamic results = JsonConvert.DeserializeObject <dynamic>(response_parts[1]);

                        foreach (dynamic r in results)
                        {
                            User user = r as User;
                            users.Add(new User(r.id.ToString(), r.password.ToString(), Int32.Parse(r.age.ToString()),
                                               Boolean.Parse(r.gender.ToString()), Int32.Parse(r.weight.ToString()),
                                               Boolean.Parse(r.isDoctor.ToString())));

                            int i = 1;

                            foreach (dynamic ses in r.sessions)
                            {
                                Session tempSession = new Session(i);
                                i++;

                                foreach (dynamic m in ses.measurements)
                                {
                                    Measurement measurement = new Measurement((int)m.pulse, (int)m.rpm, (int)m.speed, (int)m.distance, (int)m.requestedPower, (int)m.energy, (int)m.actualPower, (int)m.time);;
                                    tempSession.AddMeasurement(measurement);
                                }

                                users.Last().AddSession(tempSession);
                            }
                            Console.WriteLine(users);
                        }
                        break;

                    case "10":
                        if (!currentData.isDoctor)
                        {
                            PatientModel.patientModel.CurrentDoctorID = response_parts[3].TrimEnd('\0');
                            if (response_parts[1] == "1")
                            {
                                PatientModel.patientModel.startSession();
                            }
                            else if (response_parts[1] == "0")
                            {
                                StopSessoin();
                            }
                        }
                        else
                        {
                            if (response_parts[1] == "1")
                            {
                                StartNewSession(true, response_parts[2]);
                            }
                        }
                        break;

                    case "20":
                        PatientModel.patientModel.setDistanceMode(response_parts[1].TrimEnd('\0'), false);
                        break;

                    case "21":
                        PatientModel.patientModel.setTimeMode(response_parts[1].TrimEnd('\0') + response_parts[2].TrimEnd('\0'), false);
                        break;

                    case "22":
                        //PatientModel.patientModel.setPower(response_parts[1].TrimEnd('\0'));

                        PatientModel.patientModel.powerLog = response_parts[1].TrimEnd('\0');
                        break;
                    }
                }
            }
        }
        public void receive()
        {
            while (client.Connected)
            {
                byte[] bytesFrom = new byte[(int)client.ReceiveBufferSize];
                try
                {
                    sslStream.Read(bytesFrom, 0, client.ReceiveBufferSize);
                    
                }
                catch (IOException e)
                {
                    Console.WriteLine(e.StackTrace);
                    break;
                }

                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")
                                {
                                    currentData = new CurrentData(userID);
                                    currentData.isDoctor = true;
                                    SendGet(1);
                                }
                                else if (response_parts[2] == "0" && response_parts[1] == "1")
                                {

                                    currentData = new CurrentData(userID);
                                    currentData.isDoctor = false;
                                    SendGet(1);
                                }
                                else
                                    new Login("Geen gebruiker gevonden");
                            }
                            break;
                        case "1":
                            response_parts[1] = response_parts[1].TrimEnd('\0');
                            currentData.setSessionList(JsonConvert.DeserializeObject<List<Session>>(response_parts[1]));

                            if (currentData.isDoctor == true)
                            {
                                Form activeForm = Form.ActiveForm;
                                activeForm.Invoke((MethodInvoker)delegate ()
                               {
                                   DoctorForm doctorForm = new DoctorForm(this);
                                   activeForm.Hide();
                                   doctorForm.Show();
                               });
                            }
                            else
                            {
                                Form activeForm = Form.ActiveForm;
                                if (activeForm != null)
                                {
                                    activeForm.Invoke((MethodInvoker)delegate ()
                                    {
                                        PatientForm patientForm = new PatientForm(this);
                                        activeForm.Hide();
                                        patientForm.Show();
                                    });
                                }
                            }

                            break;
                        case "2":
                            dynamic DynMeasurement = JsonConvert.DeserializeObject<dynamic>(response_parts[2].TrimEnd('\0'));
                            Measurement outputMeasurement = new Measurement((int)DynMeasurement.pulse, (int)DynMeasurement.rpm, (int)DynMeasurement.speed, (int)DynMeasurement.distance, (int)DynMeasurement.requestedPower, (int)DynMeasurement.energy, (int)DynMeasurement.actualPower, (int)DynMeasurement.time);
                            currentData.GetSessions().Last().AddMeasurement(outputMeasurement);
                            Forms.DoctorSessionUC sessionUC = null;
                            bool b = DoctorModel.doctorModel.doctorSessions.TryGetValue(response_parts[1], out sessionUC);
                            if (b)
                            {
                                sessionUC.HandleSessionBikeData(outputMeasurement);
                            }
                                break;
                        case "7":
                            //                                        sender              receiver          message
                            onIncomingChatMessage(new string[] { response_parts[1], response_parts[2], response_parts[3].TrimEnd('\0') });
                            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].TrimEnd('\0') == "-1")
                            {
                                DoctorModel.doctorModel.onlinePatients = new List<String>();
                            }
                            break;
                        case "9":
                            dynamic results = JsonConvert.DeserializeObject<dynamic>(response_parts[1]);

                            foreach (dynamic r in results)
                            {
                                User user = r as User;
                                users.Add(new User(r.id.ToString(), r.password.ToString(), Int32.Parse(r.age.ToString()),
                                    Boolean.Parse(r.gender.ToString()), Int32.Parse(r.weight.ToString()),
                                    Boolean.Parse(r.isDoctor.ToString())));

                                int i = 1;

                                foreach (dynamic ses in r.sessions)
                                {
                                    Session tempSession = new Session(i);
                                    i++;

                                    foreach (dynamic m in ses.measurements)
                                    {
                                        Measurement measurement = new Measurement((int)m.pulse, (int)m.rpm, (int)m.speed, (int)m.distance, (int)m.requestedPower, (int)m.energy, (int)m.actualPower, (int)m.time); ;
                                        tempSession.AddMeasurement(measurement);
                                    }

                                    users.Last().AddSession(tempSession);
                                }
                                Console.WriteLine(users);
                            }
                            break;
                        case "10":
                            if (!currentData.isDoctor)
                            {
                                PatientModel.patientModel.CurrentDoctorID = response_parts[3].TrimEnd('\0');
                                if (response_parts[1] == "1")
                                {
                                    PatientModel.patientModel.startSession();
                                }
                                else if (response_parts[1] == "0")
                                {
                                    StopSessoin();
                                }
                            }
                            else
                            {
                                if (response_parts[1] == "1") 
                                {
                                    StartNewSession(true, response_parts[2]);
                                }
                            }
                            break;
                        case "20":
                            PatientModel.patientModel.setDistanceMode(response_parts[1].TrimEnd('\0'), false);
                            break;
                        case "21":
                            PatientModel.patientModel.setTimeMode(response_parts[1].TrimEnd('\0') + response_parts[2].TrimEnd('\0'), false);
                            break;
                        case "22":
                            //PatientModel.patientModel.setPower(response_parts[1].TrimEnd('\0'));

                            PatientModel.patientModel.powerLog = response_parts[1].TrimEnd('\0');
                            break;
                    }
                }
            }
        }