2016-07-23 15 views
1

當我使用以下代碼訂閱主題時,我無法使用相同的代碼來訂閱某個不同的主題。我怎樣才能訂閱不同的主題?如何訂閱Volttron中的不同主題

@PubSub.subscribe('pubsub', 'EnergyManagement/CurrentPrice') 
def on_match(self, peer, sender, bus, topic, headers, message): 

@PubSub.subscribe('pubsub', 'EnergyManagement/futurePrice') 
def on_match(self, peer, sender, bus, topic, headers, message): 

回答

3

你可以這樣做兩種不同的方式:

您可以使用多個調用self.vip.pubsub.subscribe。

這些調用必須在代理完成啓動後發生。正如阿明在他的回答中提到的那樣,你可以用一個「onstart」方法來做到這一點。此方法可用於在代理啓動後隨時動態更改訂閱。

@Core.receiver('onstart') 
def my_onstart_method(self, sender, **kwargs): 
    self.vip.pubsub.subscribe(peer='pubsub', prefix="path/to/topic1", callback=self.on_match) 
    self.vip.pubsub.subscribe(peer='pubsub', prefix="path/to/topic2", callback=self.on_match) 

或者你也可以在同一個類的方法使用多個裝飾:

@PubSub.subscribe('pubsub', 'EnergyManagement/CurrentPrice') 
@PubSub.subscribe('pubsub', 'EnergyManagement/futurePrice') 
def on_match(self, peer, sender, bus, topic, headers, message): 
    pass 
1

如果您想使用相同的方法來訂閱多個主題,您可以使用備用方式進行訂閱。你可以在你的「的OnStart」的方法做到這一點:

self.vip.pubsub.subscribe(peer='pubsub',prefix=」topic1」,callback=callback_method) 

self.vip.pubsub.subscribe(peer='pubsub',prefix=」topic2」,callback=callback_method)