Esempio n. 1
0
    def _check_restart_required(self):
        if not BackendSettings.get_solo().restart_required:
            return

        BackendSettings.objects.update(restart_required=False)
        logger.warning(
            'Detected backend restart required, stopping process...')
        raise StopInfiniteRun()
Esempio n. 2
0
def initialize():
    """ Initializes the MQTT client and returns client instance. """
    broker_settings = MQTTBrokerSettings.get_solo()

    if not broker_settings.hostname:
        logger.warning(
            'MQTT: No hostname found in settings, restarting in a minute...')
        time.sleep(60)
        raise StopInfiniteRun()

    mqtt_client = paho.Client(client_id=broker_settings.client_id)
    mqtt_client.on_connect = on_connect
    mqtt_client.on_disconnect = on_disconnect
    mqtt_client.on_log = on_log
    mqtt_client.on_publish = on_publish

    if broker_settings.username:
        mqtt_client.username_pw_set(broker_settings.username,
                                    broker_settings.password)

    # SSL/TLS.
    if broker_settings.secure == MQTTBrokerSettings.SECURE_CERT_NONE:
        logger.debug('MQTT: Initializing secure connection (ssl.CERT_NONE)')
        mqtt_client.tls_set(cert_reqs=ssl.CERT_NONE)
    elif broker_settings.secure == MQTTBrokerSettings.SECURE_CERT_REQUIRED:
        logger.debug(
            'MQTT: Initializing secure connection (ssl.CERT_REQUIRED)')
        mqtt_client.tls_set(cert_reqs=ssl.CERT_REQUIRED)
    else:
        logger.debug('MQTT: Initializing insecure connection (no TLS)')

    try:
        mqtt_client.connect(host=broker_settings.hostname,
                            port=broker_settings.port)
    except Exception as error:
        logger.error(
            'MQTT: Failed to connect to broker, restarting in a minute: %s',
            error)
        time.sleep(60)
        raise StopInfiniteRun()

    return mqtt_client
Esempio n. 3
0
    def run(self, **options):
        """ InfiniteManagementCommandMixin listens to handle() and calls run() in a loop. """
        # Check on each run. In case MQTT was either disabled, enabled or settings were changed.
        if MQTTBrokerSettings.get_solo().restart_required:
            MQTTBrokerSettings.objects.update(restart_required=False)
            logger.warning(
                'MQTT | --- Detected settings change, requiring process restart, stopping...'
            )
            raise StopInfiniteRun()

        dsmr_mqtt.services.broker.run(mqtt_client=self.mqtt_client)
Esempio n. 4
0
def on_disconnect(client, userdata, rc):
    """ MQTT client callback for disconnecting. Outputs some debug logging. """
    """
    From the docs, rc value:
        If MQTT_ERR_SUCCESS (0), the callback was called in response to a disconnect() call.
        If any other value the disconnection was unexpected, such as might be caused by a network error.
    """
    logger.debug('MQTT | Paho client: on_disconnect(userdata, rc) %s %s', userdata, rc)

    if rc != paho.MQTT_ERR_SUCCESS:
        logger.warning('MQTT | --- Unexpected disconnect, re-connecting...')

        try:
            client.reconnect()
        except Exception as error:
            logger.error('MQTT | Failed to re-connect to broker: %s', error)
            raise StopInfiniteRun()  # Don't bother even to continue, just reset everything.