Merge pull request #494 from AndreasK79/master

Mass upload for QRZ.com, suitable for a cron job.
pull/502/head
Peter Goodhall 2020-05-12 18:49:28 +01:00 zatwierdzone przez GitHub
commit 1b0de2d26b
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
2 zmienionych plików z 126 dodań i 4 usunięć

Wyświetl plik

@ -0,0 +1,61 @@
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
Controller to interact with the QRZ.com API
*/
class Qrz extends CI_Controller {
// Show frontend if there is one
public function index() {
$this->config->load('config');
}
// Upload QSO to QRZ.com
public function upload() {
$this->config->load('config');
ini_set('memory_limit', '-1');
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$this->load->model('logbook_model');
$station_ids = $this->logbook_model->get_station_id_with_qrz_api();
if ($station_ids) {
foreach ($station_ids as $station_id) {
$qrz_api_key = $this->logbook_model->exists_qrz_api_key($station_id);
$this->mass_upload_qsos($station_id, $qrz_api_key);
}
} else {
echo "No station_id's with a QRZ API Key found";
log_message('info', "No station_id's with a QRZ API Key found");
}
}
function mass_upload_qsos($station_id, $qrz_api_key) {
$data['qsos'] = $this->logbook_model->get_qrz_qsos($station_id);
if ($data['qsos']) {
foreach ($data['qsos'] as $qso) {
$adif = $this->logbook_model->create_adif_from_data($qso);
$result = $this->logbook_model->push_qso_to_qrz($qrz_api_key, $adif);
if ($result) {
$this->markqso($qso['COL_PRIMARY_KEY']);
}
}
echo "QSOs has been uploaded to QRZ.com.";
log_message('info', 'QSOs has been uploaded to QRZ.com.');
} else {
echo "No QSOs found for upload.";
log_message('info', 'No QSOs found for upload.');
}
}
function markqso($primarykey) {
$this->logbook_model->mark_qrz_qsos_sent($primarykey);
}
}

Wyświetl plik

@ -326,7 +326,8 @@ class Logbook_model extends CI_Model {
// Push qso to qrz if apikey is set
if ($apikey = $this->exists_qrz_api_key($data['station_id'])) {
IF ($this->push_qso_to_qrz($data, $apikey)) {
$adif = $this->create_adif_from_data($data);
IF ($this->push_qso_to_qrz($apikey, $adif)) {
$data['COL_QRZCOM_QSO_UPLOAD_STATUS'] = 'Y';
$data['COL_QRZCOM_QSO_UPLOAD_DATE'] = date("Y-m-d H:i:s", strtotime("now"));
}
@ -336,6 +337,9 @@ class Logbook_model extends CI_Model {
$this->db->insert($this->config->item('table_name'), $data);
}
/*
* Function checks if a QRZ API Key exists in the table with the given station id
*/
function exists_qrz_api_key($station_id) {
$sql = 'select qrzapikey from station_profile
where station_id = ' . $station_id;
@ -352,11 +356,13 @@ class Logbook_model extends CI_Model {
}
}
function push_qso_to_qrz($data, $apikey) {
/*
* Function uploads a QSO to QRZ with the API given.
* $adif contains a line with the QSO in the ADIF format. QSO ends with an <eor>
*/
function push_qso_to_qrz($apikey, $adif) {
$url = 'http://logbook.qrz.com/api'; // TODO: Move this to database
$adif = $this->create_adif_from_data($data);
$post_data['KEY'] = $apikey;
$post_data['ACTION'] = 'INSERT';
$post_data['ADIF'] = $adif;
@ -383,6 +389,26 @@ class Logbook_model extends CI_Model {
curl_close($ch);
}
/*
* Function marks QSOs as uploaded to QRZ.
* $primarykey is the unique id for that QSO in the logbook
*/
function mark_qrz_qsos_sent($primarykey) {
$data = array(
'COL_QRZCOM_QSO_UPLOAD_DATE' => date("Y-m-d H:i:s", strtotime("now")),
'COL_QRZCOM_QSO_UPLOAD_STATUS' => 'Y',
);
$this->db->where('COL_PRIMARY_KEY', $primarykey);
$this->db->update($this->config->item('table_name'), $data);
return true;
}
/*
* Function is used to build an ADIF string from an array that contains the QSO data
*/
function create_adif_from_data($data) {
$adif = '<call:' . strlen($data['COL_CALL']) . '>' . $data['COL_CALL'];
$adif .= '<band:' . strlen($data['COL_BAND']) . '>' . $data['COL_BAND'];
@ -797,6 +823,41 @@ class Logbook_model extends CI_Model {
return $query;
}
/*
* Function returns the QSOs from the logbook, which have not been either marked as uploaded to qrz, or has been modified with an edit
*/
function get_qrz_qsos($station_id){
$sql = 'select * from ' . $this->config->item('table_name') .
' where station_id = ' . $station_id .
' and (COL_QRZCOM_QSO_UPLOAD_STATUS = NULL
or COL_QRZCOM_QSO_UPLOAD_STATUS = ""
or COL_QRZCOM_QSO_UPLOAD_STATUS = "M"
or COL_QRZCOM_QSO_UPLOAD_STATUS = "N")';
$query = $this->db->query($sql);
return $query->result_array();
}
/*
* Function returns all the station_id's with QRZ API Key's
*/
function get_station_id_with_qrz_api() {
$sql = 'select station_id from station_profile
where coalesce(qrzapikey, "") <> ""';
$query = $this->db->query($sql);
$result = $query->row();
if ($result) {
return $result;
}
else {
return null;
}
}
function get_last_qsos($num) {
$CI =& get_instance();