如何在Django 3.1中实现Paho python客户端 [英] How to implement paho python client in Django 3.1

查看:82
本文介绍了如何在Django 3.1中实现Paho python客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自这个SO问题,我实现了一个订阅者我的Django项目中的客户端如下:

from this SO question, I implemented a subscriber client in my Django project as following:

mqtt.py 中,我创建了一个客户端,并连接到本地代理并订阅了一个主题.

in mqtt.py, I create a client and connect to a local broker and subscribe to a topic.

#myapp/mqtt.py:
import paho.mqtt.client as paho
import json
import django
django.setup()
from .models import Temperature, Turbidity, Refined_fuels, Crude_oil
from datetime import datetime

# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
    print("CONNACK received with code %d." % (rc))
    client.subscribe("sensor/temp", qos=0)


def on_subscribe(client, userdata, mid, granted_qos):
    print("Subscribed: "+str(mid)+" "+str(granted_qos))

# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
    print(msg.topic+", "+'QOS: ' +str(msg.qos)+",\n"+str(msg.payload, 'utf-8'))
    message_dict = json.loads(msg.payload)
    
    now = datetime.now()
    captured_timestamp = datetime.utcfromtimestamp(int(message_dict['Timestamp'])).strftime('%Y-%m-%d %H:%M:%S')
    print('timestamp: ', captured_timestamp )
    if message_dict['Temperature'] and message_dict['D850'] and message_dict['D280']:
            
        temp = Temperature(captured_timestamp=captured_timestamp, data=message_dict['Temperature'], received_timestamp=now)
        temp.save()
        refined_fuels = Refined_fuels(captured_timestamp=captured_timestamp, data=float(message_dict['D850']), received_timestamp=now)
        refined_fuels.save()
        crude_oil = Crude_oil(captured_timestamp=captured_timestamp, data=float(message_dict['D280']), received_timestamp=now)
        crude_oil.save()

# defining client
client = paho.Client(client_id="testSubscriber",
                     clean_session=True, userdata=None,
                     protocol=paho.MQTTv311)

# adding callbacks to client
client.on_connect = on_connect
client.on_subscribe = on_subscribe
client.on_message = on_message

client.connect(host="localhost", port=1883, keepalive=60,
               bind_address="" )

__ init __.py 中,我称呼客户端的 loop_start():

in __init__.py I call the loop_start() of client:

# myapp/__init__.py
from . import mqtt

mqtt.client.loop_start()

对于发布商客户端,我使用了 paho C客户端.对于经纪人,我使用了 hivemq-4.5.1 企业试用版.而且,我正在ubuntu 18.04上运行该项目.

For publisher client I used paho C client. and for broker I used hivemq-4.5.1 enterprise trial version. And, I'm running the project on ubuntu 18.04.

现在,当我运行Django服务器( python manage.py runserver )时,它会继续调用 on_connect()方法并继续 on_connect()方法,但是服务器未运行,我无法从 localhost:8000 访问该项目.

now, When I run the Django server(python manage.py runserver), it keeps calling the on_connect() method and continues on_connect() method, But the server is not running and I cant access the project from localhost:8000.

这是 on_connect() on_subscribe()方法继续打印其消息之前的django错误:

this is the django error before the on_connect() and on_subscribe() methods keep printing their messages:

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/usr/local/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/home/shahriar/webapp/venv/lib/python3.8/site-packages/django/utils/autoreload.py", line 53, in wrapper
    fn(*args, **kwargs)
  File "/home/shahriar/webapp/venv/lib/python3.8/site-packages/django/core/management/commands/runserver.py", line 110, in inner_run
    autoreload.raise_last_exception()
  File "/home/shahriar/webapp/venv/lib/python3.8/site-packages/django/utils/autoreload.py", line 76, in raise_last_exception
    raise _exception[1]
  File "/home/shahriar/webapp/venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 357, in execute
    autoreload.check_errors(django.setup)()
  File "/home/shahriar/webapp/venv/lib/python3.8/site-packages/django/utils/autoreload.py", line 53, in wrapper
    fn(*args, **kwargs)
  File "/home/shahriar/webapp/venv/lib/python3.8/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/shahriar/webapp/venv/lib/python3.8/site-packages/django/apps/registry.py", line 94, in populate
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Application labels aren't unique, duplicates: graphs

这是正常的代理日志,没有显示错误:

and this is the broker log that is ok and shows no error:

