MainClient.cs 8.6 KB

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