Esempio n. 1
0
        public static ÄstrandTest GetAstrandTestData(string filename)
        {
            lock (filename)
            {
                if (File.Exists(testsFolderPath + @"/" + filename + ".json"))
                {
                    string fileContent = DataEncryptor.Decrypt(File.ReadAllText(testsFolderPath + @"/" + filename + ".json"), DataEncryptor.FileKey);

                    if (!String.IsNullOrEmpty(fileContent))
                    {
                        try
                        {
                            ÄstrandTest astrandTest = new ÄstrandTest();

                            JObject historydataJson = JObject.Parse(fileContent);

                            JObject personalData = historydataJson.GetValue("personaldata").ToObject <JObject>();
                            astrandTest.Username  = personalData.GetValue("name").ToString();
                            astrandTest.BirthYear = int.Parse(personalData.GetValue("birthyear").ToString());
                            astrandTest.Weight    = int.Parse(personalData.GetValue("weight").ToString());
                            astrandTest.IsMan     = (personalData.GetValue("gender").ToString() == "man") ? true : false;

                            JObject testResultData = historydataJson.GetValue("testresult").ToObject <JObject>();
                            astrandTest.HasSteadyState = testResultData.GetValue("hassteadystate").ToObject <bool>();
                            astrandTest.VO2            = double.Parse(testResultData.GetValue("vo2").ToString());

                            JArray heartratesJson   = historydataJson.GetValue("heartrates").ToObject <JArray>();
                            JArray distancesJson    = historydataJson.GetValue("distances").ToObject <JArray>();
                            JArray speedsJson       = historydataJson.GetValue("speeds").ToObject <JArray>();
                            JArray cycleRhythmsJson = historydataJson.GetValue("cyclerhythms").ToObject <JArray>();

                            foreach (JObject heartrateJson in heartratesJson)
                            {
                                astrandTest.HeartrateValues.Add((int.Parse(heartrateJson.GetValue("heartrate").ToString()), DateTime.Parse(heartrateJson.GetValue("time").ToString())));
                            }

                            foreach (JObject distanceJson in distancesJson)
                            {
                                astrandTest.DistanceValues.Add((int.Parse(distanceJson.GetValue("distance").ToString()), DateTime.Parse(distanceJson.GetValue("time").ToString())));
                            }

                            foreach (JObject speedJson in speedsJson)
                            {
                                astrandTest.SpeedValues.Add((int.Parse(speedJson.GetValue("speed").ToString()), DateTime.Parse(speedJson.GetValue("time").ToString())));
                            }

                            foreach (JObject cycleRhythmJson in cycleRhythmsJson)
                            {
                                astrandTest.CycleRhythmValues.Add((int.Parse(cycleRhythmJson.GetValue("cyclerhythm").ToString()), DateTime.Parse(cycleRhythmJson.GetValue("time").ToString())));
                            }

                            return(astrandTest);
                        }
                        catch (Exception e) { }
                    }
                }
                return(null);
            }
        }
