2011-11-14 21:56:36 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 James Coxon, Daniel Richman, Robert Harrison,
|
|
|
|
* Philip Heron, Adam Greig, Simrun Basuita
|
|
|
|
* License: GNU GPL 3
|
2011-11-19 21:49:42 +00:00
|
|
|
*
|
|
|
|
* dl_fldigi.cxx: glue, startup/cleanup and misc functions
|
2011-11-14 21:56:36 +00:00
|
|
|
*/
|
|
|
|
|
2011-11-17 22:50:24 +00:00
|
|
|
#include "dl_fldigi/dl_fldigi.h"
|
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
#include <time.h>
|
|
|
|
|
2011-12-11 00:43:47 +00:00
|
|
|
#include <FL/Fl.H>
|
2011-11-17 22:50:24 +00:00
|
|
|
|
|
|
|
#include "configuration.h"
|
|
|
|
#include "debug.h"
|
|
|
|
#include "confdialog.h"
|
|
|
|
#include "fl_digi.h"
|
|
|
|
|
|
|
|
#include "dl_fldigi/flights.h"
|
|
|
|
#include "dl_fldigi/hbtint.h"
|
|
|
|
#include "dl_fldigi/location.h"
|
|
|
|
#include "dl_fldigi/gps.h"
|
2012-09-04 21:17:19 +00:00
|
|
|
#include "dl_fldigi/update.h"
|
2011-11-17 22:50:24 +00:00
|
|
|
|
2011-11-14 21:56:36 +00:00
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
namespace dl_fldigi {
|
|
|
|
|
2011-11-17 22:50:24 +00:00
|
|
|
bool hab_ui_exists, shutting_down;
|
2011-11-19 21:49:42 +00:00
|
|
|
time_t last_rx;
|
2011-11-14 21:56:36 +00:00
|
|
|
|
2012-09-05 17:52:18 +00:00
|
|
|
static bool dl_online, first_online;
|
2011-11-14 21:56:36 +00:00
|
|
|
static int dirty;
|
2011-11-19 21:49:42 +00:00
|
|
|
static time_t last_warn;
|
2011-11-14 21:56:36 +00:00
|
|
|
|
|
|
|
static const time_t period = 10;
|
|
|
|
|
|
|
|
static void periodically(void *);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Functions init, ready and cleanup should only be called from main().
|
2012-11-02 21:56:57 +00:00
|
|
|
* thread_death and periodically are called by FLTK, which will have the Fl
|
|
|
|
* lock.
|
2011-11-14 21:56:36 +00:00
|
|
|
*/
|
|
|
|
void init()
|
|
|
|
{
|
|
|
|
flights::init();
|
|
|
|
hbtint::init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ready(bool hab_mode)
|
|
|
|
{
|
|
|
|
hab_ui_exists = hab_mode;
|
|
|
|
|
2011-11-17 22:50:24 +00:00
|
|
|
flights::load_cache();
|
|
|
|
hbtint::start();
|
2011-11-19 21:49:42 +00:00
|
|
|
location::start();
|
2011-11-14 21:56:36 +00:00
|
|
|
|
|
|
|
/* online will call uthr->settings() if hab_mode since it online will
|
|
|
|
* "change" from false to true) */
|
|
|
|
online(hab_mode);
|
|
|
|
|
|
|
|
Fl::add_timeout(period, periodically);
|
|
|
|
|
|
|
|
if (hab_ui_exists)
|
|
|
|
habTimeSinceLastRx->value("ages");
|
|
|
|
}
|
|
|
|
|
|
|
|
void cleanup()
|
|
|
|
{
|
|
|
|
LOG_DEBUG("cleaning up");
|
|
|
|
shutting_down = true;
|
|
|
|
|
|
|
|
gps::cleanup();
|
|
|
|
hbtint::cleanup();
|
|
|
|
flights::cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void periodically(void *)
|
|
|
|
{
|
|
|
|
if (shutting_down)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Fl::repeat_timeout(period, periodically);
|
|
|
|
|
|
|
|
time_t now = time(NULL);
|
|
|
|
|
|
|
|
if (hab_ui_exists)
|
|
|
|
{
|
|
|
|
time_t rx_delta = now - last_rx;
|
|
|
|
|
|
|
|
if (rx_delta > 3600 * 24)
|
|
|
|
{
|
|
|
|
habTimeSinceLastRx->value("ages");
|
|
|
|
}
|
|
|
|
else if (rx_delta < 3)
|
|
|
|
{
|
|
|
|
habTimeSinceLastRx->value("just now");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ostringstream sval;
|
|
|
|
|
|
|
|
if (rx_delta < 60)
|
|
|
|
goto seconds;
|
|
|
|
if (rx_delta < 3600)
|
|
|
|
goto minutes;
|
|
|
|
|
|
|
|
sval << (rx_delta / 3600) << "h ";
|
|
|
|
rx_delta %= 3600;
|
|
|
|
minutes:
|
|
|
|
sval << (rx_delta / 60) << "m ";
|
|
|
|
rx_delta %= 60;
|
|
|
|
seconds:
|
|
|
|
sval << rx_delta << "s";
|
|
|
|
|
|
|
|
habTimeSinceLastRx->value(sval.str().c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* All other functions should hopefully be thread safe */
|
2011-11-17 22:50:24 +00:00
|
|
|
|
|
|
|
/* How does online/offline work? if online() is false, uthr->settings() will
|
|
|
|
* reset the UploaderThread, leaving it unintialised */
|
2011-11-14 21:56:36 +00:00
|
|
|
void online(bool val)
|
|
|
|
{
|
|
|
|
Fl_AutoLock lock;
|
|
|
|
|
|
|
|
bool changed;
|
|
|
|
|
|
|
|
changed = (dl_online != val);
|
|
|
|
dl_online = val;
|
|
|
|
|
|
|
|
if (changed)
|
|
|
|
{
|
2011-11-17 22:50:24 +00:00
|
|
|
hbtint::uthr->settings();
|
2011-11-14 21:56:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (changed && dl_online)
|
|
|
|
{
|
2012-09-05 17:52:18 +00:00
|
|
|
if (!first_online)
|
|
|
|
{
|
|
|
|
if (progdefaults.check_for_updates)
|
|
|
|
update::check();
|
|
|
|
first_online = true;
|
|
|
|
}
|
2012-09-04 21:17:19 +00:00
|
|
|
|
2012-08-23 20:09:44 +00:00
|
|
|
if (!flights::downloaded_flights_once)
|
2011-11-17 22:50:24 +00:00
|
|
|
hbtint::uthr->flights();
|
2012-08-23 20:09:44 +00:00
|
|
|
if (!flights::downloaded_payloads_once)
|
|
|
|
hbtint::uthr->payloads();
|
2011-11-14 21:56:36 +00:00
|
|
|
|
2012-08-23 20:09:44 +00:00
|
|
|
hbtint::uthr->listener_information();
|
2011-11-17 22:50:24 +00:00
|
|
|
hbtint::uthr->listener_telemetry();
|
2011-11-14 21:56:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
confdialog_dl_online->value(val);
|
|
|
|
set_menu_dl_online(val);
|
|
|
|
set_menu_dl_refresh_active(dl_online);
|
|
|
|
|
|
|
|
if (dl_online)
|
2012-08-23 20:09:44 +00:00
|
|
|
{
|
|
|
|
flight_docs_refresh_a->activate();
|
|
|
|
flight_docs_refresh_b->activate();
|
|
|
|
}
|
2011-11-14 21:56:36 +00:00
|
|
|
else
|
2012-08-23 20:09:44 +00:00
|
|
|
{
|
|
|
|
flight_docs_refresh_a->deactivate();
|
|
|
|
flight_docs_refresh_b->deactivate();
|
|
|
|
}
|
2011-11-14 21:56:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool online()
|
|
|
|
{
|
|
|
|
Fl_AutoLock lock;
|
|
|
|
return dl_online;
|
|
|
|
}
|
|
|
|
|
|
|
|
void changed(enum changed_groups thing)
|
|
|
|
{
|
|
|
|
Fl_AutoLock lock;
|
|
|
|
dirty |= thing;
|
|
|
|
}
|
|
|
|
|
|
|
|
void commit()
|
|
|
|
{
|
|
|
|
Fl_AutoLock lock;
|
|
|
|
|
|
|
|
/* Update something if its settings change; fairly simple: */
|
|
|
|
if (dirty & CH_UTHR_SETTINGS)
|
|
|
|
{
|
2012-08-23 20:09:44 +00:00
|
|
|
flights::downloaded_flights_once = false;
|
|
|
|
flights::downloaded_payloads_once = false;
|
2011-11-14 21:56:36 +00:00
|
|
|
|
2011-11-17 22:50:24 +00:00
|
|
|
hbtint::uthr->settings();
|
|
|
|
hbtint::uthr->flights();
|
2012-08-23 20:09:44 +00:00
|
|
|
hbtint::uthr->payloads();
|
2011-11-14 21:56:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (dirty & CH_LOCATION_MODE)
|
|
|
|
{
|
2011-11-17 22:50:24 +00:00
|
|
|
location::current_location_mode = location::new_location_mode;
|
2011-11-14 21:56:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((dirty & CH_LOCATION_MODE) || (dirty & CH_GPS_SETTINGS))
|
|
|
|
{
|
2011-11-17 22:50:24 +00:00
|
|
|
gps::configure_gps();
|
2011-11-14 21:56:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If the info has been updated, or the upload settings changed... */
|
|
|
|
if (dirty & (CH_UTHR_SETTINGS | CH_INFO))
|
|
|
|
{
|
2012-08-23 20:09:44 +00:00
|
|
|
hbtint::uthr->listener_information();
|
2011-11-14 21:56:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* if stationary and (settings changed, or if we just switched to
|
|
|
|
* stationary mode from gps mode, or if the upload settings changed) */
|
2011-11-17 22:50:24 +00:00
|
|
|
if (location::current_location_mode == location::LOC_STATIONARY &&
|
2011-11-14 21:56:36 +00:00
|
|
|
(dirty & (CH_STATIONARY_LOCATION | CH_LOCATION_MODE |
|
|
|
|
CH_UTHR_SETTINGS)))
|
|
|
|
{
|
2011-11-17 22:50:24 +00:00
|
|
|
hbtint::uthr->listener_telemetry();
|
2011-11-14 21:56:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dirty = CH_NONE;
|
|
|
|
}
|
|
|
|
|
2011-11-19 21:49:42 +00:00
|
|
|
void status(const string &message)
|
|
|
|
{
|
|
|
|
Fl_AutoLock lock;
|
2011-11-19 21:58:22 +00:00
|
|
|
LOG_DEBUG("unimp status: %s", message.c_str());
|
2011-11-19 21:49:42 +00:00
|
|
|
|
|
|
|
/* don't overwrite a warning */
|
|
|
|
if (time(NULL) - last_warn > 10)
|
2011-11-19 21:58:22 +00:00
|
|
|
put_status_safe(message.c_str(), 10);
|
2011-11-19 21:49:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void status_important(const string &message)
|
|
|
|
{
|
|
|
|
Fl_AutoLock lock;
|
2011-11-19 21:58:22 +00:00
|
|
|
LOG_DEBUG("warn status: %s", message.c_str());
|
2011-11-19 21:49:42 +00:00
|
|
|
|
2012-09-03 21:07:20 +00:00
|
|
|
string temp = "WARNING! " + message;
|
2011-11-19 21:49:42 +00:00
|
|
|
put_status_safe(temp.c_str(), 10);
|
|
|
|
last_warn = time(NULL);
|
|
|
|
}
|
|
|
|
|
2011-11-14 21:56:36 +00:00
|
|
|
} /* namespace dl_fldigi */
|