Added cron job and button under debug for update TLE

This commit is contained in:
Andreas Kristiansen
2024-12-07 20:04:28 +01:00
parent 9db7610f45
commit 899eb67a1f
6 changed files with 101 additions and 55 deletions

View File

@@ -22,7 +22,7 @@ $config['migration_enabled'] = TRUE;
|
*/
$config['migration_version'] = 229;
$config['migration_version'] = 230;
/*
|--------------------------------------------------------------------------

View File

@@ -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");
}

View File

@@ -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 <br />";
echo "Records inserted: " . $count . " <br/>";
$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();
}

View File

@@ -0,0 +1,33 @@
<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Migration_tle_cron extends CI_Migration {
public function up() {
if ($this->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;
}
}

View File

@@ -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 <br />";
echo "Records inserted: " . $count . " <br/>";
}
}

View File

@@ -549,6 +549,11 @@
<td><?= __("WWFF file download"); ?></td>
<td><?php echo $wwff_update->last_run ?? __("never"); ?></td>
<td><a class="btn btn-sm btn-primary" href="<?php echo site_url('update/update_wwff'); ?>"><?= __("Update"); ?></a></td>
</tr>
<tr>
<td><?= __("TLE update"); ?></td>
<td><?php echo $tle_update->last_run ?? __("never"); ?></td>
<td><a class="btn btn-sm btn-primary" href="<?php echo site_url('update/update_tle'); ?>"><?= __("Update"); ?></a></td>
</tr>
</table>
</div>