Example #1
0
 public void HandleCommand(NetCommand command)
 {
     switch (command.Type)
     {
         case NetCommand.CommandType.DATA:
             if (command != null && command.Meting != null)
             {
                 lock (Metingen)
                 {
                     Metingen.Add(command.Meting);
                 }
                 window.Invoke(window.updateMetingen, new Object[] { command.Meting });
             }
             break;
         case NetCommand.CommandType.CHAT:
             ChatMessage chat = new ChatMessage(command.DisplayName, command.ChatMessage, command.IsDoctor);
             Chat.Add(chat);
             window.panelClientChat.Invoke(window.panelClientChat.passChatMessage, new Object[] { chat });
             break;
         case NetCommand.CommandType.STEP:
             int stepID = command.stepID;
             window.panelClientChat.Invoke(window.panelClientChat.passStep, new Object[] { stepID });
             break;
     }
 }
Example #2
0
 public void HandleCommand(NetCommand command)
 {
     switch (command.Type)
     {
         case NetCommand.CommandType.DATA:
             if (command != null && command.Meting != null)
             {
                 lock (Metingen)
                 {
                     Metingen.Add(command.Meting);
                 }
                 window.Invoke(window.updateMetingen, new Object[] { command.Meting });
             }
             break;
         case NetCommand.CommandType.CHAT:
             ChatMessage chat = new ChatMessage(command.DisplayName, command.ChatMessage, command.IsDoctor);
             Chat.Add(chat);
             window.panelClientChat.Invoke(window.panelClientChat.passChatMessage, new Object[] { chat });
             break;
         case NetCommand.CommandType.UITLEG:
             window.steps.UitlegText.Invoke((MethodInvoker)(() => window.steps.UitlegText.Text = command.UitlegText));
             break;
         case NetCommand.CommandType.PERSONDATA:
             window.panelClientChat.Invoke(window.panelClientChat.passChatMessage, new Object[] { new ChatMessage("Test", "DATA Gewicht=" + command.Gewicht + " Geslacht=" + command.Geslacht + " Leeftijd=" + command.Leeftijd + " Lengte=" + command.Lengte + ".", false) });
             Console.WriteLine("Not fully implemented");
             //NOG DOEN
             break;
         case NetCommand.CommandType.TESTRESULT:
             window.panelClientChat.Invoke(window.panelClientChat.passChatMessage, new Object[] { new ChatMessage("Test", "DATA Vo2Max=" + command.VO2Max + " MET=" + command.MET + " PopulationAvg=" + command.PopulationAvg + " Zscore=" + command.ZScore + " Rating=" + command.Rating + ".", false) });
             Console.WriteLine("Not fully implemented");
             //NOG DOEN
             break;
     }
 }
        private static NetCommand ParseLogoutRequest(int session, string[] args)
        {
            if (args.Length != 1)
            {
                throw new MissingFieldException("Error in NetCommand: Logout Request is missing arguments");
            }

            NetCommand temp = new NetCommand(CommandType.LOGOUT, session);

            if (args[0] != "logout")
            {
                throw new FormatException("Error in NetCommand: " + args[0] + " is not a valid logout request");
            }

            return(temp);
        }
Example #4
0
        private static NetCommand ParseLoginRequest(int session, string[] args)
        {
            bool doctor = bool.Parse(args[2]);

            if (args.Length != 3)
            {
                throw new MissingFieldException("Error in NetCommand: Doctor login is missing arguments");
            }

            NetCommand temp = new NetCommand(CommandType.LOGIN, session);

            temp.IsDoctor    = doctor;
            temp.DisplayName = args[0];
            temp.Password    = Helper.Base64Decode(args[1]);

            return(temp);
        }
        public static NetCommand ReadNetCommand(TcpClient client)
        {
            /*
             * byte[] bytesFrom = new byte[(int) client.ReceiveBufferSize];
             * client.GetStream().Read(bytesFrom, 0, (int)client.ReceiveBufferSize);
             * string response = Encoding.Unicode.GetString(bytesFrom);
             * NetCommand net = NetCommand.Parse(response);
             * return net;
             */

            string     str;
            NetCommand net;

            str = ReadString(client);

            if (str != "")
            {
                try
                {
                    str = AESEncrypt.DecryptString(str);
                }
                catch (Exception e)
                {
                    str = "";
                    net = new NetCommand(NetCommand.CommandType.ERROR, 0);
                }
                try {
                    net = NetCommand.Parse(str);
                }
                catch (Exception e)
                {
                    net = new NetCommand(NetCommand.CommandType.ERROR, 0);
                }
            }
            else
            {
                net = new NetCommand(NetCommand.CommandType.ERROR, 0);
            }
            return(net);
        }
