Skip to content

Commit

Permalink
MQTT: make usage of connect_async optional
Browse files Browse the repository at this point in the history
  • Loading branch information
stlehmann committed Feb 8, 2020
1 parent 158fafc commit 41ef2fe
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 12 deletions.
79 changes: 79 additions & 0 deletions environment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
name: Flask-MQTT
channels:
- conda-forge
- pytorch
- defaults
dependencies:
- attrs=19.3.0=py_0
- bleach=3.1.0=py_0
- ca-certificates=2019.11.28=hecc5488_0
- certifi=2019.11.28=py37_0
- cffi=1.13.2=py37h33e799b_0
- chardet=3.0.4=py_1004
- click=7.0=py_0
- cmarkgfm=0.4.2=py37h0b31af3_2
- cryptography=2.8=py37hafa8578_1
- dnspython=1.16.0=py_1
- docutils=0.16=py37_0
- ecdsa=0.13=py_0
- entrypoints=0.3=py37_1000
- eventlet=0.25.1=py37_0
- filelock=3.0.10=py_0
- flask=1.1.1=py_1
- flask-socketio=3.3.2=py_0
- future=0.18.2=py37_0
- gmp=6.2.0=h4a8c4bd_1
- greenlet=0.4.15=py37h0b31af3_0
- idna=2.8=py37_1000
- importlib_metadata=0.23=py37_0
- inflect=4.1.0=py37_0
- itsdangerous=1.1.0=py_0
- jaraco.itertools=5.0.0=py_0
- jinja2=2.11.1=py_0
- keyring=21.1.0=py37_0
- libcxx=9.0.1=1
- libffi=3.2.1=h6de7cb9_1006
- markupsafe=1.1.1=py37h0b31af3_0
- monotonic=1.5=py_0
- more-itertools=8.2.0=py_0
- ncurses=6.1=h0a44026_1002
- openssl=1.1.1d=h0b31af3_0
- packaging=20.1=py_0
- paho-mqtt=1.5.0=py_0
- pip=20.0.2=py_2
- pkginfo=1.5.0.1=py_0
- pluggy=0.12.0=py_0
- py=1.8.1=py_0
- pycparser=2.19=py37_1
- pycryptodome=3.9.6=py37h65ac59c_0
- pygments=2.5.2=py_0
- pyopenssl=19.1.0=py37_0
- pyparsing=2.4.6=py_0
- pysocks=1.7.1=py37_0
- pytest=5.3.5=py37_0
- python=3.7.6=h5c2c468_2
- python-engineio=3.0.0=py_0
- python-socketio=3.1.2=py_0
- readline=8.0=hcfe32e1_0
- readme_renderer=24.0=py_0
- requests=2.22.0=py37_1
- requests-toolbelt=0.9.1=py_0
- setuptools=45.1.0=py37_0
- six=1.14.0=py37_0
- sqlite=3.30.1=h93121df_0
- tk=8.6.10=hbbe82c9_0
- toml=0.10.0=py_0
- tox=3.14.3=py_0
- tqdm=4.42.1=py_0
- twine=3.1.1=py37_0
- urllib3=1.25.7=py37_0
- virtualenv=16.7.5=py_0
- wcwidth=0.1.8=py_0
- webencodings=0.5.1=py_1
- werkzeug=1.0.0=py_0
- wheel=0.34.2=py_1
- xz=5.2.4=h1de35cc_1001
- zipp=2.1.0=py_0
- zlib=1.2.11=h0b31af3_1006
prefix: /Users/stefan/.pyenv/versions/anaconda3-2019.07/envs/Flask-MQTT

30 changes: 18 additions & 12 deletions flask_mqtt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@
class Mqtt():
"""Main Mqtt class."""

def __init__(self, app=None, mqtt_logging=False):
# type: (Flask) -> None
def __init__(self, app=None, connect_async=False, mqtt_logging=False):
# type: (Flask, bool, bool) -> None
self.app = app
self._connect_async = connect_async # type: bool
self._connect_handler = None # type: Optional[Callable]
self._disconnect_handler = None # type: Optional[Callable]
self.topics = {} # type: Dict[str, TopicQos]
Expand Down Expand Up @@ -147,20 +148,25 @@ def _connect(self):
if self.tls_insecure:
self.client.tls_insecure_set(self.tls_insecure)

res = self.client.connect_async(
self.broker_url, self.broker_port, keepalive=self.keepalive
)

if res == 0:
logger.debug(
"Connected client '{0}' to broker {1}:{2}"
.format(self.client_id, self.broker_url, self.broker_port)
if self._connect_async:
# if connect_async is used
self.client.connect_async(
self.broker_url, self.broker_port, keepalive=self.keepalive
)
else:
logger.error(
"Could not connect to MQTT Broker, Error Code: {0}".format(res)
res = self.client.connect(
self.broker_url, self.broker_port, keepalive=self.keepalive
)

if res == 0:
logger.debug(
"Connected client '{0}' to broker {1}:{2}"
.format(self.client_id, self.broker_url, self.broker_port)
)
else:
logger.error(
"Could not connect to MQTT Broker, Error Code: {0}".format(res)
)
self.client.loop_start()

def _disconnect(self):
Expand Down

0 comments on commit 41ef2fe

Please sign in to comment.