/**
  * Display a listing of the resource.
  *
  * @return Response
  */
 public function getWeather()
 {
     $key = env('FORECAST_IO_API_KEY');
     $forecast = new Forecast($key);
     $weather = $forecast->get('34.101655', '-117.707591');
     for ($i = 0; $i < 7; $i++) {
         if ($i == 0) {
             $current = $weather->currently->temperature;
         } else {
             $current = -1;
         }
         $date = Carbon::createFromTimeStamp($weather->daily->data[$i]->time)->toDateTimeString();
         $dateId = substr($date, 0, 10);
         $id = DB::table('email_articles')->where('post_date', $dateId)->value('article_id');
         $icon = $weather->daily->data[$i]->icon;
         if (strpos($icon, 'night') !== false) {
             $icon = 'clear-day';
         }
         $max = $weather->daily->data[$i]->temperatureMax;
         $min = $weather->daily->data[$i]->temperatureMin;
         $sunset = Carbon::createFromTimeStamp($weather->daily->data[$i]->sunsetTime)->toDateTimeString();
         $sunrise = Carbon::createFromTimeStamp($weather->daily->data[$i]->sunriseTime)->toDateTimeString();
         echo strpos($icon, 'night');
         if (Weather::where('article_id', '=', $id)->exists()) {
             DB::table('weather')->where('article_id', $id)->update(array('icon' => $icon, 'current_temp' => $current, 'max' => $max, 'min' => $min, 'sunriseTime' => $sunrise, 'sunsetTime' => $sunset, 'updated_at' => Carbon::now()));
             echo "Updated " . $date;
         } else {
             $entry = new Weather();
             $entry->article_id = $id;
             $entry->icon = $icon;
             $entry->current_temp = $current;
             $entry->max = $max;
             $entry->min = $min;
             $entry->sunsetTime = $sunset;
             $entry->sunriseTime = $sunrise;
             $entry->save();
             echo "Stored " . $date . "!";
         }
     }
 }
Example #2
0
 private function getForecastData($displayId)
 {
     $defaultLat = Config::GetSetting('DEFAULT_LAT');
     $defaultLong = Config::GetSetting('DEFAULT_LONG');
     if ($this->GetOption('useDisplayLocation') == 1) {
         // Use the display ID or the default.
         if ($displayId != 0) {
             $display = new Display();
             $display->displayId = $displayId;
             $display->Load();
             $defaultLat = $display->latitude;
             $defaultLong = $display->longitude;
         }
     } else {
         $defaultLat = $this->GetOption('latitude', $defaultLat);
         $defaultLong = $this->GetOption('longitude', $defaultLong);
     }
     $apiKey = $this->GetSetting('apiKey');
     if ($apiKey == '') {
         die(__('Incorrectly configured module'));
     }
     // Query the API and Dump the Results.
     $forecast = new Forecast($apiKey);
     $apiOptions = array('units' => $this->GetOption('units', 'auto'), 'lang' => $this->GetOption('lang', 'en'), 'exclude' => 'flags,minutely,hourly');
     $key = md5($defaultLat . $defaultLong . 'null' . implode('.', $apiOptions));
     if (!Cache::has($key)) {
         Debug::LogEntry('audit', 'Getting Forecast from the API', $this->type, __FUNCTION__);
         if (!($data = $forecast->get($defaultLat, $defaultLong, null, $apiOptions))) {
             return false;
         }
         // If the response is empty, cache it for less time
         $cacheDuration = $this->GetSetting('cachePeriod');
         // Cache
         Cache::put($key, $data, $cacheDuration);
     } else {
         Debug::LogEntry('audit', 'Getting Forecast from the Cache with key: ' . $key, $this->type, __FUNCTION__);
         $data = Cache::get($key);
     }
     //Debug::Audit('Data: ' . var_export($data, true));
     // Icon Mappings
     $icons = array('unmapped' => 'wi-alien', 'clear-day' => 'wi-day-sunny', 'clear-night' => 'wi-night-clear', 'rain' => 'wi-rain', 'snow' => 'wi-snow', 'sleet' => 'wi-hail', 'wind' => 'wi-windy', 'fog' => 'wi-fog', 'cloudy' => 'wi-cloudy', 'partly-cloudy-day' => 'wi-day-cloudy', 'partly-cloudy-night' => 'wi-night-partly-cloudy');
     // Temperature Unit Mappings
     $temperatureUnit = '';
     foreach ($this->unitsAvailable() as $unit) {
         if ($unit['id'] == $this->GetOption('units', 'auto')) {
             $temperatureUnit = $unit['tempUnit'];
             break;
         }
     }
     // Are we set to only show daytime weather conditions?
     if ($this->GetOption('dayConditionsOnly') == 1) {
         if ($data->currently->icon == 'partly-cloudy-night') {
             $data->currently->icon = 'clear-day';
         }
     }
     $data->currently->wicon = isset($icons[$data->currently->icon]) ? $icons[$data->currently->icon] : $icons['unmapped'];
     $data->currently->temperatureFloor = isset($data->currently->temperature) ? floor($data->currently->temperature) : '--';
     $data->currently->summary = isset($data->currently->summary) ? $data->currently->summary : '--';
     $data->currently->weekSummary = isset($data->daily->summary) ? $data->daily->summary : '--';
     $data->currently->temperatureUnit = $temperatureUnit;
     // Convert a stdObject to an array
     $data = json_decode(json_encode($data), true);
     // Process the icon for each day
     for ($i = 0; $i < 7; $i++) {
         // Are we set to only show daytime weather conditions?
         if ($this->GetOption('dayConditionsOnly') == 1) {
             if ($data['daily']['data'][$i]['icon'] == 'partly-cloudy-night') {
                 $data['daily']['data'][$i]['icon'] = 'clear-day';
             }
         }
         $data['daily']['data'][$i]['wicon'] = isset($icons[$data['daily']['data'][$i]['icon']]) ? $icons[$data['daily']['data'][$i]['icon']] : $icons['unmapped'];
         $data['daily']['data'][$i]['temperatureMaxFloor'] = isset($data['daily']['data'][$i]['temperatureMax']) ? floor($data['daily']['data'][$i]['temperatureMax']) : '--';
         $data['daily']['data'][$i]['temperatureMinFloor'] = isset($data['daily']['data'][$i]['temperatureMin']) ? floor($data['daily']['data'][$i]['temperatureMin']) : '--';
         $data['daily']['data'][$i]['temperatureFloor'] = $data['daily']['data'][$i]['temperatureMinFloor'] != '--' && $data['daily']['data'][$i]['temperatureMaxFloor'] != '--' ? floor(($data['daily']['data'][$i]['temperatureMinFloor'] + $data['daily']['data'][$i]['temperatureMaxFloor']) / 2) : '--';
         $data['daily']['data'][$i]['temperatureUnit'] = $temperatureUnit;
     }
     return $data;
 }