Ejemplo n.º 1
0
        private void TrackerMode_Click(object sender, EventArgs e)
        {
            try
            {
                BreakCurrentMode();

                ArduinoRequests arduinoRequests = new ArduinoRequests();
                List <KeyValuePair <string, float> > userLocalization = arduinoRequests.SetLocalizationDataByUserInput(LatitudeTextBox.Text, LongitudeTextBox.Text);

                if (userLocalization.Count == 0)
                {
                    MessageBox.Show(InternalMessages.NoDataFromCordsTextboxes, MessageCaptions.Error.ToString(), MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                portConnector.SendDataToCom(ApplicationProtocol.FirstModeCommand);
                DisplayPortDataInLogger(InternalMessages.SelectedTrackerMode);

                Uri processedUriAdress = new RequestLinkFormater().GenerateLocationIQLink(userLocalization);

                string       jsonApiData   = new HttpClient().GetStringAsync(processedUriAdress).Result;
                Localization parsedApiData = JsonConvert.DeserializeObject <Localization>(jsonApiData.ToString());

                List <KeyValuePair <string, double> > solarTimeCalculations = new SolarTimeCalculations().CalculateSolarTimes(parsedApiData.Longitude);
                Others others = new Others();
                SolarCalculationResults solarCalculationResults = new SolarCalculationResults()
                {
                    MagneticDeclination  = others.CountEarthMagneticDeclination(),
                    EquationOfTime       = solarTimeCalculations[0].Value,
                    TimeCorrectionFactor = solarTimeCalculations[1].Value,
                    LocalSolarTime       = solarTimeCalculations[2].Value,
                    HourAngle            = solarTimeCalculations[3].Value,
                };
                solarCalculationResults.Altitude = new AltitudeCalculations().CountCurrentSunAltitude(parsedApiData.Latitude, solarCalculationResults.MagneticDeclination, solarCalculationResults.HourAngle);
                solarCalculationResults.Azimuth  = new AzimuthCalculations().CalculateSunAzimuth(parsedApiData.Latitude, solarCalculationResults.Altitude, solarCalculationResults.MagneticDeclination, solarCalculationResults.HourAngle);
                solarCalculationResults.SunRise  = others.SunRiseTimeCalculation(parsedApiData.Latitude, parsedApiData.Longitude, solarCalculationResults.MagneticDeclination, solarCalculationResults.EquationOfTime);
                solarCalculationResults.SunSet   = others.SunSetTimeCalculation(parsedApiData.Latitude, parsedApiData.Longitude, solarCalculationResults.MagneticDeclination, solarCalculationResults.EquationOfTime);

                var limitedAltitude = solarCalculationResults.Altitude < 10 ? 10 : solarCalculationResults.Altitude;
                var mappedAzimuth   = (270.00 / 360.00) * solarCalculationResults.Azimuth;
                mappedAzimuth = mappedAzimuth > 180 ? 180: mappedAzimuth;
                portConnector.SendDataToCom($"{ApplicationProtocol.BottomServoCommand} {Math.Abs(mappedAzimuth - 180)}");
                portConnector.SendDataToCom($"{ApplicationProtocol.TopServoCommand} {limitedAltitude}");
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }