FileHandler.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. using ErgometerLibrary;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. namespace ErgometerServer
  7. {
  8. public class FileHandler
  9. {
  10. public static string DataFolder { get; } = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments), "Ergometer");
  11. public static string UsersFile { get; } = Path.Combine(DataFolder, "users.ergo");
  12. public static void CheckStorage()
  13. {
  14. if (!Directory.Exists(DataFolder))
  15. {
  16. Directory.CreateDirectory(DataFolder);
  17. }
  18. if (!File.Exists(UsersFile))
  19. {
  20. using (Stream stream = File.Open(UsersFile, FileMode.Create))
  21. {
  22. BinaryWriter writer = new BinaryWriter(stream);
  23. writer.Write(1);
  24. writer.Write("Doctor0tVfW");
  25. writer.Write(Helper.Base64Encode("password"));
  26. }
  27. }
  28. }
  29. //SESSION
  30. public static int GenerateSession()
  31. {
  32. string[] existingSessions = Directory.GetDirectories(DataFolder);
  33. Random rand = new Random();
  34. int sessionID = rand.Next(0, int.MaxValue);
  35. while (existingSessions.Contains(sessionID.ToString()))
  36. {
  37. sessionID = rand.Next(int.MinValue, int.MaxValue);
  38. }
  39. return sessionID;
  40. }
  41. public static void CreateSession(int session, string naam)
  42. {
  43. Directory.CreateDirectory(GetSessionFolder(session));
  44. using (File.Create(Path.Combine(GetSessionFile(session)))) ;
  45. using (File.Create(Path.Combine(GetSessionMetingen(session)))) ;
  46. using (File.Create(Path.Combine(GetSessionChat(session)))) ;
  47. using (File.Create(Path.Combine(GetSessionTest(session)))) ;
  48. using (File.Create(Path.Combine(GetSessionPersonalData(session)))) ;
  49. File.WriteAllText(GetSessionFile(session), naam + Environment.NewLine + Helper.Now);
  50. Console.WriteLine("Created session at " + Helper.MillisecondsToTime(Helper.Now));
  51. }
  52. public static void WriteMetingen(int session, List<Meting> metingen)
  53. {
  54. if (metingen.Count <= 20 && Directory.Exists(GetSessionFolder(session)))
  55. {
  56. Directory.Delete(GetSessionFolder(session), true);
  57. }
  58. else
  59. {
  60. string json = Newtonsoft.Json.JsonConvert.SerializeObject(metingen.ToArray());
  61. File.WriteAllText(GetSessionMetingen(session), json);
  62. Console.WriteLine("Writing metingen: " + GetSessionMetingen(session));
  63. File.WriteAllText(GetSessionFile(session), File.ReadAllText(GetSessionFile(session)) + Environment.NewLine + Helper.Now);
  64. }
  65. }
  66. public static List<Meting> ReadMetingen(int session)
  67. {
  68. string json = File.ReadAllText(GetSessionMetingen(session));
  69. List<Meting> metingen = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Meting>>(json);
  70. if (metingen == null)
  71. metingen = new List<Meting>();
  72. Console.WriteLine("Reading metingen: " + GetSessionMetingen(session));
  73. return metingen;
  74. }
  75. public static List<Tuple<int, string, double>> GetAllSessions()
  76. {
  77. string[] directories = Directory.GetDirectories(DataFolder);
  78. List<Tuple<int,string,double>> sessiondata = new List<Tuple<int, string, double>>();
  79. for (int i = 0; i < directories.Length; i++)
  80. {
  81. string directoryname = Path.GetFileName(directories[i]);
  82. string props = File.ReadAllText(GetSessionFile(int.Parse(directoryname)));
  83. string[] stringSeparators = new string[] { "\r\n" };
  84. string[] properties = props.Split(stringSeparators, StringSplitOptions.None);
  85. int session = int.Parse(directoryname);
  86. bool active = false;
  87. foreach(ClientThread t in Server.clients)
  88. {
  89. if (t.session == session)
  90. active = true;
  91. }
  92. string name = properties[0];
  93. double date = double.Parse(properties[1]);
  94. if (!active)
  95. {
  96. Tuple<int, string, double> tup = new Tuple<int, string, double>(session, name, date);
  97. sessiondata.Add(tup);
  98. }
  99. }
  100. return sessiondata;
  101. }
  102. public static void WriteChat(int session, List<ChatMessage> chat)
  103. {
  104. if (Directory.Exists(GetSessionFolder(session)))
  105. {
  106. /*
  107. string write = "";
  108. foreach (ChatMessage c in chat)
  109. {
  110. write += c.ToString() + "\n";
  111. }
  112. File.WriteAllText(GetSessionChat(session), write);
  113. Console.WriteLine("Writing chat: " + GetSessionChat(session));
  114. */
  115. string json = Newtonsoft.Json.JsonConvert.SerializeObject(chat.ToArray());
  116. File.WriteAllText(GetSessionChat(session), json);
  117. Console.WriteLine("Writing chat: " + GetSessionChat(session));
  118. }
  119. }
  120. public static List<ChatMessage> ReadChat(int session)
  121. {
  122. string json = File.ReadAllText(GetSessionChat(session));
  123. List<ChatMessage> chat = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ChatMessage>>(json);
  124. if (chat == null)
  125. chat = new List<ChatMessage>();
  126. return chat;
  127. }
  128. private static string GetSessionFolder(int session)
  129. {
  130. return Path.Combine(DataFolder, session.ToString());
  131. }
  132. private static string GetSessionFile(int session)
  133. {
  134. return Path.Combine(DataFolder, session.ToString(), "session.prop");
  135. }
  136. private static string GetSessionMetingen(int session)
  137. {
  138. return Path.Combine(DataFolder, session.ToString(), "metingen.ergo");
  139. }
  140. private static string GetSessionChat(int session)
  141. {
  142. return Path.Combine(DataFolder, session.ToString(), "chat.ergo");
  143. }
  144. private static string GetSessionTest(int session)
  145. {
  146. return Path.Combine(DataFolder, session.ToString(), "testresult.ergo");
  147. }
  148. private static string GetSessionPersonalData(int session)
  149. {
  150. return Path.Combine(DataFolder, session.ToString(), "personal.ergo");
  151. }
  152. //USER MANAGEMENT
  153. public static Dictionary<string, string> LoadUsers()
  154. {
  155. Dictionary<string, string> users = new Dictionary<string, string>();
  156. using (Stream stream = File.Open(UsersFile, FileMode.Open))
  157. {
  158. BinaryReader reader = new BinaryReader(stream);
  159. int count = reader.ReadInt32();
  160. for (int n = 0; n < count; n++)
  161. {
  162. var key = reader.ReadString();
  163. var value = Helper.Base64Decode(reader.ReadString());
  164. users.Add(key, value);
  165. }
  166. }
  167. return users;
  168. }
  169. public static void SaveUsers(Dictionary<string, string> users)
  170. {
  171. using (Stream stream = File.Open(UsersFile, FileMode.Open))
  172. {
  173. BinaryWriter writer = new BinaryWriter(stream);
  174. writer.Write(users.Count);
  175. foreach (var kvp in users)
  176. {
  177. writer.Write(kvp.Key);
  178. writer.Write(Helper.Base64Encode(kvp.Value));
  179. }
  180. writer.Flush();
  181. }
  182. }
  183. // TESTRESULTS
  184. public static void SaveTestResult(NetCommand input)
  185. {
  186. using (Stream stream = File.Open(GetSessionTest(input.Session), FileMode.Open))
  187. {
  188. BinaryWriter writer = new BinaryWriter(stream);
  189. writer.Write(input.VO2Max);
  190. writer.Write(input.MET);
  191. writer.Write(input.PopulationAvg);
  192. writer.Write(input.ZScore);
  193. writer.Write(input.Rating);
  194. writer.Flush();
  195. }
  196. }
  197. public static NetCommand ReadTestResult(int session)
  198. {
  199. NetCommand command = new NetCommand(NetCommand.CommandType.ERROR, session);
  200. using (Stream stream = File.Open(GetSessionTest(session), FileMode.Open))
  201. {
  202. command = new NetCommand(NetCommand.CommandType.TESTRESULT, session);
  203. BinaryReader reader = new BinaryReader(stream);
  204. try {
  205. command.VO2Max = reader.ReadDouble();
  206. command.MET = reader.ReadDouble();
  207. command.PopulationAvg = reader.ReadDouble();
  208. command.ZScore = reader.ReadDouble();
  209. command.Rating = reader.ReadString();
  210. }
  211. catch(Exception e)
  212. {
  213. command.VO2Max = 0;
  214. command.MET = 0;
  215. command.PopulationAvg = 0;
  216. command.ZScore = 0;
  217. command.Rating = "Niet afgerond";
  218. }
  219. }
  220. return command;
  221. }
  222. // PERSONALDATA
  223. public static void SavePersonalData(NetCommand input)
  224. {
  225. using (Stream stream = File.Open(GetSessionPersonalData(input.Session), FileMode.Open))
  226. {
  227. BinaryWriter writer = new BinaryWriter(stream);
  228. writer.Write(input.Geslacht);
  229. writer.Write(input.Gewicht);
  230. writer.Write(input.Leeftijd);
  231. writer.Write(input.Lengte);
  232. writer.Flush();
  233. }
  234. }
  235. public static NetCommand ReadPersonalData(int session)
  236. {
  237. NetCommand command = new NetCommand(NetCommand.CommandType.ERROR, session);
  238. using (Stream stream = File.Open(GetSessionPersonalData(session), FileMode.Open))
  239. {
  240. command = new NetCommand(NetCommand.CommandType.PERSONDATA, session);
  241. BinaryReader reader = new BinaryReader(stream);
  242. try {
  243. command.Geslacht = reader.ReadChar();
  244. command.Gewicht = reader.ReadInt32();
  245. command.Leeftijd = reader.ReadInt32();
  246. command.Lengte = reader.ReadInt32();
  247. }
  248. catch (Exception e)
  249. {
  250. command = new NetCommand(NetCommand.CommandType.ERROR, session);
  251. }
  252. }
  253. return command;
  254. }
  255. }
  256. }