feed class
This commit is contained in:
113
inc/feed.php
Normal file
113
inc/feed.php
Normal file
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
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';
|
||||
|
||||
//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) {
|
||||
parent::__construct(__CLASS__, Settings::DEBUG);
|
||||
$this->oDb = &$oDb;
|
||||
$this->setFeedId($iFeedId);
|
||||
}
|
||||
|
||||
public function setFeedId($iFeedId) {
|
||||
$this->iFeedId = $iFeedId;
|
||||
$asFeed = $this->oDb->selectRow(self::FEED_TABLE, $this->iFeedId);
|
||||
$this->sRefFeedId = $asFeed['ref_feed_id'];
|
||||
$this->iLastUpdate = strtotime($asFeed['last_update']);
|
||||
}
|
||||
|
||||
public function getMessages($asActivePeriod = array()) {
|
||||
$asInfo = array(
|
||||
'select' => array('id_message', 'ref_msg_id', 'type', 'latitude', 'longitude', 'site_time', 'unix_time'),
|
||||
'from' => self::MSG_TABLE,
|
||||
'constraint'=> array(Db::getId(self::FEED_TABLE) => $this->iFeedId),
|
||||
'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";
|
||||
}
|
||||
|
||||
return $this->oDb->selectRows($asInfo);
|
||||
}
|
||||
|
||||
public function checkUpdateFeed($sProjectMode) {
|
||||
//Feed updated once a day in Blog Mode
|
||||
if($sProjectMode == Project::MODE_BLOG && date('Y-m-d', $this->iLastUpdate) != date('Y-m-d')) $this->updateFeed();
|
||||
}
|
||||
|
||||
private function updateFeed() {
|
||||
$asData = $this->retrieveFeed();
|
||||
$asMsgs = $asData['response']['feedMessageResponse']['messages'];
|
||||
$asFeed = $asData['response']['feedMessageResponse']['feed'];
|
||||
|
||||
if(!empty($asMsgs))
|
||||
{
|
||||
//Update Spot Info
|
||||
$asFirstMsg = array_values($asMsgs)[0];
|
||||
$asSpotInfo = array(
|
||||
'ref_spot_id' => $asFirstMsg['messengerId'],
|
||||
'name' => $asFirstMsg['messengerName'],
|
||||
'model' => $asFirstMsg['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'],
|
||||
Db::getId(self::SPOT_TABLE) => $iSpotId,
|
||||
'name' => $asFeed['name'],
|
||||
'description' => $asFeed['description'],
|
||||
'status' => $asFeed['status'],
|
||||
'last_update' => date(Db::TIMESTAMP_FORMAT)
|
||||
);
|
||||
$iFeedId = $this->oDb->insertUpdateRow(self::FEED_TABLE, $asFeedInfo, array('ref_feed_id'));
|
||||
|
||||
//Update Messages
|
||||
foreach($asMsgs as $asMsg)
|
||||
{
|
||||
$asMsg = array(
|
||||
'ref_msg_id' => $asMsg['id'],
|
||||
Db::getId(self::FEED_TABLE) => $iFeedId,
|
||||
'type' => $asMsg['messageType'],
|
||||
'latitude' => $asMsg['latitude'],
|
||||
'longitude' => $asMsg['longitude'],
|
||||
'iso_time' => $asMsg['dateTime'], //ISO 8601 time (backup)
|
||||
'site_time' => date(Db::TIMESTAMP_FORMAT, $asMsg['unixTime']), //Conversion to Site Time (see Settings::TIMEZONE)
|
||||
'unix_time' => $asMsg['unixTime'], //UNIX Time (backup)
|
||||
'content' => $asMsg['messageContent'],
|
||||
'battery_state' => $asMsg['batteryState']
|
||||
);
|
||||
$this->oDb->insertUpdateRow(self::MSG_TABLE, $asMsg, array('ref_msg_id'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function retrieveFeed() {
|
||||
$sUrl = self::FEED_HOOK.$this->sRefFeedId.self::FEED_TYPE_JSON;
|
||||
$sContent = file_get_contents($sUrl);
|
||||
return json_decode($sContent, true);
|
||||
}
|
||||
}
|
||||
@@ -113,9 +113,17 @@ class Project extends PhpObject {
|
||||
return $bSpecificProj?$asProject:$asProjects;
|
||||
}
|
||||
|
||||
public function getFeedIds() {
|
||||
return $this->oDb->selectColumn(
|
||||
Feed::FEED_TABLE,
|
||||
Db::getId(Feed::FEED_TABLE),
|
||||
array(Db::getId(self::PROJ_TABLE) => $this->getProjectId())
|
||||
);
|
||||
}
|
||||
|
||||
private function setProjectInfo() {
|
||||
if($this->iProjectId > 0) {
|
||||
$asProject = $this->getProjects($this->iProjectId);
|
||||
if($this->getProjectId() > 0) {
|
||||
$asProject = $this->getProjects($this->getProjectId());
|
||||
|
||||
$this->sMode = $asProject['mode'];
|
||||
$this->asActive = array('from'=>$asProject['active_from'], 'to'=>$asProject['active_to']);
|
||||
|
||||
126
inc/spot.php
126
inc/spot.php
@@ -16,15 +16,7 @@
|
||||
|
||||
class Spot extends Main
|
||||
{
|
||||
//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';
|
||||
|
||||
//Database
|
||||
const MSG_TABLE = 'messages';
|
||||
const FEED_TABLE = 'feeds';
|
||||
const SPOT_TABLE = 'spots';
|
||||
const POST_TABLE = 'posts';
|
||||
|
||||
const FORMAT_TIME = 'd/m/Y à H:i';
|
||||
@@ -46,6 +38,7 @@ class Spot extends Main
|
||||
public function __construct($oClassManagement, $sProcessPage)
|
||||
{
|
||||
$asClasses = array(
|
||||
array('name'=>'feed', 'project'=>true),
|
||||
array('name'=>'project', 'project'=>true),
|
||||
array('name'=>'picture', 'project'=>true),
|
||||
array('name'=>'cacher', 'project'=>true),
|
||||
@@ -69,9 +62,9 @@ class Spot extends Main
|
||||
(
|
||||
'tables' => array
|
||||
(
|
||||
self::MSG_TABLE => array('ref_msg_id', Db::getId(self::FEED_TABLE), 'type', 'latitude', 'longitude', 'iso_time', 'site_time', 'unix_time', 'content', 'battery_state'),
|
||||
self::FEED_TABLE => array('ref_feed_id', Db::getId(self::SPOT_TABLE), Db::getId(Project::PROJ_TABLE), 'name', 'description', 'status', 'last_update'),
|
||||
self::SPOT_TABLE => array('ref_spot_id', 'name', 'model'),
|
||||
Feed::MSG_TABLE => array('ref_msg_id', Db::getId(Feed::FEED_TABLE), 'type', 'latitude', 'longitude', 'iso_time', 'site_time', 'unix_time', 'content', 'battery_state'),
|
||||
Feed::FEED_TABLE => array('ref_feed_id', Db::getId(Feed::SPOT_TABLE), Db::getId(Project::PROJ_TABLE), 'name', 'description', 'status', 'last_update'),
|
||||
Feed::SPOT_TABLE => array('ref_spot_id', 'name', 'model'),
|
||||
Project::PROJ_TABLE => array('name', 'codename', 'active_from', 'active_to', 'geofile', 'timezone'),
|
||||
self::POST_TABLE => array(Db::getId(Project::PROJ_TABLE), 'name', 'content', 'site_time'),
|
||||
Picture::PIC_TABLE => array(Db::getId(Project::PROJ_TABLE), 'filename', 'taken_on', 'posted_on', 'rotate')
|
||||
@@ -106,18 +99,18 @@ class Spot extends Main
|
||||
),
|
||||
'constraints' => array
|
||||
(
|
||||
self::MSG_TABLE => "UNIQUE KEY `uni_ref_msg_id` (`ref_msg_id`)",
|
||||
self::FEED_TABLE => "UNIQUE KEY `uni_ref_feed_id` (`ref_feed_id`)",
|
||||
self::SPOT_TABLE => "UNIQUE KEY `uni_ref_spot_id` (`ref_spot_id`)",
|
||||
self::MSG_TABLE => "INDEX(`ref_msg_id`)",
|
||||
self::FEED_TABLE => "INDEX(`ref_feed_id`)",
|
||||
self::SPOT_TABLE => "INDEX(`ref_spot_id`)",
|
||||
Feed::MSG_TABLE => "UNIQUE KEY `uni_ref_msg_id` (`ref_msg_id`)",
|
||||
Feed::FEED_TABLE => "UNIQUE KEY `uni_ref_feed_id` (`ref_feed_id`)",
|
||||
Feed::SPOT_TABLE => "UNIQUE KEY `uni_ref_spot_id` (`ref_spot_id`)",
|
||||
Feed::MSG_TABLE => "INDEX(`ref_msg_id`)",
|
||||
Feed::FEED_TABLE => "INDEX(`ref_feed_id`)",
|
||||
Feed::SPOT_TABLE => "INDEX(`ref_spot_id`)",
|
||||
Project::PROJ_TABLE => "UNIQUE KEY `uni_proj_name` (`codename`)",
|
||||
Picture::PIC_TABLE => "UNIQUE KEY `uni_file_name` (`filename`)"
|
||||
),
|
||||
'cascading_delete' => array
|
||||
(
|
||||
self::SPOT_TABLE=>array(self::MSG_TABLE)
|
||||
Feed::SPOT_TABLE=>array(Feed::MSG_TABLE)
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -156,64 +149,6 @@ class Spot extends Main
|
||||
$this->oProject->setProjectId($iProjectId);
|
||||
}
|
||||
|
||||
/* Getting & Storing messages */
|
||||
|
||||
private function getFeed($sRefFeedId)
|
||||
{
|
||||
$sUrl = self::FEED_HOOK.$sRefFeedId.self::FEED_TYPE_JSON;
|
||||
$sContent = file_get_contents($sUrl);
|
||||
return json_decode($sContent, true);
|
||||
}
|
||||
|
||||
private function updateFeed($sRefFeedId)
|
||||
{
|
||||
$asData = $this->getFeed($sRefFeedId);
|
||||
$asMsgs = $asData['response']['feedMessageResponse']['messages'];
|
||||
$asFeed = $asData['response']['feedMessageResponse']['feed'];
|
||||
|
||||
if(!empty($asMsgs))
|
||||
{
|
||||
//Update Spot Info
|
||||
$asFirstMsg = array_values($asMsgs)[0];
|
||||
$asSpotInfo = array(
|
||||
'ref_spot_id' => $asFirstMsg['messengerId'],
|
||||
'name' => $asFirstMsg['messengerName'],
|
||||
'model' => $asFirstMsg['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' => $sRefFeedId,
|
||||
Db::getId(self::SPOT_TABLE) => $iSpotId,
|
||||
Db::getId(Project::PROJ_TABLE) => $this->oProject->getProjectId(),
|
||||
'name' => $asFeed['name'],
|
||||
'description' => $asFeed['description'],
|
||||
'status' => $asFeed['status'],
|
||||
'last_update' => date(Db::TIMESTAMP_FORMAT)
|
||||
);
|
||||
$iFeedId = $this->oDb->insertUpdateRow(self::FEED_TABLE, $asFeedInfo, array('ref_feed_id'));
|
||||
|
||||
//Update Messages
|
||||
foreach($asMsgs as $asMsg)
|
||||
{
|
||||
$asMsg = array(
|
||||
'ref_msg_id' => $asMsg['id'],
|
||||
Db::getId(self::FEED_TABLE) => $iFeedId,
|
||||
'type' => $asMsg['messageType'],
|
||||
'latitude' => $asMsg['latitude'],
|
||||
'longitude' => $asMsg['longitude'],
|
||||
'iso_time' => $asMsg['dateTime'], //ISO 8601 time (backup)
|
||||
'site_time' => date(Db::TIMESTAMP_FORMAT, $asMsg['unixTime']), //Conversion to Site Time (see Settings::TIMEZONE)
|
||||
'unix_time' => $asMsg['unixTime'], //UNIX Time (backup)
|
||||
'content' => $asMsg['messageContent'],
|
||||
'battery_state' => $asMsg['batteryState']
|
||||
);
|
||||
$this->oDb->insertUpdateRow(self::MSG_TABLE, $asMsg, array('ref_msg_id'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function getMarkers()
|
||||
{
|
||||
$asMessages = $this->getSpotMessages();
|
||||
@@ -247,33 +182,13 @@ class Spot extends Main
|
||||
|
||||
private function getSpotMessages()
|
||||
{
|
||||
//Get Feed IDs
|
||||
$asFeeds = $this->oDb->selectRows(array(
|
||||
'select' => array(Db::getId(self::FEED_TABLE), 'ref_feed_id', 'last_update'),
|
||||
'from' => self::FEED_TABLE,
|
||||
'constraint'=> array(Db::getId(Project::PROJ_TABLE) => $this->oProject->getProjectId()))
|
||||
);
|
||||
//Get messages from all feeds belonging to the project
|
||||
$asFeeds = $this->oProject->getFeedIds();
|
||||
foreach($asFeeds as $iFeedId) {
|
||||
$oFeed = new Feed($this->oDb, $iFeedId);
|
||||
$oFeed->checkUpdateFeed($this->oProject->getMode());
|
||||
|
||||
//Update on Blog Mode and not updated already today
|
||||
foreach($asFeeds as $asFeed) {
|
||||
|
||||
//Feed updated once a day in Blog Mode
|
||||
if($this->oProject->getMode() == Project::MODE_BLOG && mb_substr($asFeed['last_update'], 0, 10) != date('Y-m-d')) {
|
||||
$this->updateFeed($asFeed['ref_feed_id']);
|
||||
}
|
||||
|
||||
$asInfo = array(
|
||||
'select' => array('id_message', 'ref_msg_id', 'type', 'latitude', 'longitude', 'site_time', 'unix_time'),
|
||||
'from' => self::MSG_TABLE,
|
||||
'constraint'=> array(
|
||||
Db::getId(Spot::FEED_TABLE) => $asFeed[Db::getId(self::FEED_TABLE)],
|
||||
'site_time' => $this->oProject->getActivePeriod()),
|
||||
'constOpe' => array(
|
||||
Db::getId(Spot::FEED_TABLE) => "=",
|
||||
'site_time' => "BETWEEN"),
|
||||
'orderBy' => array('site_time'=>'ASC'));
|
||||
|
||||
$asMessages = $this->oDb->selectRows($asInfo);
|
||||
$asMessages = $oFeed->getMessages($this->oProject->getActivePeriod());
|
||||
foreach($asMessages as $iIndex=>&$asMessage)
|
||||
{
|
||||
$asMessage['latitude'] = floatval($asMessage['latitude']);
|
||||
@@ -352,7 +267,7 @@ class Spot extends Main
|
||||
$asFeeds = array();
|
||||
$asFeedTypes = array(
|
||||
'message' => array(
|
||||
'table' => self::MSG_TABLE,
|
||||
'table' => Feed::MSG_TABLE,
|
||||
'feed' => $this->getSpotMessages(),
|
||||
'priority' => 0
|
||||
),
|
||||
@@ -458,11 +373,6 @@ class Spot extends Main
|
||||
}
|
||||
}
|
||||
|
||||
public function convertGpxToGeoJson() {
|
||||
$oConverter = new Converter();
|
||||
return $oConverter->convertToGeoJson($this->oProject->getGeoFile());
|
||||
}
|
||||
|
||||
public static function DecToDMS($dValue, $sType='lat') {
|
||||
if($sType=='lat') $sDirection = ($dValue >= 0)?'N':'S';
|
||||
else $sDirection = ($dValue >= 0)?'E':'W';
|
||||
|
||||
@@ -53,9 +53,6 @@ if($sAction!='')
|
||||
case 'sync_pics':
|
||||
$sResult = $oSpot->syncPics();
|
||||
break;
|
||||
case 'convert':
|
||||
$sResult = $oSpot->convertGpxToGeoJson();
|
||||
break;
|
||||
default:
|
||||
$sResult = Spot::getJsonResult(false, Spot::NOT_FOUND);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user