本文整理汇总了Python中transitions.Machine.connect_fail方法的典型用法代码示例。如果您正苦于以下问题:Python Machine.connect_fail方法的具体用法?Python Machine.connect_fail怎么用?Python Machine.connect_fail使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类transitions.Machine
的用法示例。
在下文中一共展示了Machine.connect_fail方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from transitions import Machine [as 别名]
# 或者: from transitions.Machine import connect_fail [as 别名]
class MQTTClient:
states = ['new', 'connecting', 'connected', 'disconnected']
def __init__(self, client_id=None, config={}, loop=None):
"""
:param config: Example yaml config
broker:
host: localhost
port: 1883
scheme: mqtt
username: xxx
password: yyy
# OR
uri: mqtt:[email protected]//localhost:1883/
# OR a mix ot both
keep_alive: 60
cleansession: true
will:
retain: false
topic: some/topic
message: Will message
qos: 0
default_qos: 0
default_retain: false
topics:
a/b:
qos: 2
retain: true
:param loop:
:return:
"""
self.logger = logging.getLogger(__name__)
self.config = config.copy()
self.config.update(_defaults)
if client_id is not None:
self.client_id = client_id
else:
self.client_id = gen_client_id()
self.logger.debug("Using generated client ID : %s" % self.client_id)
self._init_states()
if loop is not None:
self._loop = loop
else:
self._loop = asyncio.get_event_loop()
self.session = None
self._handler = None
def _init_states(self):
self.machine = Machine(states=MQTTClient.states, initial='new')
self.machine.add_transition(trigger='connect', source='new', dest='connecting')
self.machine.add_transition(trigger='connect', source='disconnected', dest='connecting')
self.machine.add_transition(trigger='connect_fail', source='connecting', dest='disconnected')
self.machine.add_transition(trigger='connect_success', source='connecting', dest='connected')
self.machine.add_transition(trigger='disconnect', source='idle', dest='disconnected')
self.machine.add_transition(trigger='disconnect', source='connected', dest='disconnected')
@asyncio.coroutine
def connect(self, host=None, port=None, username=None, password=None, uri=None, cleansession=None):
try:
self.machine.connect()
self.session = self._initsession(host, port, username, password, uri, cleansession)
self.logger.debug("Connect with session parameters: %s" % self.session)
yield from self._connect_coro()
self.machine.connect_success()
except MachineError:
msg = "Connect call incompatible with client current state '%s'" % self.machine.current_state
self.logger.warn(msg)
self.machine.connect_fail()
raise ClientException(msg)
except Exception as e:
self.machine.connect_fail()
self.logger.warn("Connection failed: %s " % e)
raise ClientException("Connection failed: %s " % e)
@asyncio.coroutine
def disconnect(self):
try:
yield from self._handler.mqtt_disconnect()
yield from self._handler.stop()
except Exception as e:
self.logger.warn("Unhandled exception: %s" % e)
raise ClientException("Unhandled exception: %s" % e)
except MachineError as me:
self.logger.debug("Invalid method call at this moment: %s" % me)
raise ClientException("Client instance can't be disconnected: %s" % me)
self.session = None
@asyncio.coroutine
def ping(self):
"""
Send a MQTT ping request and wait for response
:return: None
"""
self._handler.mqtt_ping()
@asyncio.coroutine
def publish(self, topic, message, dup=False, qos=None, retain=None):
#.........这里部分代码省略.........