Example #6
0
        public static NetCommand ReadNetCommand(TcpClient client)
        {
            /*
            byte[] bytesFrom = new byte[(int) client.ReceiveBufferSize];
            client.GetStream().Read(bytesFrom, 0, (int)client.ReceiveBufferSize);
            string response = Encoding.Unicode.GetString(bytesFrom);
            NetCommand net = NetCommand.Parse(response);
            return net;
            */

            string str;
            NetCommand net;
            str = ReadString(client);

            if (str != "")
            {
                try
                {
                    str = AESEncrypt.DecryptString(str);
                }
                catch (Exception e)
                {
                    str = "";
                    net = new NetCommand(NetCommand.CommandType.ERROR, 0);
                }
                try {
                    net = NetCommand.Parse(str);
                }
                catch (Exception e)
                {
                    net = new NetCommand(NetCommand.CommandType.ERROR, 0);
                }
            }
            else
                net = new NetCommand(NetCommand.CommandType.ERROR, 0);
            return net;
        }
Example #7
0
        private static NetCommand ParseData(int session, string[] args)
        {
            if (args.Length != 9)
                throw new MissingFieldException("Error in NetCommand: Data is missing arguments");

            NetCommand temp = new NetCommand(CommandType.DATA, session);
            temp.Meting = Meting.Parse(string.Join("\t", args));

            return temp;
        }
Example #8
0
        private static void HandToClient(NetCommand command)
        {
            bool handled = false;
            foreach (ClientThread cl in clients)
            {
                if (cl.Session == command.Session)
                {
                    cl.HandleCommand(command);
                    handled = true;
                }
            }

            if(!handled)
            {
                backlog.Add(command);
            }
        }
Example #9
0
 // 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();
     }
 }
Example #10
0
        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;
        }
Example #11
0
        private static NetCommand ParseUitleg(int session, string[] args)
        {
            if (args.Length != 1)
                throw new MissingFieldException("Error in NetCommand: Uitleg is missing arguments");

            NetCommand temp = new NetCommand(session, args[0].Replace('«', '\n'));

            return temp;
        }
Example #12
0
        private static NetCommand ParseSessionData(int session, string[] args)
        {
            if (args.Length != 2)
                throw new MissingFieldException("Error in NetCommand: Session Data is missing arguments");

            NetCommand temp = new NetCommand(args[0], double.Parse(args[1]), session);

            return temp;
        }
Example #13
0
        private static NetCommand ParsePersonData(int session, string[] args)
        {
            if (args.Length != 4)
                throw new MissingFieldException("Error in NetCommand: PersonData is missing arguments");

            NetCommand temp = new NetCommand(int.Parse(args[0]), int.Parse(args[1]), int.Parse(args[2]), char.Parse(args[3]), session);

            return temp;
        }
Example #14
0
        private static NetCommand ParseStepID(int session, string[] args)
        {
            if (args.Length != 1)
                throw new MissingFieldException("Error in NetCommand: STEP Message is missing arguments");

            NetCommand temp = new NetCommand(int.Parse(args[0]), session);

            return temp;
        }
Example #15
0
        public static bool Connect(string comport, string name, string password, out string error)
        {
            error = "Succes";

            if (!ComPort.IsOpen())
            {
                if (ComPort.Connect(comport))
                {
                    ComPort.Write("RS");
                    string temp = ComPort.Read().Replace("\r", "").Replace("\n", "");
                    Console.WriteLine(temp);
                    if (temp.ToLower() != "ack")
                    {
                        ComPort.Disconnect();
                        error = "De Ergometer is niet verbonden";
                        return false;
                    }
                    Thread.Sleep(200);

                    ComPort.Write("CM");
                    ComPort.Read();

                    ComPort.Write("PW " + 25);
                    ComPort.Read();

                    ComPort.Write("ST");
                    string response = ComPort.Read();

                    SaveMeting(response);
                }
                else
                {
                    error = "De ergometer is niet verbonden";
                    return false;
                }
            }

            if (Doctor == null || !Doctor.Connected)
            {
                if (Doctor == null)
                    Doctor = new TcpClient();

                try
                {
                    Doctor.Connect(HOST, PORT);
                }
                catch (Exception e)
                {
                    error = "Server is niet online.";
                    return false;
                }

                Name = name;

                NetCommand net = NetHelper.ReadNetCommand(Doctor);
                if (net.Type == NetCommand.CommandType.SESSION)
                    Session = net.Session;
                else
                    throw new Exception("Session not assigned");

                running = true;

                t = new Thread(run);
                t.IsBackground = true;
                t.Start();
            }

            if (!Loggedin)
            {
                NetCommand command = new NetCommand(name, false, password, Session);
                NetHelper.SendNetCommand(Doctor, command);

                NetCommand response = NetHelper.ReadNetCommand(Doctor);
                if (response.Type == NetCommand.CommandType.RESPONSE && response.Response == NetCommand.ResponseType.LOGINWRONG)
                {
                    Loggedin = false;
                    error = "De inloggegevens zijn onjuist.";
                    return false;
                }

                Loggedin = true;
            }

            return true;
        }
Example #16
0
        private static void ParseValueSet(NetCommand command)
        {
            switch(command.Value)
            {
                case NetCommand.ValueType.DISTANCE:
                    ComPort.Write("RS");
                    ComPort.Read();
                    Thread.Sleep(200);
                    ComPort.Write("CM");
                    ComPort.Read();
                    Thread.Sleep(700);
                    ComPort.Write("PD " + command.SetValue.ToString());
                    ComPort.Read();
                    break;
                case NetCommand.ValueType.ENERGY:
                    ComPort.Write("RS");
                    ComPort.Read();
                    Thread.Sleep(200);
                    ComPort.Write("CM");
                    ComPort.Read();
                    Thread.Sleep(700);
                    ComPort.Write("PE " + command.SetValue.ToString());
                    ComPort.Read();
                    break;
                case NetCommand.ValueType.POWER:
                    ComPort.Write("CM");
                    ComPort.Read();
                    Thread.Sleep(200);
                    ComPort.Write("PW " + command.SetValue.ToString());
                    ComPort.Read();
                    break;
                case NetCommand.ValueType.TIME:
                    ComPort.Write("RS");
                    ComPort.Read();
                    Thread.Sleep(200);
                    ComPort.Write("CM");
                    ComPort.Read();
                    Thread.Sleep(700);
                    string temp = (command.SetValue / 60) + "";
                    if(temp.Length < 2)
                    {
                        temp = "0" + temp;
                    }
                    string temp2 = (command.SetValue % 60) + "";
                    if (temp2.Length < 2)
                    {
                        temp2 = "0" + temp2;
                    }

                    string time = temp + temp2;
                    ComPort.Write("PT " + time);
                    ComPort.Read();
                    break;
                default:
                    throw new FormatException("Error in NetCommand: ValueSet is not recognized");
            }
        }
