NetCommand.cs 17 KB

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