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("ST");
  56. string response = ComPort.Read();
  57. SaveMeting(response);
  58. }
  59. else
  60. {
  61. error = "De ergometer is niet verbonden";
  62. return false;
  63. }
  64. }
  65. if (Doctor == null || !Doctor.Connected)
  66. {
  67. if (Doctor == null)
  68. Doctor = new TcpClient();
  69. try
  70. {
  71. Doctor.Connect(HOST, PORT);
  72. }
  73. catch (Exception e)
  74. {
  75. error = "Server is niet online.";
  76. return false;
  77. }
  78. Name = name;
  79. NetCommand net = NetHelper.ReadNetCommand(Doctor);
  80. if (net.Type == NetCommand.CommandType.SESSION)
  81. Session = net.Session;
  82. else
  83. throw new Exception("Session not assigned");
  84. running = true;
  85. t = new Thread(run);
  86. t.IsBackground = true;
  87. t.Start();
  88. }
  89. if(! Loggedin)
  90. {
  91. NetCommand command = new NetCommand(name, false, password, Session);
  92. NetHelper.SendNetCommand(Doctor, command);
  93. NetCommand response = NetHelper.ReadNetCommand(Doctor);
  94. if (response.Type == NetCommand.CommandType.RESPONSE && response.Response == NetCommand.ResponseType.LOGINWRONG)
  95. {
  96. Loggedin = false;
  97. error = "De inloggegevens zijn onjuist.";
  98. return false;
  99. }
  100. Loggedin = true;
  101. }
  102. return true;
  103. }
  104. public static void Disconnect()
  105. {
  106. if (ComPort.IsOpen())
  107. {
  108. if (ComPort.Disconnect())
  109. {
  110. }
  111. else
  112. throw new Exception("Comport was unable to disconnect");
  113. }
  114. if (Doctor != null && Doctor.Connected)
  115. {
  116. NetHelper.SendNetCommand(Doctor, new NetCommand(NetCommand.CommandType.LOGOUT, Session));
  117. Loggedin = false;
  118. running = false;
  119. Doctor.Close();
  120. Doctor = null;
  121. }
  122. }
  123. public static Meting SaveMeting(string meting)
  124. {
  125. Meting m = null;
  126. try
  127. {
  128. m = Meting.Parse(meting);
  129. }
  130. catch(Exception e)
  131. {
  132. return new Meting(0, 0, 0, 0, 0, 0, 0, 0, 0);
  133. }
  134. Metingen.Add(m);
  135. if (Doctor.Connected)
  136. {
  137. NetHelper.SendNetCommand(Doctor, new NetCommand(m, Session));
  138. }
  139. return m;
  140. }
  141. public static Meting GetLastMeting()
  142. {
  143. if (Metingen.Count > 1)
  144. return Metingen.Last();
  145. else
  146. return new Meting(0, 0, 0, 0, 0, 0, 0, 0, 0);
  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. }