NetCommand.cs 15 KB

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