// periodic call, fetches actual data and triggers autolanding function private void updaterTimer_Tick(object sender, EventArgs e) { if (landingID != -1) { LandingState state = makeRequest(serverAddress + "/stav?id=" + landingID); if (autoland) { calculateAutoLand(state); // autoland calculations } showState(state); switch (state.state) { case LandingState.SUCCESS: landingID = -1; //MessageBox.Show("Přistání bylo úspěšné!", "Stav přistání", MessageBoxButtons.OK, MessageBoxIcon.Information); enableControls(); break; case LandingState.FAILURE: landingID = -1; //MessageBox.Show("Přistání se nezdařilo.", "Stav přistání", MessageBoxButtons.OK, MessageBoxIcon.Error); enableControls(); break; case LandingState.LANDING: showData(state); drawScheme(state); lastState = state; break; } } }
// occurs when autolanding button is clicked private void buttonStartAuto_Click(object sender, EventArgs e) { autoland = true; serverAddress = textBoxAddress.Text; lastState = makeRequest(serverAddress + "/start"); landingID = lastState.id; disableControls(); }
// handles making requests to the server w/ parsing response into LandingState struct defined at the bottom private LandingState makeRequest(string address) { string text = string.Empty; // do HTTP request HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://" + address); request.AutomaticDecompression = DecompressionMethods.GZip; // to be sure using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) using (Stream stream = response.GetResponseStream()) using (StreamReader reader = new StreamReader(stream)) { text = reader.ReadToEnd(); // get the response } LandingState result = new LandingState(); string[] linesArray = text.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None); if (linesArray[0] == "Zdar!") { result.state = LandingState.SUCCESS; return(result); } if (linesArray[0] == "Krach!") { result.state = LandingState.FAILURE; return(result); } // parsing response into struct result.state = LandingState.LANDING; result.id = int.Parse(linesArray[0]); result.moduleX = float.Parse(linesArray[1].Split(new char[] { ' ' })[0], CultureInfo.InvariantCulture); result.moduleY = float.Parse(linesArray[1].Split(new char[] { ' ' })[1], CultureInfo.InvariantCulture); result.velocityX = float.Parse(linesArray[2].Split(new char[] { ' ' })[0], CultureInfo.InvariantCulture); result.velocityY = float.Parse(linesArray[2].Split(new char[] { ' ' })[1], CultureInfo.InvariantCulture); result.fuel = float.Parse(linesArray[3], CultureInfo.InvariantCulture); result.landingL = float.Parse(linesArray[4].Split(new char[] { ' ' })[0], CultureInfo.InvariantCulture); result.landingR = float.Parse(linesArray[4].Split(new char[] { ' ' })[1], CultureInfo.InvariantCulture); return(result); }
private void Form1_Load(object sender, EventArgs e) { // start landing automatically if called from command line string[] args = Environment.GetCommandLineArgs(); if (args.Length == 3) { autoland = true; maxSpeed = int.Parse(args[2]); textBoxAddress.Text = args[1]; serverAddress = args[1]; lastState = makeRequest(serverAddress + "/start"); landingID = lastState.id; disableControls(); } }
// output the data into information labels private void showData(LandingState state) { labelID.Text = "ID: " + state.id.ToString(); labelX.Text = "X: " + Math.Round(state.moduleX, 1).ToString(); labelY.Text = "Y: " + Math.Round(state.moduleY, 1).ToString(); if (state.velocityX > -5 && state.velocityX < 5) { labelVX.ForeColor = Color.Green; } if (state.velocityX < -5 || state.velocityX > 5) { labelVX.ForeColor = Color.Red; } labelVX.Text = "vX: " + Math.Round(state.velocityX, 1).ToString(); if (state.velocityY > -5) { labelVY.ForeColor = Color.Green; } if (state.velocityY < -5) { labelVY.ForeColor = Color.Red; } labelVY.Text = "vY: " + Math.Round(state.velocityY, 1).ToString(); if (state.fuel < 5) { labelFuel.ForeColor = Color.Red; } if (state.fuel > 5 && state.fuel < 10) { labelFuel.ForeColor = Color.Orange; } if (state.fuel > 10) { labelFuel.ForeColor = Color.Green; } labelFuel.Text = "P: " + Math.Round(state.fuel, 1).ToString(); labelLandingL.Text = "L: " + Math.Round(state.landingL, 1).ToString(); labelLandingR.Text = "R: " + Math.Round(state.landingR, 1).ToString(); }
// draw the picture of the situation private void drawScheme(LandingState state) { Bitmap bmp = new Bitmap(2000, 2000); using (Graphics g = Graphics.FromImage(bmp)) { g.Clear(Color.Transparent); } scheme.Image = bmp; using (Graphics g = Graphics.FromImage(scheme.Image)) { g.DrawLine(new Pen(Color.Black, 20), state.landingL * 10, 2000, state.landingR * 10, 2000); object o = Resources.ResourceManager.GetObject("lunarModule"); Image lunarModule = (Image)o; g.DrawImage(lunarModule, 10 * state.moduleX, 2000 - 10 * state.moduleY - 70); } }
// show colored state info private void showState(LandingState state) { switch (state.state) { case LandingState.LANDING: labelState.Text = "Přistání probíhá..."; labelState.ForeColor = Color.Orange; break; case LandingState.SUCCESS: labelState.Text = "Přistání bylo úspěšné!"; labelState.ForeColor = Color.Green; break; case LandingState.FAILURE: labelState.Text = "Přistání se nezdařilo."; labelState.ForeColor = Color.Red; break; } labelState.Visible = true; }
// calculates autolanding moves and sends the requests private void calculateAutoLand(LandingState state) { bool thrustUp = false; bool thrustLeft = false; bool thrustRight = false; // vertical if ((state.velocityY < -4 && state.moduleY < 40) || (state.velocityY < -8 && state.moduleY < 100) || (state.velocityY < -12 && state.moduleY < 150) || (state.velocityY < -15 && state.moduleY < 160)) { thrustUp = true; } // to fit the board if ((state.moduleX - 20 < state.landingL)) { thrustRight = true; } if ((state.moduleX + 20 > state.landingR)) { thrustLeft = true; } // horizontal overspeed if ((state.velocityX > 5)) { thrustRight = false; thrustLeft = true; } if ((state.velocityX < -5)) { thrustRight = true; thrustLeft = false; } // create and send the request string url = serverAddress + "/stav?id=" + landingID; if (thrustUp) { url += "&nahoru=1"; } else { url += "&nahoru=0"; } if (thrustLeft) { url += "&doleva=1"; } else { url += "&doleva=0"; } if (thrustRight) { url += "&doprava=1"; } else { url += "&doprava=0"; } makeRequest(url); }