NetCommand.cs 19 KB

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