Esempio n. 1
0
        public async Task SetLocation(string location, double latitude, double longitude, RemoteViews widgetView)
        {
            var weather = Settings.Settings.Instance.GetWeather(latitude, longitude);

            if (weather is null)
            {
                try
                {
                    weather = await OpenWeather.GetWeather(latitude, longitude, OpenWeather.Units.Metric);

                    Settings.Settings.Instance.UpdatePlace(latitude, longitude, weather);
                }
                catch (Exception)
                {
                    weather = Settings.Settings.Instance.GetWeatherFull(latitude, longitude);
                    if (weather is null)
                    {
                        return;
                    }
                }
            }
            widgetView.SetTextViewText(Resource.Id.Temperature, FormatTemperature(weather.Current.Temperature));
            widgetView.SetTextViewText(Resource.Id.Sunrise, string.Format(Resources.Translations.GetSunriseText(),
                                                                          UnixTimeStampToDateTimeConverter.Convert(weather.Current.Sunrise).ToString("t")));
            widgetView.SetTextViewText(Resource.Id.Sunset, string.Format(Resources.Translations.GetSunsetText(),
                                                                         UnixTimeStampToDateTimeConverter.Convert(weather.Current.Sunset).ToString("t")));
            widgetView.SetTextViewText(Resource.Id.Pressure, weather.Current.Pressure.ToString("F0") + "hPa");
            widgetView.SetTextViewText(Resource.Id.WindSpeed, FormatWindSpeed(weather.Current.WindSpeed));

            if (weather.Current.WeatherList.Count > 0)
            {
                SetWeatherIcon(widgetView, weather.Current.WeatherList[0].Icon);
                widgetView.SetTextViewText(Resource.Id.Description, weather.Current.WeatherList[0].Description.FirstCharToUpper());
            }
        }
Esempio n. 2
0
 static async void Measure()
 {
     try
     {
         var content = new Message(telemetry.ToJson(await openWeather.GetWeather()));
         await deviceClient.SendEventAsync(content);
     }
     catch (Exception ex)
     {
         Debug.WriteLine(ex.Message);
     }
 }
Esempio n. 3
0
        public async Task Weather(IDialogContext context, LuisResult result)
        {
            if (result.Entities != null && result.Entities.Count > 0)
            {
                await context.PostAsync(await OpenWeather.GetWeather(result.Entities[0].Entity));
            }
            else
            {
                await context.PostAsync("This is not an valid request, please type a valid city name.");
            }

            context.Wait(MessageReceived);
        }
        /// <summary>
        /// POST: api/Messages
        /// Receive a message from a user and reply to it
        /// </summary>
        public async Task <HttpResponseMessage> Post([FromBody] Activity activity)
        {
            ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
            Activity        reply     = null;

            if (activity.Type == ActivityTypes.Message)
            {
                //Here, i'm using LuisDialog
                await Conversation.SendAsync(activity, () => new BotDialog());

                //Here, a very classic way
                string Response = String.Empty;
                try
                {
                    LuisResponse _luisResponse = await LUIS.GetLuisResponse(activity.Text);

                    if (_luisResponse.intents.Count() > 0)
                    {
                        switch (_luisResponse.intents[0].intent)
                        {
                        case "Greetings":
                            Response = "Hi there !";
                            break;

                        case "Weather":
                            var entities = _luisResponse.entities?.Count() ?? 0;
                            Response = entities > 0 ? await OpenWeather.GetWeather(_luisResponse?.entities?[0]?.entity) : "If you're asking about weather, please enter a valid city name";

                            break;

                        default:
                            Response = "Sorry, I am not getting you...";
                            break;
                        }
                    }
                    else
                    {
                        Response = "Sorry, I am not getting you...";
                    }
                }
                catch (Exception ex)
                {
                    Response = ex.Message;
                }
                // return our reply to the user
                reply = activity.CreateReply(Response);
            }
            else
            {
                reply = HandleSystemMessage(activity);
            }

            if (reply != null)
            {
                await connector.Conversations.ReplyToActivityAsync(reply);
            }

            var response = Request.CreateResponse(HttpStatusCode.OK);

            return(response);
        }
