Exemplo n.º 1
0
        // Shop openfileDialoge
        private void selectFileButton_Click(object sender, EventArgs e)
        {
            openFileDialog1.InitialDirectory = System.Environment.CurrentDirectory;
            openFileDialog1.FileName         = "";
            openFileDialog1.Filter           = "Api Key text files|*.txt";
            ApiKeyHolder apiKey = ApiKeyHolder.Instance;
            DialogResult result = openFileDialog1.ShowDialog();

            if (result == DialogResult.OK)
            {
                Properties.Settings.Default["apiKeyFileName"] = openFileDialog1.FileName;
                Properties.Settings.Default.Save();
                try
                {
                    apiKey.readKeysFromFile(Properties.Settings.Default.apiKeyFileName);
                    realTimeKeyTextBox.Text = apiKey.ApiKeyDepartures;
                    stationKeyTextBox.Text  = apiKey.ApiKeyStation;
                }
                catch (Exception err)
                {
                    MessageBox.Show("Error occured when trying to load settings from file:\n" +
                                    openFileDialog1.FileName + "\n" +
                                    "Please make sure file is correctly formatted"
                                    , "Error occured", MessageBoxButtons.OK
                                    , MessageBoxIcon.Error);
                }
            }
        }
Exemplo n.º 2
0
        private void APIKeyForm_Shown(object sender, EventArgs e)
        {
            ApiKeyHolder apiKey = ApiKeyHolder.Instance;

            if (!String.IsNullOrWhiteSpace(Properties.Settings.Default.apiKeyFileName))
            {
                try
                {
                    apiKey.readKeysFromFile(Properties.Settings.Default.apiKeyFileName);
                }
                catch (Exception err)
                {
                }
            }
            realTimeKeyTextBox.Text = apiKey.ApiKeyDepartures;
            stationKeyTextBox.Text  = apiKey.ApiKeyStation;
        }
Exemplo n.º 3
0
        // Search for stations
        private void stationSearch()
        {
            ApiKeyHolder apiKey = ApiKeyHolder.Instance;

            if (string.IsNullOrWhiteSpace(stationSeachTextBox.Text))
            {
                MessageBox.Show("Enter seach term!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            int    maxResults     = 15; // I'm usually against this kind of hardcoding....but....fuckit.
            string downloadString = "http://api.sl.se/api2/typeahead.json?key=" + apiKey.ApiKeyStation +
                                    "&searchstring=" + stationSeachTextBox.Text
                                    + "&stationsonly=true&maxresults=" + maxResults;

            using (WebClient wc = new WebClient())
            {
                stationSeachResultListBox.Items.Clear();

                wc.Encoding = System.Text.Encoding.UTF8;
                var stationMessage = JsonConvert.DeserializeObject <stationMessage>(wc.DownloadString(downloadString));


                if (stationMessage.StatusCode != 0)
                {
                    MessageBox.Show("Errorcode: " + stationMessage.StatusCode + "\nMessage:\n" +
                                    stationMessage.Message
                                    , "Error Communicating with SL ", MessageBoxButtons.OK
                                    , MessageBoxIcon.Error);
                    return;
                }

                foreach (siteObject site in stationMessage.ResponseData)
                {
                    stationSeachResultListBox.Items.Add(site);
                }
            }
        }
Exemplo n.º 4
0
        // does the actual dataretrieval for realtime response
        private void fetchRealTimeData()
        {
            progressBar1.Value = 0;
            blockTimer.Start();
            fetchDataButton.Enabled = false;

            //downnloadString
            int timeWindow;
            int siteID;

            if (stationSeachResultListBox.SelectedIndex == -1)
            {
                MessageBox.Show("Please select a station first", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                try
                {
                    siteID    = ((siteObject)stationSeachResultListBox.SelectedItem).SiteId;
                    this.Text = "SLApp - " + ((siteObject)stationSeachResultListBox.SelectedItem).Name;
                }
                catch (Exception err)
                {
                    MessageBox.Show("Error trying to access selected station:\n" + err,
                                    "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }

            timeWindow = trackBar1.Value; // int.Parse(timeWindowLabel.Text);

            ApiKeyHolder apiKey         = ApiKeyHolder.Instance;
            string       downloadString = "http://api.sl.se/api2/realtimedepartures.JSON?key=" +
                                          apiKey.ApiKeyDepartures +
                                          "&siteid=" + siteID +
                                          "&timewindow=" + timeWindow;

            clearAllTextBoxes();


            using (WebClient wc = new WebClient())
            {
                wc.Encoding = System.Text.Encoding.UTF8; // Swedish chars turned out funny, otherwise

                var realtime = JsonConvert.DeserializeObject <realTimeMessage>(wc.DownloadString(downloadString));

                if (realtime.StatusCode != 0)
                {
                    MessageBox.Show("Errorcode: " + realtime.StatusCode + "\nMessage:\n" +
                                    realtime.Message
                                    , "Error Communicating with SL", MessageBoxButtons.OK
                                    , MessageBoxIcon.Error);
                    return;
                }


                metroTab.Text     = "metro (" + realtime.DepartureObject.Metros.Count + ")";
                busTab.Text       = "Buses (" + realtime.DepartureObject.Buses.Count + ")";
                trainTab.Text     = "Trains (" + realtime.DepartureObject.Trains.Count + ")";
                tramTab.Text      = "Trams (" + realtime.DepartureObject.Trams.Count + ")";
                shipTab.Text      = "Ships (" + realtime.DepartureObject.Ships.Count + ")";
                deviationTab.Text = "Deviations (" + realtime.DepartureObject.StopPointDeviations.Count + ")";


                /*
                 * TODO this section really should be rewritten with some generic method....
                 * But ....tricky shit.....
                 *
                 */
                // METROS
                if (realtime.DepartureObject.Metros.Count > 0)
                {
                    foreach (Metros met in realtime.DepartureObject.Metros)
                    {
                        metroObjectListView.AddObject(met);
                    }
                }

                // BUSSES
                if (realtime.DepartureObject.Buses.Count > 0)
                {
                    foreach (Buses bus in realtime.DepartureObject.Buses)
                    {
                        busObjectListView.AddObject(bus);
                    }
                }

                // TRAINS
                if (realtime.DepartureObject.Trains.Count > 0)
                {
                    foreach (Trains train in realtime.DepartureObject.Trains)
                    {
                        trainObjectListView.AddObject(train);
                    }
                }

                // TRAMS
                if (realtime.DepartureObject.Trams.Count > 0)
                {
                    foreach (Trams tram in realtime.DepartureObject.Trams)
                    {
                        tramObjectListView.AddObject(tram);
                    }
                }

                // SHIPS
                if (realtime.DepartureObject.Ships.Count > 0)
                {
                    foreach (Ships ship in realtime.DepartureObject.Ships)
                    {
                        shipObjectListView.AddObject(ship);
                    }
                }

                // DEVIATIONS
                if (realtime.DepartureObject.StopPointDeviations.Count > 0)
                {
                    foreach (StopPointDeviations stop in realtime.DepartureObject.StopPointDeviations)
                    {
                        deviationObjectListView.AddObject(stop);
                    }
                }
            }
        }