| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414 |
- using FietsLibrary.JSONObjecten;
- using FietsLibrary;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net.Security;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading;
- using System.Windows.Forms;
- using System.Security.Cryptography.X509Certificates;
- using System.Security.Authentication;
- using System.IO;
- namespace FietsClient
- {
- public class TcpConnection
- {
- public TcpClient client;
- public bool isConnectedFlag { private set; get; }
- private SslStream sslStream;
- public CurrentData currentData { private set; get; }
- public string userID { private set; get; }
- private Thread receiveThread;
- public delegate void ChatmassegeDelegate(string[] data);
- public event ChatmassegeDelegate IncomingChatmessageEvent;
- public List<User> users = new List<User>();
- public TcpConnection()
- {
- client = new TcpClient();
- connect();
- }
- 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
- sslStream = new SslStream(client.GetStream(), false,
- new RemoteCertificateValidationCallback(CertificateValidationCallback),
- new LocalCertificateSelectionCallback(CertificateSelectionCallback));
- bool authenticationPassed = true;
- try
- {
- string serverName = System.Environment.MachineName;
- X509Certificate cert = GetServerCert();
- X509CertificateCollection certs = new X509CertificateCollection();
- certs.Add(cert);
- sslStream.AuthenticateAsClient(
- serverName,
- certs,
- SslProtocols.Default,
- false); // check cert revokation
- }
- catch (AuthenticationException)
- {
- authenticationPassed = false;
- }
- if (authenticationPassed)
- {
- receiveThread = new Thread(receive);
- receiveThread.Start();
- isConnectedFlag = true;
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex);
- Thread.Sleep(1000);
- isConnectedFlag = false;
- }
- }
- static X509Certificate CertificateSelectionCallback(object sender,
- string targetHost,
- X509CertificateCollection localCertificates,
- X509Certificate remoteCertificate,
- string[] acceptableIssuers)
- {
- return localCertificates[0];
- }
- private X509Certificate GetServerCert()
- {
- X509Certificate cert = new X509Certificate2(
- @"testcert.pfx",
- "jancoow");
- return cert;
- }
- private bool CertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
- {
- return true;
- }
- public void disconnect()
- {
- sslStream.Close();
- client.Close();
- receiveThread.Abort();
- isConnectedFlag = false;
- }
- 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 "3":
-
-
- 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")
- {
- StartNewSession(false, currentData.GetUserID());
- }
- else if (response_parts[1] == "0")
- {
- StopSessoin();
- }
- }
- else
- {
- if (response_parts[1] == "1")
- {
- StartNewSession(true, response_parts[2]);
- }
- }
- break;
- case "20":
- PatientModel.patientModel.setTimeMode(response_parts[1].TrimEnd('\0') + response_parts[2].TrimEnd('\0'), false);
- break;
- case "21":
- PatientModel.patientModel.setDistanceMode(response_parts[1].TrimEnd('\0'), false);
- break;
- case "22":
- PatientModel.patientModel.setPower(response_parts[1].TrimEnd('\0'));
- break;
- }
- }
- }
- }
- public void StartNewSession(bool isDoctor, string PatientID)
- {
- Session session = new Session(currentData.sessions.Count + 1);
- currentData.sessions.Add(session);
- if (!isDoctor)
- {
- SendNewSession();
- PatientModel.patientModel.startAskingData();
- }
- else if(isDoctor)
- {
- Forms.DoctorSessionUC sessionUC = null;
- bool b = DoctorModel.doctorModel.doctorSessions.TryGetValue(PatientID, out sessionUC);
- if (b)
- {
- sessionUC.ClearOldSession();
- }
- }
- }
- public void StopSessoin()
- {
- PatientModel.patientModel.stopAskingData();
- }
- 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 + "|" + FietsLibrary.JsonConverter.SerializeSession(currentData.GetSessions().Last()) + "|");
- }
- public void SendNewPatient(User user)
- {
- // send command ( cmdID | username )
- SendString("4|" + user.id + "|" + user.password + "|" + user.age + "|" + user.gender + "|" + user.weight + "|");
- }
- public void SendNewMeasurement()
- {
- // send command ( usernamePatient | usernameCurrentDoctor | data )
- string currentDoctor = PatientModel.patientModel.CurrentDoctorID;
- SendString("5|" + userID + "|" + currentDoctor + "|" + FietsLibrary.JsonConverter.SerializeLastMeasurement(currentData.GetSessions().Last().GetLastMeasurement()) + "|");
- }
- public void SendChatMessage(string[] data)
- {
- String receiverID = data[1];
- if (currentData != null)
- {
- String message = data[0];
- // send command ( cmdID | username sender | username receiverID | message )
- string protocol = "6|" + this.userID + "|" + receiverID + "|" + message;
- SendString(protocol);
- }
- }
- public void SendGetActivePatients()
- {
- SendString("8|" + userID + "|");
- }
-
- public void requestUsers()
- {
- SendString("9|");
- }
- public void SendStartStopSession(bool startstop, string PatientUsername)
- {
- if (startstop)
- SendString("10|1|" + PatientUsername + "|" + currentData.GetUserID() + "|");
- else
- SendString("10|0|" + PatientUsername + "|" + currentData.GetUserID() + "|");
-
- }
- public void SendDistance(int distance)
- {
- SendString("20|" + userID + "|" + distance + "|");
- }
- public void SendTime(int Minutes, int seconds)
- {
- SendString("21|" + userID + "|" + Minutes + ":" + seconds + "|");
- }
- public void SendPower(int power)
- {
- SendString("22|" + userID + "|" + power + "|");
- }
- public void SendString(string s)
- {
- try
- {
- byte[] b = Encoding.ASCII.GetBytes(s);
- sslStream.Write(b, 0, b.Length);
- sslStream.Flush();
- }
- catch (Exception e)
- {
- Console.WriteLine(e);
- }
- }
- }
- }
|