MainClient.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 void RPMBeatAudio()
  40. {
  41. Console.Beep(1000, 50);
  42. }
  43. public static void QuickBeepAudio()
  44. {
  45. Console.Beep(1200, 150);
  46. }
  47. public static void SwitchWorkloadAudio()
  48. {
  49. Console.Beep(1000, 200);
  50. Console.Beep(1100, 200);
  51. Console.Beep(1200, 100);
  52. }
  53. public static void SwitchTestModeAudio()
  54. {
  55. Console.Beep(1000, 200);
  56. Console.Beep(900, 200);
  57. Console.Beep(800, 100);
  58. }
  59. public static bool Connect(string comport, string name, string password, out string error)
  60. {
  61. error = "Succes";
  62. if (!ComPort.IsOpen())
  63. {
  64. if (ComPort.Connect(comport))
  65. {
  66. ComPort.Write("RS");
  67. string temp = ComPort.Read();
  68. if (temp.ToLower() != "ack")
  69. {
  70. ComPort.Disconnect();
  71. error = "De Ergometer is niet verbonden";
  72. return false;
  73. }
  74. Thread.Sleep(200);
  75. ComPort.Write("ST");
  76. string response = ComPort.Read();
  77. SaveMeting(response);
  78. }
  79. else
  80. {
  81. error = "De ergometer is niet verbonden";
  82. return false;
  83. }
  84. }
  85. if (Doctor == null || !Doctor.Connected)
  86. {
  87. if (Doctor == null)
  88. Doctor = new TcpClient();
  89. try
  90. {
  91. Doctor.Connect(HOST, PORT);
  92. }
  93. catch (Exception e)
  94. {
  95. error = "Server is niet online.";
  96. return false;
  97. }
  98. Name = name;
  99. NetCommand net = NetHelper.ReadNetCommand(Doctor);
  100. if (net.Type == NetCommand.CommandType.SESSION)
  101. Session = net.Session;
  102. else
  103. throw new Exception("Session not assigned");
  104. running = true;
  105. t = new Thread(run);
  106. t.IsBackground = true;
  107. t.Start();
  108. }
  109. if(! Loggedin)
  110. {
  111. NetCommand command = new NetCommand(name, false, password, Session);
  112. NetHelper.SendNetCommand(Doctor, command);
  113. NetCommand response = NetHelper.ReadNetCommand(Doctor);
  114. if (response.Type == NetCommand.CommandType.RESPONSE && response.Response == NetCommand.ResponseType.LOGINWRONG)
  115. {
  116. Loggedin = false;
  117. error = "De inloggegevens zijn onjuist.";
  118. return false;
  119. }
  120. Loggedin = true;
  121. }
  122. return true;
  123. }
  124. public static void Disconnect()
  125. {
  126. if (ComPort.IsOpen())
  127. {
  128. if (ComPort.Disconnect())
  129. {
  130. }
  131. else
  132. throw new Exception("Comport was unable to disconnect");
  133. }
  134. if (Doctor != null && Doctor.Connected)
  135. {
  136. NetHelper.SendNetCommand(Doctor, new NetCommand(NetCommand.CommandType.LOGOUT, Session));
  137. Loggedin = false;
  138. running = false;
  139. Doctor.Close();
  140. Doctor = null;
  141. }
  142. }
  143. public static Meting SaveMeting(string meting)
  144. {
  145. Meting m = null;
  146. try
  147. {
  148. m = Meting.Parse(meting);
  149. }
  150. catch(Exception e)
  151. {
  152. return new Meting(0, 0, 0, 0, 0, 0, 0, 0, 0);
  153. }
  154. Metingen.Add(m);
  155. if (Doctor.Connected)
  156. {
  157. NetHelper.SendNetCommand(Doctor, new NetCommand(m, Session));
  158. }
  159. return m;
  160. }
  161. public static Meting GetLastMeting()
  162. {
  163. if (Metingen.Count > 1)
  164. return Metingen.Last();
  165. else
  166. return new Meting(0, 0, 0, 0, 0, 0, 0, 0, 0);
  167. }
  168. public static void run()
  169. {
  170. while(running)
  171. {
  172. if(Doctor.Connected)
  173. {
  174. NetCommand command = NetHelper.ReadNetCommand(Doctor);
  175. ParseCommand(command);
  176. }
  177. }
  178. if(Doctor != null)
  179. Doctor.Close();
  180. }
  181. private static void ParseCommand(NetCommand command)
  182. {
  183. switch(command.Type)
  184. {
  185. case NetCommand.CommandType.VALUESET:
  186. ParseValueSet(command);
  187. break;
  188. case NetCommand.CommandType.CHAT:
  189. ChatMessage chat = new ChatMessage(command.DisplayName, command.ChatMessage, true);
  190. Chat.Add(chat);
  191. Client.chat.Invoke(Client.chat.passChatMessage, new Object[] { chat });
  192. break;
  193. case NetCommand.CommandType.RESPONSE:
  194. Console.WriteLine(command.Response.ToString());
  195. break;
  196. case NetCommand.CommandType.SESSION:
  197. Session = command.Session;
  198. break;
  199. case NetCommand.CommandType.ERROR:
  200. Console.WriteLine("An error occured, ignoring");
  201. break;
  202. default:
  203. throw new FormatException("Error in Netcommand: Received command not recognized");
  204. }
  205. }
  206. public static void SendNetCommand(NetCommand command)
  207. {
  208. if(! NetHelper.SendNetCommand(Doctor, command))
  209. {
  210. Disconnect();
  211. }
  212. }
  213. private static void ParseValueSet(NetCommand command)
  214. {
  215. return;
  216. switch(command.Value)
  217. {
  218. case NetCommand.ValueType.DISTANCE:
  219. ComPort.Write("RS");
  220. ComPort.Read();
  221. Thread.Sleep(200);
  222. ComPort.Write("CM");
  223. ComPort.Read();
  224. Thread.Sleep(700);
  225. ComPort.Write("PD " + command.SetValue.ToString());
  226. ComPort.Read();
  227. break;
  228. case NetCommand.ValueType.ENERGY:
  229. ComPort.Write("RS");
  230. ComPort.Read();
  231. Thread.Sleep(200);
  232. ComPort.Write("CM");
  233. ComPort.Read();
  234. Thread.Sleep(700);
  235. ComPort.Write("PE " + command.SetValue.ToString());
  236. ComPort.Read();
  237. break;
  238. case NetCommand.ValueType.POWER:
  239. ComPort.Write("CM");
  240. ComPort.Read();
  241. Thread.Sleep(200);
  242. ComPort.Write("PW " + command.SetValue.ToString());
  243. ComPort.Read();
  244. break;
  245. case NetCommand.ValueType.TIME:
  246. ComPort.Write("RS");
  247. ComPort.Read();
  248. Thread.Sleep(200);
  249. ComPort.Write("CM");
  250. ComPort.Read();
  251. Thread.Sleep(700);
  252. string temp = (command.SetValue / 60) + "";
  253. if(temp.Length < 2)
  254. {
  255. temp = "0" + temp;
  256. }
  257. string temp2 = (command.SetValue % 60) + "";
  258. if (temp2.Length < 2)
  259. {
  260. temp2 = "0" + temp2;
  261. }
  262. string time = temp + temp2;
  263. ComPort.Write("PT " + time);
  264. ComPort.Read();
  265. break;
  266. default:
  267. throw new FormatException("Error in NetCommand: ValueSet is not recognized");
  268. }
  269. }
  270. }
  271. }