Esempio n. 2
0
        public TestDataWindow(string username, int birthYear, int weight, bool isMan, int maxIntervals, bool hasSteadyState, double vo2)
        {
            InitializeComponent();

            this.username  = username;
            this.birthYear = birthYear;
            this.weight    = weight;
            this.isMan     = isMan;

            this.hasSteadyState = hasSteadyState;
            this.vo2            = vo2;

            this.testData       = new ÄstrandTest();
            this.maxIntervals   = maxIntervals;
            con_TestData.Header = $"Cliënt data [{username}]";

            this.heartrateChart   = new LiveChartControl("Hartslag", "", "sl/pm", 40, 400, 200, this.maxIntervals, LiveChart.BlueGreenDarkTheme, true, true, true, true, false, true, true);
            this.distanceChart    = new LiveChartControl("Afstand", "", "meters", 40, 400, 200, this.maxIntervals, LiveChart.BlueGreenDarkTheme, true, true, true, true, false, true, true);
            this.speedChart       = new LiveChartControl("Snelheid", "", "m/s", 40, 400, 200, this.maxIntervals, LiveChart.BlueGreenDarkTheme, true, true, true, true, false, true, true);
            this.cycleRhythmChart = new LiveChartControl("Rotaties per minuut", "", "rpm", 40, 400, 200, this.maxIntervals, LiveChart.BlueGreenDarkTheme, true, true, true, true, false, true, true);

            grd_Grid.Children.Add(this.heartrateChart);
            grd_Grid.Children.Add(this.distanceChart);
            grd_Grid.Children.Add(this.speedChart);
            grd_Grid.Children.Add(this.cycleRhythmChart);

            Grid.SetColumn(this.heartrateChart, 0);
            Grid.SetColumn(this.distanceChart, 1);
            Grid.SetColumn(this.speedChart, 0);
            Grid.SetColumn(this.cycleRhythmChart, 1);
            Grid.SetRow(this.heartrateChart, 0);
            Grid.SetRow(this.distanceChart, 0);
            Grid.SetRow(this.speedChart, 1);
            Grid.SetRow(this.cycleRhythmChart, 1);

            lbl_Username.Content    = username;
            lbl_BirthYear.Content   = birthYear;
            lbl_Weight.Content      = weight;
            lbl_Gender.Content      = (isMan) ? "Man" : "Vrouw";
            lbl_SteadyState.Content = (hasSteadyState) ? "JA" : "NEE";
            lbl_VO2.Content         = vo2;

            if (!hasSteadyState)
            {
                lbl_SteadyStateNotification.Content = "Er was geen steady state behaald, VO2max waarde is niet betrouwbaar!";
            }
        }
Esempio n. 3
0
        private void HandleGetTestData(string filename, UserAccount user)
        {
            if (FileHandler.TestExists(filename))
            {
                ÄstrandTest astrandTest = FileHandler.GetAstrandTestData(filename);

                if (astrandTest != null)
                {
                    astrandTest.Transmit(user);
                }
                else
                {
                    this.astrandServer.Transmit(new Message(Message.ID.GET_TEST_DATA, Message.State.ERROR, Encoding.UTF8.GetBytes("Kon test niet openen!")), user);
                }
            }
            else
            {
                this.astrandServer.Transmit(new Message(Message.ID.GET_TEST_DATA, Message.State.ERROR, Encoding.UTF8.GetBytes("Kon test niet vinden!")), user);
            }
        }
