Adding Last Update
This commit is contained in:
74
inc/feed.php
74
inc/feed.php
@@ -5,45 +5,49 @@
|
||||
* Also manages spots (devices) & messages
|
||||
*/
|
||||
class Feed extends PhpObject {
|
||||
|
||||
|
||||
//Spot feed
|
||||
const FEED_HOOK = 'https://api.findmespot.com/spot-main-web/consumer/rest-api/2.0/public/feed/';
|
||||
const FEED_TYPE_XML = '/message.xml';
|
||||
const FEED_TYPE_JSON = '/message.json';
|
||||
const FEED_MAX_REFRESH = 5 * 60; //Seconds
|
||||
|
||||
|
||||
//DB Tables
|
||||
const SPOT_TABLE = 'spots';
|
||||
const FEED_TABLE = 'feeds';
|
||||
const MSG_TABLE = 'messages';
|
||||
|
||||
|
||||
/**
|
||||
* Database Handle
|
||||
* @var Db
|
||||
*/
|
||||
private $oDb;
|
||||
|
||||
|
||||
private $iFeedId;
|
||||
private $sRefFeedId;
|
||||
private $iLastUpdate;
|
||||
|
||||
|
||||
public function __construct(Db &$oDb, $iFeedId=0) {
|
||||
parent::__construct(__CLASS__, Settings::DEBUG);
|
||||
$this->oDb = &$oDb;
|
||||
if($iFeedId > 0) $this->setFeedId($iFeedId);
|
||||
}
|
||||
|
||||
|
||||
public function getFeedId() {
|
||||
return $this->iFeedId;
|
||||
}
|
||||
|
||||
|
||||
public function getLastUpdate(): int {
|
||||
return $this->iLastUpdate;
|
||||
}
|
||||
|
||||
public function setFeedId($iFeedId) {
|
||||
$this->iFeedId = $iFeedId;
|
||||
$asFeed = $this->getFeed();
|
||||
$this->sRefFeedId = $asFeed['ref_feed_id'];
|
||||
$this->iLastUpdate = strtotime($asFeed['last_update']);
|
||||
}
|
||||
|
||||
|
||||
public function createFeedId($oProjectId) {
|
||||
$this->setFeedId($this->oDb->insertRow(self::FEED_TABLE, array(
|
||||
Db::getId(Project::PROJ_TABLE) => $oProjectId,
|
||||
@@ -51,39 +55,39 @@ class Feed extends PhpObject {
|
||||
)));
|
||||
return $this->getFeedId();
|
||||
}
|
||||
|
||||
|
||||
public function setRefFeedId($sRefFeedId) {
|
||||
return $this->updateField('ref_feed_id', $sRefFeedId);
|
||||
}
|
||||
|
||||
|
||||
public function setSpotId($iSpotId) {
|
||||
return $this->updateField(Db::getId(self::SPOT_TABLE), $iSpotId);
|
||||
}
|
||||
|
||||
|
||||
public function setProjectId($iProjectId) {
|
||||
return $this->updateField(Db::getId(Project::PROJ_TABLE), $iProjectId);
|
||||
}
|
||||
|
||||
|
||||
public function getSpots() {
|
||||
$asSpots = $this->oDb->selectRows(array('from'=>self::SPOT_TABLE));
|
||||
foreach($asSpots as &$asSpot) $asSpot['id'] = $asSpot[Db::getId(self::SPOT_TABLE)];
|
||||
return $asSpots;
|
||||
}
|
||||
|
||||
|
||||
public function getFeeds($iFeedId=0) {
|
||||
$asInfo = array('from'=>self::FEED_TABLE);
|
||||
if($iFeedId > 0) $asInfo['constraint'] = array(Db::getId(self::FEED_TABLE)=>$iFeedId);
|
||||
$asFeeds = $this->oDb->selectRows($asInfo);
|
||||
|
||||
|
||||
foreach($asFeeds as &$asFeed) $asFeed['id'] = $asFeed[Db::getId(self::FEED_TABLE)];
|
||||
return $asFeeds;
|
||||
}
|
||||
|
||||
|
||||
public function getFeed() {
|
||||
$asFeeds = $this->getFeeds($this->getFeedId());
|
||||
return array_shift($asFeeds);
|
||||
}
|
||||
|
||||
|
||||
public function getMessages($asActivePeriod = array()) {
|
||||
$asInfo = array(
|
||||
'select' => array('id_message', 'ref_msg_id', 'type', 'latitude', 'longitude', 'site_time', 'unix_time'),
|
||||
@@ -92,7 +96,7 @@ class Feed extends PhpObject {
|
||||
'constOpe' => array(Db::getId(self::FEED_TABLE) => "="),
|
||||
'orderBy' => array('site_time'=>'ASC')
|
||||
);
|
||||
|
||||
|
||||
if(!empty($asActivePeriod)) {
|
||||
$asInfo['constraint']['site_time'] = $asActivePeriod;
|
||||
$asInfo['constOpe']['site_time'] = "BETWEEN";
|
||||
@@ -100,22 +104,22 @@ class Feed extends PhpObject {
|
||||
|
||||
return $this->oDb->selectRows($asInfo);
|
||||
}
|
||||
|
||||
|
||||
public function checkUpdateFeed($sProjectMode) {
|
||||
$bNewMsg = false;
|
||||
|
||||
|
||||
//Spam Check: no more than 1 API request per 5 minutes
|
||||
if($sProjectMode == Project::MODE_BLOG) {
|
||||
$oLastUpdate = new DateTime('@'.$this->iLastUpdate);
|
||||
$oNow = new DateTime('now');
|
||||
$iSecDiff = $oNow->getTimestamp() - $oLastUpdate->getTimestamp();
|
||||
|
||||
|
||||
if($iSecDiff > self::FEED_MAX_REFRESH) $bNewMsg = $this->updateFeed();
|
||||
}
|
||||
|
||||
|
||||
return $bNewMsg;
|
||||
}
|
||||
|
||||
|
||||
private function updateFeed() {
|
||||
$bNewMsg = false;
|
||||
$asData = $this->retrieveFeed();
|
||||
@@ -123,14 +127,14 @@ class Feed extends PhpObject {
|
||||
if(!isset($asData['response']['errors']) || empty($asData['response']['errors'])) {
|
||||
$asMsgs = $asData['response']['feedMessageResponse']['messages'];
|
||||
$asFeed = $asData['response']['feedMessageResponse']['feed'];
|
||||
|
||||
|
||||
//Fix unstable Spot API Structure
|
||||
if(array_key_exists('message', $asMsgs)) $asMsgs = $asMsgs['message']; //Sometimes adds an extra "message" level
|
||||
if(!array_key_exists(0, $asMsgs)) $asMsgs = array($asMsgs); //Jumps a level when there is only 1 message
|
||||
|
||||
|
||||
//Update Spot, Feed & Messages
|
||||
if(!empty($asMsgs) && array_key_exists('messengerId', $asMsgs[0])) {
|
||||
|
||||
|
||||
//Update Spot Info from the first message
|
||||
$asSpotInfo = array(
|
||||
'ref_spot_id' => $asMsgs[0]['messengerId'],
|
||||
@@ -138,7 +142,7 @@ class Feed extends PhpObject {
|
||||
'model' => $asMsgs[0]['modelId']
|
||||
);
|
||||
$iSpotId = $this->oDb->insertUpdateRow(self::SPOT_TABLE, $asSpotInfo, array('ref_spot_id'));
|
||||
|
||||
|
||||
//Update Feed Info and last update date
|
||||
$asFeedInfo = array(
|
||||
'ref_feed_id' => $asFeed['id'],
|
||||
@@ -149,7 +153,7 @@ class Feed extends PhpObject {
|
||||
'last_update' => $sLastUpdate
|
||||
);
|
||||
$iFeedId = $this->oDb->insertUpdateRow(self::FEED_TABLE, $asFeedInfo, array('ref_feed_id'));
|
||||
|
||||
|
||||
//Update Messages
|
||||
foreach($asMsgs as $asMsg) {
|
||||
$asMsg = array(
|
||||
@@ -164,7 +168,7 @@ class Feed extends PhpObject {
|
||||
'content' => $asMsg['messageContent'],
|
||||
'battery_state' => $asMsg['batteryState']
|
||||
);
|
||||
|
||||
|
||||
$iMsgId = $this->oDb->selectId(self::MSG_TABLE, array('ref_msg_id'=>$asMsg['ref_msg_id']));
|
||||
if(!$iMsgId) {
|
||||
$this->oDb->insertRow(self::MSG_TABLE, $asMsg);
|
||||
@@ -175,10 +179,10 @@ class Feed extends PhpObject {
|
||||
}
|
||||
}
|
||||
else $this->oDb->updateRow(self::FEED_TABLE, $this->getFeedId(), array('last_update'=>$sLastUpdate));
|
||||
|
||||
|
||||
return $bNewMsg;
|
||||
}
|
||||
|
||||
|
||||
private function retrieveFeed() {
|
||||
$sContent = '[]';
|
||||
if($this->sRefFeedId !='') {
|
||||
@@ -187,14 +191,14 @@ class Feed extends PhpObject {
|
||||
}
|
||||
return json_decode($sContent, true);
|
||||
}
|
||||
|
||||
|
||||
private function updateField($sField, $oValue) {
|
||||
$bResult = ($this->oDb->updateRow(self::FEED_TABLE, $this->getFeedId(), array($sField=>$oValue)) > 0);
|
||||
$this->setFeedId($this->getFeedId());
|
||||
|
||||
|
||||
return $bResult;
|
||||
}
|
||||
|
||||
|
||||
public function delete() {
|
||||
$sDesc = '';
|
||||
if($this->getFeedId() > 0) {
|
||||
@@ -202,7 +206,7 @@ class Feed extends PhpObject {
|
||||
if(!$bSuccess) $sDesc = $this->oDb->getLastError();
|
||||
}
|
||||
else $sDesc = 'Error while setting project: no Feed ID';
|
||||
|
||||
|
||||
return $sDesc;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user