17
17
@author: Danny Lade
18
18
"""
19
19
import uuid
20
+ import json
20
21
import functools
21
22
22
23
import paho .mqtt .client as mqtt
23
24
24
25
from .log import setup_custom_logger
25
- from .topic import MqttTopic
26
26
27
27
__all__ = [
28
28
"TrazeMqttAdapter"
29
29
]
30
30
31
31
32
+ class _MqttTopic :
33
+
34
+ def __init__ (self , client , name , * args ):
35
+ def topic_name (topicName , * args ):
36
+ if not args :
37
+ return topicName
38
+ return topicName .replace ('+' , '%s' ) % (args )
39
+
40
+ self ._client = client
41
+ self ._name = topic_name (name , * args )
42
+ self .functions = set ()
43
+
44
+ def subscribe (self , on_payload_func ):
45
+ def on_message (client , userdata , message ):
46
+ payload = json .loads (str (message .payload , 'utf-8' ))
47
+ for on_payload in self .functions :
48
+ on_payload (payload )
49
+
50
+ if not self .functions :
51
+ self ._client .subscribe (self ._name )
52
+ self ._client .message_callback_add (self ._name , on_message )
53
+
54
+ if on_payload_func not in self .functions :
55
+ self .functions .add (on_payload_func )
56
+
57
+ def publish (self , obj = None ):
58
+ self ._client .publish (self ._name , json .dumps (obj ))
59
+
60
+
32
61
class TrazeMqttAdapter :
33
62
34
63
def __init__ (self , host = 'traze.iteratec.de' , port = 8883 , transport = 'tcp' ):
35
- self .logger = setup_custom_logger (name = type ( self ). __name__ )
64
+ self .logger = setup_custom_logger (self )
36
65
37
66
def _on_connect (client , userdata , flags , rc ):
38
67
self .logger .info ("Connected the MQTT broker." )
@@ -50,16 +79,17 @@ def _on_disconnect(client, userdata, rc):
50
79
self ._client .connect (host , port )
51
80
self ._client .loop_start ()
52
81
53
- def on_heartbeat ( self , game_name , on_heartbeat ):
54
- # there is no heartbeat from server but the grid-event is a good base
55
- self . __get_topic__ ( 'traze/+/grid' , game_name ). subscribe ( on_heartbeat )
56
-
82
+ #
83
+ # world based topic(s)
84
+ # - parameters: None
85
+ #
57
86
def on_game_info (self , on_game_info ):
58
87
self .__get_topic__ ('traze/games' ).subscribe (on_game_info )
59
88
60
- def on_player_info (self , game_name , on_player_info ):
61
- self .__get_topic__ ('traze/+/player/+' , game_name , self .__client_id__ ).subscribe (on_player_info ) # noqa
62
-
89
+ #
90
+ # game based topic(s)
91
+ # - parameters: game_name
92
+ #
63
93
def on_grid (self , game_name , on_grid ):
64
94
self .__get_topic__ ('traze/+/grid' , game_name ).subscribe (on_grid )
65
95
@@ -69,6 +99,13 @@ def on_players(self, game_name, on_players):
69
99
def on_ticker (self , game_name , on_ticker ):
70
100
self .__get_topic__ ('traze/+/ticker' , game_name ).subscribe (on_ticker )
71
101
102
+ def on_player_info (self , game_name , on_player_info ):
103
+ self .__get_topic__ ('traze/+/player/+' , game_name , self .__client_id__ ).subscribe (on_player_info ) # noqa
104
+
105
+ #
106
+ # player based topic(s)
107
+ # - parameters: game_name, player_id/player_name
108
+ #
72
109
def publish_join (self , game_name , player_name ):
73
110
self .__get_topic__ ('traze/+/join' , game_name ).publish ({'name' : player_name , 'mqttClientName' : self .__client_id__ }) # noqa
74
111
@@ -81,6 +118,6 @@ def publish_bail(self, game_name, player_id, player_token):
81
118
def disconnect (self ):
82
119
self ._client .disconnect ()
83
120
84
- @functools .lru_cache ()
121
+ @functools .lru_cache () # singleton by parameter (for same arguments always return the same object)
85
122
def __get_topic__ (self , topic_name , * args ):
86
- return MqttTopic (self ._client , topic_name , * args )
123
+ return _MqttTopic (self ._client , topic_name , * args )
0 commit comments