Esempio n. 4
0
        public void OnMessageReceived(Message message, UserAccount user)
        {
            List <byte> content = new List <byte>(message.GetContent());

            switch (message.GetId())
            {
            case Message.ID.CLIENT_REGISTER:
            {
                string username  = Encoding.UTF8.GetString(content.GetRange(65, content[64]).ToArray());
                string password  = Encoding.UTF8.GetString(content.GetRange(0, 64).ToArray());
                int    birthYear = int.Parse(Encoding.UTF8.GetString(content.GetRange(username.Length + password.Length + 2, content[username.Length + password.Length + 1]).ToArray()));
                int    weight    = content[username.Length + password.Length + content[(username.Length + password.Length + 1)] + 2];
                bool   isMan     = (content[username.Length + password.Length + content[(username.Length + password.Length + 1)] + 3] == 1);

                HandleClientRegister(username, password, birthYear, weight, isMan, user);
                break;
            }

            case Message.ID.SPECIALIST_REGISTER:
            {
                string username = Encoding.UTF8.GetString(content.GetRange(64, content.Count - 64).ToArray());
                string password = Encoding.UTF8.GetString(content.GetRange(0, 64).ToArray());

                HandleSpecialistRegister(username, password, user);
                break;
            }

            case Message.ID.CLIENT_LOGIN:
            {
                string username = Encoding.UTF8.GetString(content.GetRange(64, content.Count - 64).ToArray());
                string password = Encoding.UTF8.GetString(content.GetRange(0, 64).ToArray());

                HandleClientLogin(username, password, user);
                break;
            }

            case Message.ID.SPECIALIST_LOGIN:
            {
                string username = Encoding.UTF8.GetString(content.GetRange(64, content.Count - 64).ToArray());
                string password = Encoding.UTF8.GetString(content.GetRange(0, 64).ToArray());

                HandleSpecialistLogin(username, password, user);
                break;
            }

            case Message.ID.LOGOUT:
            {
                HandleLogout(user);
                break;
            }

            case Message.ID.START_TEST:
            {
                if (user.IsAuthorized && this.clients.Contains(user))
                {
                    if (!this.currentTests.Keys.Contains(user))
                    {
                        (int birthYear, int weight, bool isMan)personalData = FileHandler.GetPersonalData(user.Username);
                        this.currentTests.Add(user, new ÄstrandTest(user.Username, personalData.birthYear, personalData.weight, personalData.isMan));
                        BroadcastToSpecialists(new Message(Message.ID.START_TEST, Message.State.OK, Encoding.UTF8.GetBytes(user.Username)));
                    }
                }
                break;
            }

            case Message.ID.END_TEST:
            {
                if (user.IsAuthorized && this.clients.Contains(user))
                {
                    if (this.currentTests.Keys.Contains(user))
                    {
                        if (message.GetContent().Length != 0)
                        {
                            bool   hasSteadyState = (content[0] == 1);
                            double vo2            = double.Parse(Encoding.UTF8.GetString(content.GetRange(1, content.Count() - 1).ToArray()));

                            ÄstrandTest astrandTest = this.currentTests[user];
                            astrandTest.HasSteadyState = hasSteadyState;
                            astrandTest.VO2            = vo2;

                            FileHandler.SaveAstrandTestData(astrandTest);
                        }
                        this.currentTests.Remove(user);
                        BroadcastToSpecialists(new Message(Message.ID.END_TEST, Message.State.OK, Encoding.UTF8.GetBytes(user.Username)));
                    }
                }
                break;
            }

            case Message.ID.BIKEDATA:
            {
                if (user.IsAuthorized && this.clients.Contains(user) && this.currentTests.Keys.Contains(user))
                {
                    HandleBikeData(message.GetContent(), user);
                }
                break;
            }

            case Message.ID.GET_TESTS:
            {
                if (user.IsAuthorized)
                {
                    HandleGetTests(user);
                }
                break;
            }

            case Message.ID.GET_TEST_DATA:
            {
                if (user.IsAuthorized)
                {
                    string filename = Encoding.UTF8.GetString(message.GetContent());
                    HandleGetTestData(filename, user);
                }
                break;
            }
            }
        }
Esempio n. 5
0
        private void HandleBikeData(byte[] bikeData, UserAccount user)
        {
            List <byte> bikeDataBytes = new List <byte>();

            bikeDataBytes.Add((byte)user.Username.Length);
            bikeDataBytes.AddRange(Encoding.UTF8.GetBytes(user.Username));
            bikeDataBytes.AddRange(bikeData);
            BroadcastToSpecialists(new Message(Message.ID.BIKEDATA, Message.State.OK, bikeDataBytes.ToArray()));

            ÄstrandTest astrandTest = this.currentTests[user];

            List <byte> bytes = new List <byte>(bikeData);

            //for (int i = 0; i < bytes.Count; i += 2)
            //{
            //    Message.ValueId valueType = (Message.ValueId)bytes[i];
            //    int value = bytes[i + 1];
            //    DateTime dateTime = DateTime.Now;

            //    switch (valueType)
            //    {
            //        case Message.ValueId.HEARTRATE:
            //            {
            //                astrandTest.HeartrateValues.Add((heartRate: value, time: dateTime));
            //                break;
            //            }
            //        case Message.ValueId.DISTANCE:
            //            {
            //                astrandTest.DistanceValues.Add((distance: value, time: dateTime));
            //                break;
            //            }
            //        case Message.ValueId.SPEED:
            //            {
            //                astrandTest.SpeedValues.Add((speed: value, time: dateTime));
            //                break;
            //            }
            //        case Message.ValueId.CYCLE_RHYTHM:
            //            {
            //                astrandTest.CycleRhythmValues.Add((cycleRhythm: value, time: dateTime));
            //                break;
            //            }
            //    }
            //}
            int skip = 2;

            for (int i = 0; i < bytes.Count; i += skip)
            {
                Message.ValueId valueType = (Message.ValueId)bytes[i];
                DateTime        dateTime  = DateTime.Now;

                switch (valueType)
                {
                case Message.ValueId.HEARTRATE:
                {
                    skip = 2;
                    int value = bytes[i + 1];
                    astrandTest.HeartrateValues.Add((heartRate: value, time: dateTime));
                    break;
                }

                case Message.ValueId.DISTANCE:
                {
                    skip = 2 + bytes[i + 1];
                    int value = int.Parse(Encoding.UTF8.GetString(bytes.GetRange(i + 2, bytes[i + 1]).ToArray()));
                    astrandTest.DistanceValues.Add((distance: value, time: dateTime));
                    break;
                }

                case Message.ValueId.SPEED:
                {
                    skip = 2;
                    int value = bytes[i + 1];
                    astrandTest.SpeedValues.Add((speed: value, time: dateTime));
                    break;
                }

                case Message.ValueId.CYCLE_RHYTHM:
                {
                    skip = 2;
                    int value = bytes[i + 1];
                    astrandTest.CycleRhythmValues.Add((cycleRhythm: value, time: dateTime));
                    break;
                }
                }
            }
        }