Example #17
0
 private static void ParseCommand(NetCommand command)
 {
     switch(command.Type)
     {
         case NetCommand.CommandType.VALUESET:
             ParseValueSet(command);
             break;
         case NetCommand.CommandType.CHAT:
             ChatMessage chat = new ChatMessage(command.DisplayName, command.ChatMessage, true);
             Chat.Add(chat);
             Client.chat.Invoke(Client.chat.passChatMessage, new Object[] { chat });
             break;
         case NetCommand.CommandType.RESPONSE:
             Console.WriteLine(command.Response.ToString());
             break;
         case NetCommand.CommandType.SESSION:
             Session = command.Session;
             break;
         case NetCommand.CommandType.ERROR:
             Console.WriteLine("An error occured, ignoring");
             break;
         default:
             throw new FormatException("Error in Netcommand: Received command not recognized");
     }
 }
Example #18
0
 public static void SendNetCommand(NetCommand command)
 {
     if(! NetHelper.SendNetCommand(Doctor, command))
     {
         Disconnect();
     }
 }
Example #19
0
        public static bool Connect(string password, out string error)
        {
            error = "Succes";

            if (Server == null || !Server.Connected)
            {
                if (Server == null)
                    Server = new TcpClient();

                try
                {
                    Server.Connect(HOST, PORT);
                }
                catch (Exception e)
                {
                    error = "Server is niet online.";
                    return false;
                }

                NetCommand net = NetHelper.ReadNetCommand(Server);
                if (net.Type == NetCommand.CommandType.SESSION)
                    Session = net.Session;
                else
                    throw new Exception("Session not assigned");

                running = true;
                t = new Thread(run);
                t.IsBackground = true;
                t.Start();
            }

            if (!loggedin)
            {
                NetCommand command = new NetCommand("Doctor0tVfW", true, password, Session);
                NetHelper.SendNetCommand(Server, command);

                NetCommand response = NetHelper.ReadNetCommand(Server);
                if (response.Type == NetCommand.CommandType.RESPONSE && response.Response == NetCommand.ResponseType.LOGINWRONG)
                {
                    loggedin = false;
                    error = "Het wachtwoord is onjuist.";
                    return false;
                }

                loggedin = true;
            }

            SendNetCommand(new NetCommand(NetCommand.RequestType.SESSIONDATA, Session));
            Thread.Sleep(15);
            SendNetCommand(new NetCommand(NetCommand.RequestType.USERS, Session));

            Thread.Sleep(200);

            return true;
        }
Example #20
0
        private static NetCommand ParseLoginRequest(int session, string[] args)
        {
            bool doctor = bool.Parse(args[2]);
            if (args.Length != 3)
                throw new MissingFieldException("Error in NetCommand: Doctor login is missing arguments");

            NetCommand temp = new NetCommand(CommandType.LOGIN, session);
            temp.IsDoctor = doctor;
            temp.DisplayName = args[0];
            temp.Password = Helper.Base64Decode(args[1]);

            return temp;
        }
Example #21
0
        private static NetCommand ParseLogoutRequest(int session, string[] args)
        {
            if (args.Length != 1)
                throw new MissingFieldException("Error in NetCommand: Logout Request is missing arguments");

            NetCommand temp = new NetCommand(CommandType.LOGOUT, session);
            if (args[0] != "logout")
                throw new FormatException("Error in NetCommand: " + args[0] + " is not a valid logout request");

            return temp;
        }
Example #22
0
 private void sendCommand(NetCommand command)
 {
     if(! IsOldData)
         MainClient.SendNetCommand(command);
 }
Example #23
0
 private static NetCommand ParseSession(int session)
 {
     NetCommand temp = new NetCommand(CommandType.SESSION, session);
     return temp;
 }
 private static void HandToClient(NetCommand command)
 {
     foreach (ClientThread cl in clients)
     {
         if (cl.Session == command.Session)
         {
             cl.HandleCommand(command);
         }
     }
 }
Example #25
0
        private static NetCommand ParseTestResult(int session, string[] args)
        {
            if (args.Length != 5)
                throw new MissingFieldException("Error in NetCommand: TestResult is missing arguments");

            NetCommand temp = new NetCommand(double.Parse(args[0]), double.Parse(args[1]), double.Parse(args[2]), double.Parse(args[3]), args[4], session);

            return temp;
        }
