11import _thread
22import random
33import uuid
4-
54import paho .mqtt .publish as mqtt_pub
65import paho .mqtt .client as mqtt
76import json
87
9- MOBBER_LIST = "MobberList"
8+ TOPIC_MOBBER_LIST = "MobberList"
109
11- TIME_CHANGE = "TimeChange"
10+ TOPIC_TIME_CHANGE = "TimeChange"
1211
13- SAY_HELLO = "SayHello"
12+ TOPIC_SAY_HELLO = "SayHello"
1413
15- START_TIMER = "StartTimer"
14+ TOPIC_START_TIMER = "StartTimer"
1615
1716
1817class DojoManager (object ):
@@ -24,12 +23,11 @@ def __init__(self, controller):
2423 self .mobber_change_increment = 0
2524 self .session_id = uuid .uuid4 ().__str__ ()
2625
27-
2826 self .switch_dictionary = {
29- MOBBER_LIST : self .sub_mobber_list ,
30- TIME_CHANGE : self .sub_time_change ,
31- SAY_HELLO : self .sub_say_hello ,
32- START_TIMER : self .sub_start_timer ,
27+ TOPIC_MOBBER_LIST : self .sub_mobber_list ,
28+ TOPIC_TIME_CHANGE : self .sub_time_change ,
29+ TOPIC_SAY_HELLO : self .sub_say_hello ,
30+ TOPIC_START_TIMER : self .sub_start_timer ,
3331 }
3432 self .station_drivers = {}
3533 self .other_stations = []
@@ -42,15 +40,22 @@ def __init__(self, controller):
4240 self .dojo_topic_root = self .controller .settings_manager .get_dojo_topic_root ()
4341 self .controller .mobber_manager .subscribe_to_mobber_list_change (self .publish_mobber_list_changes )
4442 self .controller .time_options_manager .subscribe_to_timechange (self .publish_time_change )
43+ self .controller .countdown_manager .subscribe_to_time_changes (self .publish_countdown_change )
4544 self .subscribe_to_time_change ()
4645 self .say_hello ()
4746
4847 def say_hello (self ):
49- topic = self .generate_topic (SAY_HELLO )
48+ topic = self .generate_topic (TOPIC_SAY_HELLO )
49+ self .publish (topic , "" )
50+
51+ def publish_countdown_change (self , negative_if_time_expired , minutes , seconds , origin_station_name = None ):
52+ if negative_if_time_expired < 0 : return
53+ print ("publish_countdown_change" , minutes , seconds )
54+ topic = self .generate_topic (TOPIC_START_TIMER )
5055 self .publish (topic , "" )
5156
5257 def publish_time_change (self , time_string , minutes , seconds , origin_station_name = None ):
53- topic = self .generate_topic (TIME_CHANGE )
58+ topic = self .generate_topic (TOPIC_TIME_CHANGE )
5459 station_name = self .dojo_mob_station_name
5560 station_uuid = self .dojo_station_uuid
5661 if origin_station_name is not None :
@@ -65,7 +70,7 @@ def publish_time_change(self, time_string, minutes, seconds, origin_station_name
6570 self .publish (topic , payload )
6671
6772 def publish_mobber_list_changes (self , mobber_list , driver_index , next_driver_index ):
68- topic = self .generate_topic (MOBBER_LIST )
73+ topic = self .generate_topic (TOPIC_MOBBER_LIST )
6974 payload_object = {
7075 "driver_index" : driver_index ,
7176 "next_driver_index" : next_driver_index ,
@@ -94,23 +99,24 @@ def on_message(self, client, userdata, msg):
9499 station_uuid = topic_parts [2 ]
95100 station_name = topic_parts [3 ]
96101 message_type = topic_parts [4 ]
97- print ("on_message" ,msg .topic ,"station_uuid" ,station_uuid )
98- self .switch_statement_dictionary_trick (station_uuid ,station_name , message_type , msg .payload )
102+ print ("on_message" , msg .topic , "station_uuid" , station_uuid )
103+ self .switch_statement_dictionary_trick (station_uuid , station_name , message_type , msg .payload )
99104
100- def switch_statement_dictionary_trick (self , station_uuid ,station_name , message_type , payload ):
101- self .switch_dictionary [message_type ](station_uuid ,station_name , message_type , payload )
105+ def switch_statement_dictionary_trick (self , station_uuid , station_name , message_type , payload ):
106+ self .switch_dictionary [message_type ](station_uuid , station_name , message_type , payload )
102107
103- def sub_mobber_list (self , station_uuid ,station_name , message_type , payload ):
108+ def sub_mobber_list (self , station_uuid , station_name , message_type , payload ):
104109 if not station_uuid == self .dojo_station_uuid :
105110 payload_dictionary = json .loads (payload .decode ("utf-8" ))
106111 mobber_list = payload_dictionary ["mobber_list" ]
107- print ("sub_mobber_list" ,mobber_list )
112+ print ("sub_mobber_list" , mobber_list )
108113 self .controller .mobber_manager .set_mobber_list (mobber_list )
109114
110115 def generate_topic (self , message_type ):
111- topic = "{}/{}/{}/{}/{}" .format (self .dojo_topic_root , self .dojo_session_id ,self .dojo_station_uuid , self .dojo_mob_station_name ,
112- message_type )
113- print ("generate_topic" ,topic )
116+ topic = "{}/{}/{}/{}/{}" .format (self .dojo_topic_root , self .dojo_session_id , self .dojo_station_uuid ,
117+ self .dojo_mob_station_name ,
118+ message_type )
119+ print ("generate_topic" , topic )
114120 return topic
115121
116122 def sub_time_change (self , station_uuid , station_name , message_type , payload ):
@@ -124,11 +130,11 @@ def sub_time_change(self, station_uuid, station_name, message_type, payload):
124130 self .controller .time_options_manager .minutes == minutes and self .controller .time_options_manager .seconds == seconds ):
125131 self .controller .time_options_manager .set_countdown_time (minutes , seconds , origin_station_name )
126132
127- def sub_say_hello (self , station_uuid ,station_name , message_type , payload ):
133+ def sub_say_hello (self , station_uuid , station_name , message_type , payload ):
128134 if not station_uuid == self .dojo_station_uuid :
129135 if not self .other_stations .__contains__ (station_name ):
130136 self .other_stations .append (station_name )
131- topic = self .generate_topic (SAY_HELLO )
137+ topic = self .generate_topic (TOPIC_SAY_HELLO )
132138 self .publish (topic , "" )
133139
134140 def sub_start_timer (self , station_uuid , station_name , message_type , payload ):
@@ -141,4 +147,3 @@ def subscribe_to_dojo(self):
141147 client .on_message = self .on_message
142148 client .connect (self .dojo_broker , self .dojo_port , 60 )
143149 client .loop_forever ()
144-
0 commit comments