MainClient.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. if (Metingen.Count > 1)
  147. return Metingen.Last();
  148. else
  149. return new Meting(0, 0, 0, 0, 0, 0, 0, 0, 0);
  150. }
  151. public static void run()
  152. {
  153. while(running)
  154. {
  155. if(Doctor.Connected)
  156. {
  157. NetCommand command = NetHelper.ReadNetCommand(Doctor);
  158. ParseCommand(command);
  159. }
  160. }
  161. if(Doctor != null)
  162. Doctor.Close();
  163. }
  164. private static void ParseCommand(NetCommand command)
  165. {
  166. switch(command.Type)
  167. {
  168. case NetCommand.CommandType.VALUESET:
  169. ParseValueSet(command);
  170. break;
  171. case NetCommand.CommandType.CHAT:
  172. ChatMessage chat = new ChatMessage(command.DisplayName, command.ChatMessage, true);
  173. Chat.Add(chat);
  174. Client.chat.Invoke(Client.chat.passChatMessage, new Object[] { chat });
  175. break;
  176. case NetCommand.CommandType.RESPONSE:
  177. Console.WriteLine(command.Response.ToString());
  178. break;
  179. case NetCommand.CommandType.SESSION:
  180. Session = command.Session;
  181. break;
  182. case NetCommand.CommandType.ERROR:
  183. Console.WriteLine("An error occured, ignoring");
  184. break;
  185. default:
  186. throw new FormatException("Error in Netcommand: Received command not recognized");
  187. }
  188. }
  189. public static void SendNetCommand(NetCommand command)
  190. {
  191. if(! NetHelper.SendNetCommand(Doctor, command))
  192. {
  193. Disconnect();
  194. }
  195. }
  196. private static void ParseValueSet(NetCommand command)
  197. {
  198. switch(command.Value)
  199. {
  200. case NetCommand.ValueType.DISTANCE:
  201. ComPort.Write("RS");
  202. ComPort.Read();
  203. Thread.Sleep(200);
  204. ComPort.Write("CM");
  205. ComPort.Read();
  206. Thread.Sleep(700);
  207. ComPort.Write("PD " + command.SetValue.ToString());
  208. ComPort.Read();
  209. break;
  210. case NetCommand.ValueType.ENERGY:
  211. ComPort.Write("RS");
  212. ComPort.Read();
  213. Thread.Sleep(200);
  214. ComPort.Write("CM");
  215. ComPort.Read();
  216. Thread.Sleep(700);
  217. ComPort.Write("PE " + command.SetValue.ToString());
  218. ComPort.Read();
  219. break;
  220. case NetCommand.ValueType.POWER:
  221. ComPort.Write("CM");
  222. ComPort.Read();
  223. Thread.Sleep(200);
  224. ComPort.Write("PW " + command.SetValue.ToString());
  225. ComPort.Read();
  226. break;
  227. case NetCommand.ValueType.TIME:
  228. ComPort.Write("RS");
  229. ComPort.Read();
  230. Thread.Sleep(200);
  231. ComPort.Write("CM");
  232. ComPort.Read();
  233. Thread.Sleep(700);
  234. string temp = (command.SetValue / 60) + "";
  235. if(temp.Length < 2)
  236. {
  237. temp = "0" + temp;
  238. }
  239. string temp2 = (command.SetValue % 60) + "";
  240. if (temp2.Length < 2)
  241. {
  242. temp2 = "0" + temp2;
  243. }
  244. string time = temp + temp2;
  245. ComPort.Write("PT " + time);
  246. ComPort.Read();
  247. break;
  248. default:
  249. throw new FormatException("Error in NetCommand: ValueSet is not recognized");
  250. }
  251. }
  252. }
  253. }