NetCommand.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Microsoft.Win32;
  7. namespace ErgometerLibrary
  8. {
  9. public class NetCommand
  10. {
  11. public enum CommandType { LOGIN, DATA, CHAT, LOGOUT, SESSION, VALUESET, USER, RESPONSE, REQUEST, LENGTH, SESSIONDATA, OLDSESSIONDATA, ERROR, BROADCAST, UITLEG, PERSONDATA, TESTRESULT}
  12. public enum RequestType { USERS, ALLSESSIONS, OLDDATA, SESSIONDATA, CHAT, PERSONALDATA, TESTRESULT }
  13. public enum ResponseType { LOGINOK, LOGINWRONG, ERROR, NOTLOGGEDIN }
  14. public enum ValueType { TIME, POWER, ENERGY, DISTANCE }
  15. public enum LengthType { USERS, SESSIONS, SESSIONDATA, DATA }
  16. public double Timestamp { get; set; }
  17. public int Session { get; set; }
  18. public CommandType Type { get; set; }
  19. public ResponseType Response { get; set; }
  20. public ValueType Value { get; set; }
  21. public RequestType Request { get; set; }
  22. public LengthType Length { get; set; }
  23. public int SetValue { get; set; }
  24. public string DisplayName { get; set; }
  25. public bool IsDoctor { get; set; }
  26. public string Password { get; set; }
  27. public string ChatMessage { get; set; }
  28. public string UitlegText { get; set; }
  29. public Meting Meting { get; set; }
  30. public int LengthValue { get; set; }
  31. public int Gewicht { get; set; }
  32. public int Lengte { get; set; }
  33. public char Geslacht { get; set; }
  34. public int Leeftijd { get; set; }
  35. public double VO2Max { get; set; }
  36. public double MET { get; set; }
  37. public double PopulationAvg { get; set; }
  38. public double ZScore { get; set; }
  39. public string Rating { get; set; }
  40. //SESSION
  41. public NetCommand(int session)
  42. {
  43. Type = CommandType.SESSION;
  44. Session = session;
  45. Timestamp = Helper.Now;
  46. }
  47. //TESTRESULT
  48. public NetCommand(double vo2max, double met, double populationavg, double zscore, string rating, int session)
  49. {
  50. Type = CommandType.TESTRESULT;
  51. Session = session;
  52. Timestamp = Helper.Now;
  53. VO2Max = vo2max;
  54. MET = met;
  55. PopulationAvg = populationavg;
  56. ZScore = zscore;
  57. Rating = rating;
  58. }
  59. //PERSONDATA
  60. public NetCommand(int gewicht, int lengte, int leeftijd, char geslacht, int session)
  61. {
  62. Type = CommandType.PERSONDATA;
  63. Session = session;
  64. Timestamp = Helper.Now;
  65. Gewicht = gewicht;
  66. Lengte = lengte;
  67. Leeftijd = leeftijd;
  68. Geslacht = geslacht;
  69. }
  70. //UITLEG
  71. public NetCommand(int session, string uitlegtext)
  72. {
  73. Type = CommandType.UITLEG;
  74. Session = session;
  75. Timestamp = Helper.Now;
  76. UitlegText = uitlegtext;
  77. }
  78. //SESSIONDATA
  79. public NetCommand(string name, double timestamp, int session)
  80. {
  81. Type = CommandType.SESSIONDATA;
  82. Session = session;
  83. Timestamp = timestamp;
  84. DisplayName = name;
  85. }
  86. //SESSIONDATA
  87. public NetCommand(string name, double timestamp, int session, bool old)
  88. {
  89. if (old)
  90. {
  91. Type = CommandType.OLDSESSIONDATA;
  92. Session = session;
  93. Timestamp = timestamp;
  94. DisplayName = name;
  95. }
  96. else
  97. {
  98. Type = CommandType.SESSIONDATA;
  99. Session = session;
  100. Timestamp = timestamp;
  101. DisplayName = name;
  102. }
  103. }
  104. //RESPONSE
  105. public NetCommand(ResponseType response, int session)
  106. {
  107. Type = CommandType.RESPONSE;
  108. Session = session;
  109. Timestamp = Helper.Now;
  110. Response = response;
  111. }
  112. //Length
  113. public NetCommand(LengthType lengthtype, int length, int session)
  114. {
  115. Type = CommandType.LENGTH;
  116. Length = lengthtype;
  117. Session = session;
  118. Timestamp = Helper.Now;
  119. LengthValue = length;
  120. }
  121. //REQUEST
  122. public NetCommand(RequestType request, int session)
  123. {
  124. Type = CommandType.REQUEST;
  125. Session = session;
  126. Timestamp = Helper.Now;
  127. Request = request;
  128. }
  129. //STANDARD
  130. public NetCommand(CommandType commandtype, int session)
  131. {
  132. Type = commandtype;
  133. Session = session;
  134. Timestamp = Helper.Now;
  135. }
  136. //METING
  137. public NetCommand(Meting m, int session)
  138. {
  139. Type = CommandType.DATA;
  140. Session = session;
  141. Timestamp = Helper.Now;
  142. Meting = m;
  143. }
  144. //CHAT
  145. public NetCommand(string chat, bool isDoctor, int session)
  146. {
  147. Type = CommandType.CHAT;
  148. Session = session;
  149. IsDoctor = isDoctor;
  150. Timestamp = Helper.Now;
  151. ChatMessage = chat.Replace("\n", "");
  152. }
  153. //BROADCAST
  154. public NetCommand(string broadcast, int session)
  155. {
  156. Type = CommandType.BROADCAST;
  157. Session = session;
  158. Timestamp = Helper.Now;
  159. ChatMessage = broadcast.Replace("\n", "");
  160. }
  161. //SETVALUE
  162. public NetCommand(ValueType value, int val, int session)
  163. {
  164. Type = CommandType.VALUESET;
  165. Session = session;
  166. Value = value;
  167. SetValue = val;
  168. }
  169. //USER
  170. public NetCommand(string username, string password, int session)
  171. {
  172. Type = CommandType.USER;
  173. Session = session;
  174. DisplayName = username;
  175. Password = password;
  176. }
  177. //LOGIN
  178. public NetCommand(string name, bool doctor, string password, int session)
  179. {
  180. Type = CommandType.LOGIN;
  181. Session = session;
  182. Timestamp = Helper.Now;
  183. DisplayName = name;
  184. IsDoctor = doctor;
  185. Password = password;
  186. }
  187. public static NetCommand Parse(string command)
  188. {
  189. string[] com = command.Split('»');
  190. int comType = 0;
  191. try
  192. {
  193. comType = int.Parse(com[0]);
  194. }
  195. catch(Exception e)
  196. {
  197. return new NetCommand(CommandType.ERROR, 0);
  198. }
  199. int session = 0;
  200. if (com[1].StartsWith("ses"))
  201. session = int.Parse(com[1].Substring(3));
  202. else
  203. throw new FormatException("Error in NetCommand: " + com[1] + " is not a valid session.");
  204. string[] args = new string[com.Length - 2];
  205. for (int i = 2; i < com.Length; i++)
  206. {
  207. args[i - 2] = com[i];
  208. }
  209. switch (comType)
  210. {
  211. case 1:
  212. return ParseLoginRequest(session, args);
  213. case 2:
  214. return ParseData(session, args);
  215. case 3:
  216. return ParseChatMessage(session, args);
  217. case 4:
  218. return ParseLogoutRequest(session, args);
  219. case 5:
  220. return ParseSession(session);
  221. case 6:
  222. return ParseResponse(session, args);
  223. case 7:
  224. return ParseValue(session, args);
  225. case 8:
  226. return ParseUser(session, args);
  227. case 9:
  228. return ParseRequest(session, args);
  229. case 10:
  230. return ParseLength(session, args);
  231. case 11:
  232. return ParseSessionData(session, args);
  233. case 12:
  234. return ParseBroadcast(session, args);
  235. case 13:
  236. return ParseUitleg(session, args);
  237. case 14:
  238. return ParsePersonData(session, args);
  239. case 15:
  240. return ParseTestResult(session, args);
  241. case 16:
  242. return ParseOldSessionData(session, args);
  243. default:
  244. throw new FormatException("Error in NetCommand: " + comType + " is not a valid command type.");
  245. }
  246. }
  247. private static NetCommand ParseOldSessionData(int session, string[] args)
  248. {
  249. if (args.Length != 2)
  250. throw new MissingFieldException("Error in NetCommand: OLD Session Data is missing arguments");
  251. NetCommand temp = new NetCommand(args[0], double.Parse(args[1]), session, true);
  252. return temp;
  253. }
  254. private static NetCommand ParseTestResult(int session, string[] args)
  255. {
  256. if (args.Length != 5)
  257. throw new MissingFieldException("Error in NetCommand: TestResult is missing arguments");
  258. NetCommand temp = new NetCommand(double.Parse(args[0]), double.Parse(args[1]), double.Parse(args[2]), double.Parse(args[3]), args[4], session);
  259. return temp;
  260. }
  261. private static NetCommand ParsePersonData(int session, string[] args)
  262. {
  263. if (args.Length != 4)
  264. throw new MissingFieldException("Error in NetCommand: PersonData is missing arguments");
  265. NetCommand temp = new NetCommand(int.Parse(args[0]), int.Parse(args[1]), int.Parse(args[2]), char.Parse(args[3]), session);
  266. return temp;
  267. }
  268. private static NetCommand ParseUitleg(int session, string[] args)
  269. {
  270. if (args.Length != 1)
  271. throw new MissingFieldException("Error in NetCommand: Uitleg is missing arguments");
  272. NetCommand temp = new NetCommand(session, args[0].Replace('«', '\n'));
  273. return temp;
  274. }
  275. private static NetCommand ParseBroadcast(int session, string[] args)
  276. {
  277. if (args.Length != 1)
  278. throw new MissingFieldException("Error in NetCommand: Broadcast Message is missing arguments");
  279. NetCommand temp = new NetCommand(args[0], session);
  280. return temp;
  281. }
  282. private static NetCommand ParseSessionData(int session, string[] args)
  283. {
  284. if (args.Length != 2)
  285. throw new MissingFieldException("Error in NetCommand: Session Data is missing arguments");
  286. NetCommand temp = new NetCommand(args[0], double.Parse(args[1]), session);
  287. return temp;
  288. }
  289. private static NetCommand ParseLength(int session, string[] args)
  290. {
  291. if (args.Length != 2)
  292. throw new MissingFieldException("Error in NetCommand: Length is missing arguments");
  293. switch (args[0])
  294. {
  295. case "users":
  296. return new NetCommand(LengthType.USERS, int.Parse(args[1]), session);
  297. case "sessiondata":
  298. return new NetCommand(LengthType.SESSIONDATA, int.Parse(args[1]), session);
  299. case "sessions":
  300. return new NetCommand(LengthType.SESSIONS, int.Parse(args[1]), session);
  301. case "data":
  302. return new NetCommand(LengthType.DATA, int.Parse(args[1]), session);
  303. default:
  304. throw new FormatException("Error in NetCommand: Length type not recognised");
  305. }
  306. }
  307. private static NetCommand ParseRequest(int session, string[] args)
  308. {
  309. if (args.Length != 1)
  310. throw new MissingFieldException("Error in NetCommand: Request is missing arguments");
  311. switch (args[0])
  312. {
  313. case "users":
  314. return new NetCommand(RequestType.USERS, session);
  315. case "allsessions":
  316. return new NetCommand(RequestType.ALLSESSIONS, session);
  317. case "olddata":
  318. return new NetCommand(RequestType.OLDDATA, session);
  319. case "sessiondata":
  320. return new NetCommand(RequestType.SESSIONDATA, session);
  321. case "chat":
  322. return new NetCommand(RequestType.CHAT, session);
  323. case "personaldata":
  324. return new NetCommand(RequestType.PERSONALDATA, session);
  325. case "testresult":
  326. return new NetCommand(RequestType.TESTRESULT, session);
  327. default:
  328. throw new FormatException("Error in NetCommand: Request type not recognised");
  329. }
  330. }
  331. private static NetCommand ParseUser(int session, string[] args)
  332. {
  333. if (args.Length != 2)
  334. throw new MissingFieldException("Error in NetCommand: User is missing arguments");
  335. NetCommand temp = new NetCommand(args[0], Helper.Base64Decode(args[1]), session);
  336. return temp;
  337. }
  338. private static NetCommand ParseValue(int session, string[] args)
  339. {
  340. if (args.Length != 2)
  341. throw new MissingFieldException("Error in NetCommand: SetValue is missing arguments");
  342. switch (args[0])
  343. {
  344. case "time":
  345. return new NetCommand(ValueType.TIME, int.Parse(args[1]), session);
  346. case "power":
  347. return new NetCommand(ValueType.POWER, int.Parse(args[1]), session);
  348. case "energy":
  349. return new NetCommand(ValueType.ENERGY, int.Parse(args[1]), session);
  350. case "distance":
  351. return new NetCommand(ValueType.DISTANCE, int.Parse(args[1]), session);
  352. default:
  353. throw new FormatException("Error in NetCommand: SetValue type not recognised");
  354. }
  355. }
  356. private static NetCommand ParseResponse(int session, string[] args)
  357. {
  358. if (args.Length != 1)
  359. throw new MissingFieldException("Error in NetCommand: Response is missing arguments");
  360. switch (args[0])
  361. {
  362. case "loginok":
  363. return new NetCommand(ResponseType.LOGINOK, session);
  364. case "loginwrong":
  365. return new NetCommand(ResponseType.LOGINWRONG, session);
  366. case "notloggedin":
  367. return new NetCommand(ResponseType.NOTLOGGEDIN, session);
  368. case "error":
  369. return new NetCommand(ResponseType.ERROR, session);
  370. default:
  371. throw new FormatException("Error in NetCommand: Response type not recognised");
  372. }
  373. }
  374. private static NetCommand ParseSession(int session)
  375. {
  376. NetCommand temp = new NetCommand(CommandType.SESSION, session);
  377. return temp;
  378. }
  379. private static NetCommand ParseLogoutRequest(int session, string[] args)
  380. {
  381. if (args.Length != 1)
  382. throw new MissingFieldException("Error in NetCommand: Logout Request is missing arguments");
  383. NetCommand temp = new NetCommand(CommandType.LOGOUT, session);
  384. if (args[0] != "logout")
  385. throw new FormatException("Error in NetCommand: " + args[0] + " is not a valid logout request");
  386. return temp;
  387. }
  388. private static NetCommand ParseChatMessage(int session, string[] args)
  389. {
  390. if (args.Length != 2)
  391. throw new MissingFieldException("Error in NetCommand: Chat Message is missing arguments");
  392. NetCommand temp = new NetCommand(CommandType.CHAT, session);
  393. temp.ChatMessage = args[0];
  394. temp.IsDoctor = bool.Parse(args[1]);
  395. return temp;
  396. }
  397. private static NetCommand ParseData(int session, string[] args)
  398. {
  399. if (args.Length != 9)
  400. throw new MissingFieldException("Error in NetCommand: Data is missing arguments");
  401. NetCommand temp = new NetCommand(CommandType.DATA, session);
  402. temp.Meting = Meting.Parse(string.Join("\t", args));
  403. return temp;
  404. }
  405. private static NetCommand ParseLoginRequest(int session, string[] args)
  406. {
  407. bool doctor = bool.Parse(args[2]);
  408. if (args.Length != 3)
  409. throw new MissingFieldException("Error in NetCommand: Doctor login is missing arguments");
  410. NetCommand temp = new NetCommand(CommandType.LOGIN, session);
  411. temp.IsDoctor = doctor;
  412. temp.DisplayName = args[0];
  413. temp.Password = Helper.Base64Decode(args[1]);
  414. return temp;
  415. }
  416. public override string ToString()
  417. {
  418. string command = "";
  419. switch (Type)
  420. {
  421. case CommandType.LOGIN:
  422. command += "1»ses" + Session + "»" + DisplayName + "»" + Helper.Base64Encode(Password) + "»" + IsDoctor;
  423. break;
  424. case CommandType.DATA:
  425. command += "2»ses" + Session + "»" + Meting.ToCommand();
  426. break;
  427. case CommandType.CHAT:
  428. command += "3»ses" + Session + "»" + ChatMessage + "»" + IsDoctor;
  429. break;
  430. case CommandType.LOGOUT:
  431. command += "4»ses" + Session + "»logout";
  432. break;
  433. case CommandType.SESSION:
  434. command += "5»ses" + Session;
  435. break;
  436. case CommandType.RESPONSE:
  437. command += "6»ses" + Session + "»" + Response.ToString().ToLower();
  438. break;
  439. case CommandType.VALUESET:
  440. command += "7»ses" + Session + "»" + Value.ToString().ToLower() + "»" + SetValue;
  441. break;
  442. case CommandType.USER:
  443. command += "8»ses" + Session + "»" + DisplayName + "»" + Helper.Base64Encode(Password);
  444. break;
  445. case CommandType.REQUEST:
  446. command += "9»ses" + Session + "»" + Request.ToString().ToLower();
  447. break;
  448. case CommandType.LENGTH:
  449. command += "10»ses" + Session + "»" + Length.ToString().ToLower() + "»" + LengthValue;
  450. break;
  451. case CommandType.SESSIONDATA:
  452. command += "11»ses" + Session + "»" + DisplayName + "»" + Timestamp;
  453. break;
  454. case CommandType.BROADCAST:
  455. command += "12»ses" + Session + "»" + ChatMessage;
  456. break;
  457. case CommandType.UITLEG:
  458. command += "13»ses" + Session + "»" + UitlegText.Replace('\n', '«');
  459. break;
  460. case CommandType.PERSONDATA:
  461. command += "14»ses" + Session + "»" + Gewicht + "»" + Lengte + "»" + Leeftijd + "»" + Geslacht;
  462. Console.WriteLine(command);
  463. break;
  464. case CommandType.TESTRESULT:
  465. command += "15»ses" + Session + "»" + VO2Max + "»" + MET + "»" + PopulationAvg + "»" + ZScore + "»" + Rating;
  466. break;
  467. case CommandType.OLDSESSIONDATA:
  468. command += "16»ses" + Session + "»" + DisplayName + "»" + Timestamp;
  469. break;
  470. case CommandType.ERROR:
  471. command += "ERROR IN NETCOMMAND";
  472. break;
  473. default:
  474. throw new FormatException("Error in NetCommand: Cannot find type of command");
  475. }
  476. return command;
  477. }
  478. }
  479. }