Ejemplo n.º 1
0
        private void UpdateValues()
        {
            //Console.Write("I'm trying to update nodes ");
            for (int index = 0; index < variables.Count; index++)
            {
                var item      = variables.ElementAt(index);
                var itemKey   = item.Key;   //padre
                var itemValue = item.Value; //figlio

                WeatherData weatherInfo = new WeatherData();
                weatherInfo = GetNewValue(itemKey.SymbolicName.ToString());



                if (weatherInfo != null)
                {
                    itemKey.Timestamp  = DateTime.UtcNow;
                    itemKey.Value      = weatherInfo;
                    itemKey.StatusCode = StatusCodes.Good;
                    Console.WriteLine("For node " + itemKey.SymbolicName.ToString() + " Found  " + itemValue.Count + "childrens");

                    for (int ii = 0; ii < itemValue.Count; ii++)
                    {
                        Console.WriteLine(" Child: " + itemValue[ii].SymbolicName.ToString());
                        switch (itemValue[ii].SymbolicName.ToString())
                        {
                        case "Temperature":
                            itemValue[ii].Timestamp  = itemKey.Timestamp;
                            itemValue[ii].Value      = weatherInfo.Temperature;
                            itemValue[ii].StatusCode = itemKey.StatusCode;
                            break;

                        case "Pressure":
                            itemValue[ii].Timestamp  = itemKey.Timestamp;
                            itemValue[ii].Value      = weatherInfo.Pressure;
                            itemValue[ii].StatusCode = itemKey.StatusCode;
                            break;

                        case "MaxTemperature":
                            itemValue[ii].Timestamp  = itemKey.Timestamp;
                            itemValue[ii].Value      = weatherInfo.MaxTemperature;
                            itemValue[ii].StatusCode = itemKey.StatusCode;
                            break;

                        case "MinTemperature":
                            itemValue[ii].Timestamp  = itemKey.Timestamp;
                            itemValue[ii].Value      = weatherInfo.MinTemperature;
                            itemValue[ii].StatusCode = itemKey.StatusCode;
                            break;


                        case "City":
                            itemValue[ii].Timestamp  = itemKey.Timestamp;
                            itemValue[ii].Value      = weatherInfo.CityName;
                            itemValue[ii].StatusCode = itemKey.StatusCode;
                            break;

                        default:
                            itemValue[ii].Timestamp  = itemKey.Timestamp;
                            itemValue[ii].Value      = null;
                            itemValue[ii].StatusCode = StatusCodes.Bad;
                            break;
                        }
                    } //chiusura figli


                    Console.WriteLine("Update follow Node " + itemKey.SymbolicName + " ");
                    itemKey.ClearChangeMasks(SystemContext, true);
                }
                else
                {
                    Console.WriteLine("I can't update nodes");
                }

                List <BaseInstanceState> child = new List <BaseInstanceState>();
                itemKey.GetChildren(SystemContext, child);
                Console.WriteLine(child.Count);
            }
        }
Ejemplo n.º 2
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);
        }