Merge pull request #1332 from AndreasK79/satpass_date

Satpass date
This commit is contained in:
Andreas Kristiansen
2024-12-09 09:34:44 +01:00
committed by GitHub
10 changed files with 164 additions and 63 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

@@ -21,6 +21,16 @@ class Satellite extends CI_Controller {
$pageData['satellites'] = $this->satellite_model->get_all_satellites();
if($this->session->userdata('user_date_format')) {
// If Logged in and session exists
$custom_date_format = $this->session->userdata('user_date_format');
} else {
// Get Default date format from /config/wavelog.php
$custom_date_format = $this->config->item('qso_date_format');
}
$pageData['custom_date_format'] = $custom_date_format;
$footerData = [];
$footerData['scripts'] = [
'assets/js/sections/satellite.js?' . filemtime(realpath(__DIR__ . "/../../assets/js/sections/satellite.js")),
@@ -285,7 +295,7 @@ class Satellite extends CI_Controller {
$tle = new Predict_TLE($sat_tle->satellite, $temp[0], $temp[1]); // Instantiate it
$sat = new Predict_Sat($tle); // Load up the satellite data
$now = Predict_Time::get_current_daynum(); // get the current time as Julian Date (daynum)
$now = $this->get_daynum_from_date($this->security->xss_clean($this->input->post('date'))); // get the current time as Julian Date (daynum)
// You can modify some preferences in Predict(), the defaults are below
//
@@ -316,4 +326,17 @@ class Satellite extends CI_Controller {
$data['format'] = $format;
$this->load->view('satellite/passtable', $data);
}
public static function get_daynum_from_date($date) {
// Convert a Y-m-d date to a day number
// Convert date to Unix timestamp
$timestamp = strtotime($date);
if ($timestamp === false) {
throw new Exception("Invalid date format. Expected Y-m-d.");
}
// Calculate the day number
return Predict_Time::unix2daynum($timestamp, 0);
}
}

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,50 @@
<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Migration_tle_cron extends CI_Migration {
public function up() {
if ($this->db->table_exists('tle')) {
$this->db->query("DROP TABLE tle");
}
$this->db->query("CREATE TABLE `tle` (`id` int(6) unsigned NOT NULL AUTO_INCREMENT, `satelliteid` int(6) unsigned NOT NULL,
`tle` text DEFAULT NULL, `updated` timestamp NOT NULL DEFAULT current_timestamp(),
PRIMARY KEY (`satelliteid`), UNIQUE KEY `tle_unique_id` (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4");
$this->dbtry("ALTER TABLE tle ADD CONSTRAINT tle_satellite_FK FOREIGN KEY (satelliteid) REFERENCES satellite(id) ON DELETE CASCADE");
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'");
}
// No way back to tle-table
}
function chk4cron($cronkey) {
$query = $this->db->query("select count(id) as cid from cron where id=?",$cronkey);
$row = $query->row();
return $row->cid ?? 0;
}
function dbtry($what) {
try {
$this->db->query($what);
} catch (Exception $e) {
log_message("error", "Something gone wrong while altering FKs: ".$e." // Executing: ".$this->db->last_query());
}
}
}

View File

@@ -3,10 +3,11 @@
class Satellite_model extends CI_Model {
function get_all_satellites() {
$sql = "select satellite.id, satellite.name as satname, group_concat(distinct satellitemode.name separator ', ') as modename, satellite.displayname, satellite.orbit, satellite.lotw as lotw
$sql = "select satellite.id, satellite.name as satname, group_concat(distinct satellitemode.name separator ', ') as modename, satellite.displayname, satellite.orbit, satellite.lotw as lotw, tle.updated
from satellite
left outer join satellitemode on satellite.id = satellitemode.satelliteid
group by satellite.name, satellite.displayname, satellite.orbit, satellite.id";
left outer join tle on satellite.id = tle.satelliteid
group by satellite.name, satellite.displayname, satellite.orbit, satellite.id, tle.updated";
return $this->db->query($sql)->result();
}

View File

@@ -306,4 +306,66 @@ 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 {
// 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 = ?
ON DUPLICATE KEY UPDATE
tle = VALUES(tle), updated = now()
";
$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>

View File

@@ -23,6 +23,7 @@
<th><?= __("Orbit"); ?></th>
<th><?= __("Mode"); ?></th>
<th><?= __("LoTW"); ?></th>
<th><?= __("TLE"); ?></th>
<th><?= __("Edit"); ?></th>
<th><?= __("Delete"); ?></th>
</tr>
@@ -62,6 +63,15 @@
echo '<span class="badge bg-warning">'.__("Unknown").'</span>';
break;
}
echo '</td>';
?>
<?php echo '<td style="text-align: center; vertical-align: middle;">';
if ($sat->updated != null) {
echo '<span class="badge bg-success" data-bs-toggle="tooltip" title="Last TLE updated was ' . date($custom_date_format . " H:i", strtotime($sat->updated)) . '">'.__("Yes").'</span>';
} else {
echo '<span class="badge bg-danger">'.__("No").'</span>';
}
echo '</td>';
?>
<td style="text-align: center; vertical-align: middle;"><button onclick="editSatelliteDialog(<?php echo $sat->id ?>)" class="btn btn-sm btn-success"><i class="fas fa-edit"></i></i></button></td>

View File

@@ -469,12 +469,10 @@
<option ">Etc/GMT+12</option>
</select>
</div>
<!-- <div class="mb-3 w-auto">
<div class="mb-3 w-auto">
<label class="my-1 me-sm-2 w-auto" for="date"><?= __("Date"); ?></label>
<select class="my-1 me-sm-2 w-auto form-select" id="date" name="start">
<option selected value="0"><?= __("Today"); ?></option>
</select>
</div> -->
<input name="date" id="date" type="date" class="form-control form-control-sm my-1 me-sm-2 w-auto" value="<?php echo date('Y-m-d'); ?>" >
</div>
<div class="mb-3 w-auto">
<label class="my-1 me-sm-2 w-auto" for="mintime"><?= __("Min. time"); ?></label>
<select class="my-1 me-sm-2 w-auto form-select" id="mintime" name="mintime">