示例#1
0
        /// <summary>
        /// Builds forecast page model for given app-chain job IDs and location
        /// </summary>
        /// <param name="jobId"></param>
        /// <param name="jobId2"></param>
        /// <param name="city"></param>
        /// <returns></returns>
        public RunResult Build(string jobId, string jobId2, string city)
        {
            mode = service.GetInfo(userName).Temperature ?? TemperatureMode.F;

            var _weatherWorker = new WeatherWorker(userName);

            var _forecastRoot = _weatherWorker.GetForecast10(city);
            var _runResult    = new RunResult
            {
                Weather = _forecastRoot.current_observation.weather + " " +
                          (mode == TemperatureMode.F
                                                 ? _forecastRoot.current_observation.temp_f + "F"
                                                 : _forecastRoot.current_observation.temp_c + "C"),
                Forecast       = _forecastRoot,
                CurrentWeather = _forecastRoot.current_observation
            };
            var _appChainResults = GetAppChainResultingRisks(jobId, jobId2);

            if (_forecastRoot != null)
            {
                var _alertCode       = _forecastRoot.alerts.Count == 0 ? "--" : _forecastRoot.alerts[0].type;
                var _riskDescription = GetPersonalizedRiskDescription(_forecastRoot.forecast.simpleforecast.forecastday[0].conditions,
                                                                      _alertCode, _appChainResults, userName, Options.ApplicationName);
                _runResult.Risk        = _riskDescription;
                _runResult.RawRisk     = _appChainResults.MelanomaAppChainResult.ToString();
                _runResult.Temperature = mode;
                _runResult.JobDateTime = _appChainResults.AppChainRunDt;
            }
            return(_runResult);
        }
示例#2
0
        /// <summary>
        /// Checks all users and sends emails/sms/on-site-alerts when appropriate
        /// </summary>
        public void CheckAllAndSendEmails()
        {
            using (var _ctx = new WeatherAppDbEntities())
            {
                var _api   = new MandrillApi(Options.MandrillApi);
                var _infos =
                    _ctx.SendInfo.Where(info => (info.SendEmail ?? false) || (info.SendSms ?? false)).ToList();
                foreach (var _info in _infos)
                {
                    var _mode = _info.Temperature ?? TemperatureMode.F;
                    var _rrb  = new PersonalizedForecastResultBuilder(_info.UserName, _mode);
                    LogManager.GetLogger(GetType()).Debug("Processing sendInfo:" + _info.Id);
                    if (IsRightTime(_info))
                    {
                        LogManager.GetLogger(GetType()).Debug("Processing sendInfo, time was right:" + _info.Id);
                        var _weatherWorker = new WeatherWorker(_info.UserName);
                        var _forecastRoot  = _weatherWorker.GetForecast10(_info.City);
                        LogManager.GetLogger(GetType()).Debug("Received forecast:" + _info.Id);
                        if (string.IsNullOrEmpty(_info.DataFileId))
                        {
                            continue;
                        }
                        var _jobId = GetJobId(_info);
                        LogManager.GetLogger(GetType()).Debug("Started job:" + _info.Id);

                        var _riskValue = _rrb.GetAppChainResultingRisks(_jobId.Item1.ToString(), _jobId.Item2.ToString());
                        var _alertCode = _forecastRoot.alerts.Count == 0 ? "--" : _forecastRoot.alerts[0].type;

                        var _riskDescription = _rrb.GetPersonalizedRiskDescription(_forecastRoot.forecast.simpleforecast.forecastday[0].conditions, _alertCode, _riskValue, _info.UserName, Options.ApplicationName);
                        var _subj            =
                            string.Format("Forecast for " +
                                          DateTime.Now.ToString("dddd MMMM d"));
                        var _city          = WeatherWorker.ConvertFromIDToName(_info.City);
                        var _time          = _forecastRoot.current_observation.observation_time;
                        var _todayForecast = _mode == TemperatureMode.F ?
                                             _forecastRoot.forecast.txt_forecast.forecastday[0].fcttext :
                                             _forecastRoot.forecast.txt_forecast.forecastday[0].fcttext_metric;
                        var _currentObservation = _forecastRoot.current_observation.weather + " and " + (_mode == TemperatureMode.F
                                                 ? _forecastRoot.current_observation.temp_f + "F"
                                                 : _forecastRoot.current_observation.temp_c + "C");

                        if (_info.SendSms ?? false)
                        {
                            LogManager.GetLogger(GetType()).Debug("Sending sms:" + _info.Id);
                            SendSmsNotification(_info, _city, _todayForecast, _currentObservation, _riskDescription);
                        }

                        if (notificationService.IsUserSubscribed(_info.Id))
                        {
                            LogManager.GetLogger(GetType()).Debug("Sending push:" + _info.Id);
                            SendPushNotification(_info, _city, _todayForecast, _currentObservation, _riskDescription);
                        }

                        if (_info.SendEmail ?? false)
                        {
                            LogManager.GetLogger(GetType()).Debug("Sending email:" + _info.Id);
                            SendEmailNotification(_info, _city, _todayForecast, _currentObservation, _riskDescription, _forecastRoot, _mode, _api, _subj);
                        }

                        _info.LastSendDt = DateTime.Now;
                        _ctx.SaveChanges();
                    }
                }
            }
        }