Esempio n. 6
0
        public static void SaveAstrandTestData(ÄstrandTest testData)
        {
            JObject personalData     = new JObject();
            JObject testResultData   = new JObject();
            JArray  heartratesJson   = new JArray();
            JArray  distancesJson    = new JArray();
            JArray  speedsJson       = new JArray();
            JArray  cycleRhythmsjson = new JArray();

            personalData.Add("name", testData.Username);
            personalData.Add("birthyear", testData.BirthYear);
            personalData.Add("weight", testData.Weight);
            personalData.Add("gender", (testData.IsMan) ? "man" : "woman");

            testResultData.Add("hassteadystate", testData.HasSteadyState);
            testResultData.Add("vo2", testData.VO2);

            foreach ((int heartrate, DateTime time)heartrateData in testData.HeartrateValues)
            {
                JObject heartrateJson = new JObject();
                heartrateJson.Add("heartrate", heartrateData.heartrate);
                heartrateJson.Add("time", heartrateData.time.ToString());
                heartratesJson.Add(heartrateJson);
            }

            foreach ((int distance, DateTime time)distanceData in testData.DistanceValues)
            {
                JObject distanceJson = new JObject();
                distanceJson.Add("distance", distanceData.distance);
                distanceJson.Add("time", distanceData.time.ToString());
                distancesJson.Add(distanceJson);
            }

            foreach ((int speed, DateTime time)speedData in testData.SpeedValues)
            {
                JObject speedJson = new JObject();
                speedJson.Add("speed", speedData.speed);
                speedJson.Add("time", speedData.time.ToString());
                speedsJson.Add(speedJson);
            }

            foreach ((int cycleRhythm, DateTime time)cycleRhythmData in testData.CycleRhythmValues)
            {
                JObject cycleRhythmJson = new JObject();
                cycleRhythmJson.Add("cyclerhythm", cycleRhythmData.cycleRhythm);
                cycleRhythmJson.Add("time", cycleRhythmData.time.ToString());
                cycleRhythmsjson.Add(cycleRhythmJson);
            }

            JObject testJson = new JObject();

            testJson.Add("personaldata", personalData);
            testJson.Add("testresult", testResultData);
            testJson.Add("heartrates", heartratesJson);
            testJson.Add("distances", distancesJson);
            testJson.Add("speeds", speedsJson);
            testJson.Add("cyclerhythms", cycleRhythmsjson);

            string filename = testData.Username + "_" + DateTime.Now.ToString();

            filename = filename.Replace("/", "-");
            filename = filename.Replace(":", "#");
            File.WriteAllText(testsFolderPath + @"/" + filename + ".json", DataEncryptor.Encrypt(testJson.ToString(), DataEncryptor.FileKey));
        }