Example #1
0
        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            BikeDataPackage bdp = bike.ReadData();

            if (bdp.Minutes >= 2)
            {
                if (bdp.HeartRate < 130 && bdp.Rpm > 65)
                {
                    AddPower(bdp.Power, 5);
                }
                else if (bdp.HeartRate >= 130 && bdp.Rpm < 55)
                {
                    AddPower(bdp.Power, -5);
                }
            }

            string data = JsonConvert.SerializeObject(new
            {
                power     = bdp.Power,
                rpm       = bdp.Rpm,
                minutes   = bdp.Minutes,
                seconds   = bdp.Seconds,
                heartrate = bdp.HeartRate,
            });

            WriteTextMessage(CreateJsonCommand("bike_data", data));
        }
Example #2
0
        private void StartCourse(BikeDataPackage bdp)
        {
            DateTime newBikeTime = new DateTime(1970, 01, 01, 00, (int)Math.Floor(int.Parse(bdp.time) / 60d), int.Parse(bdp.time) % 60);

            SerialDataHandler.getInstance().ResetWithVariables(int.Parse(bdp.power), Convert.ToInt32(newBikeTime.Minute), Convert.ToInt32(newBikeTime.Second), (int)(double.Parse(bdp.distance) * 1000));

            dataRequested = true;

            //VRHandler.getInstance().SetupScene(400, 400);

            // Set variables
            // Start sending variables
            // Setup VR
        }
Example #3
0
        public Connection(string ip, int port)
        {
            dataRequested = false;
            SerialDataHandler.getInstance().AddSubscriberToHandler(this);
            ConnectToServer(ip, port);

            latestBikeData             = new BikeDataPackage("1970-01-01-00:00:00", "0", "0", "0", "0");
            dataRequestTimer           = new System.Timers.Timer(100);
            dataRequestTimer.Elapsed  += OnTimerTick;
            dataRequestTimer.AutoReset = true;
            dataRequestTimer.Enabled   = true;
            dataRequestTimer.Start();

            client.GetStream().Flush();
            SendMessage(ServerCommands.ClientType());
        }
 public static string SendBikeData(BikeDataPackage package)
 {
     return(JsonConvert.SerializeObject(new
     {
         command = "data",
         origin = BikeID,
         data = new
         {
             time = package.time,
             rpm = package.rpm,
             distance = package.distance,
             energy = package.energy,
             power = package.power
         }
     }));
 }
Example #5
0
        private void AddDataToPanel(BikeDataPackage e)
        {
            // Find and clear the panel
            string panelUID = FindUidInEntitiesList("Panel");

            ClearPanel(panelUID);


            SwapPanelBuffer(panelUID);

            // Add bike info on the left half of the panel
            AddTextBufferToPanel(panelUID, "Bike Info", 40, new double[] { 50, 50 }, new float[] { 0, 0, 0, 1 });
            AddTextBufferToPanel(panelUID, $"TIME: {e.time} seconds", 25, new double[] { 50, 100 }, new float[] { 0, 0, 0, 1 });
            AddTextBufferToPanel(panelUID, $"SPEED: {e.rpm} rpm", 25, new double[] { 50, 130 }, new float[] { 0, 0, 0, 1 });
            AddTextBufferToPanel(panelUID, $"ENERGY: {e.energy} m/s", 25, new double[] { 50, 160 }, new float[] { 0, 0, 0, 1 });
            AddTextBufferToPanel(panelUID, $"DISTANCE: {e.distance} km", 25, new double[] { 50, 190 }, new float[] { 0, 0, 0, 1 });
            AddTextBufferToPanel(panelUID, $"DIFFICULTY: {e.power} NM", 25, new double[] { 50, 210 }, new float[] { 0, 0, 0, 1 });

            // Divider in the middle
            AddPanelLine(panelUID, new int[] { 256, 0 }, new int[] { 256, 512 }, new float[] { 0, 0, 0, 1 });

            // Add the chatlog on the right half of the panel
            AddTextBufferToPanel(panelUID, "Chat Log", 40, new double[] { 300, 50 }, new float[] { 0, 0, 0, 1 });
            if (chatMessages.Count > 10)
            {
                int startValue = chatMessages.Count - 10;
                for (int i = 0; i < 10; i++)
                {
                    AddTextBufferToPanel(panelUID, chatMessages[startValue + i], 25, new double[] { 300, 100 + 30 * i }, new float[] { 0, 0, 0, 1 });
                }
            }
            else if (chatMessages.Count > 0)
            {
                for (int i = 0; i < chatMessages.Count; i++)
                {
                    AddTextBufferToPanel(panelUID, chatMessages[i], 25, new double[] { 300, 100 + 30 * i }, new float[] { 0, 0, 0, 1 });
                }
            }
            else
            {
                AddTextBufferToPanel(panelUID, "No messages yet", 25, new double[] { 300, 100 }, new float[] { 0, 0, 0, 0.8f });
            }

            // Put the new data onto the panel
            SwapPanelBuffer(panelUID);
        }
Example #6
0
        private void HandleData(string command, string data)
        {
            dynamic receivedData = JsonConvert.DeserializeObject(data);

            switch (command)
            {
            case "chat":
                string directMessage = "[DM]:" + receivedData.message;
                Console.WriteLine("New message received: " + directMessage);
                break;

            case "broadcast":
                string broadcastMessage = "[BC]:" + receivedData.message;
                Console.WriteLine("New message received: " + broadcastMessage);
                break;

            case "course_start":
                string          time     = receivedData.time;
                string          power    = receivedData.power;
                string          distance = receivedData.distance;
                BikeDataPackage package  = new BikeDataPackage(time, power, "0", distance, "0");
                StartCourse(package);
                break;

            case "course_stop":
                EndCourse();
                break;

            case "set_power":
                int increment = receivedData.increment;
                SetPower(increment);
                break;

            case "client/id":
                ServerCommands.BikeID = Convert.ToInt32(receivedData.id);
                break;

            case "stop_vr":
                VRHandler.getInstance().ResetScene();
                break;
            }
        }
Example #7
0
        private void ParseData(string message)
        {
            try
            {
                string[] Variables = message.Split(Convert.ToChar("|"));
                string   Rpm       = Variables[1];
                string   Energy    = Variables[2];
                string   Distance  = Variables[5];
                string   Power     = Variables[4];
                string   Time      = Variables[6];

                BikeDataPackage pack = new BikeDataPackage(Time, Power, Rpm, Distance, Energy);
                //TODO do something with the received package
                foreach (IBikeDataListener l in subscribers)
                {
                    l.OnBikeDataReceived(pack);
                }
                //Listener.OnMessageReceived(new X7Data(Rpm, Power, Distance, Time, Energy));
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine(e.GetType());
            }
        }
Example #8
0
 public void OnBikeDataReceived(BikeDataPackage e)
 {
     ChangeSpeed(Int32.Parse(e.rpm));
     AddDataToPanel(e);
 }
Example #9
0
 public void OnBikeDataReceived(BikeDataPackage package)
 {
     latestBikeData = package;
 }