2021-03-01 10:06:38,059 INFO  - Sent PUBLISH to client 'testSubscriber' on topic 'sensor/temp': Payload: '{ "Timestamp": 1609446782, "Temperature": "30.13", "D850": "102.48", "D280": "4845.83" }', QoS: '0', Retained: 'false'
2021-03-01 10:06:38,075 INFO  - Received CONNECT from client 'testSubscriber': Protocol version: 'V_3_1_1', Clean Start: 'true', Session Expiry Interval: '0'
2021-03-01 10:06:38,207 INFO  - Sent CONNACK to client 'testSubscriber': Reason Code: 'SUCCESS', Session Present: 'false'
2021-03-01 10:06:38,209 INFO  - Received SUBSCRIBE from client 'testSubscriber': Topics: { [Topic: 'sensor/temp', QoS: '0'] }
2021-03-01 10:06:38,209 INFO  - Sent SUBACK to client 'testSubscriber': Suback Reason Codes: { [Reason Code: 'GRANTED_QOS_0'] }
2021-03-01 10:06:39,056 INFO  - Received PUBREL from client 'testPublisher': Reason Code: 'SUCCESS'
2021-03-01 10:06:39,057 INFO  - Sent PUBCOMP to client 'testPublisher': Reason Code: 'SUCCESS'
2021-03-01 10:06:39,131 INFO  - Received CONNECT from client 'testSubscriber': Protocol version: 'V_3_1_1', Clean Start: 'true', Session Expiry Interval: '0'
2021-03-01 10:06:39,158 INFO  - Received PUBLISH from client 'testPublisher' for topic 'sensor/temp': Payload: '{ "Timestamp": 1609446783, "Temperature": "30.13", "D850": "102.48", "D280": "4845.83" }', QoS: '2', Retained: 'false'
2021-03-01 10:06:39,161 INFO  - Sent PUBREC to client 'testPublisher': Reason Code: 'SUCCESS'
2021-03-01 10:06:39,200 INFO  - Sent CONNACK to client 'testSubscriber': Reason Code: 'SUCCESS', Session Present: 'false'
2021-03-01 10:06:39,201 INFO  - Received SUBSCRIBE from client 'testSubscriber': Topics: { [Topic: 'sensor/temp', QoS: '0'] }
2021-03-01 10:06:39,203 INFO  - Sent SUBACK to client 'testSubscriber': Suback Reason Codes: { [Reason Code: 'GRANTED_QOS_0'] }
2021-03-01 10:06:40,162 INFO  - Received PUBREL from client 'testPublisher': Reason Code: 'SUCCESS'
2021-03-01 10:06:40,162 INFO  - Sent PUBCOMP to client 'testPublisher': Reason Code: 'SUCCESS'
2021-03-01 10:06:40,262 INFO  - Received PUBLISH from client 'testPublisher' for topic 'sensor/temp': Payload: '{ "Timestamp": 1609446784, "Temperature": "30.13", "D850": "102.48", "D280": "4845.83" }', QoS: '2', Retained: 'false'
2021-03-01 10:06:40,263 INFO  - Sent PUBREC to client 'testPublisher': Reason Code: 'NO_MATCHING_SUBSCRIBERS'
2021-03-01 10:06:41,262 INFO  - Received PUBREL from client 'testPublisher': Reason Code: 'SUCCESS'
2021-03-01 10:06:41,263 INFO  - Sent PUBCOMP to client 'testPublisher': Reason Code: 'SUCCESS'

现在的问题是,Django中的paho python客户端实现是否错误?

由于我想将消息存储在数据库中,因此我将模型导入到 mqtt.py 中,这导致了错误:

Since I wanted to store messages in DB, I imported the models in mqtt.py and that caused an error:

提高AppRegistryNotReady(尚未加载应用程序.")django.core.exceptions.AppRegistryNotReady:应用尚未加载.

raise AppRegistryNotReady("Apps aren't loaded yet.") django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

过来.我放

import django
django.setup()

在导入 mqtt.py 中的模型之前,就可以解决该问题.据我所知,它会启动其中 mqtt.py 位于其中的应用程序,该应用程序位于主应用程序之前.因此,django看到了该应用程序的两个实例,并引发了此错误:

before importing models in mqtt.py and that solved the problem. to the best of my knowledge, it starts the app which mqtt.py is inside of it before the main app. So the django see two instance of that app and throws this error:

不正确地进行配置(django.core.exceptions.ImproperlyConfigured:应用程序标签不是唯一的,重复的是:图

raise ImproperlyConfigured( django.core.exceptions.ImproperlyConfigured: Application labels aren't unique, duplicates: graphs

因此,我知道我的实现是错误的.但是如何纠正呢?

So, I understand that my implementation is wrong. but how to correct it?

推荐答案

在对您的内容进行了类似的设置后,我遇到了类似的问题: https://stackoverflow.com/a/41017210/13001393

I had a similar problem after following a similar setup as to what you have, here: https://stackoverflow.com/a/41017210/13001393

重复连接的解决方案是由于在MQTT服务器上进行了一项设置,其中用户名设置为clientid.

The resolution for the repeated connection was due to a setting on the MQTT server where the user name is set as the clientid.

mosquitto.conf

# Set use_username_as_clientid to true to replace the clientid that a client
# connected with with its username. This allows authentication to be tied to
# the clientid, which means that it is possible to prevent one client
# disconnecting another by using the same clientid.
# If a client connects with no username it will be disconnected as not
# authorised when this option is set to true.
# Do not use in conjunction with clientid_prefixes.
# See also use_identity_as_username.
use_username_as_clientid true

您可能要检查该设置.并确保您为每个mqtt客户端使用不同的用户名.

You may want to check that setting. and make sure that you are using different usernames for each mqtt client.

这篇关于如何在Django 3.1中实现Paho python客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