Change file name to match class name

This commit is contained in:
2021-06-28 20:48:36 +02:00
parent df2c6da047
commit ed0eeaf776

View File

@@ -1,221 +1,221 @@
<?php <?php
namespace Franzz\Spot; namespace Franzz\Spot;
use Franzz\Objects\PhpObject; use Franzz\Objects\PhpObject;
use Franzz\Objects\Db; use Franzz\Objects\Db;
use \Settings; use \Settings;
/** /**
* Feed Class * Feed Class
* Also manages spots (devices) & messages * Also manages spots (devices) & messages
*/ */
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';
const FEED_MAX_REFRESH = 5 * 60; //Seconds const FEED_MAX_REFRESH = 5 * 60; //Seconds
//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=0) { public function __construct(Db &$oDb, $iFeedId=0) {
parent::__construct(__CLASS__, Settings::DEBUG); parent::__construct(__CLASS__, Settings::DEBUG);
$this->oDb = &$oDb; $this->oDb = &$oDb;
if($iFeedId > 0) $this->setFeedId($iFeedId); if($iFeedId > 0) $this->setFeedId($iFeedId);
} }
public function getFeedId() { public function getFeedId() {
return $this->iFeedId; return $this->iFeedId;
} }
public function getLastUpdate(): int { public function getLastUpdate(): int {
return $this->iLastUpdate; return $this->iLastUpdate;
} }
public function setFeedId($iFeedId) { public function setFeedId($iFeedId) {
$this->iFeedId = $iFeedId; $this->iFeedId = $iFeedId;
$asFeed = $this->getFeed(); $asFeed = $this->getFeed();
$this->sRefFeedId = $asFeed['ref_feed_id']; $this->sRefFeedId = $asFeed['ref_feed_id'];
$this->iLastUpdate = $asFeed['last_update']=='0000-00-00 00:00:00'?0:strtotime($asFeed['last_update']); $this->iLastUpdate = $asFeed['last_update']=='0000-00-00 00:00:00'?0:strtotime($asFeed['last_update']);
} }
public function createFeedId($oProjectId) { public function createFeedId($oProjectId) {
$this->setFeedId($this->oDb->insertRow(self::FEED_TABLE, array( $this->setFeedId($this->oDb->insertRow(self::FEED_TABLE, array(
Db::getId(Project::PROJ_TABLE) => $oProjectId, Db::getId(Project::PROJ_TABLE) => $oProjectId,
'status' => 'INACTIVE' 'status' => 'INACTIVE'
))); )));
return $this->getFeedId(); return $this->getFeedId();
} }
public function setRefFeedId($sRefFeedId) { public function setRefFeedId($sRefFeedId) {
return $this->updateField('ref_feed_id', $sRefFeedId); return $this->updateField('ref_feed_id', $sRefFeedId);
} }
public function setSpotId($iSpotId) { public function setSpotId($iSpotId) {
return $this->updateField(Db::getId(self::SPOT_TABLE), $iSpotId); return $this->updateField(Db::getId(self::SPOT_TABLE), $iSpotId);
} }
public function setProjectId($iProjectId) { public function setProjectId($iProjectId) {
return $this->updateField(Db::getId(Project::PROJ_TABLE), $iProjectId); return $this->updateField(Db::getId(Project::PROJ_TABLE), $iProjectId);
} }
public function getSpots() { public function getSpots() {
$asSpots = $this->oDb->selectRows(array('from'=>self::SPOT_TABLE)); $asSpots = $this->oDb->selectRows(array('from'=>self::SPOT_TABLE));
foreach($asSpots as &$asSpot) $asSpot['id'] = $asSpot[Db::getId(self::SPOT_TABLE)]; foreach($asSpots as &$asSpot) $asSpot['id'] = $asSpot[Db::getId(self::SPOT_TABLE)];
return $asSpots; return $asSpots;
} }
public function getFeeds($iFeedId=0) { public function getFeeds($iFeedId=0) {
$asInfo = array('from'=>self::FEED_TABLE); $asInfo = array('from'=>self::FEED_TABLE);
if($iFeedId > 0) $asInfo['constraint'] = array(Db::getId(self::FEED_TABLE)=>$iFeedId); if($iFeedId > 0) $asInfo['constraint'] = array(Db::getId(self::FEED_TABLE)=>$iFeedId);
$asFeeds = $this->oDb->selectRows($asInfo); $asFeeds = $this->oDb->selectRows($asInfo);
foreach($asFeeds as &$asFeed) $asFeed['id'] = $asFeed[Db::getId(self::FEED_TABLE)]; foreach($asFeeds as &$asFeed) $asFeed['id'] = $asFeed[Db::getId(self::FEED_TABLE)];
return $asFeeds; return $asFeeds;
} }
public function getFeed() { public function getFeed() {
$asFeeds = $this->getFeeds($this->getFeedId()); $asFeeds = $this->getFeeds($this->getFeedId());
return array_shift($asFeeds); return array_shift($asFeeds);
} }
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', 'timezone', 'unix_time'), 'select' => array('id_message', 'ref_msg_id', 'type', 'latitude', 'longitude', 'site_time', 'timezone', 'unix_time'),
'from' => self::MSG_TABLE, 'from' => self::MSG_TABLE,
'constraint'=> array(Db::getId(self::FEED_TABLE) => $this->getFeedId()), 'constraint'=> array(Db::getId(self::FEED_TABLE) => $this->getFeedId()),
'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) {
$bNewMsg = false; $bNewMsg = false;
//Spam Check: no more than 1 API request per 5 minutes //Spam Check: no more than 1 API request per 5 minutes
if($sProjectMode == Project::MODE_BLOG) { if($sProjectMode == Project::MODE_BLOG) {
$oLastUpdate = new \DateTime('@'.$this->iLastUpdate); $oLastUpdate = new \DateTime('@'.$this->iLastUpdate);
$oNow = new \DateTime('now'); $oNow = new \DateTime('now');
$iSecDiff = $oNow->getTimestamp() - $oLastUpdate->getTimestamp(); $iSecDiff = $oNow->getTimestamp() - $oLastUpdate->getTimestamp();
if($iSecDiff > self::FEED_MAX_REFRESH && !Settings::DEBUG) $bNewMsg = $this->updateFeed(); if($iSecDiff > self::FEED_MAX_REFRESH && !Settings::DEBUG) $bNewMsg = $this->updateFeed();
} }
return $bNewMsg; return $bNewMsg;
} }
private function updateFeed() { private function updateFeed() {
$bNewMsg = false; $bNewMsg = false;
$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']; $asMsgs = $asData['response']['feedMessageResponse']['messages'];
$asFeed = $asData['response']['feedMessageResponse']['feed']; $asFeed = $asData['response']['feedMessageResponse']['feed'];
//Fix unstable Spot API Structure //Fix unstable Spot API Structure
if(array_key_exists('message', $asMsgs)) $asMsgs = $asMsgs['message']; //Sometimes adds an extra "message" level 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 if(!array_key_exists(0, $asMsgs)) $asMsgs = array($asMsgs); //Jumps a level when there is only 1 message
//Update Spot, Feed & Messages //Update Spot, Feed & Messages
if(!empty($asMsgs) && array_key_exists('messengerId', $asMsgs[0])) { if(!empty($asMsgs) && array_key_exists('messengerId', $asMsgs[0])) {
//Update Spot Info from the first message //Update Spot Info from the first message
$asSpotInfo = array( $asSpotInfo = array(
'ref_spot_id' => $asMsgs[0]['messengerId'], 'ref_spot_id' => $asMsgs[0]['messengerId'],
'name' => $asMsgs[0]['messengerName'], 'name' => $asMsgs[0]['messengerName'],
'model' => $asMsgs[0]['modelId'] 'model' => $asMsgs[0]['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 'site_time' => date(Db::TIMESTAMP_FORMAT, $asMsg['unixTime']), //Conversion to Site Time
'timezone' => Spot::getTimeZoneFromDate($asMsg['dateTime']), //Local Time Zone 'timezone' => Spot::getTimeZoneFromDate($asMsg['dateTime']), //Local Time Zone
'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']
); );
$iMsgId = $this->oDb->selectId(self::MSG_TABLE, array('ref_msg_id'=>$asMsg['ref_msg_id'])); $iMsgId = $this->oDb->selectId(self::MSG_TABLE, array('ref_msg_id'=>$asMsg['ref_msg_id']));
if(!$iMsgId) { if(!$iMsgId) {
$this->oDb->insertRow(self::MSG_TABLE, $asMsg); $this->oDb->insertRow(self::MSG_TABLE, $asMsg);
$bNewMsg = true; $bNewMsg = true;
} }
else $this->oDb->updateRow(self::MSG_TABLE, $iMsgId, $asMsg); else $this->oDb->updateRow(self::MSG_TABLE, $iMsgId, $asMsg);
} }
} }
} }
else $this->oDb->updateRow(self::FEED_TABLE, $this->getFeedId(), array('last_update'=>$sLastUpdate)); else $this->oDb->updateRow(self::FEED_TABLE, $this->getFeedId(), array('last_update'=>$sLastUpdate));
return $bNewMsg; return $bNewMsg;
} }
private function retrieveFeed() { private function retrieveFeed() {
$sContent = '[]'; $sContent = '[]';
if($this->sRefFeedId !='') { if($this->sRefFeedId !='') {
$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);
} }
private function updateField($sField, $oValue) { private function updateField($sField, $oValue) {
$bResult = ($this->oDb->updateRow(self::FEED_TABLE, $this->getFeedId(), array($sField=>$oValue)) > 0); $bResult = ($this->oDb->updateRow(self::FEED_TABLE, $this->getFeedId(), array($sField=>$oValue)) > 0);
$this->setFeedId($this->getFeedId()); $this->setFeedId($this->getFeedId());
return $bResult; return $bResult;
} }
public function delete() { public function delete() {
$asResult = array(); $asResult = array();
if($this->getFeedId() > 0) { if($this->getFeedId() > 0) {
$asResult = array( $asResult = array(
'id' => $this->getFeedId(), 'id' => $this->getFeedId(),
'del' => $this->oDb->deleteRow(self::FEED_TABLE, $this->getFeedId()), 'del' => $this->oDb->deleteRow(self::FEED_TABLE, $this->getFeedId()),
'desc' => $this->oDb->getLastError() 'desc' => $this->oDb->getLastError()
); );
} }
else $asResult = array('del'=>false, 'desc'=>'Error while setting project: no Feed ID'); else $asResult = array('del'=>false, 'desc'=>'Error while setting project: no Feed ID');
return $asResult; return $asResult;
} }
} }