oDb = &$oDb; if($iFeedId > 0) $this->setFeedId($iFeedId); } public function getFeedId() { return $this->iFeedId; } public function setFeedId($iFeedId) { $this->iFeedId = $iFeedId; $asFeed = $this->getFeed(); $this->sRefFeedId = $asFeed['ref_feed_id']; $this->iLastUpdate = strtotime($asFeed['last_update']); } public function createFeedId($oProjectId) { $this->setFeedId($this->oDb->insertRow(self::FEED_TABLE, array( Db::getId(Project::PROJ_TABLE) => $oProjectId, 'status' => 'INACTIVE' ))); return $this->getFeedId(); } public function setRefFeedId($sRefFeedId) { return $this->updateField('ref_feed_id', $sRefFeedId); } public function setSpotId($iSpotId) { return $this->updateField(Db::getId(self::SPOT_TABLE), $iSpotId); } public function setProjectId($iProjectId) { return $this->updateField(Db::getId(Project::PROJ_TABLE), $iProjectId); } public function getSpots() { $asSpots = $this->oDb->selectRows(array('from'=>self::SPOT_TABLE)); foreach($asSpots as &$asSpot) $asSpot['id'] = $asSpot[Db::getId(self::SPOT_TABLE)]; return $asSpots; } public function getFeeds($iFeedId=0) { $asInfo = array('from'=>self::FEED_TABLE); if($iFeedId > 0) $asInfo['constraint'] = array(Db::getId(self::FEED_TABLE)=>$iFeedId); $asFeeds = $this->oDb->selectRows($asInfo); foreach($asFeeds as &$asFeed) $asFeed['id'] = $asFeed[Db::getId(self::FEED_TABLE)]; return $asFeeds; } public function getFeed() { $asFeeds = $this->getFeeds($this->getFeedId()); return array_shift($asFeeds); } 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->getFeedId()), '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 every hour in Blog Mode if($sProjectMode == Project::MODE_BLOG && date('Y-m-d-H', $this->iLastUpdate) != date('Y-m-d-H')) $this->updateFeed(); } private function updateFeed() { $asData = $this->retrieveFeed(); $sLastUpdate = date(Db::TIMESTAMP_FORMAT); if(!isset($asData['response']['errors']) || empty($asData['response']['errors'])) { $asMsgs = $asData['response']['feedMessageResponse']['messages']; if(array_key_exists('message', $asMsgs)) $asMsgs = $asMsgs['message']; $asFeed = $asData['response']['feedMessageResponse']['feed']; if(!empty($asMsgs)) { //Update Spot Info from the first message $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' => $sLastUpdate ); $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')); } } } else $this->oDb->updateRow(self::FEED_TABLE, $this->getFeedId(), array('last_update'=>$sLastUpdate)); } private function retrieveFeed() { $sContent = '[]'; if($this->sRefFeedId !='') { $sUrl = self::FEED_HOOK.$this->sRefFeedId.self::FEED_TYPE_JSON; $sContent = file_get_contents($sUrl); } return json_decode($sContent, true); } private function updateField($sField, $oValue) { $bResult = ($this->oDb->updateRow(self::FEED_TABLE, $this->getFeedId(), array($sField=>$oValue)) > 0); $this->setFeedId($this->getFeedId()); return $bResult; } }