TCPConnection.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using FietsClient.JSONObjecten;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Net.Sockets;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Windows.Forms;
  10. namespace FietsClient
  11. {
  12. public class TcpConnection
  13. {
  14. public TcpClient client;
  15. public bool isConnectedFlag { private set; get; }
  16. private NetworkStream serverStream;
  17. public CurrentData currentData { private set; get; }
  18. private string userID;
  19. private Thread receiveThread;
  20. public TcpConnection()
  21. {
  22. client = new TcpClient();
  23. connect();
  24. }
  25. public void connect()
  26. {
  27. try
  28. {
  29. client.Connect("145.48.226.156", 1288);
  30. // create streams
  31. serverStream = client.GetStream();
  32. receiveThread = new Thread(receive);
  33. receiveThread.Start();
  34. isConnectedFlag = true;
  35. }
  36. catch (Exception)
  37. {
  38. Thread.Sleep(1000);
  39. isConnectedFlag = false;
  40. }
  41. }
  42. public void receive()
  43. {
  44. while (true)
  45. {
  46. byte[] bytesFrom = new byte[(int)client.ReceiveBufferSize];
  47. serverStream.Read(bytesFrom, 0, (int)client.ReceiveBufferSize);
  48. string response = Encoding.ASCII.GetString(bytesFrom);
  49. string[] response_parts = response.Split('|');
  50. if (response_parts.Length > 0)
  51. {
  52. switch (response_parts[0])
  53. {
  54. case "0": //login and display correct window after login
  55. if (response_parts.Length == 4)
  56. {
  57. if (response_parts[1] == "1" && response_parts[2] == "1")
  58. {
  59. DoctorForm doctorForm = new DoctorForm(this);
  60. Form activeForm = Form.ActiveForm;
  61. activeForm.Invoke((MethodInvoker)delegate ()
  62. {
  63. activeForm.Hide();
  64. doctorForm.Show();
  65. });
  66. currentData = new CurrentData(userID);
  67. }
  68. else if (response_parts[2] == "0" && response_parts[1] == "1")
  69. {
  70. PatientForm patientForm = new PatientForm(this);
  71. Form activeForm = Form.ActiveForm;
  72. activeForm.Invoke((MethodInvoker)delegate ()
  73. {
  74. activeForm.Hide();
  75. patientForm.Show();
  76. });
  77. currentData = new CurrentData(userID);
  78. }
  79. else
  80. new Login("Geen gebruiker gevonden");
  81. }
  82. break;
  83. case "1":
  84. currentData.setSessionList(JsonConvert.DeserializeObject<List<Session>>(response_parts[1]));
  85. break;
  86. case "2":
  87. currentData.GetSessions().Last().AddMeasurement(JsonConvert.DeserializeObject<Measurement>(response_parts[1]));
  88. break;
  89. }
  90. }
  91. }
  92. }
  93. public void SendLogin(string username, string password)
  94. {
  95. // send command ( cmdID | username | password )
  96. this.userID = username;
  97. SendString("0|" + username + "|" + password + "|");
  98. }
  99. public void SendGet(int GetWhat)
  100. {
  101. // send command ( cmdID | username )
  102. SendString(GetWhat + "|" + userID);
  103. }
  104. public void SendNewSession()
  105. {
  106. // send command ( cmdID | username )
  107. SendString("3|" + userID + lib.JsonConverter.SerializeSession(currentData.GetSessions().Last()));
  108. }
  109. public void SendNewMeasurement()
  110. {
  111. // send command ( cmdID | username )
  112. SendString("5|" + userID + lib.JsonConverter.SerializeLastMeasurement(currentData.GetSessions().Last().GetLastMeasurement()));
  113. }
  114. public void SendString(string s)
  115. {
  116. byte[] b = Encoding.ASCII.GetBytes(s);
  117. serverStream.Write(b, 0, b.Length);
  118. serverStream.Flush();
  119. }
  120. public void SendDistance(int distance)
  121. {
  122. SendString("10|" + userID + "|" + distance);
  123. }
  124. public void SendTime(int Minutes, int seconds)
  125. {
  126. SendString("11|" + userID + "|" + Minutes + ":" + seconds);
  127. }
  128. public void SendPower(int power)
  129. {
  130. SendString("12|" + userID + "|" + power);
  131. }
  132. }
  133. }