NetCommand.cs 18 KB

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