Esempio n. 5
0
        public async void SetLocation(string location, double latitude, double longitude)
        {
            IsLoading        = true;
            CurrentLocation  = location;
            Latitude         = latitude;
            Longitude        = longitude;
            OlderDataVisible = false;

            var weather = Settings.Settings.Instance.GetWeather(latitude, longitude);

            if (weather is null)
            {
                try
                {
                    weather = await OpenWeather.GetWeather(latitude, longitude, OpenWeather.Units.Metric);

                    Settings.Settings.Instance.UpdatePlace(latitude, longitude, weather);
                }
                catch (Exception)
                {
                    weather = Settings.Settings.Instance.GetWeatherFull(latitude, longitude);
                    if (weather is null)
                    {
                        IsLoading = false;
                        return;
                    }

                    OlderDataVisible = true;
                    OlderDataText    = string.Format(Resources.AppTranslations.OldDataFrom,
                                                     Settings.Settings.Instance.GetWeatherRefreshTime(latitude, longitude)?.ToString("g"));
                }
            }


            DateText = DateTime.Now.ToString("d");
            if (weather.Current.WeatherList.Count > 0)
            {
                WeatherIcon        = StringToIconConverter.Convert(weather.Current.WeatherList[0].Icon);
                WeatherDescription = weather.Current.WeatherList[0].Description.FirstCharToUpper();
            }

            WeatherTemperature = FormatTemperature(weather.Current.Temperature);
            if (weather.Current.Rain != null)
            {
                var max = Math.Max(weather.Current.Rain.ThreeHours, weather.Current.Rain.OneHour);
                WeatherRain = max.ToString("F0") + "mm";
            }
            if (weather.Current.Snow != null)
            {
                var max = Math.Max(weather.Current.Snow.ThreeHours, weather.Current.Snow.OneHour);
                WeatherRain = max.ToString("F0") + "mm";
            }

            WeatherWind = FormatWindSpeed(weather.Current.WindSpeed);

            WeatherPressure = weather.Current.Pressure.ToString("F0") + "hPa";
            WeatherSunrise  = string.Format(AppTranslations.Sunrise,
                                            UnixTimeStampToDateTimeConverter.Convert(weather.Current.Sunrise).ToString("t"));
            WeatherSunset = string.Format(AppTranslations.Sunset,
                                          UnixTimeStampToDateTimeConverter.Convert(weather.Current.Sunset).ToString("t"));

            HoursForecastItems.Clear();
            foreach (var hourlyWeather in weather.Hourly)
            {
                var forecast = new HourForecast()
                {
                    Time = UnixTimeStampToDateTimeConverter.Convert(hourlyWeather.Datetime)
                           .ToString("t"),
                    Temperature = FormatTemperature(hourlyWeather.Temperature)
                };
                if (hourlyWeather.WeatherList.Count > 0)
                {
                    forecast.IconSource = StringToIconConverter.Convert(hourlyWeather.WeatherList[0].Icon);
                }

                HoursForecastItems.Add(forecast);
                if (HoursForecastItems.Count >= 24)
                {
                    break;
                }
            }

            DaysForecastItems.Clear();
            foreach (var dailyWeather in weather.Daily)
            {
                var forecast = new DayForecast()
                {
                    Temperature = FormatTemperature(dailyWeather.Temperature.Day),
                    Time        = UnixTimeStampToDateTimeConverter.Convert(dailyWeather.Datetime + weather.TimezoneOffset)
                                  .ToString("ddd"),
                    Rain = (dailyWeather.Rain + dailyWeather.Snow).ToString("F0") + "mm",
                };
                if (dailyWeather.WeatherList.Count > 0)
                {
                    forecast.IconSource = StringToIconConverter.Convert(dailyWeather.WeatherList[0].Icon);
                }

                DaysForecastItems.Add(forecast);

                if (DaysForecastItems.Count >= 7)
                {
                    break;
                }
            }

            RefreshFavoritePlaces();
            IsLoading = false;
        }