def mqtt(self): if not hasattr(self, '_mqtt_client'): self.responses = {} self.ready = False client = MQTTClient() client.username_pw_set(self.apikey) client.on_connect = self._on_connect client.on_message = self._on_message client.on_subscribe = self._on_subscribe client.connect(self.client.endpoint.replace('mqtt://', '')) self._mqtt_client = client # Start the loop and wait for the connection to be ready self._mqtt_client.loop_start() while not self.ready: time.sleep(.1) return self._mqtt_client
def connect(client: mqtt.Client, args: argparse.Namespace): """Connect to an MQTT broker with supplied arguments.""" if args.username: client.username_pw_set(args.username, args.password) # TLS if args.tls: # TLS is enabled if args.tls_version is None: # Use highest TLS version args.tls_version = ssl.PROTOCOL_TLS if args.tls_ca_certs is not None: args.tls_ca_certs = os.path.expandvars(args.tls_ca_certs) if args.tls_certfile is not None: args.tls_certfile = os.path.expandvars(args.tls_certfile) if args.tls_keyfile is not None: args.tls_keyfile = os.path.expandvars(args.tls_keyfile) client.tls_set( ca_certs=args.tls_ca_certs, certfile=args.tls_certfile, keyfile=args.tls_keyfile, cert_reqs=getattr(ssl, args.tls_cert_reqs), tls_version=args.tls_version, ciphers=(args.tls_ciphers or None), ) client.connect(args.host, args.port)
class BugfixClient(PAHO_MQTT_Client): def connect(self): homie.mqtt.mqtt_base.MQTT_Base.connect(self) #self.mqtt_client = MQTTClientWrapper(client_id=self.mqtt_settings['MQTT_CLIENT_ID']) self.mqtt_client = Client(client_id=self.mqtt_settings['MQTT_CLIENT_ID']) self.mqtt_client.on_connect = self._on_connect self.mqtt_client.on_message = self._on_message #self.mqtt_client.on_publish = self._on_publish self.mqtt_client.on_disconnect = self._on_disconnect self.mqtt_client.enable_logger(homie.mqtt.paho_mqtt_client.mqtt_logger) self.mqtt_client.enable_logger() if self.mqtt_settings ['MQTT_USERNAME']: self.mqtt_client.username_pw_set( self.mqtt_settings ['MQTT_USERNAME'], password=self.mqtt_settings ['MQTT_PASSWORD'] ) try: self.mqtt_client.connect( self.mqtt_settings ['MQTT_BROKER'], port=self.mqtt_settings ['MQTT_PORT'], keepalive=self.mqtt_settings ['MQTT_KEEPALIVE'], ) self.mqtt_client.loop_start() except Exception as e: homie.mqtt.paho_mqtt_client.logger.warning ('MQTT Unable to connect to Broker {}'.format(e))
class Seller: ''' Set objects for sale and publish them on 'Available-items' topic ''' def __init__(self, number_items, broker='localhost', auth=None): self.number_of_items = number_items self.auth = auth self.client = Client() if auth != None: (usr, pwd) = auth self.client.username_pw_set(usr, pwd) self.client.connect(broker) def sell_process(self, update=False): available_itms = ''.join( [str(x) + ' ' for x in range(1, self.number_of_items + 1)]) self.client.publish('Available-items', available_itms) while update: self.client.publish('Available-items', available_itms) time.sleep(3 * random.random()) def sell(self): Process(target=self.sell_process, args=()).start()
def main(): client = Client(protocol=MQTTv311) client.username_pw_set(USERNAME, password=PASSWORD) client.connect(HOST, port=PORT, keepalive=60) host1 = "98:07:2D:35:7B:00" host2 = "98:07:2D:40:95:83" print("Connecting to SensorTags") tag1 = SensorTag(host1) tag2 = SensorTag(host2) sensor_enabler(tag1) sensor_enabler(tag2) sleep(1.0) try: while True: data1 = read_sensor_data(tag1) data2 = read_sensor_data(tag2) sleep(1.0) pprint({"SensorTag1": data1}) pprint({"SensorTag2": data2}) client.publish(TOPIC1, payload=dumps(data1)) client.publish(TOPIC2, payload=dumps(data2)) sleep(30) except KeyboardInterrupt: print("Disconnected to SensorTags") tag1.disconnect() tag2.disconnect()
def main(): global args, write_api parser = ArgumentParser(description=__doc__) parser.add_argument("--host", default='localhost', help='hostname of mqtt broker') parser.add_argument( "-t", action='append', default=['velogen/raw'], help='MQTT topic to subscribe to. Can be put multiple times.') parser.add_argument("-d", action='store_true', help='enable debug output') parser.add_argument("--user", default=None, help="username") parser.add_argument("--pw", default=None, help="password") args = parser.parse_args() client = Client() client.on_connect = on_connect client.on_disconnect = on_disconnect client.on_message = on_message if args.user is not None and args.pw is not None: client.username_pw_set(args.user, args.pw) client.connect(args.host) # For influxdb2 only db = InfluxDBClient(url='http://localhost:8086', token=token, debug=args.d) write_api = db.write_api(write_options=SYNCHRONOUS) while True: client.loop(timeout=1.0)
def _are_valid_credentials(self, username, password): """Checks whether credentials are valid""" def _on_connect(client, userdata, flags, rc): """A callback that matches the MQTT client signature, that triggers when connection was established""" userdata.set_return_code(rc) client.disconnect() client.loop_stop() client.should_stop = True con_result = ConnectionResult() client = Client(userdata=con_result) client.username_pw_set(username, password) client.on_connect = _on_connect start_time = time.time() client.should_stop = False client.connect_async(self.host, self.port) client.loop_start() while not client.should_stop and time.time( ) - start_time < self.timeout: time.sleep(0.001) return con_result.did_succeed
def get_client(project_id: str, cloud_region: str, registry_id: str, device_id: str, password: str, mqtt_bridge_hostname: str, mqtt_bridge_port: str, ca_certs: str): client_id = 'projects/{}/locations/{}/registries/{}/devices/{}'.format( project_id, cloud_region, registry_id, device_id) secho('Client ID: ', fg='bright_green', nl=False) echo('\'{}\''.format(client_id)) client = Client(client_id=client_id) client.username_pw_set(username='******', password=password) client.tls_set(ca_certs=ca_certs, tls_version=ssl.PROTOCOL_TLSv1_2) # Assign callbacks client.on_connect = on_connect client.on_publish = on_publish client.on_disconnect = on_disconnect client.on_message = on_message # Connect to MQTT bridge client.connect(mqtt_bridge_hostname, mqtt_bridge_port) client.loop_start() return client
def config(self, mqtt_client: MqttClient): if self.username and self.password: mqtt_client.username_pw_set(self.username, self.password) mqtt_client.connect( self.host, self.port, )
def create_client(self, host, port, username, password, clientid, cafile=None): """Creating an MQTT Client Object""" client = MqttClient(clientid) if username and password: client.username_pw_set(username=username, password=password) else: self.logger.warn("Proceeding without username and password") if cafile: client.tls_set(ca_certs=cafile) else: self.logger.warn("Proceeding without certificate file") try: client.on_connect = self.on_connect client.on_message = self.on_message client.connect(host=host, port=port) except OSError as error: self.logger.error(error) return client
class mqtt_client_connect(): def __init__(self, broker="10.129.7.199", port=1883, username="******", password="******", client_id="12345"): self.broker = broker self.port = port self.username = username self.password = password self.payload = None self.client_id = client_id self.num = 1 self.flag = 0 try: # self.mqttc=Client(clean_session=False,client_id="12345") self.mqttc = Client(client_id=self.client_id) self.mqttc.on_connect = self.on_connect self.mqttc.on_publish = self.on_publish self.mqttc.on_subscribe = self.on_subscribe self.mqttc.username_pw_set(self.username, self.password) self.mqttc.connect(self.broker, port=self.port) self.mqttc.loop_start() except: print( "mqtt_client_connect error: mqttc connect failed Please check Broker and Port...." ) # ====================================================== def on_connect(self, client, userdata, flags, rc): #rc为0 返回连接成功 if rc == 0: self.flag = 1 print("OnConnetc, rc: " + str(rc), 'successful ' + str(client._username)) else: self.flag = 0 print("OnConnetc, rc: " + str(rc), 'unsuccessful ' + str(client._username)) def on_disconnect(self, client, userdata, rc): if rc != 0: self.flag = 0 print("Unexpected MQTT disconnection. Will auto-reconnect") def on_publish(self, client, userdata, mid): print("OnPublish, mid: " + str(mid) + " " + str(client._username)) def on_subscribe(self, client, userdata, mid, granted_qos): print("Subscribed: " + str(mid) + " " + str(granted_qos) + " 订阅成功 " + str(client._username)) self.mqttc.on_message = self.on_message def on_message(self, client, userdata, msg): strcurtime = time.strftime("%Y-%m-%d %H:%M:%S") print(strcurtime + ": " + msg.topic + " " + str(msg.qos) + " " + str(msg.payload) + str(client._username))
class MQTTClient(object): """Manages Paho MQTT client lifecycle and callbacks""" def __init__(self, config: dict, message_processor=None): self.config = config self.client = Client( client_id=config.mqtt_client, clean_session=config.mqtt_clean_session, userdata={"client": config.mqtt_client}, ) self.client.username_pw_set(config.mqtt_username, config.mqtt_password) if self.config.mqtt_debug: self.client.on_log = self._on_log self.client.on_connect = self._on_connect self.client.on_subscribe = self._on_subscribe self.client.on_message = self._on_message self.client.on_publish = self._on_publish self.client.on_disconnect = self._on_disconnect self.client.connect(config.mqtt_host, config.mqtt_port, 60) if message_processor: self.message_processor = message_processor def _on_log(self, client, userdata, level, buf): click.echo(f"{buf}, origin: {userdata['client']}") def _on_connect(self, client, userdata, flags, rc): click.echo(f"Connected {userdata['client']}, result code: {str(rc)} {str(flags)}") click.echo(f"Subscribing to all topics...") self.client.subscribe(self.config.mqtt_topics) def _on_subscribe(self, client, userdata, mid, granted_qos): click.echo(f"Subscribed {userdata['client']}, mid: {mid}, granted qos: {granted_qos}") click.echo(f"Listening for {userdata['client']} messages...") def _on_disconnect(self, client, userdata, rc): click.echo(f"Disconnected {userdata['client']}, result code: {str(rc)}") def _on_message(self, client, userdata, msg): if hasattr(self, "message_processor"): self.message_processor(client, userdata, msg) else: click.echo(f"Topic: {msg.topic}, Mid: {msg.mid}, Payload: {msg.payload.decode('utf-8')}") def _on_publish(self, client, userdata, mid): click.echo(f"Published by {userdata['client']}, mid: {mid}") def listen(self): try: self.client.loop_forever() except KeyboardInterrupt: click.echo(f"Received KeyboardInterrupt, disconnecting {self.config.mqtt_client}") self.client.disconnect()
def main(): influxdb_client = InfluxDBClient(INFLUXDB_HOST, INFLUXDB_PORT, INFLUXDB_USERNAME, INFLUXDB_PASSWORD, INFLUXDB_DATABASE) #First the database is initialized mqtt_client = MQTTClient( MQTT_CLIENT_ID, userdata=influxdb_client) #Then we create a client object mqtt_client.username_pw_set(MQTT_USERNAME, MQTT_PASSWORD) #and set the username and password for the MQTT client mqtt_client.tls_set() mqtt_client.on_connect = mqtt_connect_callback #We tell the client which functions are to be run on connecting mqtt_client.on_message = mqtt_message_callback #and on receiving a message mqtt_client.connect(MQTT_HOST, MQTT_PORT) #we can connect to the broker with the broker host and port mqtt_client.loop_forever()
def create_client(host, port, username, password): """Creating an MQTT Client Object""" client = MqttClient() if username and password: client.username_pw_set(username=username, password=password) client.connect(host=host, port=port) return client
def connect_mqtt_with_credentials(client: mqtt.Client): settings_env = os.environ.get("MQTT_SETTINGS", None) if settings_env is not None: settings = json.loads(settings_env) else: with open('mqtt_settings.json') as json_settings_file: settings = json.load(json_settings_file) client.username_pw_set(settings["username"], settings["password"]) client.connect(settings["server_url"], settings["port"])
def main(): client = Client(protocol=MQTTv311) client.username_pw_set(USERNAME, password=PASSWORD) client.connect(HOST, port=PORT, keepalive=60) while True: data = read_sensor_data() print(data) client.publish(TOPIC, payload=dumps(data)) sleep(5)
def test_traffic_mqtt(): client = Client(client_id="test") client.on_connect = on_connect client.username_pw_set(username="******", password="******") client.connect(host="52.164.202.250", port=1883) client.loop_start() t.sleep(5) t.sleep(2) client.disconnect() t.sleep(2)
class mqtt_Sub(Task): def on_connect(self, client, userdata, flags, rc): print('on connect rc', rc) def on_connect1(self, client, userdata, flags, rc): print('on connect rc', rc) def on_message(self, client, userdata, msg): device = eval(str(msg.payload)[2:-1]) print(device) sub = SubDevice.objects.create( subdevice_name=device["create_params"][0]["subdevice_name"], subdevice_type=device["create_params"][0]["subdevice_type"], subdevice_position=device["create_params"][0] ["subdevice_position"], subdevice_model=device["create_params"][0]["subdevice_model"], subdevice_remark=device["create_params"][0]["subdevice_remark"]) sub.save() def on_publish(self, client, userdata, mid): print('on publish mid', mid) def on_subscrib(self, client, userdata, mid, qos): print('on subscribe mid', mid) def on_disconnect(self, client, userdata, rc): print("disconnect reconnect later!") def cnct(self): print("连接MQTT...") # 5、整理得到的結果提取 username,password,port,host # try: username = "******" password = "******" port = 1883 host = "10.129.7.199" self.mqttClient = Client(client_id="F1334790") self.mqttClient.on_connect = self.on_connect self.mqttClient.on_message = self.on_message self.mqttClient.on_publish = self.on_publish self.mqttClient.on_subscribe = self.on_subscrib self.mqttClient.on_disconnect = self.on_disconnect self.mqttClient.username_pw_set(username=username, password=password) self.mqttClient.connect(port=port, host=host, keepalive=90) self.mqttClient.loop_start() print("MQTT连接OK ----") # except Exception as e: # print("MQTT连接异常:", e) # time.sleep(3) def get_data(self): self.cnct() self.mqttClient.subscribe(topic="/data/EdgeBox/gateway/create", qos=1)
def loop(self, ip, port, clientId='', username='', password='', **kwargs): if MqttConnect.mqtt: MqttConnect.mqtt.loop_stop() mqtt = Client(clientId) mqtt.username_pw_set(username, password) mqtt.connect(ip, int(port)) mqtt.on_log = self.on_log mqtt.on_connect = self.on_connect mqtt.on_message = self.on_message mqtt.loop_start() MqttConnect.mqtt = mqtt
def setup_mqtt_client(): def on_connect(client, *args): client.subscribe(MQTT_TOPIC) temp_client = Client() temp_client.username_pw_set(**MQTT_CREDS) temp_client.on_connect = on_connect temp_client.on_message = on_message temp_client.connect(MQTT_BROKER, 1883, 60) return temp_client
def main(): influxdb_client = InfluxDBClient(INFLUXDB_HOST, INFLUXDB_PORT, INFLUXDB_USERNAME, INFLUXDB_PASSWORD, INFLUXDB_DATABASE) mqtt_client = MQTTClient(MQTT_CLIENT_ID, userdata=influxdb_client) mqtt_client.username_pw_set(MQTT_USERNAME, MQTT_PASSWORD) mqtt_client.tls_set() mqtt_client.on_connect = mqtt_connect_callback mqtt_client.on_message = mqtt_message_callback mqtt_client.connect(MQTT_HOST, MQTT_PORT) mqtt_client.loop_forever()
def send_to_mqtt(packet, db = "testcargo"): packet['database'] = db client = Client(client_id="test") client.on_connect = on_connect client.username_pw_set(username="******", password="******") client.connect(host="52.164.202.250", port=1883) client.loop_start() t.sleep(5) transmit(client) t.sleep(2) client.disconnect() t.sleep(2)
def create_client(host, port, username, password, cafile=None): """Creating an MQTT Client Object""" client = MqttClient() if username and password: client.username_pw_set(username=username, password=password) if cafile: client.tls_set(ca_certs=cafile) client.connect(host=host, port=port) return client
def create_client(host, port, username, password, clientid, cafile): """Creating an MQTT Client Object""" client = MqttClient(clientid) if username and password: client.username_pw_set(username=username, password=password) try: client.tls_set(ca_certs=cafile) except: print("Proceeding without certificate file") client.connect(host=host, port=port) return client
class Producer: def __init__(self): self.client = Client("sample-producer") self.client.on_connect = self.on_connect self.client.on_publish = self.on_publish self.client.username_pw_set(username=config.RABBITMQ_USERNAME, password=config.RABBITMQ_PASSWORD) self.client.connect(config.RABBITMQ_HOST, config.RABBITMQ_PORT, 60) def on_connect(self, cli, userdata, flags, rc): logging.debug("Connected: " + str(rc)) def on_publish(self, mqttc, metadata, rc): logging.debug("Published: " + str(rc))
class AzureIoT(IotProvider): def __init__(self, iot_provider_cfg): # 1. Set device_name. # 2. Set tls_version. # 3. Set MQTT Protocol version # 4. Call parent class' __init__ self.device_name = iot_provider_cfg["device_name"] self.tls_version = eval( f"ssl.PROTOCOL_TLSv1_{iot_provider_cfg['tls_version']}") self.mqtt_version = eval( f"mqtt.MQTTv{iot_provider_cfg['mqtt_version']}") super().__init__(iot_provider_cfg) def on_connect(self, client, userdata, flags, rc): # Event handler for connection event. Subscribe to topic(s) here. client.subscribe(self.subscribe_topic, qos=0) def on_disconnect(client, userdata, rc): print(f"Disconnected with code {rc}") def onmsg(self, client, userdata, msg): # Wraps core event handler for incoming messages msg_payload = msg.payload super().onmsg(msg_payload) def connect(self): # A connection to iot is established at the beginning and if publish fails self.azure_iot_comm = Client(client_id=self.device_name, protocol=self.mqtt_version) self.azure_iot_comm.on_connect = self.on_connect self.azure_iot_comm.on_disconnect = self.on_disconnect self.azure_iot_comm.on_message = self.onmsg self.azure_iot_comm.username_pw_set( username=f"{self.iot_broker}/{self.device_name}") self.azure_iot_comm.tls_set(self.iot_ca_cert_path, self.iot_client_cert_path, self.iot_client_key_path, tls_version=self.tls_version, cert_reqs=ssl.CERT_REQUIRED) self.azure_iot_comm.connect(self.iot_broker, self.iot_port) self.azure_iot_comm.loop_start() def disconnect(self): self.azure_iot_comm.disconnect() def publish(self, publish_topic, msg_str, qos): # Overriding qos to 0 because Azure doesn't seem to like any other qos self.azure_iot_comm.publish(publish_topic, msg_str, qos=0)
def publish_soil_status_mqtt( analog_signal, soil_wet, ): mqttBroker = "localhost" client = Client("Temperature_Inside") client.username_pw_set(username=uname, password=passwd) client.connect(mqttBroker) moisture_sensor_number = analog_signal + 1 moisture_sensor_name = "MOISTURE" + str(moisture_sensor_number) if soil_wet == 1: payload = "WET SOIL" elif soil_wet == 0: payload = "DRY SOIL" client.publish(moisture_sensor_name, payload) print("Published MQTT " + moisture_sensor_name + ": " + payload)
class MQTTPublisher(): def __init__(self): client_id = os.environ.get('MQTT_CLIENT_ID', socket.gethostname()) self._client = Client(client_id) self._client.enable_logger(logger=logging) user = os.environ.get('MQTT_USER') password = os.environ.get('MQTT_PASSWORD') if user is not None and password is not None: self._client.username_pw_set(username=user, password=password) self._host = os.environ.get('MQTT_HOST', 'localhost') self._port = int(os.environ.get('MQTT_PORT', '1883')) self.connect() def _log_success_sent(self, topic, payload): logging.info(f'sent {payload} to topic {topic}') def _log_failed_sent(self, topic, payload): logging.error(f'failed to send {payload} to topic {topic}') def publish(self, topic, payload, retain): try: result = self._client.publish(topic=topic, payload=payload, retain=retain) if result[0] == 0: self._log_success_sent(topic, payload) else: self._log_failed_sent(topic, payload) time.sleep(5) self.connect() self.publish(topic, payload, retain) except Exception as e: logging.exception(e) time.sleep(5) self.connect() self.publish(topic, payload, retain) def connect(self): try: self._client.connect(self._host, self._port) except Exception as e: logging.exception( [f'failed to connect to MQTT, will retry later', e])
def main(): p = argparse.ArgumentParser() p.add_argument('-a', '--account-path', help='Path to JSON file containing Google account details') args = p.parse_args() google_session = get_google_session(args.account_path) brokers = {} clients = [] reporter = Reporter(google_session) for device_id, device, desc in generate_devices_from_config(): if not device.url.startswith('mqtt://'): continue device_name, host, port, username, password = parse_mqtt_url( device.url) broker = (host, port, username, password) brokers.setdefault(broker, {}).setdefault(device_name, []).append(device) for (host, port, username, password), devices in brokers.items(): client = Client(clean_session=True, userdata=devices) client.on_message = reporter.on_msg if username is not None: client.username_pw_set(username, password) client.connect(host, port) for device_name in devices: # log(f'{device_name} --> STATE') client.subscribe(f'stat/{device_name}/RESULT') client.publish(f'cmnd/{device_name}/state') client.loop_start() clients.append(client) try: while 1: time.sleep(5) except KeyboardInterrupt: print('Ctrl-C') for client in clients: client.loop_stop()
class Consumer: def __init__(self): self.client = Client("sample-subscriber") self.client.on_connect = self.on_connect self.client.on_message = self.on_message self.client.on_subscribe = self.on_subscribe self.client.username_pw_set(username=config.RABBITMQ_USERNAME, password=config.RABBITMQ_PASSWORD) self.client.connect(config.RABBITMQ_HOST, config.RABBITMQ_PORT, 60) self.client.subscribe(config.RABBITMQ_TOPIC) def on_connect(self, client, userdata, flags, rc): logging.debug("Connected with result code " + str(rc)) def on_message(self, client, userdata, msg): logging.info("Received: " + str(msg.payload.decode())) def on_subscribe(self, client, obj, mid, granted_qos): logging.debug("Subscribed: " + str(mid) + " " + str(granted_qos))
class Messenger(object): """ MQTT client for Herald transport. """ def __init__(self, peer): """ Initialize client :param peer: The peer behind the MQTT client. :return: """ self.__peer = peer self.__mqtt = MqttClient() self.__mqtt.on_connect = self._on_connect self.__mqtt.on_disconnect = self._on_disconnect self.__mqtt.on_message = self._on_message self.__callback_handler = None self.__WILL_TOPIC = "/".join( (TOPIC_PREFIX, peer.app_id, RIP_TOPIC)) def __make_uid_topic(self, subtopic): """ Constructs a complete UID topic. :param subtopic: The UID :return: Fully qualified topic :rtype : str """ return "/".join( (TOPIC_PREFIX, self.__peer.app_id, UID_TOPIC, subtopic)) def __make_group_topic(self, subtopic): """ Constructs a complete group topic. :param subtopic: The group name :return: Fully qualified topic :rtype : str """ return "/".join( (TOPIC_PREFIX, self.__peer.app_id, GROUP_TOPIC, subtopic)) def __handle_will(self, message): if self.__callback_handler and self.__callback_handler.on_peer_down: self.__callback_handler.on_peer_down( message.payload.decode('utf-8')) else: _log.debug("Missing callback for on_peer_down.") def _on_connect(self, *args, **kwargs): """ Handles a connection-established event. :param args: unnamed arguments :param kwargs: named arguments :return: """ _log.info("Connection established.") _log.debug("Subscribing for topic %s.", self.__make_uid_topic(self.__peer.uid)) self.__mqtt.subscribe(self.__make_uid_topic(self.__peer.uid)) self.__mqtt.subscribe(self.__make_group_topic("all")) self.__mqtt.subscribe(self.__WILL_TOPIC) for group in self.__peer.groups: _log.debug("Subscribing for topic %s.", self.__make_group_topic(group)) self.__mqtt.subscribe(self.__make_group_topic(group)) if self.__callback_handler and self.__callback_handler.on_connected: self.__callback_handler.on_connected() else: _log.warning("Missing callback for on_connect.") def _on_disconnect(self, *args, **kwargs): """ Handles a connection-lost event. :param args: unnamed arguments :param kwargs: named arguments :return: """ _log.info("Connection lost.") if self.__callback_handler and self.__callback_handler.on_disconnected: self.__callback_handler.on_disconnected() def _on_message(self, client, data, message): """ Handles an incoming message. :param client: the client instance for this callback :param data: the private user data :param message: an instance of MQTTMessage :type message: paho.mqtt.client.MQTTMessage :return: """ _log.info("Message received.") if message.topic == self.__WILL_TOPIC: self.__handle_will(message) return if self.__callback_handler and self.__callback_handler.on_message: self.__callback_handler.on_message(message.payload.decode('utf-8')) else: _log.warning("Missing callback for on_message.") def fire(self, peer_uid, message): """ Sends a message to another peer. :param peer_uid: Peer UID :param message: Message content :return: """ self.__mqtt.publish( self.__make_uid_topic(peer_uid), message, 1 ) def fire_group(self, group, message): """ Sends a message to a group of peers. :param group: Group's name :param message: Message content :return: """ self.__mqtt.publish( self.__make_group_topic(group), message, 1 ) def set_callback_listener(self, listener): """ Sets callback listener. :param listener: the listener :return: """ self.__callback_handler = listener def login(self, username, password): """ Set credentials for an MQTT broker. :param username: Username :param password: Password :return: """ self.__mqtt.username_pw_set(username, password) def connect(self, host, port): """ Connects to an MQTT broker. :param host: broker's host name :param port: broker's port number :return: """ _log.info("Connecting to MQTT broker at %s:%s ...", host, port) self.__mqtt.will_set(self.__WILL_TOPIC, self.__peer.uid, 1) self.__mqtt.connect(host, port) self.__mqtt.loop_start() def disconnect(self): """ Diconnects from an MQTT broker. :return: """ _log.info("Disconnecting from MQTT broker...") self.__mqtt.publish(self.__WILL_TOPIC, self.__peer.uid, 1) self.__mqtt.loop_stop() self.__mqtt.disconnect()
class MQTTEventSink(EventSink): def __init__(self, broker, topic="iot-1/d/%012x/evt/%s/json", hostname=None, hostport=1883, username=None, password=None, keepalive=60): EventSink.__init__(self, broker) self._client = Paho() self._client.on_connect = \ lambda mosq, obj, rc: self._on_connect(mosq, obj, rc) self._client.on_disconnect = \ lambda mosq, obj, rc: self._on_disconnect(mosq, obj, rc) self._client.on_publish = \ lambda mosq, obj, mid: self._on_publish(mosq, obj, mid) self._topic_format = topic self._topic = self._topic_format % (0, "%s") self._hostname = hostname self._hostport = hostport self._username = username self._password = password self._keepalive = keepalive self._loopflag = False self._neta = None def _on_connect(self, mosq, obj, rc): self._topic = self._topic_format % (get_mac(), "%s") log.debug("MQTT publisher connected: " + str(rc)) def _on_disconnect(self, mosq, obj, rc): log.debug("MQTT publisher disconnected: " + str(rc)) def _on_publish(self, mosq, obj, mid): #log.debug("MQTT publisher published: " + str(mid)) pass def _try_connect(self): if self._username is not None and self._password is not None: self._client.username_pw_set(self._username, self._password) try: self._client.connect(self._hostname, self._hostport, self._keepalive) except socket.gaierror: return False self._client.loop_start() self._loopflag = True return True def on_start(self): return self._try_connect() def send(self, encoded_event): # Fill in the blank "%s" left in self._topic import json # extract the actual topic string event = json.loads(encoded_event) topic_event_type = event["d"]["event"] topic = self._topic % topic_event_type # Check to see if event is from neighbors # and need to be published to Mqtt server if "published" in event["d"]: if event["d"]["published"] == 1: return True else: del event["d"]["published"] # Publish message res, mid = self._client.publish(topic, encoded_event) if res == paho.mqtt.client.MQTT_ERR_SUCCESS: log.info("MQTT message published to " + topic) elif res == paho.mqtt.client.MQTT_ERR_NO_CONN: log.error("MQTT publisher failure: No connection") return False else: log.error("MQTT publisher failure: Unknown error") return False return True def check_available(self, event): if self._neta is not None and not self._neta: return False if not self._loopflag: if not self._try_connect(): log.error("MQTT publisher failure: Cannot connect") return False return True def on_event(self, event, topic): et = event.get_type() ed = event.get_raw_data() if et == "internet_access": self._neta = ed def encode_event(self, event): # return event.to_json() return json.dumps({"d": event.to_map()})
class JMSClient(object): """Class JMSClient """ _mh = None _client = None _host = None _port = None _user = None _passw = None _verbose = None _is_connected = None _messages = [] def __init__(self, verbose=False): """Class constructor Called when the object is initialized Args: verbose (bool): verbose mode """ try: self._mh = MasterHead.get_head() self._client = Client() self._client.on_message = self._on_message self._verbose = verbose if (self._verbose): self._client.on_log = self._on_log except MQTTException as ex: self._mh.demsg('htk_on_error', ex, self._mh.fromhere()) @property def client(self): """ MQTT client property getter """ return self._client @property def host(self): """ server host property getter """ return self._host @property def port(self): """ server port property getter """ return self._port @property def user(self): """ username property getter """ return self._user @property def passw(self): """ user password property getter """ return self._passw @property def verbose(self): """ verbose property getter """ return self._verbose @property def is_connected(self): """ is_connected property getter """ return self._is_connected def _on_log(self, client, obj, level, string): """ Callback for on_log event """ print(string) def _on_message(self, client, obj, msg): """ Callback for on_message event """ self._messages.append(msg.payload.decode()) def connect(self, host, port=1883, user=None, passw=None, timeout=10): """Method connects to server Args: host (str): hostname port (str): port user (str): username passw (str): password timeout (int): timeout Returns: bool: result Raises: event: jms_before_connect event: jms_after_connected """ try: msg = 'host:{0}, port:{1}, user:{2}, passw:{3}, timeout:{4}'.format( host, port, user, passw, timeout) self._mh.demsg('htk_on_debug_info', self._mh._trn.msg( 'htk_jms_connecting', msg), self._mh.fromhere()) ev = event.Event( 'jms_before_connect', host, port, user, passw, timeout) if (self._mh.fire_event(ev) > 0): host = ev.argv(0) port = ev.argv(1) user = ev.argv(2) passw = ev.argv(3) timeout = ev.argv(4) self._host = host self._port = port self._user = user self._passw = passw if (ev.will_run_default()): if (self._user != None): self._client.username_pw_set(self._user, self._passw) setdefaulttimeout(timeout) self._client.connect(self._host, self._port) self._is_connected = True self._mh.demsg('htk_on_debug_info', self._mh._trn.msg( 'htk_jms_connected'), self._mh.fromhere()) ev = event.Event('jms_after_connect') self._mh.fire_event(ev) return True except (MQTTException, error, ValueError) as ex: self._mh.demsg('htk_on_error', ex, self._mh.fromhere()) return False def disconnect(self): """Method disconnects from server Args: none Returns: bool: result """ try: self._mh.demsg('htk_on_debug_info', self._mh._trn.msg( 'htk_jms_disconnecting'), self._mh.fromhere()) if (not self._is_connected): self._mh.demsg('htk_on_warning', self._mh._trn.msg( 'htk_jms_not_connected'), self._mh.fromhere()) return False else: self._client.disconnect() self._is_connected = False self._mh.demsg('htk_on_debug_info', self._mh._trn.msg( 'htk_jms_disconnected'), self._mh.fromhere()) return True except (MQTTException, error, ValueError) as ex: self._mh.demsg('htk_on_error', ex, self._mh.fromhere()) return False def send(self, destination_name, message): """Method sends message Args: destination_name (str): topic name message (str): message Returns: bool: result Raises: event: jms_before_send event: jms_after_send """ try: msg = 'destination_name:{0}, message:{1}'.format( destination_name, message) self._mh.demsg('htk_on_debug_info', self._mh._trn.msg( 'htk_jms_sending_msg', msg), self._mh.fromhere()) if (not self._is_connected): self._mh.demsg('htk_on_warning', self._mh._trn.msg( 'htk_jms_not_connected'), self._mh.fromhere()) return False ev = event.Event('jms_before_send', destination_name, message) if (self._mh.fire_event(ev) > 0): destination_name = ev.argv(0) message = ev.argv(1) if (ev.will_run_default()): res, id = self._client.publish(destination_name, message) if (res != 0): self._mh.demsg('htk_on_error', self._mh._trn.msg( 'htk_jms_sending_error'), self._mh.fromhere()) self._mh.demsg('htk_on_debug_info', self._mh._trn.msg( 'htk_jms_msg_sent'), self._mh.fromhere()) ev = event.Event('jms_after_send') self._mh.fire_event(ev) return True except (MQTTException, error, ValueError) as ex: self._mh.demsg('htk_on_error', ex, self._mh.fromhere()) return False def receive(self, destination_name, cnt=1, timeout=10): """Method receives messages Args: destination_name (str): queue name cnt (int): count of messages timeout (int): timeout to receive message Returns: list: messages Raises: event: jms_before_receive event: jms_after_receive """ try: msg = 'destination_name:{0}, count:{1}'.format( destination_name, cnt) self._mh.demsg('htk_on_debug_info', self._mh._trn.msg( 'htk_jms_receiving_msg', msg), self._mh.fromhere()) if (not self._is_connected): self._mh.demsg('htk_on_warning', self._mh._trn.msg( 'htk_jms_not_connected'), self._mh.fromhere()) return None ev = event.Event('jms_before_receive', destination_name, cnt) if (self._mh.fire_event(ev) > 0): destination_name = ev.argv(0) cnt = ev.argv(1) if (ev.will_run_default()): res, id = self._client.subscribe(destination_name) if (res != 0): self._mh.demsg('htk_on_error', self._mh._trn.msg( 'htk_jms_sending_error'), self._mh.fromhere()) return None res = 0 cnt_before = 0 start = time() while (res == 0): res = self._client.loop() cnt_after = len(self._messages) if (cnt_after > cnt_before and cnt_after < cnt): cnt_before = cnt_after elif (cnt_after == cnt or time() > start + timeout): res = -1 messages = self._messages self._client.unsubscribe(destination_name) self._messages = [] self._mh.demsg('htk_on_debug_info', self._mh._trn.msg( 'htk_jms_msg_received', len(messages)), self._mh.fromhere()) ev = event.Event('jms_after_receive') self._mh.fire_event(ev) return messages except (MQTTException, error, ValueError) as ex: self._mh.demsg('htk_on_error', ex, self._mh.fromhere()) return None
class MqttApplication(Application): """ An abstract base Application for running some type of MQTT client-based Application. """ def __init__(self, broker, hostname=None, hostport=1883, username=None, password=None, keepalive=60, **kwargs): super(MqttApplication, self).__init__(broker=broker, **kwargs) self._client = MqttClient() self._client.on_connect = \ lambda mqtt_client, obj, rc: self._on_connect(mqtt_client, obj, rc) self._client.on_disconnect = \ lambda mqtt_client, obj, rc: self._on_disconnect(mqtt_client, obj, rc) self._client.on_publish = \ lambda mqtt_client, obj, mid: self._on_publish(mqtt_client, obj, mid) self._client.on_subscribe = \ lambda mqtt_client, userdata, mid, qos: self._on_subscribe(mqtt_client, mid, qos) self._client.on_message = \ lambda mqtt_client, userdata, msg: self._on_message(mqtt_client, msg.payload, msg.topic, msg.qos, msg.retain) self._hostname = hostname self._hostport = hostport self._username = username self._password = password self._keepalive = keepalive self._is_connected = False @property def is_connected(self): return self._is_connected # Your API for pub-sub via MQTT: def mqtt_publish(self, raw_data, topic, **kwargs): """ Publishes the given data to the specified topic. :param raw_data: :type raw_data: str :param topic: :param kwargs: e.g. qos, retain; passed to self._client.publish :return: err_code, msg_id """ return self._client.publish(topic, raw_data, **kwargs) def mqtt_subscribe(self, topic, **kwargs): """ Subscribes to the specified topic. :param topic: :param kwargs: e.g. qos; passed to self._client.publish :return: err_code, msg_id """ return self._client.subscribe(topic, **kwargs) # To make use of either publish or subscribe, make sure to implement these! def _on_publish(self, mqtt_client, obj, mid): log.debug("MQTT app %s published msg %d: %s" % (self.name, mid, obj)) def _on_subscribe(self, mqtt_client, mid, qos): log.debug("MQTT app %s subscribe response msg %d: qos=%s" % (self.name, mid, qos)) def _on_message(self, mqtt_client, payload, topic, qos, retain): """Called when a message arrives on a topic we subscribed to.""" log.debug("MQTT app %s received message on topic %s: %s" % (self.name, topic, payload)) # You may want to override these functions in your concrete Application, but make sure to call super()! def _on_connect(self, mqtt_client, obj, rc): log.debug("MQTT app %s connected: %s" % (self.name, str(rc))) # need to start AFTER connecting, which is async! self._is_connected = True def _on_disconnect(self, mqtt_client, obj, rc): # sink will try reconnecting once EventReporter queries if it's available. self._is_connected = False log.debug("MQTT app %s disconnected: %s" % (self.name, str(rc))) def _try_connect(self): if self._username is not None and self._password is not None: self._client.username_pw_set(self._username, self._password) try: # NOTE: this is an async connection! self._client.connect(self._hostname, self._hostport, self._keepalive) self._client.loop_start() except socket.gaierror: return False return True def on_start(self): super(MqttApplication, self).on_start() return self._try_connect()