Exemplo n.º 1
0
        public void SetMessage(IModelResponse responseModel)
        {
            if (string.IsNullOrEmpty(responseModel.Body))
            {
                return;
            }

            WidgetResponse widgetResponse = null;

            try
            {
                widgetResponse = _parseResponseWidget(responseModel.Body);
            }
            catch (ParseException)
            {
                OnRecieveModel?.Invoke(new DataModelListQc {
                    Status = EQcResponseStatus.Error
                });
                return;
            }

            if (widgetResponse.Module == ESocketModuleAnswer.rates)
            {
                ParseRates(responseModel.Status, widgetResponse.Args);
            }
        }
Exemplo n.º 2
0
        public JsonResult GetLocation(string location)
        {
            // IF the User denied the location access, Widget will return unavailable.
            if (location == "UnAvailable")
            {
                return(Json(new { isSucess = false, location = "" }));
            }
            // Else continue calling the API
            WidgetResponse widgetResponse = new WidgetResponse();

            // Get if the session is available
            if (_session != null && !string.IsNullOrEmpty(_session.GetString("Location")))
            {
                widgetResponse.Address       = _session.GetString("Address");
                widgetResponse.FeelsLikeTemp = Convert.ToInt32(_session.GetString("FeelsLikeTemp"));
                widgetResponse.Icon          = _session.GetString("Icon");
                widgetResponse.Ozone         = Convert.ToDouble(_session.GetString("Ozone"));
                widgetResponse.Temp          = Convert.ToInt32(_session.GetString("Temp"));
                widgetResponse.Aqi           = _session.GetString("Aqi");
            }
            else
            {
                _session.SetString("Location", location);
                ViewData["Location"] = location;
                WeatherRequest weatherRequest = new WeatherRequest();
                weatherRequest.Latitude  = Convert.ToDouble(location.Split(',')[0]);
                weatherRequest.Longitude = Convert.ToDouble(location.Split(',')[1]);
                try
                {
                    widgetResponse = _weatherApi.GetWeather(weatherRequest);
                    _session.SetString("Address", widgetResponse.Address);
                    _session.SetString("FeelsLikeTemp", widgetResponse.FeelsLikeTemp.ToString());
                    _session.SetString("Icon", widgetResponse.Icon);
                    _session.SetString("Ozone", widgetResponse.Ozone.ToString());
                    _session.SetString("Temp", widgetResponse.Temp.ToString());
                    _session.SetString("Aqi", widgetResponse.Aqi.ToString());
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex.StackTrace);
                }
            }
            return(Json(new { isSucess = true, location = widgetResponse }));
        }
Exemplo n.º 3
0
        public WidgetResponse GetWeather(WeatherRequest request)
        {
            WidgetResponse widgetResponse = new WidgetResponse();

            try
            {
                WeatherResponse weatherResponse = new WeatherResponse();
                var             url             = string.Format("{0}lat={1}&lon={2}&appid={3}", BaseURL, request.Latitude, request.Longitude, apiKey);

                var jsonString = new WebClient().DownloadString(url);
                weatherResponse              = JsonConvert.DeserializeObject <WeatherResponse>(jsonString);
                widgetResponse.Temp          = Convert.ToInt32(weatherResponse.Current.Temp - 273);
                widgetResponse.FeelsLikeTemp = Convert.ToInt32(weatherResponse.Current.FeelsLike - 273);
                widgetResponse.Icon          = @"https://openweathermap.org/img/wn/" + weatherResponse.Current.Weather[0].Icon + "@2x.png";
                // Locationfetch

                string         locUrl           = string.Format("{0}{1}&q={2}+{3}&pretty=1&no_annotations=1", locationURL, locationApiKey, request.Latitude, request.Longitude);
                var            locationJson     = new WebClient().DownloadString(locUrl);
                LocationEntity locationResponse = new LocationEntity();
                locationResponse = JsonConvert.DeserializeObject <LocationEntity>(locationJson);
                string station = string.Empty;
                // Ozone value Fetch
                if (locationResponse != null && locationResponse.Status.Code == 200)
                {
                    if (locationResponse.Results != null &&
                        locationResponse.Results[0].Components != null)
                    {
                        var postCode = Convert.ToInt32(locationResponse.Results[0].Components.Postcode.Split(' ')[locationResponse.Results[0].Components.Postcode.Split(' ').Length - 1]);
                        widgetResponse.Address = string.Format("{0},{1}", locationResponse.Results[0].Components.Suburb, locationResponse.Results[0].Components.Country);

                        if (postCode >= 3000 && postCode <= 3999)
                        {
                            station = "Melbourne";
                        }
                        //2000—2599 2619—2899 2921—2999
                        else if ((postCode >= 2000 && postCode <= 2599) || (postCode >= 2619 && postCode <= 2899) || (postCode >= 2921 && postCode <= 2999))
                        {
                            station = "Sydney";
                        }
                        //7000—7799
                        else if (postCode >= 7000 && postCode <= 7799)
                        {
                            station = "hobart";
                        }
                        //2600—26182900—2920
                        else if ((postCode >= 2600 && postCode <= 2618) || (postCode >= 2900 && postCode <= 2920))
                        {
                            station = "Canberra";
                        }
                        //5000—5799
                        else if (postCode >= 5000 && postCode <= 5799)
                        {
                            station = "Adelaide";
                        }
                        //6000—6797
                        else if (postCode >= 6000 && postCode <= 6797)
                        {
                            station = "australia/western/camersham";
                        }
                        //4000—4999
                        else if (postCode >= 4000 && postCode <= 4999)
                        {
                            station = "Brisbane";
                        }
                        //0800—0899
                        else if (postCode >= 0800 && postCode <= 0899)
                        {
                            station = "Darwin";
                        }
                        else
                        {
                            station = "Melbourne";
                        }
                    }
                }
                string      ozoneString     = string.Format("{0}{1}/?token={2}", ozoneUrl, station, ozoneApikey);
                var         ozoneJsonString = new WebClient().DownloadString(ozoneString);
                OzoneEntity ozoneResponse   = new OzoneEntity();
                ozoneResponse        = JsonConvert.DeserializeObject <OzoneEntity>(ozoneJsonString);
                widgetResponse.Ozone = ozoneResponse.Data.Iaqi.O3.V;
                widgetResponse.Aqi   = ozoneResponse.Data.Aqi;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
            }
            return(widgetResponse);
        }