diff --git a/decoder/decoder.py b/decoder/decoder.py index 6253ec01..baa24695 100755 --- a/decoder/decoder.py +++ b/decoder/decoder.py @@ -5,7 +5,7 @@ import sys import argparse import telnetlib import time -import sqlite3 +import mysql.connector as mariadb import image import position @@ -18,64 +18,64 @@ parser.add_argument('-v', '--verbose', help='Activates more debug messages', act args = parser.parse_args() # Open SQLite database -sqlite = sqlite3.connect("decoder.sqlite") -sqlite.cursor().execute(""" - CREATE TABLE IF NOT EXISTS position +db = mariadb.connect(user='decoder', password='decoder', database='decoder') +db.cursor().execute(""" + CREATE TABLE IF NOT EXISTS `position` ( - call TEXT, - rxtime INTEGER, - org TEXT, + `call` VARCHAR(10), + `rxtime` INTEGER, + `org` VARCHAR(3), - reset INTEGER, - id INTEGER, - time INTEGER, + `reset` INTEGER, + `id` INTEGER, + `time` INTEGER, - adc_vsol INTEGER, - adc_vbat INTEGER, - pac_vsol INTEGER, - pac_vbat INTEGER, - pac_pbat INTEGER, - pac_psol INTEGER, + `adc_vsol` INTEGER, + `adc_vbat` INTEGER, + `pac_vsol` INTEGER, + `pac_vbat` INTEGER, + `pac_pbat` INTEGER, + `pac_psol` INTEGER, - light_intensity INTEGER, + `light_intensity` INTEGER, - gps_time INTEGER, - gps_lock INTEGER, - gps_sats INTEGER, - gps_ttff INTEGER, - gps_pdop INTEGER, - gps_alt INTEGER, - gps_lat INTEGER, - gps_lon INTEGER, + `gps_time` INTEGER, + `gps_lock` INTEGER, + `gps_sats` INTEGER, + `gps_ttff` INTEGER, + `gps_pdop` INTEGER, + `gps_alt` INTEGER, + `gps_lat` INTEGER, + `gps_lon` INTEGER, - sen_i1_press INTEGER, - sen_e1_press INTEGER, - sen_e2_press INTEGER, - sen_i1_temp INTEGER, - sen_e1_temp INTEGER, - sen_e2_temp INTEGER, - sen_i1_hum INTEGER, - sen_e1_hum INTEGER, - sen_e2_hum INTEGER, + `sen_i1_press` INTEGER, + `sen_e1_press` INTEGER, + `sen_e2_press` INTEGER, + `sen_i1_temp` INTEGER, + `sen_e1_temp` INTEGER, + `sen_e2_temp` INTEGER, + `sen_i1_hum` INTEGER, + `sen_e1_hum` INTEGER, + `sen_e2_hum` INTEGER, - stm32_temp INTEGER, - si4464_temp INTEGER, + `stm32_temp` INTEGER, + `si4464_temp` INTEGER, - sys_time INTEGER, - sys_error INTEGER, - PRIMARY KEY (call,reset,id,time) + `sys_time` INTEGER, + `sys_error` INTEGER, + PRIMARY KEY (`call`,`reset`,`id`,`time`) ) """) -sqlite.cursor().execute(""" - CREATE TABLE IF NOT EXISTS image +db.cursor().execute(""" + CREATE TABLE IF NOT EXISTS `image` ( - id INTEGER, - call TEXT, - rxtime INTEGER, - imageID INTEGER, - packetID INTEGER, - data TEXT, - PRIMARY KEY (call,id,packetID) + `id` INTEGER, + `call` VARCHAR(10), + `rxtime` INTEGER, + `imageID` INTEGER, + `packetID` INTEGER, + `data` VARCHAR(1024), + PRIMARY KEY (`call`,`id`,`packetID`) ) """) @@ -105,7 +105,7 @@ def received_data(data): if pos: # Position packet (with comment and telementry) comm = pos.group(4) - position.insert_position(sqlite, call, comm, 'pos') + position.insert_position(db, call, comm, 'pos') elif dat: # Data packet @@ -113,9 +113,9 @@ def received_data(data): data = dat.group(4) if typ is 'I': # Image packet - image.insert_image(sqlite, rxer, call, data) + image.insert_image(db, rxer, call, data) elif typ is 'L': # Log packet - position.insert_position(sqlite, call, data, 'log') + position.insert_position(db, call, data, 'log') if args.device == 'I': # Source APRS-IS diff --git a/decoder/html/Database.class.php b/decoder/html/Database.class.php index b0ad3924..c427cf89 100644 --- a/decoder/html/Database.class.php +++ b/decoder/html/Database.class.php @@ -1,46 +1,50 @@ open("/var/www/dl7ad/pecanpico10/decoder/decoder.sqlite"); - if($this->lastErrorCode()) - echo $this->lastErrorMsg(); + private function __construct() { + $this->con = parent::__construct("localhost", "decoder", "decoder", "decoder"); + if(mysqli_connect_errno()) + die(mysql_error()); } - function __destruct() { - parent::close(); + public function __destruct() { + $this->close(); } - static function getInstance() { + public static function getInstance() { if(self::$instance == null) self::$instance = new Database(); return self::$instance; } - function close() { + public function close() { + if(is_null($this->con)) + return; parent::close(); + $this->con = null; } - function getTracker() { + public function getTracker() { $tracker = array(); $query = $this->query(" - SELECT call,MAX(rxtime) + SELECT `call`,MAX(`rxtime`) FROM ( - SELECT call,rxtime FROM position + SELECT `call`,`rxtime` FROM `position` UNION ALL - SELECT call,rxtime FROM image - ) - GROUP BY call - ORDER BY rxtime DESC + SELECT `call`,`rxtime` FROM `image` + ) AS d + GROUP BY `call` + ORDER BY `rxtime` DESC "); - while($row = $query->fetchArray(SQLITE3_ASSOC)) + while($row = $query->fetch_assoc()) $tracker[] = new Tracker($row['call']); return $tracker; } } ?> + diff --git a/decoder/html/Telemetry.class.php b/decoder/html/Telemetry.class.php index 4c3eee38..fed7c273 100644 --- a/decoder/html/Telemetry.class.php +++ b/decoder/html/Telemetry.class.php @@ -1,56 +1,56 @@ reset = $sqlResult['reset']; - $this->id = $sqlResult['id']; + $this->reset = (int)$sqlResult['reset']; + $this->id = (int)$sqlResult['id']; $this->org = $sqlResult['org']; - $this->rxtime = $sqlResult['rxtime']; + $this->rxtime = (int)$sqlResult['rxtime']; $this->call = $sqlResult['call']; - $this->adc_vsol = $sqlResult['adc_vsol']; - $this->adc_vbat = $sqlResult['adc_vbat']; - $this->pac_vsol = $sqlResult['pac_vsol']; - $this->pac_vbat = $sqlResult['pac_vbat']; - $this->pac_pbat = $sqlResult['pac_pbat']; - $this->pac_psol = $sqlResult['pac_psol']; + $this->adc_vsol = (int)$sqlResult['adc_vsol']; + $this->adc_vbat = (int)$sqlResult['adc_vbat']; + $this->pac_vsol = (int)$sqlResult['pac_vsol']; + $this->pac_vbat = (int)$sqlResult['pac_vbat']; + $this->pac_pbat = (int)$sqlResult['pac_pbat']; + $this->pac_psol = (int)$sqlResult['pac_psol']; - $this->gps_time = $sqlResult['gps_time']; - $this->gps_lock = $sqlResult['gps_lock']; - $this->gps_sats = $sqlResult['gps_sats']; - $this->gps_ttff = $sqlResult['gps_ttff']; - $this->gps_pdop = $sqlResult['gps_pdop']; - $this->gps_alt = $sqlResult['gps_alt']; - $this->gps_lat = $sqlResult['gps_lat']; - $this->gps_lon = $sqlResult['gps_lon']; + $this->gps_time = (int)$sqlResult['gps_time']; + $this->gps_lock = (int)$sqlResult['gps_lock']; + $this->gps_sats = (int)$sqlResult['gps_sats']; + $this->gps_ttff = (int)$sqlResult['gps_ttff']; + $this->gps_pdop = (int)$sqlResult['gps_pdop']; + $this->gps_alt = (int)$sqlResult['gps_alt']; + $this->gps_lat = (int)$sqlResult['gps_lat']; + $this->gps_lon = (int)$sqlResult['gps_lon']; - $this->sen_i1_press = $sqlResult['sen_i1_press']; - $this->sen_e1_press = $sqlResult['sen_e1_press']; - $this->sen_e2_press = $sqlResult['sen_e2_press']; - $this->sen_i1_temp = $sqlResult['sen_i1_temp']; - $this->sen_e1_temp = $sqlResult['sen_e1_temp']; - $this->sen_e2_temp = $sqlResult['sen_e2_temp']; - $this->sen_i1_hum = $sqlResult['sen_i1_hum']; - $this->sen_e1_hum = $sqlResult['sen_e1_hum']; - $this->sen_e2_hum = $sqlResult['sen_e2_hum']; + $this->sen_i1_press = (int)$sqlResult['sen_i1_press']; + $this->sen_e1_press = (int)$sqlResult['sen_e1_press']; + $this->sen_e2_press = (int)$sqlResult['sen_e2_press']; + $this->sen_i1_temp = (int)$sqlResult['sen_i1_temp']; + $this->sen_e1_temp = (int)$sqlResult['sen_e1_temp']; + $this->sen_e2_temp = (int)$sqlResult['sen_e2_temp']; + $this->sen_i1_hum = (int)$sqlResult['sen_i1_hum']; + $this->sen_e1_hum = (int)$sqlResult['sen_e1_hum']; + $this->sen_e2_hum = (int)$sqlResult['sen_e2_hum']; - $this->stm32_temp = $sqlResult['stm32_temp']; - $this->si4464_temp = $sqlResult['si4464_temp']; + $this->stm32_temp = (int)$sqlResult['stm32_temp']; + $this->si4464_temp = (int)$sqlResult['si4464_temp']; - $this->light_intensity = $sqlResult['light_intensity']; + $this->light_intensity = (int)$sqlResult['light_intensity']; - $this->sys_time = $sqlResult['sys_time']; - $this->sys_error = $sqlResult['sys_error']; + $this->sys_time = (int)$sqlResult['sys_time']; + $this->sys_error = (int)$sqlResult['sys_error']; - $this->err_i2c1 = ($this->sys_error >> 0) & 0x1; - $this->err_i2c2 = ($this->sys_error >> 1) & 0x1; - $this->err_eva7m = ($this->sys_error >> 2) & 0x1; - $this->err_pac1720 = ($this->sys_error >> 3) & 0x3; - $this->err_ov5640 = ($this->sys_error >> 5) & 0x3; - $this->err_bme280_i1 = ($this->sys_error >> 8) & 0x1; - $this->err_bme280_e1 = ($this->sys_error >> 9) & 0x1; - $this->err_bme280_e2 = ($this->sys_error >> 10) & 0x1; + $this->err_i2c1 = ((int)$this->sys_error >> 0) & 0x1; + $this->err_i2c2 = ((int)$this->sys_error >> 1) & 0x1; + $this->err_eva7m = ((int)$this->sys_error >> 2) & 0x1; + $this->err_pac1720 = ((int)$this->sys_error >> 3) & 0x3; + $this->err_ov5640 = ((int)$this->sys_error >> 5) & 0x3; + $this->err_bme280_i1 = ((int)$this->sys_error >> 8) & 0x1; + $this->err_bme280_e1 = ((int)$this->sys_error >> 9) & 0x1; + $this->err_bme280_e2 = ((int)$this->sys_error >> 10) & 0x1; } } diff --git a/decoder/html/Tracker.class.php b/decoder/html/Tracker.class.php index 9007454e..0d4cb189 100644 --- a/decoder/html/Tracker.class.php +++ b/decoder/html/Tracker.class.php @@ -14,21 +14,19 @@ class Tracker { function getLastActivity() { $act = array(); - $stmt = Database::getInstance()->prepare(" + $query = Database::getInstance()->query(" SELECT * FROM ( - SELECT CAST(STRFTIME('%s', 'now') as DECIMAL) - rxtime as lasttime,'pos' as type FROM position WHERE call = :call AND org = 'pos' + SELECT UNIX_TIMESTAMP() - `rxtime` as `lasttime`,'pos' as `type` FROM `position` WHERE `call` = '" . Database::getInstance()->escape_string($this->call) . "' AND `org` = 'pos' UNION ALL - SELECT CAST(STRFTIME('%s', 'now') as DECIMAL) - rxtime as lasttime,'img' as type FROM image WHERE call = :call + SELECT UNIX_TIMESTAMP() - `rxtime` as `lasttime`,'img' as `type` FROM `image` WHERE `call` = '" . Database::getInstance()->escape_string($this->call) . "' UNION ALL - SELECT CAST(STRFTIME('%s', 'now') as DECIMAL) - rxtime as lasttime,'log' as type FROM position WHERE call = :call AND org = 'log' - ) - GROUP BY type - ORDER BY lasttime DESC + SELECT UNIX_TIMESTAMP() - `rxtime` as `lasttime`,'log' as `type` FROM `position` WHERE `call` = '" . Database::getInstance()->escape_string($this->call) . "' AND `org` = 'log' + ) AS d + GROUP BY `type` + ORDER BY `lasttime` DESC "); - $stmt->bindValue(':call', $this->call, SQLITE3_TEXT); - $query = $stmt->execute(); - while($row = $query->fetchArray(SQLITE3_ASSOC)) + while($row = $query->fetch_assoc()) $act[$row['type']] = $row['lasttime']; return $act; @@ -43,38 +41,31 @@ class Tracker { if($from - $to > 64281600) $from = $from + 64281600; // Max. 744 days (2 non leap years + 14 weeks) - $stmt = Database::getInstance()->prepare(" - SELECT t.id,call,MIN(rxtime) as time_first,MAX(rxtime) as time_last, - COUNT(*) as count,imageID,MAX(packetID) as packetID + $query = Database::getInstance()->query(" + SELECT t.`id`,`call`,MIN(`rxtime`) as `time_first`,MAX(`rxtime`) as `time_last`, + COUNT(*) as `count`,`imageID`,MAX(`packetID`) as `packetID` FROM ( - SELECT id - FROM image - WHERE :from <= rxtime - AND rxtime <= :to - AND call = :call - GROUP BY id - ORDER BY id ASC + SELECT `id` + FROM `image` + WHERE " . intval($from) . " <= `rxtime` + AND `rxtime` <= " . intval($to) . " + AND `call` = '" . Database::getInstance()->escape_string($this->call) . "' + GROUP BY `id` + ORDER BY `id` ASC ) as s - JOIN image t ON t.id = s.id - GROUP BY t.id + JOIN image t ON t.`id` = s.`id` + GROUP BY t.`id` "); - $stmt->bindValue(':call', $this->call, SQLITE3_TEXT); - $stmt->bindValue(':from', $from, SQLITE3_INTEGER); - $stmt->bindValue(':to', $to, SQLITE3_INTEGER); - $query = $stmt->execute(); $pics = array(); - while($row = $query->fetchArray(SQLITE3_ASSOC)) + while($row = $query->fetch_assoc()) $pics[] = new Image($row); return $pics; } function getLastTelemetry() { - $stmt = Database::getInstance()->prepare("SELECT * FROM position WHERE call = :call ORDER BY rxtime DESC LIMIT 1"); - $stmt->bindValue(':call', $this->call, SQLITE3_TEXT); - $query = $stmt->execute(); - - return new Telemetry($query->fetchArray(SQLITE3_ASSOC)); + $query = Database::getInstance()->query("SELECT * FROM `position` WHERE `call` = '" . Database::getInstance()->escape_string($this->call) . "' ORDER BY `rxtime` DESC LIMIT 1"); + return new Telemetry($query->fetch_assoc()); } function getTelemetry($from, $to=NULL) { if(is_null($to)) @@ -86,70 +77,65 @@ class Tracker { if($from - $to > 64281600) $from = $from + 64281600; // Max. 744 days (2 non leap years + 14 weeks) - $stmt = Database::getInstance()->prepare(" + $query = Database::getInstance()->query(" SELECT *, CASE - WHEN org = 'pos' THEN rxtime - WHEN org = 'log' THEN gps_time - END AS ordertime, - MAX(org) as org - FROM position + WHEN `org` = 'pos' THEN `rxtime` + WHEN `org` = 'log' THEN `gps_time` + END AS `ordertime`, + MAX(`org`) as `org` + FROM `position` WHERE (( - :from <= rxtime - AND rxtime <= :to - AND org = 'pos' + " . intval($from) . " <= `rxtime` + AND `rxtime` <= " . intval($to) . " + AND `org` = 'pos' ) OR ( - :from <= gps_time - AND gps_time <= :to - AND org = 'log' + " . intval($from) . " <= `gps_time` + AND `gps_time` <= " . intval($to) . " + AND `org` = 'log' )) - AND call = :call - GROUP BY reset,id - ORDER BY ordertime ASC + AND `call` = '" . Database::getInstance()->escape_string($this->call) . "' + GROUP BY `reset`,`id` + ORDER BY `ordertime` ASC "); - $stmt->bindValue(':call', $this->call, SQLITE3_TEXT); - $stmt->bindValue(':from', $from, SQLITE3_INTEGER); - $stmt->bindValue(':to', $to, SQLITE3_INTEGER); - $query = $stmt->execute(); $datasets = array(); - while($row = $query->fetchArray(SQLITE3_ASSOC)) { + while($row = $query->fetch_assoc()) { $datasets[] = new Telemetry($row); } return $datasets; } function getPacketCount() { - $stmt = Database::getInstance()->prepare("SELECT * + $query = Database::getInstance()->query("SELECT * FROM ( - SELECT COUNT(*) as cnt86400,'pos' as type FROM position WHERE call = :call AND org = 'pos' AND rxtime+86400 > CAST(STRFTIME('%s', 'now') as DECIMAL) + SELECT COUNT(*) as `cnt86400`,'pos' as `type` FROM `position` WHERE `call` = '" . Database::getInstance()->escape_string($this->call) . "' AND `org` = 'pos' AND `rxtime`+86400 > UNIX_TIMESTAMP() UNION ALL - SELECT COUNT(*) as cnt86400,'img' as type FROM image WHERE call = :call AND rxtime+86400 > CAST(STRFTIME('%s', 'now') as DECIMAL) + SELECT COUNT(*) as `cnt86400`,'img' as `type` FROM `image` WHERE `call` = '" . Database::getInstance()->escape_string($this->call) . "' AND `rxtime`+86400 > UNIX_TIMESTAMP() UNION ALL - SELECT COUNT(*) as cnt86400,'log' as type FROM position WHERE call = :call AND org = 'log' AND rxtime+86400 > CAST(STRFTIME('%s', 'now') as DECIMAL) + SELECT COUNT(*) as `cnt86400`,'log' as `type` FROM `position` WHERE `call` = '" . Database::getInstance()->escape_string($this->call) . "' AND `org` = 'log' AND `rxtime`+86400 > UNIX_TIMESTAMP() ) AS a JOIN ( - SELECT COUNT(*) as cnt3600,'pos' as type FROM position WHERE call = :call AND org = 'pos' AND rxtime+3600 > CAST(STRFTIME('%s', 'now') as DECIMAL) + SELECT COUNT(*) as `cnt3600`,'pos' as `type` FROM `position` WHERE `call` = '" . Database::getInstance()->escape_string($this->call) . "' AND `org` = 'pos' AND `rxtime`+3600 > UNIX_TIMESTAMP() UNION ALL - SELECT COUNT(*) as cnt3600,'img' as type FROM image WHERE call = :call AND rxtime+3600 > CAST(STRFTIME('%s', 'now') as DECIMAL) + SELECT COUNT(*) as `cnt3600`,'img' as `type` FROM `image` WHERE `call` = '" . Database::getInstance()->escape_string($this->call) . "' AND `rxtime`+3600 > UNIX_TIMESTAMP() UNION ALL - SELECT COUNT(*) as cnt3600,'log' as type FROM position WHERE call = :call AND org = 'log' AND rxtime+3600 > CAST(STRFTIME('%s', 'now') as DECIMAL) + SELECT COUNT(*) as `cnt3600`,'log' as `type` FROM `position` WHERE `call` = '" . Database::getInstance()->escape_string($this->call) . "' AND `org` = 'log' AND `rxtime`+3600 > UNIX_TIMESTAMP() ) AS b JOIN ( - SELECT COUNT(*) as cnt300,'pos' as type FROM position WHERE call = :call AND org = 'pos' AND rxtime+300 > CAST(STRFTIME('%s', 'now') as DECIMAL) + SELECT COUNT(*) as `cnt300`,'pos' as `type` FROM `position` WHERE `call` = '" . Database::getInstance()->escape_string($this->call) . "' AND `org` = 'pos' AND `rxtime`+300 > UNIX_TIMESTAMP() UNION ALL - SELECT COUNT(*) as cnt300,'img' as type FROM image WHERE call = :call AND rxtime+300 > CAST(STRFTIME('%s', 'now') as DECIMAL) + SELECT COUNT(*) as `cnt300`,'img' as `type` FROM `image` WHERE `call` = '" . Database::getInstance()->escape_string($this->call) . "' AND `rxtime`+300 > UNIX_TIMESTAMP() UNION ALL - SELECT COUNT(*) as cnt300,'log' as type FROM position WHERE call = :call AND org = 'log' AND rxtime+300 > CAST(STRFTIME('%s', 'now') as DECIMAL) + SELECT COUNT(*) as `cnt300`,'log' as `type` FROM `position` WHERE `call` = '" . Database::getInstance()->escape_string($this->call) . "' AND `org` = 'log' AND `rxtime`+300 > UNIX_TIMESTAMP() ) AS c - WHERE a.type = b.type - AND a.type = c.type + WHERE a.`type` = b.`type` + AND a.`type` = c.`type` "); - $stmt->bindValue(':call', $this->call, SQLITE3_TEXT); - $query = $stmt->execute(); + $ret = array(); - while($row = $query->fetchArray(SQLITE3_ASSOC)) + while($row = $query->fetch_assoc()) $ret[$row['type']] = $row; return $ret; diff --git a/decoder/html/telemetry.php b/decoder/html/telemetry.php index b887ce89..e98dcd9b 100644 --- a/decoder/html/telemetry.php +++ b/decoder/html/telemetry.php @@ -210,7 +210,7 @@ function loadRecentData() { var time = new Date(data['org'] == 'pos' ? data['rxtime']*1000 : data['gps_time']*1000); - if(last != null && time - last > 1210000) { + if(last != null && time - last > 1810000) { dataBattery.addRow([null, null, null, null]); dataSolar.addRow([null, null, null, null]); dataTemp.addRow([null,null,null,null,null,null]); @@ -428,16 +428,6 @@ include "sidebar.inc.php";