Ejemplo n.º 1
0
        private WeatherData GetNewValue(String name)

        {
            WeatherData             weatherInfo     = new WeatherData();
            OpenWeatherMapDataClass openWeatherData = apiRequests.GetWeatherDataByCity(name);

            if (openWeatherData != null)
            {
                EUInformation info_temp  = new EUInformation();
                EUInformation info_press = new EUInformation();


                info_press.DisplayName  = new String("Pa");
                info_press.Description  = new String("Pascal");
                info_press.NamespaceUri = new string("http://www.opcfoundation.org/UA/units/un/cefact");
                info_press.UnitId       = 4932940;


                info_temp.DisplayName  = new String("K");
                info_temp.Description  = new String("Kelvin");
                info_temp.NamespaceUri = new string("http://www.opcfoundation.org/UA/units/un/cefact");
                info_temp.UnitId       = 5259596;

                weatherInfo.CityName = new string(name);
                AnalogData temperature    = new AnalogData();
                AnalogData maxTemperature = new AnalogData();
                AnalogData minTemperature = new AnalogData();
                AnalogData pressure       = new AnalogData();

                maxTemperature.Data        = openWeatherData.Main.TempMax;
                maxTemperature.Info        = info_temp;
                weatherInfo.MaxTemperature = maxTemperature;
                minTemperature.Data        = openWeatherData.Main.TempMin;
                minTemperature.Info        = info_temp;
                weatherInfo.MinTemperature = minTemperature;
                temperature.Data           = openWeatherData.Main.Temp;
                temperature.Info           = info_temp;
                weatherInfo.Temperature    = temperature;
                pressure.Data        = openWeatherData.Main.Pressure;
                pressure.Info        = info_press;
                weatherInfo.Pressure = pressure;
            }
            //timer set on
            m_simulationTimer = new Timer(OnRaiseSystemEvents, null, 20000, 20000);

            return(weatherInfo);
        }
        public OpenWeatherMapDataClass GetWeatherDataByCity(string city)
        {
            var client  = new RestClient("https://community-open-weather-map.p.rapidapi.com/weather?q=" + "" + city);
            var request = new RestRequest(Method.GET);

            request.AddHeader("x-rapidapi-host", "community-open-weather-map.p.rapidapi.com");
            request.AddHeader("x-rapidapi-key", Properties.Resources.apiKey3);
            IRestResponse response = client.Execute(request);

            Console.WriteLine("Response of ApiRequest to OpenWeatherMapData: " + response.StatusCode);
            if (response.StatusCode != System.Net.HttpStatusCode.OK)
            {
                Console.WriteLine("Api Request to OpenWeatherMapData is failed");
                return(null);
            }
            OpenWeatherMapDataClass openWeatherMapData = JsonConvert.DeserializeObject <OpenWeatherMapDataClass>(response.Content);

            return(openWeatherMapData);
        }
Ejemplo n.º 3
0
        private ServiceResult WriteWeatherData(string city, string measureOfTemperature)
        {
            WeatherData   insideData         = new WeatherData();
            AnalogData    insideTempData     = new AnalogData();
            AnalogData    insideMaxTempData  = new AnalogData();
            AnalogData    insideMinTempData  = new AnalogData();
            AnalogData    insidePressureData = new AnalogData();
            EUInformation info_press         = new EUInformation();
            EUInformation info_temp          = new EUInformation();

            lock (Lock)
            {
                if (city != null)
                {
                    double conversionFactor = 0;
                    info_press.DisplayName  = new String("Pa");
                    info_press.Description  = new String("Pascal");
                    info_press.NamespaceUri = new string("http://www.opcfoundation.org/UA/units/un/cefact");
                    info_press.UnitId       = 4932940;



                    switch (measureOfTemperature)
                    {
                    case "K":
                        Console.WriteLine("Unit of measurement for Temperature choosed: " + "Kelvin");
                        info_temp.DisplayName  = new String("K");
                        info_temp.Description  = new String("Kelvin");
                        info_temp.NamespaceUri = new string("http://www.opcfoundation.org/UA/units/un/cefact");
                        info_temp.UnitId       = 5259596;
                        break;

                    case "C":
                        Console.WriteLine("Unit of measurement for Temperature choosed: " + "Celsius");

                        info_temp.DisplayName  = new String("°C");
                        info_temp.Description  = new String("degree Celsius");
                        info_temp.NamespaceUri = new string("http://www.opcfoundation.org/UA/units/un/cefact");
                        info_temp.UnitId       = 4408652;



                        conversionFactor = 273.15;


                        break;

                    default:
                        Console.WriteLine("Unit of measurement for Temperature choosed: " + "Kelvin");
                        info_temp.DisplayName  = new String("K");
                        info_temp.Description  = new String("Kelvin");
                        info_temp.NamespaceUri = new string("http://www.opcfoundation.org/UA/units/un/cefact");
                        info_temp.UnitId       = 5259596;
                        break;
                    }

                    Console.WriteLine("mesure " + measureOfTemperature);
                    OpenWeatherMapDataClass openWeatherData = apiRequests.GetWeatherDataByCity(city.ToString());

                    if (openWeatherData != null)
                    {
                        insideMaxTempData.Data    = (float)(openWeatherData.Main.TempMax - conversionFactor);
                        insideMaxTempData.Info    = info_temp;
                        insideData.MaxTemperature = insideMaxTempData;



                        insideMinTempData.Data    = (float)(openWeatherData.Main.TempMin - conversionFactor);
                        insideMinTempData.Info    = info_temp;
                        insideData.MinTemperature = insideMinTempData;


                        insideTempData.Data    = (float)(openWeatherData.Main.Temp - conversionFactor);
                        insideTempData.Info    = info_temp;
                        insideData.Temperature = insideTempData;

                        insidePressureData.Data = openWeatherData.Main.Pressure;
                        insidePressureData.Info = info_press;
                        insideData.Pressure     = insidePressureData;

                        insideData.CityName = new string(city);



                        if (insideData != null)
                        {
                            openWeatherObject.CityName.Value         = new String(insideData.CityName);
                            openWeatherObject.WeatherData.Value      = insideData;
                            openWeatherObject.WeatherData.StatusCode = StatusCodes.Good;
                            openWeatherObject.WeatherData.Timestamp  = DateTime.UtcNow;
                            return(ServiceResult.Good);
                        }
                        else
                        {
                            return(StatusCodes.BadUnknownResponse);
                        }
                    }
                }
            }

            Console.WriteLine("INPUT ERROR: I can't get informations for this city or city is null: " + city);
            return(StatusCodes.BadAggregateInvalidInputs);
        }