From 899eb67a1f0216c19fdb1cc7d1aadf78791aafc7 Mon Sep 17 00:00:00 2001
From: Andreas Kristiansen <6977712+AndreasK79@users.noreply.github.com>
Date: Sat, 7 Dec 2024 20:04:28 +0100
Subject: [PATCH] Added cron job and button under debug for update TLE
---
application/config/migration.php | 2 +-
application/controllers/Debug.php | 3 +-
application/controllers/Update.php | 57 ++-----------------------
application/migrations/230_tle_cron.php | 33 ++++++++++++++
application/models/Update_model.php | 56 ++++++++++++++++++++++++
application/views/debug/index.php | 5 +++
6 files changed, 101 insertions(+), 55 deletions(-)
create mode 100644 application/migrations/230_tle_cron.php
diff --git a/application/config/migration.php b/application/config/migration.php
index f39a5837f..0e1107dd7 100644
--- a/application/config/migration.php
+++ b/application/config/migration.php
@@ -22,7 +22,7 @@ $config['migration_enabled'] = TRUE;
|
*/
-$config['migration_version'] = 229;
+$config['migration_version'] = 230;
/*
|--------------------------------------------------------------------------
diff --git a/application/controllers/Debug.php b/application/controllers/Debug.php
index 427a8f0d5..57ec55a31 100644
--- a/application/controllers/Debug.php
+++ b/application/controllers/Debug.php
@@ -102,6 +102,7 @@ class Debug extends CI_Controller
$data['scp_update'] = $this->cron_model->cron('update_update_clublog_scp')->row();
$data['sota_update'] = $this->cron_model->cron('update_update_sota')->row();
$data['wwff_update'] = $this->cron_model->cron('update_update_wwff')->row();
+ $data['tle_update'] = $this->cron_model->cron('update_update_tle')->row();
$data['page_title'] = __("Debug");
@@ -224,7 +225,7 @@ class Debug extends CI_Controller
// Show success message
$this->session->set_flashdata('success', __("Wavelog was updated successfully!"));
-
+
} catch (\Throwable $th) {
log_message("Error","Error at selfupdating");
}
diff --git a/application/controllers/Update.php b/application/controllers/Update.php
index b3b3ef253..733f9a17e 100644
--- a/application/controllers/Update.php
+++ b/application/controllers/Update.php
@@ -390,65 +390,16 @@ class Update extends CI_Controller {
}
public function update_tle() {
- $mtime = microtime();
- $mtime = explode(" ",$mtime);
- $mtime = $mtime[1] + $mtime[0];
- $starttime = $mtime;
-
- $url = 'https://www.amsat.org/tle/dailytle.txt';
- $curl = curl_init($url);
-
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
-
- $response = curl_exec($curl);
-
- $count = 0;
-
- if ($response === false) {
- echo 'Error: ' . curl_error($curl);
- } else {
- $this->db->empty_table("tle");
- // Split the response into an array of lines
- $lines = explode("\n", $response);
-
- $satname = '';
- $tleline1 = '';
- $tleline2 = '';
- // Process each line
- for ($i = 0; $i < count($lines); $i += 3) {
- $count++;
- // Check if there are at least three lines remaining
- if (isset($lines[$i], $lines[$i + 1], $lines[$i + 2])) {
- // Get the three lines
- $satname = $lines[$i];
- $tleline1 = $lines[$i + 1];
- $tleline2 = $lines[$i + 2];
- $sql = "INSERT INTO tle (satelliteid, tle) select id, ? from satellite where name = ? or displayname = ?";
- $this->db->query($sql,array($tleline1."\n".$tleline2,$satname,$satname));
- }
- }
- }
-
- curl_close($curl);
-
- $mtime = microtime();
- $mtime = explode(" ",$mtime);
- $mtime = $mtime[1] + $mtime[0];
- $endtime = $mtime;
- $totaltime = ($endtime - $starttime);
- echo "This page was created in ".$totaltime." seconds
";
- echo "Records inserted: " . $count . "
";
- $datetime = new DateTime("now", new DateTimeZone('UTC'));
- $datetime = $datetime->format('Ymd h:i');
- $this->optionslib->update('tle_update', $datetime , 'no');
+ $this->load->model('Update_model');
+ $result = $this->Update_model->tle();
+ echo $result;
}
function version_check() {
// set the last run in cron table for the correct cron id
$this->load->model('cron_model');
$this->cron_model->set_last_run($this->router->class . '_' . $this->router->method);
-
+
$this->load->model('Update_model');
$this->Update_model->update_check();
}
diff --git a/application/migrations/230_tle_cron.php b/application/migrations/230_tle_cron.php
new file mode 100644
index 000000000..e41dfa121
--- /dev/null
+++ b/application/migrations/230_tle_cron.php
@@ -0,0 +1,33 @@
+chk4cron('update_update_tle') == 0) {
+ $data = array(
+ array(
+ 'id' => 'update_update_tle',
+ 'enabled' => '0',
+ 'status' => 'disabled',
+ 'description' => 'Update TLE for satellites',
+ 'function' => 'index.php/update/update_tle',
+ 'expression' => '45 4 * * *',
+ 'last_run' => null,
+ 'next_run' => null
+ ));
+ $this->db->insert_batch('cron', $data);
+ }
+ }
+
+ public function down() {
+ if ($this->chk4cron('update_tle') > 0) {
+ $this->db->query("delete from cron where id='update_update_tle'");
+ }
+ }
+
+ function chk4cron($cronkey) {
+ $query = $this->db->query("select count(id) as cid from cron where id=?",$cronkey);
+ $row = $query->row();
+ return $row->cid ?? 0;
+ }
+}
diff --git a/application/models/Update_model.php b/application/models/Update_model.php
index 7b432932b..9763ff65c 100644
--- a/application/models/Update_model.php
+++ b/application/models/Update_model.php
@@ -306,4 +306,60 @@ class Update_model extends CI_Model {
}
}
}
+
+ function tle() {
+ // set the last run in cron table for the correct cron id
+ $this->load->model('cron_model');
+ $this->cron_model->set_last_run($this->router->class . '_' . $this->router->method);
+ $mtime = microtime();
+ $mtime = explode(" ",$mtime);
+ $mtime = $mtime[1] + $mtime[0];
+ $starttime = $mtime;
+
+ $url = 'https://www.amsat.org/tle/dailytle.txt';
+ $curl = curl_init($url);
+
+ curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
+ curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
+
+ $response = curl_exec($curl);
+
+ $count = 0;
+
+ if ($response === false) {
+ echo 'Error: ' . curl_error($curl);
+ } else {
+ $this->db->empty_table("tle");
+ // Split the response into an array of lines
+ $lines = explode("\n", $response);
+
+ $satname = '';
+ $tleline1 = '';
+ $tleline2 = '';
+ // Process each line
+ for ($i = 0; $i < count($lines); $i += 3) {
+ $count++;
+ // Check if there are at least three lines remaining
+ if (isset($lines[$i], $lines[$i + 1], $lines[$i + 2])) {
+ // Get the three lines
+ $satname = $lines[$i];
+ $tleline1 = $lines[$i + 1];
+ $tleline2 = $lines[$i + 2];
+ $sql = "INSERT INTO tle (satelliteid, tle) select id, ? from satellite where name = ? or displayname = ?";
+ $this->db->query($sql,array($tleline1."\n".$tleline2,$satname,$satname));
+ }
+ }
+ }
+
+ curl_close($curl);
+
+ $mtime = microtime();
+ $mtime = explode(" ",$mtime);
+ $mtime = $mtime[1] + $mtime[0];
+ $endtime = $mtime;
+ $totaltime = ($endtime - $starttime);
+ echo "This page was created in ".$totaltime." seconds
";
+ echo "Records inserted: " . $count . "
";
+ }
+
}
diff --git a/application/views/debug/index.php b/application/views/debug/index.php
index 6a318b590..3f3d17018 100644
--- a/application/views/debug/index.php
+++ b/application/views/debug/index.php
@@ -549,6 +549,11 @@