MainClient.cs 9.4 KB

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