public async Task GetWaterData(DateTime start, DateTime end) { try { // Enable loading screen LoadingViewEnable(); Debug.WriteLine("GET WATER DATA: " + start.ToString() + " - " + end.ToString()); Device.BeginInvokeOnMainThread(() => { LoadingLabel.Text = "Getting water data..."; }); DateTime startTime = start; DateTime endTime = end; // Set timespan - minusSpan (UTC - FINNISH TIME) is to get UTC data relative to finnish time datePickers // e.g. 10/10/2018 00:00:00 - 11/10/2018 00:00:00 FI TIME (+2) is 9/10/2018 22:00:00 - 10/10/2018 22:00:00 UTC TIME DateTime FinlandTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, App.FinlandTimeZone); TimeSpan minusSpan = DateTime.UtcNow - FinlandTime; startTime = new DateTime(start.Year, start.Month, start.Day, 0, 0, 0).Add(minusSpan); endTime = new DateTime(end.Year, end.Month, end.Day).Add(new TimeSpan(23, 59, 59).Add(minusSpan)); // Clear old datalists SingletonWaterFlowDataList dataListFlow = SingletonWaterFlowDataList.Instance; SingletonWaterLevelDataList dataListLevel = SingletonWaterLevelDataList.Instance; dataListFlow.Clear(); dataListLevel.Clear(); // Get Data and parse it await Task.Run(async() => { String startTm = String.Format("{0:s}", startTime); String endTm = String.Format("{0:s}", endTime); var WaterFlowDataRequest = new GraphQLRequest { Query = "{ water_data_query( start_time: \"" + startTm + "\" end_time: \"" + endTm + "\" ) { flow level timestamp } }" }; Debug.WriteLine("{ water_data_query( start_time: \"" + startTm + "\" end_time: \"" + endTm + "\" ) { flow level timestamp } }"); var graphQLClient = new GraphQLClient("http://api.otpt18-c.course.tamk.cloud/"); var graphQLResponse = await graphQLClient.PostAsync(WaterFlowDataRequest); var dynamicData = graphQLResponse.Data["water_data_query"]; Device.BeginInvokeOnMainThread(() => { LoadingLabel.Text = "Updating graphs..."; }); // Get timeDifference DateTime localTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, App.FinlandTimeZone); TimeSpan timeDifference = localTime - DateTime.UtcNow; // Add fetched data into datalists foreach (JObject data in dynamicData) { DateTime time = new DateTime(1970, 1, 1).AddMilliseconds(double.Parse(data["timestamp"].ToString())).Add(timeDifference); dataListFlow.Add(new WaterFlowData( // Date time, // Flow in cubic meter per second float.Parse(data["flow"].ToString()) )); dataListLevel.Add(new WaterLevelData( // Date time, // Level in meters float.Parse(data["level"].ToString()) / 100 // value is in centimeters so dividing by 100 )); } Debug.WriteLine("GOT WATER DATA, FLOW OBJECTS: " + dataListFlow.Count + " - LEVEL OBJECTS: " + dataListLevel.Count); DateTime newestLevelDate = SingletonWaterLevelDataList.Instance.OrderByDescending(x => x.Timestamp).First().Timestamp; Debug.WriteLine("-------------------------------------- NEWEST DATE ON LIST: " + newestLevelDate.ToString()); }).ContinueWith((task) => { // Update plots Device.BeginInvokeOnMainThread(() => { // Update date limits UpdateDatePickers(); SingletonWaterLevelModel.Instance.SetSeries(SingletonWaterLevelDataList.Instance); SingletonWaterFlowModel.Instance.SetSeries(SingletonWaterFlowDataList.Instance); CreatePlot(); // Disable loading screen LoadingViewDisable(); Debug.WriteLine("----- WATER DATA GOT AND UI UPDATED"); }); }); } catch (Exception e) { Debug.WriteLine("Error: " + e.ToString()); } }
public async Task GetAllWaterDataOnAppStart() { try { Debug.WriteLine("GET DATA ON APP START"); Device.BeginInvokeOnMainThread(() => { LoadingLabel.Text = "Getting water data..."; }); // Clear old datalists SingletonWaterFlowDataList dataListFlow = SingletonWaterFlowDataList.Instance; SingletonWaterLevelDataList dataListLevel = SingletonWaterLevelDataList.Instance; dataListFlow.Clear(); dataListLevel.Clear(); // Get Data and parse it await Task.Run(async() => { var WaterFlowDataRequest = new GraphQLRequest { Query = "{ water_data_query( start_time: \"\" end_time: \"\" ) { flow level timestamp } }" }; var graphQLClient = new GraphQLClient("http://api.otpt18-c.course.tamk.cloud/"); var graphQLResponse = await graphQLClient.PostAsync(WaterFlowDataRequest); var dynamicData = graphQLResponse.Data["water_data_query"]; Device.BeginInvokeOnMainThread(() => { LoadingLabel.Text = "Updating graphs..."; }); // Get timeDifference DateTime localTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, App.FinlandTimeZone); TimeSpan timeDifference = localTime - DateTime.UtcNow; // Add fetched data into datalists foreach (JObject data in dynamicData) { DateTime time = new DateTime(1970, 1, 1).AddMilliseconds(double.Parse(data["timestamp"].ToString())).Add(timeDifference); dataListFlow.Add(new WaterFlowData( // Date time, // Flow in cubic meter per second float.Parse(data["flow"].ToString()) )); dataListLevel.Add(new WaterLevelData( // Date time, // Level in meters float.Parse(data["level"].ToString()) / 100 // value is in centimeters so dividing by 100 )); } Debug.WriteLine("GOT WATER DATA, FLOW OBJECTS: " + dataListFlow.Count + " - LEVEL OBJECTS: " + dataListLevel.Count); Debug.WriteLine("GETTING ALERT LEVEL"); var AlertLevelRequest = new GraphQLRequest { Query = "{ water_level_alarm{ alarm_level } }" }; graphQLResponse = await graphQLClient.PostAsync(AlertLevelRequest); var alertData = graphQLResponse.Data["water_level_alarm"]; JObject alertLvl = alertData; Debug.WriteLine("-------- ALERT LEVEL IS: " + alertLvl["alarm_level"].ToString()); // The alarm level is in centimeters in database so dividing by 100 to get value in meters SingletonWaterLevelModel.Instance.AlertLineValue = double.Parse(alertLvl["alarm_level"].ToString()) / 100; }).ContinueWith((task) => { // Update plots Device.BeginInvokeOnMainThread(() => { // Update date limits UpdateDatePickersOnAppStart(); SingletonWaterLevelModel.Instance.SetSeries(SingletonWaterLevelDataList.Instance); SingletonWaterFlowModel.Instance.SetSeries(SingletonWaterFlowDataList.Instance); CreatePlot(); // Add plot to the page AddPlotToPage(); // Create onclick events for buttons and datepickers WaterFlowButton.Clicked += WaterFlowButtonPressed; WaterLevelButton.Clicked += WaterLevelButtonPressed; StartDatePicker.DateSelected += StartDatePicker_DateSelected; EndDatePicker.DateSelected += EndDatePicker_DateSelected; // Disable loading screen LoadingViewDisable(); // Check alert CheckIfAlertIsOn(); Debug.WriteLine("---------- WATER DATA GOT AND UI UPDATED"); }); }); } catch (Exception e) { Debug.WriteLine("Error: " + e.ToString()); } }