当前位置: 首页>>代码示例>>Python>>正文


Python Machine.connect方法代码示例

本文整理汇总了Python中transitions.Machine.connect方法的典型用法代码示例。如果您正苦于以下问题:Python Machine.connect方法的具体用法?Python Machine.connect怎么用?Python Machine.connect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在transitions.Machine的用法示例。


在下文中一共展示了Machine.connect方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from transitions import Machine [as 别名]
# 或者: from transitions.Machine import connect [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):
#.........这里部分代码省略.........
开发者ID:gitter-badger,项目名称:hbmqtt,代码行数:103,代码来源:_client.py


注:本文中的transitions.Machine.connect方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。