MainClient.cs 9.2 KB

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