Ejemplo n.º 1
0
 public function retrieve(Request $request, Weather $weather)
 {
     if (!$request->has('lat') || !$request->has('lon')) {
         return response('Please provide a lat and lon', 400);
     }
     $input = $request->all();
     // We grab the lat and lon
     $lat = $input['lat'];
     $lon = $input['lon'];
     // We grab the latest data from this lat and long
     $rawData = file_get_contents('http://api.openweathermap.org/data/2.5/weather?lat=' . $lat . '&lon=' . $lon);
     if (!$rawData) {
         // We failed to retrieve data from the webservice
         // Just return the stuff we have
         return $weather->where('lat', $lat)->where('lon', $lon)->limit(10)->get();
     }
     $jsonData = json_decode($rawData, true);
     // We transform this data
     $data = ['dt' => $jsonData['dt'], 'lat' => $lat, 'lon' => $lon, 'type' => $jsonData['weather'][0]['main'], 'temp' => $jsonData['main']['temp'] - 273.15];
     // Check if we already have a record with same lat/lon and dt in our database
     $weatherCheck = $weather->where('lat', $lat)->where('lon', $lon)->where('dt', Carbon::createFromTimeStamp($data['dt']))->limit(1)->get();
     // Record isn't in our db yet
     if ($weatherCheck->isEmpty()) {
         // We store the data in our database
         $weather->create($data);
     }
     // We grab the last 10 weather report from given lat/lon and return it
     return $weather->where('lat', $lat)->where('lon', $lon)->limit(10)->get();
 }
Ejemplo n.º 2
0
 public function CaiyunGetNow(Request $request)
 {
     header('Access-Control-Allow-Origin:*');
     header("content-type:application/json");
     $lon = $request->input('lon');
     //经纬度数据需保留4位小数,例如:104.2569,30.6463
     $lat = $request->input('lat');
     if (!is_numeric($lon) || !is_numeric($lat)) {
         $this->throwError(501, '参数违法');
     }
     //        $url_now = 'https://api.caiyunapp.com/v2/xeeL7DagnXhxhK7u/' . $lon . ',' . $lat . '/realtime.json';
     //        $data_now = $this->Curlget($url_now, '', 'get');
     //        $data_now = json_decode($data_now);
     //        $result = $data_now->result;
     $weatherData = Weather::all();
     $weatherData = $weatherData->toArray();
     $key = rand(0, count($weatherData) - 1);
     unset($weatherData[$key]['id']);
     $caiyunNow = $weatherData[$key];
     //        $caiyunNow = array(
     //            'temperature'=>$result->temperature,
     //            'humidity'=>$result->humidity,
     //            'precipitation'=>$result->precipitation->local->intensity,
     //            'wind'=>array(
     //                'direction'=>$result->wind->direction,
     //                'speed'=>$result->wind->speed
     //            )
     //        );
     $this->show($caiyunNow);
 }
Ejemplo n.º 3
0
 /**
  * Display the specified resource.
  *
  * @param  int  $id
  * @return Response
  */
 public function show($id)
 {
     $location = Location::findOrFail($id);
     $weathers = Weather::get($location->name);
     $hospitals = Hospital::where('address', 'LIKE', "%{$location->name}%")->get();
     $request = ['weathers' => $weathers, 'hospitals' => $hospitals];
     return json_encode($request);
 }
Ejemplo n.º 4
0
 private function update_weather($list, $town_id)
 {
     for ($i = 0; $i < sizeof($list); $i++) {
         $date = date("Y-m-d H:i:s", $list[$i]['dt']);
         $temp_min = $list[$i]['main']['temp_min'];
         $temp_max = $list[$i]['main']['temp_max'];
         $temp_min = round($temp_min - 273.15);
         $temp_max = round($temp_max - 273.15);
         $weather_icon = $list[$i]['weather'][0]['icon'];
         $wind_icon = $list[$i]['wind']['deg'];
         $weather = new Weather();
         $weather->town_id = $town_id;
         $weather->temp_min = $temp_min;
         $weather->temp_max = $temp_max;
         $weather->kuupaev = $date;
         $weather->weather_icon = $weather_icon;
         $weather->wind_icon = $wind_icon;
         $weather->save();
     }
 }
