From 26b39c81dbc15ce84867a43ea131f76e814924b6 Mon Sep 17 00:00:00 2001 From: Franzz Date: Mon, 30 Aug 2021 21:31:30 +0200 Subject: [PATCH] keep original message date insert --- files/db/update_v14_to_v15.sql | 2 ++ inc/Feed.php | 27 ++++++++++++++++----------- 2 files changed, 18 insertions(+), 11 deletions(-) create mode 100644 files/db/update_v14_to_v15.sql diff --git a/files/db/update_v14_to_v15.sql b/files/db/update_v14_to_v15.sql new file mode 100644 index 0000000..6063c43 --- /dev/null +++ b/files/db/update_v14_to_v15.sql @@ -0,0 +1,2 @@ +ALTER TABLE messages ADD posted_on TIMESTAMP DEFAULT 0 AFTER battery_state; +UPDATE messages SET posted_on = led, led = led; \ No newline at end of file diff --git a/inc/Feed.php b/inc/Feed.php index 96a2c2c..f96ee91 100644 --- a/inc/Feed.php +++ b/inc/Feed.php @@ -93,23 +93,27 @@ class Feed extends PhpObject { return array_shift($asFeeds); } - public function getMessages($asActivePeriod = array()) { + public function getMessages($asConstraints=array()) { $asInfo = array( - 'select' => array('id_message', 'ref_msg_id', 'type', 'latitude', 'longitude', 'site_time', 'timezone', 'unix_time'), + 'select' => array(Db::getId(self::MSG_TABLE), 'ref_msg_id', 'type', 'latitude', 'longitude', 'site_time', 'timezone', 'unix_time'), 'from' => self::MSG_TABLE, - 'constraint'=> array(Db::getId(self::FEED_TABLE) => $this->getFeedId()), - 'constOpe' => array(Db::getId(self::FEED_TABLE) => "="), + 'join' => array(self::FEED_TABLE => Db::getId(self::FEED_TABLE)), + 'constraint'=> array(Db::getId(self::FEED_TABLE, true) => $this->getFeedId()), + 'constOpe' => array(Db::getId(self::FEED_TABLE, true) => "="), 'orderBy' => array('site_time'=>'ASC') ); - if(!empty($asActivePeriod)) { - $asInfo['constraint']['site_time'] = $asActivePeriod; - $asInfo['constOpe']['site_time'] = "BETWEEN"; - } + if(!empty($asConstraints)) $asInfo = array_merge($asInfo, $asConstraints); return $this->oDb->selectRows($asInfo); } + public function getLastMessageId($asConstraints=array()) { + + $asMessages = $this->getMessages($asConstraints); + return end($asMessages)[Db::getId(self::MSG_TABLE)]; + } + public function checkUpdateFeed($sProjectMode) { $bNewMsg = false; @@ -128,7 +132,7 @@ class Feed extends PhpObject { private function updateFeed() { $bNewMsg = false; $asData = $this->retrieveFeed(); - $sLastUpdate = date(Db::TIMESTAMP_FORMAT); + $sNow = date(Db::TIMESTAMP_FORMAT); if(!isset($asData['response']['errors']) || empty($asData['response']['errors'])) { $asMsgs = $asData['response']['feedMessageResponse']['messages']; $asFeed = $asData['response']['feedMessageResponse']['feed']; @@ -155,7 +159,7 @@ class Feed extends PhpObject { 'name' => $asFeed['name'], 'description' => $asFeed['description'], 'status' => $asFeed['status'], - 'last_update' => $sLastUpdate + 'last_update' => $sNow ); $iFeedId = $this->oDb->insertUpdateRow(self::FEED_TABLE, $asFeedInfo, array('ref_feed_id')); @@ -177,6 +181,7 @@ class Feed extends PhpObject { $iMsgId = $this->oDb->selectId(self::MSG_TABLE, array('ref_msg_id'=>$asMsg['ref_msg_id'])); if(!$iMsgId) { + $asMsg['posted_on'] = $sNow; $this->oDb->insertRow(self::MSG_TABLE, $asMsg); $bNewMsg = true; } @@ -184,7 +189,7 @@ class Feed extends PhpObject { } } } - 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'=>$sNow)); return $bNewMsg; }