MainClient.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. using ErgometerLibrary;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace ErgometerApplication
  10. {
  11. class MainClient
  12. {
  13. public static ComPort ComPort { get; }
  14. public static TcpClient Doctor { get; set; }
  15. public static List<Meting> Metingen { get; }
  16. public static List<ChatMessage> Chat { get; }
  17. public static string Name;
  18. public static int Session;
  19. public static bool Loggedin;
  20. private static Thread t;
  21. private static bool running;
  22. public static string HOST = "127.0.0.1";
  23. public static int PORT = 8888;
  24. public static ClientApplicatie Client;
  25. static MainClient()
  26. {
  27. ComPort = new ComPort();
  28. Doctor = new TcpClient();
  29. Metingen = new List<Meting>();
  30. Chat = new List<ChatMessage>();
  31. Name = "Unknown";
  32. Session = 0;
  33. Loggedin = false;
  34. }
  35. public static void Init(ClientApplicatie client)
  36. {
  37. Client = client;
  38. }
  39. public static bool Connect(string comport, string name, string password, out string error)
  40. {
  41. error = "Succes";
  42. if (!ComPort.IsOpen())
  43. {
  44. if (ComPort.Connect(comport))
  45. {
  46. ComPort.Write("RS");
  47. string temp = ComPort.Read();
  48. if (temp == "err")
  49. {
  50. ComPort.Disconnect();
  51. error = "De Ergometer is niet verbonden";
  52. return false;
  53. }
  54. Thread.Sleep(200);
  55. ComPort.Write("CM");
  56. ComPort.Read();
  57. Thread.Sleep(200);
  58. ComPort.Write("ST");
  59. string response = ComPort.Read();
  60. SaveMeting(response);
  61. }
  62. else
  63. {
  64. error = "De ergometer is niet verbonden";
  65. return false;
  66. }
  67. }
  68. if (Doctor == null || !Doctor.Connected)
  69. {
  70. if (Doctor == null)
  71. Doctor = new TcpClient();
  72. try
  73. {
  74. Doctor.Connect(HOST, PORT);
  75. }
  76. catch (Exception e)
  77. {
  78. error = "Server is niet online.";
  79. return false;
  80. }
  81. Name = name;
  82. NetCommand net = NetHelper.ReadNetCommand(Doctor);
  83. if (net.Type == NetCommand.CommandType.SESSION)
  84. Session = net.Session;
  85. else
  86. throw new Exception("Session not assigned");
  87. running = true;
  88. t = new Thread(run);
  89. t.IsBackground = true;
  90. t.Start();
  91. }
  92. if(! Loggedin)
  93. {
  94. NetCommand command = new NetCommand(name, false, password, Session);
  95. NetHelper.SendNetCommand(Doctor, command);
  96. NetCommand response = NetHelper.ReadNetCommand(Doctor);
  97. if (response.Type == NetCommand.CommandType.RESPONSE && response.Response == NetCommand.ResponseType.LOGINWRONG)
  98. {
  99. Loggedin = false;
  100. error = "De inloggegevens zijn onjuist.";
  101. return false;
  102. }
  103. Loggedin = true;
  104. }
  105. return true;
  106. }
  107. public static void Disconnect()
  108. {
  109. if (ComPort.IsOpen())
  110. {
  111. if (ComPort.Disconnect())
  112. {
  113. }
  114. else
  115. throw new Exception("Comport was unable to disconnect");
  116. }
  117. if (Doctor != null && Doctor.Connected)
  118. {
  119. NetHelper.SendNetCommand(Doctor, new NetCommand(NetCommand.CommandType.LOGOUT, Session));
  120. Loggedin = false;
  121. running = false;
  122. Doctor.Close();
  123. Doctor = null;
  124. }
  125. }
  126. public static Meting SaveMeting(string meting)
  127. {
  128. Meting m = null;
  129. try
  130. {
  131. m = Meting.Parse(meting);
  132. }
  133. catch(Exception e)
  134. {
  135. return new Meting(0, 0, 0, 0, 0, 0, 0, 0, 0);
  136. }
  137. Metingen.Add(m);
  138. if (Doctor.Connected)
  139. {
  140. NetHelper.SendNetCommand(Doctor, new NetCommand(m, Session));
  141. }
  142. return m;
  143. }
  144. public static Meting GetLastMeting()
  145. {
  146. return Metingen.Last();
  147. }
  148. public static void run()
  149. {
  150. while(running)
  151. {
  152. if(Doctor.Connected)
  153. {
  154. NetCommand command = NetHelper.ReadNetCommand(Doctor);
  155. ParseCommand(command);
  156. }
  157. }
  158. if(Doctor != null)
  159. Doctor.Close();
  160. }
  161. private static void ParseCommand(NetCommand command)
  162. {
  163. switch(command.Type)
  164. {
  165. case NetCommand.CommandType.VALUESET:
  166. ParseValueSet(command);
  167. break;
  168. case NetCommand.CommandType.CHAT:
  169. ChatMessage chat = new ChatMessage(command.DisplayName, command.ChatMessage, true);
  170. Chat.Add(chat);
  171. Client.chat.Invoke(Client.chat.passChatMessage, new Object[] { chat });
  172. break;
  173. case NetCommand.CommandType.RESPONSE:
  174. Console.WriteLine(command.Response.ToString());
  175. break;
  176. case NetCommand.CommandType.SESSION:
  177. Session = command.Session;
  178. break;
  179. case NetCommand.CommandType.ERROR:
  180. Console.WriteLine("An error occured, ignoring");
  181. break;
  182. default:
  183. throw new FormatException("Error in Netcommand: Received command not recognized");
  184. }
  185. }
  186. public static void SendNetCommand(NetCommand command)
  187. {
  188. if(! NetHelper.SendNetCommand(Doctor, command))
  189. {
  190. Disconnect();
  191. }
  192. }
  193. private static void ParseValueSet(NetCommand command)
  194. {
  195. switch(command.Value)
  196. {
  197. case NetCommand.ValueType.DISTANCE:
  198. ComPort.Write("RS");
  199. ComPort.Read();
  200. Thread.Sleep(200);
  201. ComPort.Write("CM");
  202. ComPort.Read();
  203. Thread.Sleep(700);
  204. ComPort.Write("PD " + command.SetValue.ToString());
  205. ComPort.Read();
  206. break;
  207. case NetCommand.ValueType.ENERGY:
  208. ComPort.Write("RS");
  209. ComPort.Read();
  210. Thread.Sleep(200);
  211. ComPort.Write("CM");
  212. ComPort.Read();
  213. Thread.Sleep(700);
  214. ComPort.Write("PE " + command.SetValue.ToString());
  215. ComPort.Read();
  216. break;
  217. case NetCommand.ValueType.POWER:
  218. ComPort.Write("CM");
  219. ComPort.Read();
  220. Thread.Sleep(200);
  221. ComPort.Write("PW " + command.SetValue.ToString());
  222. ComPort.Read();
  223. break;
  224. case NetCommand.ValueType.TIME:
  225. ComPort.Write("RS");
  226. ComPort.Read();
  227. Thread.Sleep(200);
  228. ComPort.Write("CM");
  229. ComPort.Read();
  230. Thread.Sleep(700);
  231. string temp = (command.SetValue / 60) + "";
  232. if(temp.Length < 2)
  233. {
  234. temp = "0" + temp;
  235. }
  236. string temp2 = (command.SetValue % 60) + "";
  237. if (temp2.Length < 2)
  238. {
  239. temp2 = "0" + temp2;
  240. }
  241. string time = temp + temp2;
  242. ComPort.Write("PT " + time);
  243. ComPort.Read();
  244. break;
  245. default:
  246. throw new FormatException("Error in NetCommand: ValueSet is not recognized");
  247. }
  248. }
  249. }
  250. }