From 25f620a486b7b3d8d5d090c9e6c4f57f1cfdad4f Mon Sep 17 00:00:00 2001 From: Peter Goodhall Date: Wed, 26 Aug 2020 21:04:17 +0100 Subject: [PATCH] Builds and uploads TQ8 File to LOTW, Marks QSOs as sent to LOTW if successful --- application/controllers/Lotw.php | 94 ++++++++++++++++++- application/models/Logbook_model.php | 19 +++- .../lotw_views/adif_views/adif_export.php | 13 +-- 3 files changed, 115 insertions(+), 11 deletions(-) diff --git a/application/controllers/Lotw.php b/application/controllers/Lotw.php index 65084e78..2a464193 100644 --- a/application/controllers/Lotw.php +++ b/application/controllers/Lotw.php @@ -150,7 +150,12 @@ class Lotw extends CI_Controller { $station_profiles = $this->Stations->all(); + // Array of QSO IDs being Uploaded + + $qso_id_array = array(); + if ($station_profiles->num_rows() >= 1) { + foreach ($station_profiles->result() as $station_profile) { @@ -168,7 +173,94 @@ class Lotw extends CI_Controller { $data['qsos'] = $this->Logbook_model->get_lotw_qsos_to_upload($data['station_profile']->station_id); - $this->load->view('lotw_views/adif_views/adif_export', $data); + foreach ($data['qsos']->result() as $temp_qso) { + array_push($qso_id_array, $temp_qso->COL_PRIMARY_KEY); + } + + //$this->load->view('lotw_views/adif_views/adif_export', $data); + + + // Build File to save + $adif_to_save = $this->load->view('lotw_views/adif_views/adif_export', $data, TRUE); + + // Build Filename + + $filename_for_saving = $data['lotw_cert_info']->callsign."-".date("Y-m-d-H-i-s")."-cloudlog.tq8"; + + $gzdata = gzencode($adif_to_save, 9); + $fp = fopen($filename_for_saving, "w"); + fwrite($fp, $gzdata); + fclose($fp); + + //The URL that accepts the file upload. + $url = 'https://lotw.arrl.org/lotw/upload'; + + //The name of the field for the uploaded file. + $uploadFieldName = 'upfile'; + + //The full path to the file that you want to upload + $filePath = realpath($filename_for_saving); + + //Initiate cURL + $ch = curl_init(); + + //Set the URL + curl_setopt($ch, CURLOPT_URL, $url); + + //Set the HTTP request to POST + curl_setopt($ch, CURLOPT_POST, true); + + //Tell cURL to return the output as a string. + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + + //If the function curl_file_create exists + if(function_exists('curl_file_create')){ + //Use the recommended way, creating a CURLFile object. + $filePath = curl_file_create($filePath); + } else{ + //Otherwise, do it the old way. + //Get the canonicalized pathname of our file and prepend + //the @ character. + $filePath = '@' . realpath($filePath); + //Turn off SAFE UPLOAD so that it accepts files + //starting with an @ + curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false); + } + + //Setup our POST fields + $postFields = array( + $uploadFieldName => $filePath + ); + + curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); + + //Execute the request + $result = curl_exec($ch); + + //If an error occured, throw an exception + //with the error message. + if(curl_errno($ch)){ + throw new Exception(curl_error($ch)); + } + + $pos = strpos($result, ""); + + if ($pos === false) { + // Upload of TQ8 Failed for unknown reason + echo "Upload Failed"; + } else { + // Upload of TQ8 was successfull + + echo "Upload Successful - ".$filename_for_saving; + + // Mark QSOs as Sent + foreach ($qso_id_array as $qso_number) { + $this->Logbook_model->mark_lotw_sent($qso_number); + } + } + + // Delete TQ8 File - This is done regardless of whether upload was succcessful + unlink(realpath($filename_for_saving)); } } else { echo "No Station Profiles"; diff --git a/application/models/Logbook_model.php b/application/models/Logbook_model.php index 0825c732..2721f827 100755 --- a/application/models/Logbook_model.php +++ b/application/models/Logbook_model.php @@ -2160,10 +2160,11 @@ class Logbook_model extends CI_Model { function get_lotw_qsos_to_upload($station_id) { - $this->db->select('COL_CALL, COL_BAND, COL_BAND_RX, COL_TIME_ON, COL_RST_RCVD, COL_RST_SENT, COL_MODE, COL_FREQ, COL_FREQ_RX, COL_GRIDSQUARE, COL_SAT_NAME, COL_PROP_MODE'); + $this->db->select('COL_PRIMARY_KEY,COL_CALL, COL_BAND, COL_BAND_RX, COL_TIME_ON, COL_RST_RCVD, COL_RST_SENT, COL_MODE, COL_FREQ, COL_FREQ_RX, COL_GRIDSQUARE, COL_SAT_NAME, COL_PROP_MODE, COL_LOTW_QSL_SENT, station_id'); $this->db->where("station_id", 1); - + $this->db->where('COL_LOTW_QSL_SENT !=', "Y"); + $this->db->where('COL_PROP_MODE !=', "INTERNET"); $this->db->order_by("COL_TIME_ON", "desc"); $this->db->limit(1); $query = $this->db->get($this->config->item('table_name')); @@ -2171,6 +2172,20 @@ class Logbook_model extends CI_Model { return $query; } + function mark_lotw_sent($qso_id) { + + $data = array( + 'COL_LOTW_QSLSDATE' => date("Y-m-d H:i:s"), + 'COL_LOTW_QSL_SENT' => 'Y', + ); + + + $this->db->where('COL_PRIMARY_KEY', $qso_id); + + $this->db->update($this->config->item('table_name'), $data); + + return "Updated"; + } } function validateADIFDate($date, $format = 'Ymd') diff --git a/application/views/lotw_views/adif_views/adif_export.php b/application/views/lotw_views/adif_views/adif_export.php index fd84b1af..05bb1e7b 100644 --- a/application/views/lotw_views/adif_views/adif_export.php +++ b/application/views/lotw_views/adif_views/adif_export.php @@ -1,6 +1,3 @@ - cert); $cert1 = str_replace("-----BEGIN CERTIFICATE-----", "", $clean_cert); @@ -21,7 +18,7 @@ $cert2 = str_replace("-----END CERTIFICATE-----", "", $cert1); adif); ?>>adif; ?> -station_gridsquare); ?>>station_gridsquare; ?> +station_gridsquare)) { ?>station_gridsquare); ?>>station_gridsquare; ?> station_itu)) { ?>station_itu); ?>>station_itu; ?> @@ -42,11 +39,11 @@ $cert2 = str_replace("-----END CERTIFICATE-----", "", $cert1); COL_FREQ != "0") { ?>COL_FREQ / 1000000; ?>> -COL_FREQ_RX) { ?>COL_FREQ_RX / 1000000; ?>> +COL_FREQ_RX != "" || $qso->COL_FREQ_RX != "0") { ?>COL_FREQ_RX / 1000000; ?>> -COL_PROP_MODE); ?>>COL_PROP_MODE); ?> +COL_PROP_MODE) { ?>COL_PROP_MODE); ?>>COL_PROP_MODE); ?> -COL_SAT_NAME); ?>>COL_SAT_NAME); ?> +COL_SAT_NAME) { ?>COL_SAT_NAME); ?>>COL_SAT_NAME); ?> COL_BAND_RX) { ?>COL_BAND_RX); ?>>COL_BAND_RX); ?> @@ -95,7 +92,7 @@ if($freq_in_mhz) { $sign_string .= strtoupper($freq_in_mhz); } -if($freq_in_mhz_rx) { +if($qso->COL_FREQ_RX != "" || $qso->COL_FREQ_RX != "0") { $sign_string .= strtoupper($freq_in_mhz_rx); }