示例#1
0
    public async Task getWaterLevel(Measurement m)
    {
        LocalDateTime timeStamp        = new LocalDateTime(m.dateOfMeasurment.Year, m.dateOfMeasurment.Month, m.dateOfMeasurment.Day, m.timeOfMeasurement.Hour, m.timeOfMeasurement.Minute);
        Instant       instantTimeStamp = Instant.FromDateTimeOffset(timeStamp.WithOffset(Offset.FromHours(1)).ToDateTimeOffset());

        string requestURL = apiURL + string.Format("lat={0}&lon={1}&fromtime={2}&totime={3}&datatype=all&refcode=cd&place=&file=&lang=en&interval=10&dst=1&tzone=1&tide_request=locationdata",
                                                   m.latitudeOfMeasurement, m.longitudeOfMeasurement, getUrlEncodedTime(timeStamp, -30), getUrlEncodedTime(timeStamp, 30));

        client.DefaultRequestHeaders.Accept.Clear();
        var stringTask = client.GetStringAsync(requestURL);

        var  msg    = await stringTask;
        Tide entity = FromXml <Tide>(msg);

        //TODO: Needs proper error handling for cases where connections times out or no response is returned.

        List <Waterlevel> waterlevel = entity.Locationdata.Data.Find(e => e.Type == "observation").Waterlevel;

        if (waterlevel.Count == 7)
        {
            float tmp;
            if (float.TryParse(waterlevel[3].Value, out tmp))
            {
                m.chartdatumWaterLevel = tmp;
            }
        }
        else
        {
            OffsetDateTimePattern offset = OffsetDateTimePattern.ExtendedIso;
            long x  = instantTimeStamp.ToUnixTimeSeconds();
            long x1 = Instant.FromDateTimeOffset(offset.Parse(waterlevel[2].Time).Value.ToDateTimeOffset()).ToUnixTimeSeconds();
            long x2 = Instant.FromDateTimeOffset(offset.Parse(waterlevel[3].Time).Value.ToDateTimeOffset()).ToUnixTimeSeconds();

            float y1 = float.Parse(waterlevel[2].Value);
            float y2 = float.Parse(waterlevel[3].Value);

            m.chartdatumWaterLevel = y1 + (((float)(x - x1) / (float)(x2 - x1)) * (y2 - y1));
        }
    }
示例#2
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            /* ------ copy and open the dB file using the SQLite-Net ORM ------ */

            string           dbPath = "";
            SQLiteConnection db     = null;

            // Get the path to the database that was deployed in Assets
            dbPath = Path.Combine(
                System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "Tides.db3");

            // It seems you can read a file in Assets, but not write to it
            // so we'll copy our file to a read/write location
            using (Stream inStream = Assets.Open("Tides.db3"))
                using (Stream outStream = File.Create(dbPath))
                    inStream.CopyTo(outStream);

            // Open the database
            db = new SQLiteConnection(dbPath);

            /* ------ Spinner initialization ------ */

            // Initialize the adapter for the spinner with stock symbols
            var distinctTides = db.Table <Tide>().GroupBy(s => s.Location).Select(s => s.First());
            var tideLocation  = distinctTides.Select(s => s.Location).ToList();
            var adapter       = new ArrayAdapter(this, Android.Resource.Layout.SimpleSpinnerItem, tideLocation);

            var locationSpinner = FindViewById <Spinner>(Resource.Id.locationSpinner);

            locationSpinner.Adapter = adapter;

            // Event handler for selected spinner item
            string selectedLocation = "";

            locationSpinner.ItemSelected += delegate(object sender, AdapterView.ItemSelectedEventArgs e) {
                Spinner spinner = (Spinner)sender;
                selectedLocation = (string)spinner.GetItemAtPosition(e.Position);
            };

            /* ------- DatePicker initialization ------- */

            var tideDatePicker = FindViewById <DatePicker>(Resource.Id.tideDatePicker);

            Tide dateTide =
                db.Get <Tide>((from s in db.Table <Tide>() select s).Min(s => s.ID));
            DateTime dateTime = DateTime.Parse(dateTide.Date);

            tideDatePicker.DateTime = dateTime;

            /* ------- Query for selected stock prices -------- */

            Button listViewButton = FindViewById <Button>(Resource.Id.listViewButton);

            listViewButton.Click += delegate
            {
                var second = new Intent(this, typeof(SecondActivity));
                second.PutExtra("Location", selectedLocation);
                second.PutExtra("Date", tideDatePicker.DateTime.ToString());
                StartActivity(second);
            };
        }