Ejemplo n.º 5
0
 /**
  * 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 . "!";
         }
     }
 }
Ejemplo n.º 6
0
 public function cronWeather()
 {
     Weather::truncate();
     $hoteluri = Hoteluri::all();
     foreach ($hoteluri as $hotel) {
         $file = "http://api.yr.no/weatherapi/locationforecast/1.9/?lat={$hotel->Latitude};lon={$hotel->Longitude};msl=70";
         $xml = new SimpleXMLElement($file, null, TRUE);
         $temp = $xml->product->time;
         foreach ($temp as $xml_time) {
             foreach ($xml_time->location as $xml_loc) {
                 if ($xml_loc->minTemperature && $xml_loc->maxTemperature && $xml_loc->symbol) {
                     $mintemp = $xml_loc->minTemperature->attributes()->value;
                     $maxtemp = $xml_loc->maxTemperature->attributes()->value;
                     $array_min = array_filter(preg_split("(T|Z)", $xml_time->attributes()->from)) + ['2' => (string) $mintemp];
                     $array_max = array_filter(preg_split("(T|Z)", $xml_time->attributes()->to)) + ['2' => (string) $maxtemp];
                     $icon = "http://api.yr.no/weatherapi/weathericon/1.1/?symbol=" . $xml_loc->symbol->attributes()->number . ";is_night=1;content_type=image/png";
                     $array = ['HotelID' => $hotel->id, 'FromDate' => $array_min[0], 'FromHour' => $array_min[1], 'MinDegrees' => $array_min[2], 'ToDate' => $array_max[0], 'ToHour' => $array_max[1], 'MaxDegrees' => $array_max[2], 'Logo' => $xml_loc->symbol->attributes()->number];
                     Weather::create($array);
                 }
             }
         }
     }
 }
Ejemplo n.º 7
0
 public function refresh($town)
 {
     $town_id = DB::table('towns')->where('town', $town)->first();
     $town_id = $town_id->id;
     //получить id города
     $response = json_decode(file_get_contents('http://api.openweathermap.org/data/2.5/forecast?q=' . $town . '&mode=json&appid=f84ba1064b0ae65792326548686f361c'), true);
     //  print_r($response);
     $id = $response['city']['id'];
     $date_today = date("Y-m-d", strtotime("+0 day"));
     DB::table('weather')->where('kuupaev', '>=', $date_today)->where('town_id', $town_id)->delete();
     for ($i = 0; $i < sizeof($response['list']); $i++) {
         $date = date("Y-m-d H:i:s", $response['list'][$i]['dt']);
         $temp_min = $response['list'][$i]['main']['temp_min'];
         $temp_max = $response['list'][$i]['main']['temp_max'];
         $temp_min = round($temp_min - 273.15);
         $temp_max = round($temp_max - 273.15);
         $weather = new Weather();
         $weather->town_id = $id;
         $weather->temp_min = $temp_min;
         $weather->temp_max = $temp_max;
         $weather->kuupaev = $date;
         $weather->save();
     }
     return redirect('weather/' . $town);
 }
Ejemplo n.º 8
0
 public function ListareDetaliiHotel($tara, $oras, $slughotel)
 {
     $hotel = Hoteluri::findBySlug($slughotel);
     $oras = Orase::findbyslug($oras);
     $hotel_id = Hoteluri::findOrFail($hotel->id);
     $weather = Weather::where('HotelID', $hotel->id)->get();
     $hoteluriSimilare = Hoteluri::where('AidaID', '<>', $hotel->AidaID)->where('City', $oras->AidaID)->take(3)->get();
     return view('frontend.detalii-hotel')->with('hotel', $hotel)->with('weather', $weather)->with('hoteluriSimilare', $hoteluriSimilare)->with('hotel_id', $hotel_id);
 }