Example #26
0
        private static NetCommand ParseSession(int session)
        {
            NetCommand temp = new NetCommand(CommandType.SESSION, session);

            return(temp);
        }
Example #27
0
        private static NetCommand ParseUser(int session, string[] args)
        {
            if (args.Length != 2)
                throw new MissingFieldException("Error in NetCommand: User is missing arguments");

            NetCommand temp = new NetCommand(args[0], Helper.Base64Decode(args[1]), session);

            return temp;
        }
Example #28
0
 public void sendToDoctor(NetCommand command)
 {
     NetHelper.SendNetCommand(client, command);
 }
Example #29
0
        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;
        }
Example #30
0
        private static NetCommand ParseChatMessage(int session, string[] args)
        {
            if (args.Length != 2)
                throw new MissingFieldException("Error in NetCommand: Chat Message is missing arguments");

            NetCommand temp = new NetCommand(CommandType.CHAT, session);
            temp.ChatMessage = args[0];
            temp.IsDoctor = bool.Parse(args[1]);

            return temp;
        }
Example #31
0
 // 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();
     }
 }
Example #32
0
 private static void HandleNetCommand(NetCommand command)
 {
     switch (command.Type)
     {
         case NetCommand.CommandType.LENGTH:
             switch (command.Length)
             {
                 case NetCommand.LengthType.USERS:
                     users.Clear();
                     UsersBeingSent = true;
                     UsersSent = 0;
                     UsersLength = command.LengthValue;
                     break;
                 case NetCommand.LengthType.SESSIONS:
                     oldSessionsData.Clear();
                     SessionsBeingSent = true;
                     SessionsSent = 0;
                     SessionsLength = command.LengthValue;
                     break;
                 case NetCommand.LengthType.SESSIONDATA:
                     activesessions.Clear();
                     ActiveSessionsBeingSent = true;
                     ActiveSessionsSent = 0;
                     ActiveSessionsLength = command.LengthValue;
                     break;
                 default:
                     throw new FormatException("Error in NetCommand: Length type is not recognised");
             }
             break;
         case NetCommand.CommandType.ERROR:
             Console.WriteLine("An error occured, ignoring");
             break;
         case NetCommand.CommandType.USER:
             if(UsersBeingSent)
             {
                 users.Add(command.DisplayName, command.Password);
                 UsersSent++;
                 if (UsersSent >= UsersLength)
                     UsersBeingSent = false;
             }
             break;
         case NetCommand.CommandType.SESSIONDATA:
             if (ActiveSessionsBeingSent)
             {
                 activesessions.Add(command.Session, command.DisplayName);
                 Console.WriteLine(command.Session + " | " + command.DisplayName);
                 ActiveSessionsSent++;
                 if (ActiveSessionsSent >= ActiveSessionsLength)
                 {
                     ActiveSessionsBeingSent = false;
                     if (Window.HeaderLabel.Text == "Actieve Sessies")
                         Window.updateSession = true;
                 }
             }
             break;
         case NetCommand.CommandType.OLDSESSIONDATA:
             if (SessionsBeingSent)
             {
                 oldSessionsData.Add(new Tuple<string, double, int>(command.DisplayName, command.Timestamp, command.Session));
                 SessionsSent++;
                 if (SessionsSent >= SessionsLength)
                     SessionsBeingSent = false;
             }
             break;
         default:
             HandToClient(command);
             break;
     }
 }
Example #33
0
 public void SendToClient(NetCommand command)
 {
     NetHelper.SendNetCommand(client, command);
     if (command.Type == NetCommand.CommandType.CHAT)
     {
         chat.Add(new ChatMessage("Doctor", command.ChatMessage, true));
     }
 }
Example #34
0
        private static NetCommand ParseBroadcast(int session, string[] args)
        {
            if (args.Length != 1)
                throw new MissingFieldException("Error in NetCommand: Broadcast Message is missing arguments");

            NetCommand temp = new NetCommand(args[0], session);

            return temp;
        }