Revert "Update feeds once an hour"

This reverts commit 92df6bcad7142169a3ce6b427777d5a39967dfdb.
This commit is contained in:
2019-08-31 16:11:41 +02:00
parent 2cc54862c3
commit eb36e4f979

View File

@@ -1,123 +1,123 @@
<?php <?php
/** /**
* Feed Class * Feed Class
* Also takes care of spot (device) & messages * Also takes care of spot (device) & messages
* *
* To add a new feed, create a new records in the feed table: * To add a new feed, create a new records in the feed table:
* INSERT INTO feeds (ref_feed_id, id_spot, id_project) VALUES ('<feed_id>', '<related_spot_id>', '<related_project_id>'); * INSERT INTO feeds (ref_feed_id, id_spot, id_project) VALUES ('<feed_id>', '<related_spot_id>', '<related_project_id>');
*/ */
class Feed extends PhpObject { class Feed extends PhpObject {
//Spot feed //Spot feed
const FEED_HOOK = 'https://api.findmespot.com/spot-main-web/consumer/rest-api/2.0/public/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_XML = '/message.xml';
const FEED_TYPE_JSON = '/message.json'; const FEED_TYPE_JSON = '/message.json';
//DB Tables //DB Tables
const SPOT_TABLE = 'spots'; const SPOT_TABLE = 'spots';
const FEED_TABLE = 'feeds'; const FEED_TABLE = 'feeds';
const MSG_TABLE = 'messages'; const MSG_TABLE = 'messages';
/** /**
* Database Handle * Database Handle
* @var Db * @var Db
*/ */
private $oDb; private $oDb;
private $iFeedId; private $iFeedId;
private $sRefFeedId; private $sRefFeedId;
private $iLastUpdate; private $iLastUpdate;
public function __construct(Db &$oDb, $iFeedId) { public function __construct(Db &$oDb, $iFeedId) {
parent::__construct(__CLASS__, Settings::DEBUG); parent::__construct(__CLASS__, Settings::DEBUG);
$this->oDb = &$oDb; $this->oDb = &$oDb;
$this->setFeedId($iFeedId); $this->setFeedId($iFeedId);
} }
public function setFeedId($iFeedId) { public function setFeedId($iFeedId) {
$this->iFeedId = $iFeedId; $this->iFeedId = $iFeedId;
$asFeed = $this->oDb->selectRow(self::FEED_TABLE, $this->iFeedId); $asFeed = $this->oDb->selectRow(self::FEED_TABLE, $this->iFeedId);
$this->sRefFeedId = $asFeed['ref_feed_id']; $this->sRefFeedId = $asFeed['ref_feed_id'];
$this->iLastUpdate = strtotime($asFeed['last_update']); $this->iLastUpdate = strtotime($asFeed['last_update']);
} }
public function getMessages($asActivePeriod = array()) { public function getMessages($asActivePeriod = array()) {
$asInfo = array( $asInfo = array(
'select' => array('id_message', 'ref_msg_id', 'type', 'latitude', 'longitude', 'site_time', 'unix_time'), 'select' => array('id_message', 'ref_msg_id', 'type', 'latitude', 'longitude', 'site_time', 'unix_time'),
'from' => self::MSG_TABLE, 'from' => self::MSG_TABLE,
'constraint'=> array(Db::getId(self::FEED_TABLE) => $this->iFeedId), 'constraint'=> array(Db::getId(self::FEED_TABLE) => $this->iFeedId),
'constOpe' => array(Db::getId(self::FEED_TABLE) => "="), 'constOpe' => array(Db::getId(self::FEED_TABLE) => "="),
'orderBy' => array('site_time'=>'ASC') 'orderBy' => array('site_time'=>'ASC')
); );
if(!empty($asActivePeriod)) { if(!empty($asActivePeriod)) {
$asInfo['constraint']['site_time'] = $asActivePeriod; $asInfo['constraint']['site_time'] = $asActivePeriod;
$asInfo['constOpe']['site_time'] = "BETWEEN"; $asInfo['constOpe']['site_time'] = "BETWEEN";
} }
return $this->oDb->selectRows($asInfo); return $this->oDb->selectRows($asInfo);
} }
public function checkUpdateFeed($sProjectMode) { public function checkUpdateFeed($sProjectMode) {
//Feed updated once an hour in Blog Mode //Feed updated once a day in Blog Mode
if($sProjectMode == Project::MODE_BLOG && date('Y-m-d-H', $this->iLastUpdate) != date('Y-m-d-H')) $this->updateFeed(); if($sProjectMode == Project::MODE_BLOG && date('Y-m-d', $this->iLastUpdate) != date('Y-m-d')) $this->updateFeed();
} }
private function updateFeed() { private function updateFeed() {
$asData = $this->retrieveFeed(); $asData = $this->retrieveFeed();
$sLastUpdate = date(Db::TIMESTAMP_FORMAT); $sLastUpdate = date(Db::TIMESTAMP_FORMAT);
if(!isset($asData['response']['errors']) || empty($asData['response']['errors'])) { if(!isset($asData['response']['errors']) || empty($asData['response']['errors'])) {
$asMsgs = $asData['response']['feedMessageResponse']['messages']['message']; $asMsgs = $asData['response']['feedMessageResponse']['messages']['message'];
$asFeed = $asData['response']['feedMessageResponse']['feed']; $asFeed = $asData['response']['feedMessageResponse']['feed'];
if(!empty($asMsgs)) if(!empty($asMsgs))
{ {
//Update Spot Info from the first message //Update Spot Info from the first message
$asFirstMsg = array_values($asMsgs)[0]; $asFirstMsg = array_values($asMsgs)[0];
$asSpotInfo = array( $asSpotInfo = array(
'ref_spot_id' => $asFirstMsg['messengerId'], 'ref_spot_id' => $asFirstMsg['messengerId'],
'name' => $asFirstMsg['messengerName'], 'name' => $asFirstMsg['messengerName'],
'model' => $asFirstMsg['modelId'] 'model' => $asFirstMsg['modelId']
); );
$iSpotId = $this->oDb->insertUpdateRow(self::SPOT_TABLE, $asSpotInfo, array('ref_spot_id')); $iSpotId = $this->oDb->insertUpdateRow(self::SPOT_TABLE, $asSpotInfo, array('ref_spot_id'));
//Update Feed Info and last update date //Update Feed Info and last update date
$asFeedInfo = array( $asFeedInfo = array(
'ref_feed_id' => $asFeed['id'], 'ref_feed_id' => $asFeed['id'],
Db::getId(self::SPOT_TABLE) => $iSpotId, Db::getId(self::SPOT_TABLE) => $iSpotId,
'name' => $asFeed['name'], 'name' => $asFeed['name'],
'description' => $asFeed['description'], 'description' => $asFeed['description'],
'status' => $asFeed['status'], 'status' => $asFeed['status'],
'last_update' => $sLastUpdate 'last_update' => $sLastUpdate
); );
$iFeedId = $this->oDb->insertUpdateRow(self::FEED_TABLE, $asFeedInfo, array('ref_feed_id')); $iFeedId = $this->oDb->insertUpdateRow(self::FEED_TABLE, $asFeedInfo, array('ref_feed_id'));
//Update Messages //Update Messages
foreach($asMsgs as $asMsg) foreach($asMsgs as $asMsg)
{ {
$asMsg = array( $asMsg = array(
'ref_msg_id' => $asMsg['id'], 'ref_msg_id' => $asMsg['id'],
Db::getId(self::FEED_TABLE) => $iFeedId, Db::getId(self::FEED_TABLE) => $iFeedId,
'type' => $asMsg['messageType'], 'type' => $asMsg['messageType'],
'latitude' => $asMsg['latitude'], 'latitude' => $asMsg['latitude'],
'longitude' => $asMsg['longitude'], 'longitude' => $asMsg['longitude'],
'iso_time' => $asMsg['dateTime'], //ISO 8601 time (backup) 'iso_time' => $asMsg['dateTime'], //ISO 8601 time (backup)
'site_time' => date(Db::TIMESTAMP_FORMAT, $asMsg['unixTime']), //Conversion to Site Time (see Settings::TIMEZONE) 'site_time' => date(Db::TIMESTAMP_FORMAT, $asMsg['unixTime']), //Conversion to Site Time (see Settings::TIMEZONE)
'unix_time' => $asMsg['unixTime'], //UNIX Time (backup) 'unix_time' => $asMsg['unixTime'], //UNIX Time (backup)
'content' => $asMsg['messageContent'], 'content' => $asMsg['messageContent'],
'battery_state' => $asMsg['batteryState'] 'battery_state' => $asMsg['batteryState']
); );
$this->oDb->insertUpdateRow(self::MSG_TABLE, $asMsg, array('ref_msg_id')); $this->oDb->insertUpdateRow(self::MSG_TABLE, $asMsg, array('ref_msg_id'));
} }
} }
} }
else $this->oDb->updateRow(self::FEED_TABLE, $this->iFeedId, array('last_update'=>$sLastUpdate)); else $this->oDb->updateRow(self::FEED_TABLE, $this->iFeedId, array('last_update'=>$sLastUpdate));
} }
private function retrieveFeed() { private function retrieveFeed() {
$sUrl = self::FEED_HOOK.$this->sRefFeedId.self::FEED_TYPE_JSON; $sUrl = self::FEED_HOOK.$this->sRefFeedId.self::FEED_TYPE_JSON;
$sContent = file_get_contents($sUrl); $sContent = file_get_contents($sUrl);
return json_decode($sContent, true); return json_decode($sContent, true);
} }
} }