[英]Is there a way to send messages to MQTT mosquitto broker when a database change happens?
I am working on a project which is eventually going to be a website displaying device data with the option to control these devices. 我正在做一个项目,该项目最终将是一个显示设备数据并可以选择控制这些设备的网站。 I am currently working on the code in python using Paho MQTT Python library.
我目前正在使用Paho MQTT Python库在python中编写代码。 I successfully managed to send and receieve ecrypted messages via mosquitto broker.
我成功地通过mosquitto代理成功发送和接收了加密的消息。 I have created a client which is a sort of monitoring client that subscribes to a wildcard topic in order to receieve all messages going through the broker and is eventually going to store them into a database.
我创建了一个客户端,它是一种监视客户端,它订阅了通配符主题,以便接收通过代理传递的所有消息,并最终将它们存储到数据库中。 I am using django 2.1.5, mosquitto broker 1.5, working in python.
我正在使用Django 2.1.5,mosquitto broker 1.5,在python中工作。
The problem I am facing right now is that I can't figure out a way to send messages to the mosquitto broker as soon as a database change happens. 我现在面临的问题是,一旦数据库发生更改,我就找不到一种将消息发送到mosquitto代理的方法。 Let's say I have a website that shows all connected devices and I want to turn one off, I'd click a button, that would change a database table, then a message has to be send to the broker to notify the device that it should switch off.
假设我有一个网站,其中显示了所有已连接的设备,并且我想将其关闭,然后单击一个按钮,这将更改数据库表,然后必须向代理发送一条消息,以通知该设备应关掉。 I know I can make the website a client, which would then use MQTT over websockets but that is not a desirable option.
我知道我可以使该网站成为客户端,然后在Websocket上使用MQTT,但这不是理想的选择。
I tried django signals, which seems to work fine. 我尝试了django信号,这似乎工作正常。 The problem is that the function that the django signal triggers creates a new instance of a new mqtt client.
问题在于django信号触发的函数会创建新的mqtt客户端的新实例。 I would like the already running monitoring client to send the message, that is trigger one of its defined functions which would send the message whenever a signal is sent.
我希望已经运行的监视客户端发送消息,即触发其定义的功能之一,该功能将在发送信号时发送消息。
class Post(models.Model):
title = models.CharField(max_length=50)
def __str__(self):
return self.title
def save_post(sender, instance, **kwargs):
message = {
"client_id": "abc",
"message": "Created new model: " + str(instance),
}
publish.single("house/StateServer/receive",
payload=json.dumps(message),
port=8081, hostname="localhost", retain=False,
auth = {'username':"abc", 'password':"abc"},
client_id="abc",tls = {"ca_certs":"","certfile":"","keyfile":"",})
post_save.connect(save_post, sender=Post)
I would like the "save_post" function to trigger a function of the monitoring client that is running 24/7 and is stored in a different file, let's say "monitor.py". 我希望“ save_post”函数触发运行24/7并存储在不同文件(例如“ monitor.py”)中的监视客户端的功能。 This function of the monitoring client would receieve the desired data: the payload of the message and the topic of the end device, and send it to the mosquitto broker.
监视客户端的此功能将接收所需的数据:消息的有效负载和终端设备的主题,并将其发送给mosquitto代理。
I recently had to do something similar from a django post_save. 我最近不得不从django post_save做类似的事情。 In my case, the equivalent "different file monitor.py" running as a different process launches a thread running a simple SimpleHTTPServer which does all the work of publishing.
就我而言,作为不同进程运行的等效“不同文件monitor.py”将启动运行简单SimpleHTTPServer的线程,该线程完成所有发布工作。 This allows you to do things asynchrounosly as well since your SimpleHTTPServer could add the received data to be published to a queue and let another thread handle the publish part while your POST handler returns right away.
由于SimpleHTTPServer可以将接收到的要发布的数据添加到队列中,并让另一个线程处理POST处理程序,而POST处理程序立即返回,因此这也使您可以随意处理事务。
For reference there is an SO about json and simplehttpserver here: Reading JSON from SimpleHTTPServer Post data 作为参考,这里有关于json和simplehttpserver的SO: 从SimpleHTTPServer读取JSON发布数据
Yes you can do it in post_save
signal See this answer . 是的,您可以在
post_save
信号中执行此操作。请参post_save
答案 。 But I would recommend doing in a thread and you can keep qos=2
for delivery but it wont guarantee in case you want the acknowledgement publish back from receiver 但是我建议在线程中执行,您可以保持
qos=2
进行传递,但是如果您希望从接收方发回确认,就不能保证
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.