Exemplo n.º 1
0
        //1.首先获取redis中记录的所有天气Key(获得天气历史的对应信息)
        //2.将所有数据都整理成对应信息,保存到MSSQL中
        public void CtrlHistoricalWeather()
        {
            var keys = GetConn().GetServer(GetConn().GetEndPoints()[0]).Keys(0, "weather:*");
            List <HistoricalWeather> listHis = new List <HistoricalWeather>();

            try
            {
                foreach (var item in keys)
                {
                    foreach (var v in GetConn().GetDatabase().HashGetAll(item))
                    {
                        Dictionary <string, List <Dictionary <string, object> > > dic = new Dictionary <string, List <Dictionary <string, object> > >();
                        dic = JsonConvert.DeserializeObject <Dictionary <string, List <Dictionary <string, object> > > >(v.Value);

                        List <Dictionary <string, object> > listDicResults = dic["results"];
                        List <Dictionary <string, string> > dicDaily       = new List <Dictionary <string, string> >();


                        dicDaily = JsonConvert.DeserializeObject <List <Dictionary <string, string> > >(listDicResults[0]["daily"].ToString());

                        foreach (var s in dicDaily)
                        {
                            HistoricalWeather his = new HistoricalWeather();
                            his.id                  = Guid.NewGuid().ToString();
                            his.CityId              = item;
                            his.CurrentDate         = Convert.ToDateTime(v.Name);
                            his.Date                = Convert.ToDateTime(s["date"]);
                            his.TextDay             = s["text_day"];
                            his.CodeDay             = Convert.ToInt32(s["code_day"]);
                            his.TextNight           = s["text_night"];
                            his.CodeNight           = Convert.ToInt32(s["code_night"]);
                            his.High                = Convert.ToInt32(s["high"]);
                            his.Low                 = Convert.ToInt32(s["low"]);
                            his.Precip              = s["precip"];
                            his.WindDirection       = s["wind_direction"];
                            his.WindDirectionDegree = s["wind_direction_degree"];
                            his.WindSpeed           = Convert.ToInt32(s["wind_speed"]);
                            his.WindScale           = Convert.ToInt32(s["wind_scale"]);

                            listHis.Add(his);
                        }
                    }
                }
                HistoricalWeathersDal hisDal = new HistoricalWeathersDal();
                hisDal.Add(listHis);
            }
            catch (Exception e)
            {
                log.ErrorFormat("MSSQL同步Redis发生异常:{0}", e);
            }
        }
Exemplo n.º 2
0
 public bool AddHistoricalWeather(HistoricalWeatherDto historicalWeatherDto)
 {
     try
     {
         //var historicalWeather = _mapper.Map<HistoricalWeather>(historicalWeatherDto);
         var historicalWeather = new HistoricalWeather
         {
             ThermalSensation = historicalWeatherDto.ThermalSensation,
             Temperature      = historicalWeatherDto.Temperature,
             City             = _context.City.FirstOrDefault(x => x.Id == historicalWeatherDto.City.Id),
             Country          = _context.Country.FirstOrDefault(x => x.Id == historicalWeatherDto.Country.Id),
         };
         _context.Add <HistoricalWeather>(historicalWeather);
         _context.SaveChanges();
     }
     catch (Exception e)
     {
         Console.WriteLine("Error al guardar el historico");
         return(false);
     }
     return(true);
 }