Compare commits

...

3 Commits

Author SHA1 Message Date
a7b552fe4a Idiot-proof unstable spot api parsing (messages) 2019-09-01 00:17:21 +02:00
e2e5df5414 Update feeds once an hour (clean commit) 2019-09-01 00:17:21 +02:00
6a9c563df1 Revert "Update feeds once an hour"
This reverts commit abad39eaa8.
2019-09-01 00:17:11 +02:00

View File

@@ -1,123 +1,125 @@
<?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-H', $this->iLastUpdate) != date('Y-m-d-H')) $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'];
$asFeed = $asData['response']['feedMessageResponse']['feed']; if(array_key_exists('message', $asMsgs)) $asMsgs = $asMsgs['message'];
if(!empty($asMsgs)) $asFeed = $asData['response']['feedMessageResponse']['feed'];
{
//Update Spot Info from the first message if(!empty($asMsgs))
$asFirstMsg = array_values($asMsgs)[0]; {
$asSpotInfo = array( //Update Spot Info from the first message
'ref_spot_id' => $asFirstMsg['messengerId'], $asFirstMsg = array_values($asMsgs)[0];
'name' => $asFirstMsg['messengerName'], $asSpotInfo = array(
'model' => $asFirstMsg['modelId'] 'ref_spot_id' => $asFirstMsg['messengerId'],
); 'name' => $asFirstMsg['messengerName'],
$iSpotId = $this->oDb->insertUpdateRow(self::SPOT_TABLE, $asSpotInfo, array('ref_spot_id')); 'model' => $asFirstMsg['modelId']
);
//Update Feed Info and last update date $iSpotId = $this->oDb->insertUpdateRow(self::SPOT_TABLE, $asSpotInfo, array('ref_spot_id'));
$asFeedInfo = array(
'ref_feed_id' => $asFeed['id'], //Update Feed Info and last update date
Db::getId(self::SPOT_TABLE) => $iSpotId, $asFeedInfo = array(
'name' => $asFeed['name'], 'ref_feed_id' => $asFeed['id'],
'description' => $asFeed['description'], Db::getId(self::SPOT_TABLE) => $iSpotId,
'status' => $asFeed['status'], 'name' => $asFeed['name'],
'last_update' => $sLastUpdate 'description' => $asFeed['description'],
); 'status' => $asFeed['status'],
$iFeedId = $this->oDb->insertUpdateRow(self::FEED_TABLE, $asFeedInfo, array('ref_feed_id')); 'last_update' => $sLastUpdate
);
//Update Messages $iFeedId = $this->oDb->insertUpdateRow(self::FEED_TABLE, $asFeedInfo, array('ref_feed_id'));
foreach($asMsgs as $asMsg)
{ //Update Messages
$asMsg = array( foreach($asMsgs as $asMsg)
'ref_msg_id' => $asMsg['id'], {
Db::getId(self::FEED_TABLE) => $iFeedId, $asMsg = array(
'type' => $asMsg['messageType'], 'ref_msg_id' => $asMsg['id'],
'latitude' => $asMsg['latitude'], Db::getId(self::FEED_TABLE) => $iFeedId,
'longitude' => $asMsg['longitude'], 'type' => $asMsg['messageType'],
'iso_time' => $asMsg['dateTime'], //ISO 8601 time (backup) 'latitude' => $asMsg['latitude'],
'site_time' => date(Db::TIMESTAMP_FORMAT, $asMsg['unixTime']), //Conversion to Site Time (see Settings::TIMEZONE) 'longitude' => $asMsg['longitude'],
'unix_time' => $asMsg['unixTime'], //UNIX Time (backup) 'iso_time' => $asMsg['dateTime'], //ISO 8601 time (backup)
'content' => $asMsg['messageContent'], 'site_time' => date(Db::TIMESTAMP_FORMAT, $asMsg['unixTime']), //Conversion to Site Time (see Settings::TIMEZONE)
'battery_state' => $asMsg['batteryState'] 'unix_time' => $asMsg['unixTime'], //UNIX Time (backup)
); 'content' => $asMsg['messageContent'],
$this->oDb->insertUpdateRow(self::MSG_TABLE, $asMsg, array('ref_msg_id')); 'battery_state' => $asMsg['batteryState']
} );
} $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() { }
$sUrl = self::FEED_HOOK.$this->sRefFeedId.self::FEED_TYPE_JSON;
$sContent = file_get_contents($sUrl); private function retrieveFeed() {
return json_decode($sContent, true); $sUrl = self::FEED_HOOK.$this->sRefFeedId.self::FEED_TYPE_JSON;
} $sContent = file_get_contents($sUrl);
} return json_decode($sContent, true);
}
}