首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么泛美卫生组织python客户端发布短消息而不发布长消息?

为什么泛美卫生组织python客户端发布短消息而不发布长消息?
EN

Stack Overflow用户
提问于 2019-12-07 20:21:49
回答 1查看 179关注 0票数 1

这样做是可行的:

代码语言:javascript
复制
while True:
    print('')
    command_input = input()
    if command_input == 'q':
        break
    mcp = Mqtt_command_publisher
    mcp.publish_command(device_ids, command_input)

但这并不意味着:

代码语言:javascript
复制
class Mqtt_command_bl:
    def update_minutes_to_run_at(json):
        if not json['device_ids']:
            return 'Request must contain device ids'

        device_ids = json['device_ids']
        minutes_to_run_at = json['minutes_to_run_at']

        minutes_to_run_at_command_section = ''
        for i in minutes_to_run_at:
            m = '"{}",'.format(i)
            if i == minutes_to_run_at[len(minutes_to_run_at) - 1]:
                m = '"{}"'.format(i)
            minutes_to_run_at_command_section += m

        #command_input = 'jq \'.+{{minutes_to_run_at:[{}]}}\' /home/pi/hallmonitor_lite/config.json > /home/pi/hallmonitor_lite/tmp.json && mv /home/pi/hallmonitor_lite/tmp.json /home/pi/hallmonitor_lite/new_config.json'.format(minutes_to_run_at_command_section)
        command_input = 'mkdir /home/pi/hallmonitor_lite/hello_world'

        mcp = Mqtt_command_publisher
        mcp.publish_command(device_ids, command_input)

        return 'Success'

他们所称的班级:

代码语言:javascript
复制
class Mqtt_command_publisher:
    def publish_command(device_ids, command_input):
        mqtt_msg = json.dumps({'device_ids':device_ids,'command':command_input})
        print('\n{}'.format(mqtt_msg))
        client = mqtt.Client()
        client.connect('********', ****, 30)
        client.publish('topic/commands', mqtt_msg)
        client.disconnect()

从Mqtt_command_publisher输出的print语句来看,输出可以是完全相同的,但是,它们中只有一个会执行,我不明白为什么其中一个工作而另一个不工作。

我尝试了下面的命令来测试:mkdir /home/pi/hallmonitor_lite/hello_world

这是接收部分:

代码语言:javascript
复制
device_id = 0

with open('/home/pi/hallmonitor_lite/config.json') as json_data_file:
    data = json.load(json_data_file)

    device_id = data['device_id']

def on_connect(client, userdata, flags, rc):
    print("Connected with result code: " + str(rc))
    client.subscribe("topic/commands")

def on_message(client, userdata, msg):
    mqtt_message = msg.payload.decode()
    print(mqtt_message)
    ids_and_command = json.loads(mqtt_message)
    if str(device_id) in ids_and_command['device_ids'] or not ids_and_command['device_ids']:
        print(('Executing: {}').format(ids_and_command['command']))
        os.system(ids_and_command['command'])



client = mqtt.Client()
client.connect("********", ****, 30)

client.on_connect = on_connect
client.on_message = on_message

client.loop_forever()

有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-12-07 21:36:30

这个问题很可能是因为第二组代码正在创建一条比单个TCP数据包更大的消息。

这是一个问题,因为您没有运行客户端网络循环,因此client.publish命令只能发送单个数据包,其余的消息将由网络循环发送,但是即使它正在运行,在发布调用后立即调用断开连接。

客户端不是为这样的单个消息而旋转的,它意味着启动,然后在您想要发送消息时只调用publish方法就可以运行。如果您不想这样做,或者由于某种原因不能这样做,那么paho python包中有一个特定的helper类,它将完成启动客户机、发送消息、然后很好地撕毁一切的所有繁重工作。单个发布的文档是这里

代码语言:javascript
复制
import paho.mqtt.publish as publish

publish.single("paho/test/single", "payload", hostname="mqtt.eclipse.org")
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59229850

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档