178 lines
6.2 KiB
PHP
Executable File
178 lines
6.2 KiB
PHP
Executable File
<?php
|
|
|
|
class Spot extends Main
|
|
{
|
|
//Spot feed
|
|
const FEED_ID = '0Y5LrvigElWeAieBGnFol0KBEuOTkFJmm';
|
|
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';
|
|
|
|
public function __construct($oClassManagement, $sProcessPage)
|
|
{
|
|
parent::__construct($oClassManagement, $sProcessPage);
|
|
}
|
|
|
|
protected function install()
|
|
{
|
|
//Install DB
|
|
$this->oMySql->install();
|
|
|
|
//Add feed
|
|
//$this->oMySql->insertRow(self::FEED_TABLE, array('ref_feed_id'=>self::FEED_ID));
|
|
}
|
|
|
|
protected function getSqlOptions()
|
|
{
|
|
return array
|
|
(
|
|
'tables' => array
|
|
(
|
|
self::MSG_TABLE => array('ref_msg_id', MySqlManager::getId(self::FEED_TABLE), 'type', 'latitude', 'longitude', 'timestamp', 'unix_timestamp', 'content', 'battery_state'),
|
|
self::FEED_TABLE => array('ref_feed_id', MySqlManager::getId(self::SPOT_TABLE), 'name', 'description', 'status'),
|
|
self::SPOT_TABLE => array('ref_spot_id', 'name', 'model')
|
|
),
|
|
'types' => array
|
|
(
|
|
'ref_msg_id' => "INT",
|
|
'type' => "VARCHAR(20)",
|
|
'latitude' => "DECIMAL(7,5)",
|
|
'longitude' => "DECIMAL(8,5)",
|
|
'timestamp' => "DATETIME",
|
|
'unix_timestamp'=> "INT",
|
|
'content' => "LONGTEXT",
|
|
'battery_state' => "VARCHAR(10)",
|
|
'ref_spot_id' => "VARCHAR(10)",
|
|
'name' => "VARCHAR(100)",
|
|
'model' => "VARCHAR(20)",
|
|
'ref_feed_id' => "VARCHAR(40)",
|
|
'description' => "VARCHAR(100)",
|
|
'status' => "VARCHAR(10)"
|
|
),
|
|
'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`)",
|
|
),
|
|
'cascading_delete' => array
|
|
(
|
|
self::SPOT_TABLE=>array(self::MSG_TABLE)
|
|
)
|
|
);
|
|
}
|
|
|
|
public function getMainPage()
|
|
{
|
|
return parent::getMainPage(array('vars'=>array('feed_id'=>self::FEED_ID)));
|
|
}
|
|
|
|
/* Getting & Storing messages */
|
|
|
|
private function getFeed($sRefFeedId=self::FEED_ID)
|
|
{
|
|
$sUrl = self::FEED_HOOK.$sRefFeedId.self::FEED_TYPE_JSON;
|
|
$sContent = file_get_contents($sUrl);
|
|
//$sContent = '{"response":{"feedMessageResponse":{"count":1,"feed":{"id":"0Y5LrvigElWeAieBGnFol0KBEuOTkFJmm","name":"Te Araroa","description":"Te Araroa","status":"ACTIVE","usage":0,"daysRange":7,"detailedMessageShown":false},"totalCount":1,"activityCount":0,"messages":{"message":{"@clientUnixTime":"0","id":477259564,"messengerId":"0-2489517","messengerName":"Francois","unixTime":1449002345,"messageType":"OK","latitude":48.85129,"longitude":2.40491,"modelId":"SPOT3","showCustomMsg":"N","dateTime":"2015-12-01T20:39:05+0000","messageDetail":"","batteryState":"GOOD","hidden":0,"messageContent":"Jusque là , tout va bien ! Click sur le lien en dessous pour voir où je suis :)\r\n\r\n@Clara: <3"}}}}}';
|
|
return json_decode($sContent, true);
|
|
}
|
|
|
|
private function updateFeed($sRefFeedId=self::FEED_ID)
|
|
{
|
|
$asData = $this->getFeed($sRefFeedId);
|
|
$asMsgs = $asData['response']['feedMessageResponse']['messages'];
|
|
$asFeed = $asData['response']['feedMessageResponse']['feed'];
|
|
|
|
$this->oMySql->setTrace(true);
|
|
|
|
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->oMySql->insertUpdateRow(self::SPOT_TABLE, $asSpotInfo, array('ref_spot_id'));
|
|
|
|
//Update Feed Info
|
|
$asFeedInfo = array('ref_feed_id' => $sRefFeedId,
|
|
MySqlManager::getId(self::SPOT_TABLE) => $iSpotId,
|
|
'name' => $asFeed['name'],
|
|
'description' => $asFeed['description'],
|
|
'status' => $asFeed['status'],
|
|
'led' => date(MySqlManager::MYSQL_TIMESTAMP)); //update with current time
|
|
$iFeedId = $this->oMySql->insertUpdateRow(self::FEED_TABLE, $asFeedInfo, array('ref_feed_id'));
|
|
|
|
//Update Messages
|
|
foreach($asMsgs as $asMsg)
|
|
{
|
|
$asMsg = array( 'ref_msg_id' => $asMsg['id'],
|
|
MySqlManager::getId(self::FEED_TABLE) => $iFeedId,
|
|
'type' => $asMsg['messageType'],
|
|
'latitude' => $asMsg['latitude'],
|
|
'longitude' => $asMsg['longitude'],
|
|
'timestamp' => date(MySqlManager::MYSQL_TIMESTAMP, strtotime($asMsg['dateTime'])), //Stored in Paris Time :s
|
|
'unix_timestamp' => $asMsg['unixTime'],
|
|
'content' => $asMsg['messageContent'],
|
|
'battery_state' => $asMsg['batteryState']);
|
|
$this->oMySql->insertUpdateRow(self::MSG_TABLE, $asMsg, array('ref_msg_id'));
|
|
}
|
|
}
|
|
}
|
|
|
|
public function getMessages($sRefFeedId=self::FEED_ID)
|
|
{
|
|
//Adding another point to test
|
|
/*
|
|
$test = $this->oMySql->selectRow(self::MSG_TABLE, 1);
|
|
unset($test['id_message']);
|
|
$test['ref_msg_id'] = $test['ref_msg_id'] + 1;
|
|
$test['latitude'] = '-41.4395';
|
|
$test['longitude'] = '172.1936';
|
|
$this->oMySql->insertUpdateRow(self::MSG_TABLE, $test, array('ref_msg_id'));
|
|
*/
|
|
|
|
$bSuccess = true;
|
|
$sDesc = '';
|
|
|
|
//Check last message & update feed if necessary (max once a day)
|
|
$sLastMsg = $this->oMySql->selectValue(self::FEED_TABLE, 'led', array('ref_feed_id'=>$sRefFeedId));
|
|
if(mb_substr($sLastMsg, 0, 10) != date('Y-m-d'))
|
|
{
|
|
$this->updateFeed($sRefFeedId);
|
|
}
|
|
|
|
$asMessages = $this->oMySql->selectRows(array('from'=>self::MSG_TABLE));
|
|
if(empty($asMessages))
|
|
{
|
|
$bSuccess = false;
|
|
$sDesc = self::NO_DATA;
|
|
}
|
|
else
|
|
{
|
|
foreach($asMessages as $iKey=>$asMessage)
|
|
{
|
|
$iUnixTimeStamp = strtotime($asMessage['timestamp']);
|
|
$asMessages[$iKey]['relative_time'] = Toolbox::getDateTimeDesc($iUnixTimeStamp);
|
|
$asMessages[$iKey]['formatted_time'] = date('d/m/Y à H:i', $iUnixTimeStamp);
|
|
}
|
|
}
|
|
|
|
return self::getJsonResult($bSuccess, $sDesc, $asMessages);
|
|
}
|
|
|
|
public function upload()
|
|
{
|
|
$this->oClassManagement->incClass('uploader', true);
|
|
$oUploader = new Uploader();
|
|
return $oUploader->getBody();
|
|
}
|
|
}
|
|
|
|
?>
|