Example #1
0
        public void AddWeatherSet(WeatherSet set)
        {
            if (rgWeatherSets == null)
            {
                rgWeatherSets = new List <WeatherSet>();
            }

            rgWeatherSets.Add(set);
            OrderListByDate();
        }
        private async Task <bool> CreateWeatherStationsFromBlob(BlobResultSegment seg)
        {
            weatherStations.Clear();

            foreach (CloudAppendBlob blobItem in seg.Results)
            {
                string text;
                try
                {
                    text = await blobItem.DownloadTextAsync();

                    Type weatherSetClass = typeof(WeatherSet);

                    string[] allDataSets = text.Split('\n');

                    foreach (String rootString in allDataSets)
                    {
                        JObject root    = JObject.Parse(rootString);
                        string  devName = root["device_name"].ToString();
                        double  lat     = Convert.ToDouble(root["location"]["lat"].ToString());
                        double  lon     = Convert.ToDouble(root["location"]["lon"].ToString());

                        int currStationIndex = weatherStations.FindIndex(x => x.StationName == devName);

                        //If a station of that type does not exist add it now and set current index as that
                        if (currStationIndex == -1)
                        {
                            weatherStations.Add(new WeatherStation(devName, lat, lon));
                            currStationIndex = weatherStations.Count - 1;
                        }

                        //Set the recorded time of this in local time, as it is stored in UTC time on server
                        WeatherSet newSet = new WeatherSet();

                        // Unix timestamp is what we store so convert it to local tie
                        double   unixTimeStamp = double.Parse(root["timestamp"].ToString());
                        DateTime dtDateTime    = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
                        newSet.RecordedTime = dtDateTime.AddSeconds(unixTimeStamp).ToLocalTime();

                        //Sensors are another array that we need to dive into
                        JArray  sensorArray = JArray.Parse(root["sensors"].ToString());
                        JObject AIArray     = JObject.Parse(root["Forecast"].ToString());

                        foreach (KeyValuePair <String, JToken> tagAI in AIArray)
                        {
                            String  tagName     = tagAI.Key.ToString();
                            String  s           = tagAI.Value.ToString();
                            JObject tagValueObj = JObject.Parse(tagAI.Value.ToString());
                            foreach (KeyValuePair <String, JToken> tagAIInner in tagValueObj)
                            {
                                string propName = tagAIInner.Key.ToString();
                                switch (tagName)
                                {
                                case "1h":
                                    if (propName == "temperature")
                                    {
                                        newSet.ai_1_hour_temperature = tagAIInner.Value.ToString();
                                    }
                                    else if (propName == "relative_humidity")
                                    {
                                        newSet.ai_1_hour_humidity = tagAIInner.Value.ToString();
                                    }
                                    else if (propName == "wind_speed")
                                    {
                                        newSet.ai_1_hour_wind = tagAIInner.Value.ToString();
                                    }
                                    break;

                                case "4h":
                                    if (propName == "temperature")
                                    {
                                        newSet.ai_4_hour_temperature = tagAIInner.Value.ToString();
                                    }
                                    else if (propName == "relative_humidity")
                                    {
                                        newSet.ai_4_hour_humidity = tagAIInner.Value.ToString();
                                    }
                                    else if (propName == "wind_speed")
                                    {
                                        newSet.ai_4_hour_wind = tagAIInner.Value.ToString();
                                    }
                                    break;

                                case "8h":
                                    if (propName == "temperature")
                                    {
                                        newSet.ai_8_hour_temperature = tagAIInner.Value.ToString();
                                    }
                                    else if (propName == "relative_humidity")
                                    {
                                        newSet.ai_8_hour_humidity = tagAIInner.Value.ToString();
                                    }
                                    else if (propName == "wind_speed")
                                    {
                                        newSet.ai_8_hour_wind = tagAIInner.Value.ToString();
                                    }
                                    break;

                                case "12h":
                                    if (propName == "temperature")
                                    {
                                        newSet.ai_12_hour_temperature = tagAIInner.Value.ToString();
                                    }
                                    else if (propName == "relative_humidity")
                                    {
                                        newSet.ai_12_hour_humidity = tagAIInner.Value.ToString();
                                    }
                                    else if (propName == "wind_speed")
                                    {
                                        newSet.ai_12_hour_wind = tagAIInner.Value.ToString();
                                    }
                                    break;

                                case "24h":
                                    if (propName == "temperature")
                                    {
                                        newSet.ai_24_hour_temperature = tagAIInner.Value.ToString();
                                    }
                                    else if (propName == "relative_humidity")
                                    {
                                        newSet.ai_24_hour_humidity = tagAIInner.Value.ToString();
                                    }
                                    else if (propName == "wind_speed")
                                    {
                                        newSet.ai_24_hour_wind = tagAIInner.Value.ToString();
                                    }
                                    break;
                                }
                            }
                        }

                        foreach (JObject sensorRoot in sensorArray)
                        {
                            JObject dataObj = JObject.Parse(sensorRoot["data"].ToString());
                            foreach (KeyValuePair <String, JToken> tag in dataObj)
                            {
                                String tagName  = tag.Key.ToString();
                                String tagValue = tag.Value.ToString();

                                //This may seem strange but this will allows us to download the data and simply check and see what sensors have data
                                //This allows the sensors at the station side to be easily turned on or off without crashing the app
                                //If we have a property that matched the data we will
                                tagName = char.ToUpper(tagName[0]) + tagName.Substring(1);
                                PropertyInfo info = weatherSetClass.GetProperty(tagName);
                                if (info != null)
                                {
                                    info.SetValue(newSet, tagValue, null);
                                }
                            }
                        }

                        //Add the set once this is done
                        weatherStations[currStationIndex].AddWeatherSet(newSet);
                    }
                }
                //This can happen when there is an empty blob object
                catch (Exception ex)
                {
                    Console.WriteLine("Failure getting a blob: " + ex.Message);
                }
            }

            return(await Task.FromResult(true));
        }