NetCommand.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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. 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. default:
  297. throw new FormatException("Error in NetCommand: Request type not recognised");
  298. }
  299. }
  300. private static NetCommand ParseUser(int session, string[] args)
  301. {
  302. if (args.Length != 2)
  303. throw new MissingFieldException("Error in NetCommand: User is missing arguments");
  304. NetCommand temp = new NetCommand(args[0], Helper.Base64Decode(args[1]), session);
  305. return temp;
  306. }
  307. private static NetCommand ParseValue(int session, string[] args)
  308. {
  309. if (args.Length != 2)
  310. throw new MissingFieldException("Error in NetCommand: SetValue is missing arguments");
  311. switch (args[0])
  312. {
  313. case "time":
  314. return new NetCommand(ValueType.TIME, int.Parse(args[1]), session);
  315. case "power":
  316. return new NetCommand(ValueType.POWER, int.Parse(args[1]), session);
  317. case "energy":
  318. return new NetCommand(ValueType.ENERGY, int.Parse(args[1]), session);
  319. case "distance":
  320. return new NetCommand(ValueType.DISTANCE, int.Parse(args[1]), session);
  321. default:
  322. throw new FormatException("Error in NetCommand: SetValue type not recognised");
  323. }
  324. }
  325. private static NetCommand ParseResponse(int session, string[] args)
  326. {
  327. if (args.Length != 1)
  328. throw new MissingFieldException("Error in NetCommand: Response is missing arguments");
  329. switch (args[0])
  330. {
  331. case "loginok":
  332. return new NetCommand(ResponseType.LOGINOK, session);
  333. case "loginwrong":
  334. return new NetCommand(ResponseType.LOGINWRONG, session);
  335. case "notloggedin":
  336. return new NetCommand(ResponseType.NOTLOGGEDIN, session);
  337. case "error":
  338. return new NetCommand(ResponseType.ERROR, session);
  339. default:
  340. throw new FormatException("Error in NetCommand: Response type not recognised");
  341. }
  342. }
  343. private static NetCommand ParseSession(int session)
  344. {
  345. NetCommand temp = new NetCommand(CommandType.SESSION, session);
  346. return temp;
  347. }
  348. private static NetCommand ParseLogoutRequest(int session, string[] args)
  349. {
  350. if (args.Length != 1)
  351. throw new MissingFieldException("Error in NetCommand: Logout Request is missing arguments");
  352. NetCommand temp = new NetCommand(CommandType.LOGOUT, session);
  353. if (args[0] != "logout")
  354. throw new FormatException("Error in NetCommand: " + args[0] + " is not a valid logout request");
  355. return temp;
  356. }
  357. private static NetCommand ParseChatMessage(int session, string[] args)
  358. {
  359. if (args.Length != 2)
  360. throw new MissingFieldException("Error in NetCommand: Chat Message is missing arguments");
  361. NetCommand temp = new NetCommand(CommandType.CHAT, session);
  362. temp.ChatMessage = args[0];
  363. temp.IsDoctor = bool.Parse(args[1]);
  364. return temp;
  365. }
  366. private static NetCommand ParseData(int session, string[] args)
  367. {
  368. if (args.Length != 9)
  369. throw new MissingFieldException("Error in NetCommand: Data is missing arguments");
  370. NetCommand temp = new NetCommand(CommandType.DATA, session);
  371. temp.Meting = Meting.Parse(string.Join("\t", args));
  372. return temp;
  373. }
  374. private static NetCommand ParseLoginRequest(int session, string[] args)
  375. {
  376. bool doctor = bool.Parse(args[2]);
  377. if (args.Length != 3)
  378. throw new MissingFieldException("Error in NetCommand: Doctor login is missing arguments");
  379. NetCommand temp = new NetCommand(CommandType.LOGIN, session);
  380. temp.IsDoctor = doctor;
  381. temp.DisplayName = args[0];
  382. temp.Password = Helper.Base64Decode(args[1]);
  383. return temp;
  384. }
  385. public override string ToString()
  386. {
  387. string command = "";
  388. switch (Type)
  389. {
  390. case CommandType.LOGIN:
  391. command += "1»ses" + Session + "»" + DisplayName + "»" + Helper.Base64Encode(Password) + "»" + IsDoctor;
  392. break;
  393. case CommandType.DATA:
  394. command += "2»ses" + Session + "»" + Meting.ToCommand();
  395. break;
  396. case CommandType.CHAT:
  397. command += "3»ses" + Session + "»" + ChatMessage + "»" + IsDoctor;
  398. break;
  399. case CommandType.LOGOUT:
  400. command += "4»ses" + Session + "»logout";
  401. break;
  402. case CommandType.SESSION:
  403. command += "5»ses" + Session;
  404. break;
  405. case CommandType.RESPONSE:
  406. command += "6»ses" + Session + "»" + Response.ToString().ToLower();
  407. break;
  408. case CommandType.VALUESET:
  409. command += "7»ses" + Session + "»" + Value.ToString().ToLower() + "»" + SetValue;
  410. break;
  411. case CommandType.USER:
  412. command += "8»ses" + Session + "»" + DisplayName + "»" + Helper.Base64Encode(Password);
  413. break;
  414. case CommandType.REQUEST:
  415. command += "9»ses" + Session + "»" + Request.ToString().ToLower();
  416. break;
  417. case CommandType.LENGTH:
  418. command += "10»ses" + Session + "»" + Length.ToString().ToLower() + "»" + LengthValue;
  419. break;
  420. case CommandType.SESSIONDATA:
  421. command += "11»ses" + Session + "»" + DisplayName + "»" + Timestamp;
  422. break;
  423. case CommandType.BROADCAST:
  424. command += "12»ses" + Session + "»" + ChatMessage;
  425. break;
  426. case CommandType.UITLEG:
  427. command += "13»ses" + Session + "»" + UitlegText.Replace('\n', '«');
  428. break;
  429. case CommandType.PERSONDATA:
  430. command += "13»ses" + Session + "»" + Gewicht + "»" + Lengte + "»" + Leeftijd + "»" + Geslacht;
  431. break;
  432. case CommandType.TESTRESULT:
  433. command += "13»ses" + Session + "»" + VO2Max + "»" + MET + "»" + PopulationAvg + "»" + ZScore + "»" + Rating;
  434. break;
  435. case CommandType.ERROR:
  436. command += "ERROR IN NETCOMMAND";
  437. break;
  438. default:
  439. throw new FormatException("Error in NetCommand: Cannot find type of command");
  440. }
  441. return command;
  442. }
  443. }
  444. }