在Python中运行基于mqqt的代码时,错误对象没有属性

kkih6yb8  于 2023-02-11  发布在  Python
关注(0)|答案(1)|浏览(127)

我在运行python代码时遇到以下错误。运行www.example.com时出错Main.py
启动设备创建和注册过程。
为他们各自的房间创建照明设备。追溯(最近调用最后):文件"C:\用户\基里提\下载\课程\解决方案\main.py",第21行,在灯光设备1 =灯光设备("灯光1","厨房")文件"C:\用户\基里提\下载\课程\解决方案\灯光设备. py",第24行,在初始化自我.客户端. on_disconnect =自我._on_disconnect属性错误:"Light_Device"对象没有属性"_on_disconnect"
进程已完成,退出代码为1

############################# LightDevice.py-启动##########################导入json导入paho.mqtt.客户端作为mqtt导入paho.mqtt.发布作为发布

主机="本地主机"端口= 1883
类灯光设备():

# setting up the intensity choices for Smart Light Bulb
_INTENSITY = ["LOW", "HIGH", "MEDIUM", "OFF"]

def __init__(self, device_id, room):
    # Assigning device level information for each of the devices.
    self._device_id = device_id
    self._room_type = room
    self._light_intensity = self._INTENSITY[0]
    self._device_type = "LIGHT"
    self._device_registration_flag = False
    self.client = mqtt.Client(self._device_id)
    self.client.on_connect = self._on_connect
    self.client.on_message = self._on_message
    self.client.on_disconnect = self._on_disconnect
    self.client.connect(HOST, PORT, keepalive=60)
    self.client.loop_start()
    self._register_device(self._device_id, self._room_type, self._device_type)
    self._switch_status = "OFF"

def _register_device(self, device_id, room_type, device_type):
    request = {"type": "register", "flag": "SYN", "device_id": device_id, "room_type": room_type,
               "device_type": device_type}
    topic_name = "home"
    publish.single(topic=topic_name, payload=json.dumps(request), hostname=HOST)

# Connect method to subscribe to various topics.
def _on_connect(self, client, userdata, flags, result_code):
    client.subscribe(self._device_id, 0)
    client.subscribe(self._device_type, 0)
    client.subscribe(self._room_type, 0)
    client.subscribe(self._room_type + "/" + self._device_type, 0)
    client.subscribe("all", 0)

# method to process the recieved messages and publish them on relevant topics
# this method can also be used to take the action based on received commands
def _on_message(self, client, userdata, msg):
    reqString = "Light Device - " + self._device_id + " : " + str(msg.payload)
    print(reqString)
    request = json.loads(msg.payload)

    if request['type'] == 'set':
        if request['flag'] == 'switch_state':
            self._set_switch_status(request['value'])
        if request['flag'] == 'light_intensity':
            self._set_light_intensity(request['value'])

        response = {"type": "set", "status": 0, "device_id": self._device_id}
        topic_name = "home"
        publish.single(topic=topic_name, payload=json.dumps(response), hostname=HOST)

    if request['type'] == 'status':
        self.get_status()

# Getting the current switch status of devices
def _get_switch_status(self):
    return self._switch_status

# Setting the the switch of devices
def _set_switch_status(self, switch_state):
    self._switch_status = switch_state

# Getting the light intensity for the devices
def _get_light_intensity(self):
    return self._light_intensity

# Setting the light intensity for devices
def _set_light_intensity(self, light_intensity):
    self._light_intensity = light_intensity

############################ 灯光设备. py-结束##################

谢谢你,

djmepvbi

djmepvbi1#

def _on_disconnect(自身、客户端、用户数据、标志、结果代码):通过
添加此功能

相关问题