TCPConnection.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. private NetworkStream serverStream;
  16. private CurrentData currentData;
  17. private string userID;
  18. private bool isConnectedFlag;
  19. public TcpConnection()
  20. {
  21. // create a connection
  22. client = new TcpClient();
  23. connect();
  24. }
  25. public bool isConnected()
  26. {
  27. return isConnectedFlag;
  28. }
  29. public void connect()
  30. {
  31. try
  32. {
  33. client.Connect("192.168.178.17", 1288);
  34. // create streams
  35. serverStream = client.GetStream();
  36. Thread t = new Thread(recieve);
  37. t.Start();
  38. isConnectedFlag = true;
  39. }
  40. catch (Exception ex)
  41. {
  42. Console.WriteLine(ex);
  43. Thread.Sleep(1000);
  44. isConnectedFlag = false;
  45. }
  46. }
  47. public void recieve ()
  48. {
  49. while (true)
  50. {
  51. byte[] bytesFrom = new byte[(int)client.ReceiveBufferSize];
  52. serverStream.Read(bytesFrom, 0, (int)client.ReceiveBufferSize);
  53. String response = Encoding.ASCII.GetString(bytesFrom);
  54. String[] response_parts = response.Split('|');
  55. if (response_parts.Length > 0)
  56. {
  57. switch (response_parts[0])
  58. {
  59. case "0": //login
  60. if (response_parts.Length == 4)
  61. {
  62. if (response_parts[1] == "1" && response_parts[2] == "1")
  63. {
  64. Form.ActiveForm.Dispose();
  65. new DoctorForm().Show();
  66. currentData = new CurrentData(userID);
  67. }
  68. else if(response_parts[2] == "0" && response_parts[1] == "1")
  69. {
  70. Form.ActiveForm.Dispose();
  71. new PatientForm().Show();
  72. currentData = new CurrentData(userID);
  73. }
  74. else
  75. {
  76. new Login("Geen gebruiker gevonden");
  77. }
  78. }
  79. break;
  80. case "1":
  81. currentData.setSessionList(JsonConvert.DeserializeObject<List<Session>>(response_parts[1]));
  82. break;
  83. case "2":
  84. currentData.GetSessions().Last().AddMeasurement(JsonConvert.DeserializeObject<Measurement>(response_parts[1]));
  85. break;
  86. }
  87. }
  88. }
  89. }
  90. public void sendLogin(string username, string password)
  91. {
  92. // send command ( cmdID | username | password )
  93. this.userID = username;
  94. sendString("0|" + username + "|" + password);
  95. }
  96. public void sendGet(int GetWhat)
  97. {
  98. // send command ( cmdID | username )
  99. sendString( GetWhat + "|" + userID );
  100. }
  101. public void sendNewSession()
  102. {
  103. // send command ( cmdID | username )
  104. sendString("3|" + userID + lib.JsonConverter.SerializeSession(currentData.GetSessions().Last()));
  105. }
  106. public void sendNewMeasurement()
  107. {
  108. // send command ( cmdID | username )
  109. sendString("5|" + userID + lib.JsonConverter.SerializeLastMeasurement(currentData.GetSessions().Last().GetLastMeasurement()));
  110. }
  111. public void sendString(string s)
  112. {
  113. byte[] b = Encoding.ASCII.GetBytes(s);
  114. serverStream.Write(b, 0, b.Length);
  115. serverStream.Flush();
  116. }
  117. }
  118. }