Adding weather data
This commit is contained in:
54
inc/Feed.php
54
inc/Feed.php
@@ -3,6 +3,7 @@
|
||||
namespace Franzz\Spot;
|
||||
use Franzz\Objects\PhpObject;
|
||||
use Franzz\Objects\Db;
|
||||
use Franzz\Objects\Translator;
|
||||
use \Settings;
|
||||
|
||||
/**
|
||||
@@ -17,6 +18,16 @@ class Feed extends PhpObject {
|
||||
const FEED_TYPE_JSON = '/message.json';
|
||||
const FEED_MAX_REFRESH = 5 * 60; //Seconds
|
||||
|
||||
//Weather
|
||||
const WEATHER_HOOK = 'https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline';
|
||||
const WEATHER_PARAM = array(
|
||||
'key' => Settings::WEATHER_TOKEN,
|
||||
'unitGroup' => 'metric',
|
||||
'lang' => 'en',
|
||||
'include' => 'current',
|
||||
'iconSet' => 'icons2'
|
||||
);
|
||||
|
||||
//DB Tables
|
||||
const SPOT_TABLE = 'spots';
|
||||
const FEED_TABLE = 'feeds';
|
||||
@@ -95,17 +106,35 @@ class Feed extends PhpObject {
|
||||
|
||||
public function getMessages($asConstraints=array()) {
|
||||
$asInfo = array(
|
||||
'select' => array(Db::getId(self::MSG_TABLE), 'ref_msg_id', 'type', 'latitude', 'longitude', 'site_time', 'timezone', 'unix_time'),
|
||||
'select' => array(
|
||||
Db::getId(self::MSG_TABLE), 'ref_msg_id', 'type', //ID
|
||||
'latitude', 'longitude', //Position
|
||||
'site_time', 'timezone', 'unix_time', //Time
|
||||
'weather_icon', 'weather_cond', 'weather_temp' //Weather
|
||||
),
|
||||
'from' => self::MSG_TABLE,
|
||||
'join' => array(self::FEED_TABLE => Db::getId(self::FEED_TABLE)),
|
||||
'constraint'=> array(Db::getId(self::FEED_TABLE, true) => $this->getFeedId()),
|
||||
'constOpe' => array(Db::getId(self::FEED_TABLE, true) => "="),
|
||||
'orderBy' => array('site_time'=>'ASC')
|
||||
);
|
||||
|
||||
if(!empty($asConstraints)) $asInfo = array_merge($asInfo, $asConstraints);
|
||||
|
||||
$asResult = $this->oDb->selectRows($asInfo);
|
||||
|
||||
return $this->oDb->selectRows($asInfo);
|
||||
/* Temporary lookup - Start */
|
||||
$iCount = 0;
|
||||
foreach($asResult as &$asMsg) {
|
||||
if($asMsg['weather_icon'] == '' && $iCount < 3) {
|
||||
$asWeather = $this->getWeather(array($asMsg['latitude'], $asMsg['longitude']), $asMsg['unix_time']);
|
||||
$asMsg = array_merge($asMsg, $asWeather);
|
||||
$this->oDb->updateRow(self::MSG_TABLE, $asMsg[Db::getId(self::MSG_TABLE)], $asWeather, false);
|
||||
$iCount++;
|
||||
}
|
||||
}
|
||||
/* Temporary lookup - End */
|
||||
|
||||
return $asResult;
|
||||
}
|
||||
|
||||
public function getLastMessageId($asConstraints=array()) {
|
||||
@@ -181,7 +210,12 @@ class Feed extends PhpObject {
|
||||
|
||||
$iMsgId = $this->oDb->selectId(self::MSG_TABLE, array('ref_msg_id'=>$asMsg['ref_msg_id']));
|
||||
if(!$iMsgId) {
|
||||
//First Catch
|
||||
$asMsg['posted_on'] = $sNow;
|
||||
|
||||
//Weather Data
|
||||
$asMsg = array_merge($asMsg, $this->getWeather(array($asMsg['latitude'], $asMsg['longitude']), $asMsg['unix_time']));
|
||||
|
||||
$this->oDb->insertRow(self::MSG_TABLE, $asMsg);
|
||||
$bNewMsg = true;
|
||||
}
|
||||
@@ -194,6 +228,20 @@ class Feed extends PhpObject {
|
||||
return $bNewMsg;
|
||||
}
|
||||
|
||||
private function getWeather($asLatLng, $iTimeStamp) {
|
||||
$sApiUrl = self::WEATHER_HOOK.'/'.$asLatLng[0].','.$asLatLng[1].'/'.$iTimeStamp.'?'.http_build_query(self::WEATHER_PARAM);
|
||||
$asWeather = json_decode(file_get_contents($sApiUrl), true);
|
||||
|
||||
//Get Condition ID
|
||||
$sCondKey = (new Translator(self::WEATHER_PARAM['lang']))->getTranslationKey($asWeather['currentConditions']['conditions']);
|
||||
|
||||
return array(
|
||||
'weather_icon' => $asWeather['currentConditions']['icon'],
|
||||
'weather_cond' => $sCondKey,
|
||||
'weather_temp' => floatval($asWeather['currentConditions']['temp'])
|
||||
);
|
||||
}
|
||||
|
||||
private function retrieveFeed() {
|
||||
$sContent = '[]';
|
||||
if($this->sRefFeedId !='') {
|
||||
|
||||
Reference in New Issue
Block a user