diff --git a/aprsd/client/client.py b/aprsd/client/client.py index 191854e..b5dcc69 100644 --- a/aprsd/client/client.py +++ b/aprsd/client/client.py @@ -38,15 +38,16 @@ class APRSDClient: keepalive_collector.KeepAliveCollector().register(cls) return cls._instance - def __init__(self): + def __init__(self, auto_connect: bool = True): + self.auto_connect = auto_connect self.connected = False self.login_status = { 'success': False, 'message': None, } - if not self.driver: - self.driver = DriverRegistry().get_driver() - self.driver.setup_connection() + self.driver = DriverRegistry().get_driver() + if self.auto_connect: + self.connect() def stats(self, serializable=False) -> dict: stats = {} @@ -54,17 +55,20 @@ class APRSDClient: stats = self.driver.stats(serializable=serializable) return stats - @property - def is_enabled(self): - if not self.driver: - return False - return self.driver.is_enabled() + @staticmethod + def is_enabled(): + for driver in DriverRegistry().drivers: + if driver.is_enabled(): + return True + return False - @property - def is_configured(self): - if not self.driver: - return False - return self.driver.is_configured() + @staticmethod + def is_configured(): + """Check if ANY driver is configured.""" + for driver in DriverRegistry().drivers: + if driver.is_configured(): + return True + return False # @property # def is_connected(self): @@ -98,6 +102,11 @@ class APRSDClient: def is_alive(self): return self.driver.is_alive() + def connect(self): + if not self.driver: + self.driver = DriverRegistry().get_driver() + self.driver.setup_connection() + def close(self): if not self.driver: return @@ -109,7 +118,8 @@ class APRSDClient: LOG.info('Resetting client connection.') if self.driver: self.driver.close() - self.driver.setup_connection() + if not self.delay_connect: + self.driver.setup_connection() if self.filter: self.driver.set_filter(self.filter) else: diff --git a/aprsd/client/drivers/aprsis.py b/aprsd/client/drivers/aprsis.py index 188fe15..d472560 100644 --- a/aprsd/client/drivers/aprsis.py +++ b/aprsd/client/drivers/aprsis.py @@ -26,6 +26,7 @@ class APRSISDriver: _client = None _checks = False + connected = False def __init__(self): max_timeout = {'hours': 0.0, 'minutes': 2, 'seconds': 0} @@ -164,7 +165,7 @@ class APRSISDriver: return core.factory(args[0]) def consumer(self, callback: Callable, raw: bool = False): - if self._client: + if self._client and self.connected: try: self._client.consumer( callback, @@ -177,7 +178,6 @@ class APRSISDriver: LOG.info(e.__cause__) raise e else: - LOG.warning('client is None, might be resetting.') self.connected = False def stats(self, serializable: bool = False) -> dict: diff --git a/aprsd/client/drivers/tcpkiss.py b/aprsd/client/drivers/tcpkiss.py index d6c456c..3f28f76 100644 --- a/aprsd/client/drivers/tcpkiss.py +++ b/aprsd/client/drivers/tcpkiss.py @@ -79,7 +79,7 @@ class TCPKISSDriver: def transport(self) -> str: return client.TRANSPORT_TCPKISS - @classmethod + @staticmethod def is_enabled(cls) -> bool: """Check if KISS is enabled in configuration. diff --git a/aprsd/conf/common.py b/aprsd/conf/common.py index cd24603..5167527 100644 --- a/aprsd/conf/common.py +++ b/aprsd/conf/common.py @@ -19,7 +19,7 @@ registry_group = cfg.OptGroup( aprsd_opts = [ cfg.StrOpt( 'callsign', - required=True, + default='NOCALL', help='Callsign to use for messages sent by APRSD', ), cfg.BoolOpt( diff --git a/aprsd/exception.py b/aprsd/exception.py index 2f15728..05febd7 100644 --- a/aprsd/exception.py +++ b/aprsd/exception.py @@ -13,3 +13,10 @@ class ConfigOptionBogusDefaultException(Exception): f"Config file option '{config_option}' needs to be " f"changed from provided default of '{default_fail}'" ) + + +class APRSClientNotConfiguredException(Exception): + """APRS client is not configured.""" + + def __init__(self): + self.message = 'APRS client is not configured.'