我试图在循环中连续运行以下代码。但是以下代码只运行一次并且只接受一个消息条目。
我在on_message函数中尝试做的是使用python apscheduler运行cron任务。
def on_message(mqttc, obj, msg):
global val
val = str(msg.payload)
print val
dow = val[0:3]
print dow
hr = val[4:6]
print hr
min = val[7:9]
print min
status = val[10:11]
print status
def plugON():
publish.single("plug/status","0", hostname="localhost")
def plugOFF():
publish.single("plug/status","1", hostname="localhost")
def cronon():
print "cron on"
def cronoff():
print "cron off"
if status == '0':
sched.add_job(plugON, trigger='cron', year='*', month='*', day='*', week='*', day_of_week=dow, hour=hr, minute=min, id='plugon')
sched.add_job(cronon, trigger='cron', year='*', month='*', day='*', week='*', day_of_week=dow, hour=hr, minute=min)
if status == '1':
sched.add_job(plugOFF, trigger='cron', year='*', month='*', day='*', week='*', day_of_week=dow, hour=hr, minute=min, id='plugoff')
sched.add_job(cronoff, trigger='cron', year='*', month='*', day='*', week='*', day_of_week=dow, hour=hr, minute=min)
sched.start()
mqtt连接脚本:
mqttc = mqtt.Client()
mqttc.on_message = on_message
mqttc.on_connect = on_connect
mqttc.on_publish = on_publish
mqttc.on_subscribe = on_subscribe
mqttc.connect("localhost", 1883, 60)
mqttc.subscribe("plug/#", 0)
#mqtt loop
mqttc.loop_forever()
在执行期间,它连接到localhost,只需一个条目。在发送类似 thu:05:47:0 的内容时,请等到5:47运行plugon / plugOFF。在5:47它运行该函数并与localhost断开连接。
如何让我的代码获取另一个条目并继续循环?
答案 0 :(得分:-1)
一些问题。
你没有详细说明python,apscheduler,mqtt的哪个版本,你已经省略了你的导入和一些有用的故障排除功能,如on_connect
所以在python3和apscheduler3.2上进行测试我认为你;
如果你使用aps< 3然后查看this,你应该可以设置standalone=false
“独立模式”的概念已经消失。对于standalone = True,请使用 而是使用BlockingScheduler,对于standalone = False,使用 BackgroundScheduler。 BackgroundScheduler匹配旧的默认值 语义。
#!/usr/bin/env python
import paho.mqtt.client as mqtt
#from apscheduler.schedulers.blocking import BlockingScheduler
from apscheduler.schedulers.background import BackgroundScheduler
# Start the scheduler
sched = BackgroundScheduler()
sched.start()
def on_message(mqttc, obj, msg):
global val
val = str(msg.payload.decode('utf-8'))
dow = val[0:3]
hr = val[4:6]
minu = val[7:9]
status = val[10:11]
print(str(val) +" "+ dow +" "+ hr +" "+ minu +" "+ status)
def plugON():
mqttc.publish("plug/status","0")
def plugOFF():
mqttc.publish("plug/status","1")
def cronon():
print("cron on")
def cronoff():
print("cron off")
try:
if status == '0':
sched.add_job(plugON, trigger='cron', year='*', month='*', day='*', week='*', day_of_week=dow, hour=hr, minute=minu, id='plugon')
sched.add_job(cronon, trigger='cron', year='*', month='*', day='*', week='*', day_of_week=dow, hour=hr, minute=minu)
if status == '1':
sched.add_job(plugOFF, trigger='cron', year='*', month='*', day='*', week='*', day_of_week=dow, hour=hr, minute=minu, id='plugoff')
sched.add_job(cronoff, trigger='cron', year='*', month='*', day='*', week='*', day_of_week=dow, hour=hr, minute=minu)
except:
print("whoops")
sched.print_jobs()
mqttc = mqtt.Client()
mqttc.on_message = on_message
#mqttc.on_connect = on_connect
#mqttc.on_publish = on_publish
#mqttc.on_subscribe = on_subscribe
mqttc.connect("local", 1883, 60)
mqttc.subscribe("plug/#")
#mqtt loop
mqttc.loop_forever()
代码 TESTED 用于处理python3.5.2和apschedulerv3.2以及paho-mqttv1.2
<强> TL:DR 强>