<commit_before>#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
mqttclient类
"""

import json
import logging
import threading
import paho.mqtt.client as mqtt

logger = logging.getLogger('plugin')


class MQTTClient(object):
    def __init__(self, mqtt_config, network_name):
        self.channel = None
        self.mqtt_config = mqtt_config
        self.server_addr = mqtt_config.get("server")
        self.server_port = mqtt_config.get("port")
        self.client_id = mqtt_config.get("client_id")
        self.gateway_topic = mqtt_config.get("gateway_topic")
        self.thread = None
        self.network_name = network_name

        # The callback for when the client receives a CONNACK response from the server.
        def on_connect(client, userdata, rc):
            logger.info("Connected with result code " + str(rc))
            # Subscribing in on_connect() means that if we lose the connection and
            # reconnect then subscriptions will be renewed.
            client.subscribe("%s/#" % self.network_name)

        # The callback for when a PUBLISH message is received from the server.
        def on_message(client, userdata, msg):
            logger.info("收到数据消息" + msg.topic + " " + str(msg.payload))
            # 消息只包含device_cmd，为json字符串
            try:
                cmd_msg = json.loads(msg.payload)
            except Exception, e:
                logger.error("消息内容错误，%r" % msg.payload)
                return

            # 调用channel处理指令
            if self.channel is not None:
                self.channel.process_cmd(cmd_msg)

            return

        self.mqtt_client = mqtt.Client()
        self.mqtt_client.on_connect = on_connect
        self.mqtt_client.on_message = on_message

    @staticmethod
    def check_config(mqtt_params):
        if "server" not in mqtt_params \
                or "port" not in mqtt_params \
                or "client_id" not in mqtt_params\
                or "gateway_topic" not in mqtt_params:<commit_msg>模版代码同步<commit_after>#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
mqttclient类
"""

import json
import logging
import threading
import paho.mqtt.client as mqtt

logger = logging.getLogger('plugin')


class MQTTClient(object):
    def __init__(self, mqtt_config, network_name):
        self.channel = None
        self.mqtt_config = mqtt_config
        self.server_addr = mqtt_config.get("server")
        self.server_port = mqtt_config.get("port")
        self.client_id = mqtt_config.get("client_id")
        self.gateway_topic = mqtt_config.get("gateway_topic")
        self.thread = None
        self.network_name = network_name

        # The callback for when the client receives a CONNACK response from the server.
        def on_connect(client, userdata, rc):
            logger.info("Connected with result code " + str(rc))
            # Subscribing in on_connect() means that if we lose the connection and
            # reconnect then subscriptions will be renewed.
            client.subscribe("%s/#" % self.network_name)

        # The callback for when a PUBLISH message is received from the server.
        def on_message(client, userdata, msg):
            logger.info("收到数据消息" + msg.topic + " " + str(msg.payload))
            # 消息只包含device_cmd，为json字符串
            try:
                cmd_msg = json.loads(msg.payload)
            except Exception, e:
                logger.error("消息内容错误，%r" % msg.payload)
                return

            # 调用channel处理指令
            if self.channel is not None:
                self.channel.process_cmd(cmd_msg)

            return

        self.mqtt_client = mqtt.Client(client_id=self.client_id)
        self.mqtt_client.on_connect = on_connect
        self.mqtt_client.on_message = on_message

    @staticmethod
    def check_config(mqtt_params):
        if "server" not in mqtt_params \
                or "port" not in mqtt_params \
                or "client_id" not in mqtt_params\
                or "gateway_topic" not in mqtt_params:
{'fraction_non_alphanumeric': 0.0903387703889586, 'fraction_numerical': 0.0005018820577164366, 'mean_word_length': 2.1990369181380416, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1024471', 'n_tokens_mistral': 1248, 'n_tokens_neox': 1241, 'n_words': 289}