2016-10-05 159 views
1

嘗試簡單訂閱,而不會斷開與Mosquitto代理的連接,以便從發佈特定主題數據的設備獲取所有消息,將它們保存在BD中並將其發佈到執行「員工」的PHP。MQTT Paho Python客戶端訂戶,如何訂閱永久?

這裏是我的subscribe.py

import paho.mqtt.client as mqtt 
from mqtt_myapp import * 

topic="topic/#"     # MQTT broker topic 
myclient="my-paho-client"   # MQTT broker My Client 
user="user"      # MQTT broker user 
pw="pass"      # MQTT broker password 
host="localhost"    # MQTT broker host 
port=1883      # MQTT broker port 
value="123"      # somethin i need for myapp 

def on_connect(mqttc, userdata, rc): 
    print('connected...rc=' + str(rc)) 
    mqttc.subscribe(topic, qos=0) 

def on_disconnect(mqttc, userdata, rc): 
    print('disconnected...rc=' + str(rc)) 

def on_message(mqttc, userdata, msg): 
    print('message received...') 
    print('topic: ' + msg.topic + ', qos: ' + 
      str(msg.qos) + ', message: ' + str(msg.payload)) 
    save_to_db(msg) 
    post_data(msg.payload,value) 

def on_subscribe(mqttc, userdata, mid, granted_qos): 
    print('subscribed (qos=' + str(granted_qos) + ')') 

def on_unsubscribe(mqttc, userdata, mid, granted_qos): 
    print('unsubscribed (qos=' + str(granted_qos) + ')') 

mqttc = mqtt.Client(myclient) 
mqttc.on_connect = on_connect 
mqttc.on_disconnect = on_disconnect 
mqttc.on_message = on_message 
mqttc.on_subscribe = on_subscribe 
mqttc.on_unsubscribe = on_unsubscribe 
mqttc.username_pw_set(user,pw) 
mqttc.connect(host, port, 60) 
mqttc.loop_forever() 

這裏是我的mqtt_myapp.py:

import MySQLdb 
import requests # pip install requests 

url = "mydomain/data_from_broker.php" 

def save_to_db(msg): 
    with db: 
     cursor = db.cursor() 
     try: 
      cursor.execute("INSERT INTO MQTT_LOGS (topic, payload) VALUES (%s,%s)", (msg.topic, msg.payload)) 
     except (MySQLdb.Error, MySQLdb.Warning) as e: 
      print('excepttion BD ' + e) 
      return None 

def post_data(payload,value): 
    datos = {'VALUE': value,'data-from-broker': payload} 
    r = requests.post(url, datos) 
    r.status_code 
    print('response POST' + str(r.status_code)) 

db = MySQLdb.connect("localhost","user_db","pass_db","db") 

當我運行在後臺我的Python腳本python -t mqtt_subscribe.py &我得到的消息給其他客戶端發佈,但運行幾個小時後,我的subscribe.py腳本就會發生套接字錯誤。

Mosquito.log:

... 
    1475614815: Received PINGREQ from my-paho-client 
    1475614815: Sending PINGRESP to my-paho-client 
    1475614872: New connection from xxx.xxx.xxx.xxx on port 1883. 
    1475614872: Client device1 disconnected. 
    1475614872: New client connected from xxx.xxx.xxx.xxx as device1(c0, k0, u'user1'). 
    1475614872: Sending CONNACK to device1(0, 0) 
    1475614873: Received PUBLISH from device1(d0, q1, r0, m1, 'topic/data', ... (33 bytes)) 
    1475614873: Sending PUBACK to device1 (Mid: 1) 
    1475614873: Sending PUBLISH to my-paho-client (d0, q0, r0, m0, 'topic/data', ... (33 bytes)) 
    1475614874: Received DISCONNECT from device1 
    1475614874: Client device1 disconnected. 
... 
    1475625566: Received PINGREQ from my-paho-client 
    1475625566: Sending PINGRESP to my-paho-client 
    1475625626: Received PINGREQ from my-paho-client 
    1475625626: Sending PINGRESP to my-paho-client 
    1475625675: New connection from xxx.xxx.xxx.xxx on port 1883. 
    1475625675: Client device1 disconnected. 
    1475625675: New client connected from xxx.xxx.xxx.xxx as device1 (c0, k0, u'user1'). 
    1475625675: Sending CONNACK to device1 (0, 0) 
    1475625677: Received PUBLISH from device1 (d0, q1, r0, m1, 'topic/data', ... (33 bytes)) 
    1475625677: Sending PUBACK to device1 (Mid: 1) 
    1475625677: Sending PUBLISH to my-paho-client (d0, q0, r0, m0, 'topic/data', ... (33 bytes)) 
    1475625677: Socket error on client my-paho-client, disconnecting. 
    1475625677: Received DISCONNECT from device1 
... 

可能是什麼問題呢?任何想法或建議?

在此先感謝

回答

1

如果你的方法「ON_MESSAGE」代碼拋出一個異常,你不抓住它,你將被斷開。 嘗試取消打印語句以外的所有語句的註釋。可能下列其中一種說法是拋出異常。

save_to_db(msg) 
post_data(msg.payload,value) 
+0

如果我想要捕獲「on_message」事件,我應該在哪裏編碼「save_to_db」? – smontoya

+0

你可以在那裏編寫代碼,只要確保你在那裏捕獲所有異常,如果你不想斷開連接。 – SebastianK

+0

試着理解,所以只是因爲我不捕捉異常,腳本斷開連接?即使我發現異常並且什麼也不做腳本不停止? SebastianK,你能否給我一個參考來研究這個問題?非常感謝。 – smontoya