2017-05-17 40 views
1

構成ファイルに基づいてトピックにサブスクライブするmqttクライアントアプリケーションがあります。何かのように:サブスクリプションをリロードするためのpaho mqttクライアントの割り込み

def connectMQTT(): 
    global Connection 
    Connection = Client() 
    Connection.on_message = handleQuery 
    for clientid in clientids.allIDs(): # clientids.allIDs() reads files to get this 
     topic = '{}/{}/Q/+'.format(Basename, clientid) 
     print('subscription:', topic) 
     Connection.subscribe(topic) 

私のような簡単な呼び出しにそれを使用している:

def main(): 
    connectMQTT() 
    Connection.loop_forever() 

loop_foreverは永遠にブロックします。しかし、私はclientids.allIDs()によって読み取られた情報が期限切れになったときに気付きたいと思います。私は再接続して、それを新たに購読する必要があります。

私はpyinotifyでファイルの変化を検出することができます。

def filesChanged(): 
    # NOT SURE WHAT TO DO HERE 

def watchForChanges(): 
    watchManager = pyinotify.WatchManager() 
    notifier = pyinotify.ThreadedNotifier(watchManager, FileEventHandler(eventCallback)) 
    notifier.start() 
    watchManager.add_watch('/etc/my/config/dir', pyinotify.IN_CLOSE_WRITE | pyinotify.IN_DELETE) 

を基本的に、私はいくつかの信号がpyinotify機械から来るまで実行するloop_forever(またはいくつかの他のPAHOのMQTTメカニズム)が必要です。私はどのようにそれらの2つを一緒に溶接するか分からない。擬似コードでは、私の事は、私は私もそれに影響するかどうかはわかりません

def main(): 
    signal = setup_directory_change_signal() 
    while True: 
     connectMQTT() 
     Connection.loop(until=signal) 
     Connection.disconnect() 

ような何かをしたいです。

+0

ドキュメント内の他の 'loop'関数を見ましたか? – hardillb

+0

ええ、私は 'loop_start()'と 'loop_stop()'で遊んでいて、今のところスレッドレスのノーティファイアを試しています。かなり運がまだありません。 –

+0

また、切断する必要はありません。新しく登録されたトピックに対して購読しているトピックのリストを保持し、不要な古いものを購読解除し、新しいものに購読します。 – hardillb

答えて

0

私は最終的には動作するように見える以下の解決策に回り回った。

def restartMQTT(): 
    if Connection: 
     Connection.loop_stop() 
    connectMQTT() 
    Connection.loop_start() 

class FileEventHandler(pyinotify.ProcessEvent): 
    def process_IN_CREATE(self, fileEvent): 
     restartMQTT() 

    def process_IN_DELETE(self, fileEvent): 
     restartMQTT() 


def main(): 
    restartMQTT() 
    watchManager = pyinotify.WatchManager() 
    notifier = pyinotify.Notifier(watchManager, FileEventHandler()) 
    watchManager.add_watch('/etc/my/config_directory', pyinotify.IN_CREATE | pyinotify.IN_DELETE) 
    notifier.loop() 

グローバルConnectionconnectMQTT店舗、新たに接続して構成されたMQTTクライアント:私はメインスレッドで別のスレッドとMQTTループに通知を実行しようとしていたのに対し、トリックは、その設定を反転しているように見えました。

関連する問題