| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- using ErgometerLibrary;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- namespace ErgometerServer
- {
- public class FileHandler
- {
- public static string DataFolder { get; } = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments), "Ergometer");
- public static string UsersFile { get; } = Path.Combine(DataFolder, "users.ergo");
- public static void CheckStorage()
- {
- if (!Directory.Exists(DataFolder))
- {
- Directory.CreateDirectory(DataFolder);
- }
- if (!File.Exists(UsersFile))
- {
- using (Stream stream = File.Open(UsersFile, FileMode.Create))
- {
- BinaryWriter writer = new BinaryWriter(stream);
- writer.Write(1);
- writer.Write("Doctor0tVfW");
- writer.Write(Helper.Base64Encode("password"));
- }
- }
- }
- //SESSION
- public static int GenerateSession()
- {
- string[] existingSessions = Directory.GetDirectories(DataFolder);
- Random rand = new Random();
- int sessionID = rand.Next(0, int.MaxValue);
- while (existingSessions.Contains(sessionID.ToString()))
- {
- sessionID = rand.Next(int.MinValue, int.MaxValue);
- }
- return sessionID;
- }
- public static void CreateSession(int session, string naam)
- {
- Directory.CreateDirectory(GetSessionFolder(session));
- using (File.Create(Path.Combine(GetSessionFile(session)))) ;
- using (File.Create(Path.Combine(GetSessionMetingen(session)))) ;
- using (File.Create(Path.Combine(GetSessionChat(session)))) ;
- using (File.Create(Path.Combine(GetSessionTest(session)))) ;
- using (File.Create(Path.Combine(GetSessionPersonalData(session)))) ;
- File.WriteAllText(GetSessionFile(session), naam + Environment.NewLine + Helper.Now);
- Console.WriteLine("Created session at " + Helper.MillisecondsToTime(Helper.Now));
- }
- public static void WriteMetingen(int session, List<Meting> metingen)
- {
- if (metingen.Count <= 20 && Directory.Exists(GetSessionFolder(session)))
- {
- Directory.Delete(GetSessionFolder(session), true);
- }
- else
- {
- string json = Newtonsoft.Json.JsonConvert.SerializeObject(metingen.ToArray());
- File.WriteAllText(GetSessionMetingen(session), json);
- Console.WriteLine("Writing metingen: " + GetSessionMetingen(session));
- File.WriteAllText(GetSessionFile(session), File.ReadAllText(GetSessionFile(session)) + Environment.NewLine + Helper.Now);
- }
-
- }
- public static List<Meting> ReadMetingen(int session)
- {
- string json = File.ReadAllText(GetSessionMetingen(session));
- List<Meting> metingen = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Meting>>(json);
- if (metingen == null)
- metingen = new List<Meting>();
- Console.WriteLine("Reading metingen: " + GetSessionMetingen(session));
- return metingen;
- }
- public static List<Tuple<int, string, double>> GetAllSessions()
- {
- string[] directories = Directory.GetDirectories(DataFolder);
- List<Tuple<int,string,double>> sessiondata = new List<Tuple<int, string, double>>();
- for (int i = 0; i < directories.Length; i++)
- {
- string directoryname = Path.GetFileName(directories[i]);
- string props = File.ReadAllText(GetSessionFile(int.Parse(directoryname)));
- string[] stringSeparators = new string[] { "\r\n" };
- string[] properties = props.Split(stringSeparators, StringSplitOptions.None);
- int session = int.Parse(directoryname);
- bool active = false;
- foreach(ClientThread t in Server.clients)
- {
- if (t.session == session)
- active = true;
- }
- string name = properties[0];
- double date = double.Parse(properties[1]);
- if (!active)
- {
- Tuple<int, string, double> tup = new Tuple<int, string, double>(session, name, date);
- sessiondata.Add(tup);
- }
- }
- return sessiondata;
- }
- public static void WriteChat(int session, List<ChatMessage> chat)
- {
- if (Directory.Exists(GetSessionFolder(session)))
- {
- /*
- string write = "";
- foreach (ChatMessage c in chat)
- {
- write += c.ToString() + "\n";
- }
- File.WriteAllText(GetSessionChat(session), write);
- Console.WriteLine("Writing chat: " + GetSessionChat(session));
- */
- string json = Newtonsoft.Json.JsonConvert.SerializeObject(chat.ToArray());
- File.WriteAllText(GetSessionChat(session), json);
- Console.WriteLine("Writing chat: " + GetSessionChat(session));
- }
- }
- public static List<ChatMessage> ReadChat(int session)
- {
- string json = File.ReadAllText(GetSessionChat(session));
- List<ChatMessage> chat = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ChatMessage>>(json);
- if (chat == null)
- chat = new List<ChatMessage>();
- return chat;
- }
- private static string GetSessionFolder(int session)
- {
- return Path.Combine(DataFolder, session.ToString());
- }
- private static string GetSessionFile(int session)
- {
- return Path.Combine(DataFolder, session.ToString(), "session.prop");
- }
- private static string GetSessionMetingen(int session)
- {
- return Path.Combine(DataFolder, session.ToString(), "metingen.ergo");
- }
- private static string GetSessionChat(int session)
- {
- return Path.Combine(DataFolder, session.ToString(), "chat.ergo");
- }
- private static string GetSessionTest(int session)
- {
- return Path.Combine(DataFolder, session.ToString(), "testresult.ergo");
- }
- private static string GetSessionPersonalData(int session)
- {
- return Path.Combine(DataFolder, session.ToString(), "personal.ergo");
- }
- //USER MANAGEMENT
- public static Dictionary<string, string> LoadUsers()
- {
- Dictionary<string, string> users = new Dictionary<string, string>();
- using (Stream stream = File.Open(UsersFile, FileMode.Open))
- {
- BinaryReader reader = new BinaryReader(stream);
- int count = reader.ReadInt32();
- for (int n = 0; n < count; n++)
- {
- var key = reader.ReadString();
- var value = Helper.Base64Decode(reader.ReadString());
- users.Add(key, value);
- }
- }
- return users;
- }
- public static void SaveUsers(Dictionary<string, string> users)
- {
- using (Stream stream = File.Open(UsersFile, FileMode.Open))
- {
- BinaryWriter writer = new BinaryWriter(stream);
- writer.Write(users.Count);
- foreach (var kvp in users)
- {
- writer.Write(kvp.Key);
- writer.Write(Helper.Base64Encode(kvp.Value));
- }
- writer.Flush();
- }
- }
- // TESTRESULTS
- public static void SaveTestResult(NetCommand input)
- {
- using (Stream stream = File.Open(GetSessionTest(input.Session), FileMode.Open))
- {
- BinaryWriter writer = new BinaryWriter(stream);
- writer.Write(input.VO2Max);
- writer.Write(input.MET);
- writer.Write(input.PopulationAvg);
- writer.Write(input.ZScore);
- writer.Write(input.Rating);
- writer.Flush();
- }
- }
- public static NetCommand ReadTestResult(int session)
- {
- NetCommand command = new NetCommand(NetCommand.CommandType.ERROR, session);
- using (Stream stream = File.Open(GetSessionTest(session), FileMode.Open))
- {
- command = new NetCommand(NetCommand.CommandType.TESTRESULT, session);
- BinaryReader reader = new BinaryReader(stream);
- try {
- command.VO2Max = reader.ReadDouble();
- command.MET = reader.ReadDouble();
- command.PopulationAvg = reader.ReadDouble();
- command.ZScore = reader.ReadDouble();
- command.Rating = reader.ReadString();
- }
- catch(Exception e)
- {
- command.VO2Max = 0;
- command.MET = 0;
- command.PopulationAvg = 0;
- command.ZScore = 0;
- command.Rating = "Niet afgerond";
- }
- }
- return command;
- }
- // PERSONALDATA
- public static void SavePersonalData(NetCommand input)
- {
- using (Stream stream = File.Open(GetSessionPersonalData(input.Session), FileMode.Open))
- {
- BinaryWriter writer = new BinaryWriter(stream);
- writer.Write(input.Geslacht);
- writer.Write(input.Gewicht);
- writer.Write(input.Leeftijd);
- writer.Write(input.Lengte);
- writer.Flush();
- }
- }
- public static NetCommand ReadPersonalData(int session)
- {
- NetCommand command = new NetCommand(NetCommand.CommandType.ERROR, session);
- using (Stream stream = File.Open(GetSessionPersonalData(session), FileMode.Open))
- {
- command = new NetCommand(NetCommand.CommandType.PERSONDATA, session);
- BinaryReader reader = new BinaryReader(stream);
- try {
- command.Geslacht = reader.ReadChar();
- command.Gewicht = reader.ReadInt32();
- command.Leeftijd = reader.ReadInt32();
- command.Lengte = reader.ReadInt32();
- }
- catch (Exception e)
- {
- command = new NetCommand(NetCommand.CommandType.ERROR, session);
- }
- }
- return command;
- }
- }
- }
|