Changed processing for race messages because of new format coming through.

This commit is contained in:
tamservo
2025-05-18 12:24:17 -04:00
parent 839ee6306c
commit e5219bc433

View File

@@ -36,10 +36,10 @@ class Robottas(commands.Bot):
.replace('False', 'false') .replace('False', 'false')
try: try:
data = json.loads(data) data = json.loads(data)
logger.warn(f"convert_message returning: {data}") #logger.warn(f"convert_message returning: {data}")
return data return data
except json.JSONDecodeError: except json.JSONDecodeError:
logger.warn("json decode error for") logger.warning("json decode error for")
return "" return ""
# End section adapted from FastF1 # End section adapted from FastF1
@@ -81,8 +81,9 @@ class Robottas(commands.Bot):
while len(self.message_queue) > 0 and \ while len(self.message_queue) > 0 and \
self.message_queue[0][0] < time.time() - self.delay: self.message_queue[0][0] < time.time() - self.delay:
message = self.message_queue.pop(0)[1] message = self.message_queue.pop(0)[1]
self.logger.warning(f'process_delay_messages: {message[:50]}')
await self.send_message(message) await self.send_message(message)
await asyncio.sleep(1) await asyncio.sleep(3)
async def send_status_report(self, report): async def send_status_report(self, report):
self.send_delay_message(report) self.send_delay_message(report)
@@ -125,47 +126,72 @@ class Robottas(commands.Bot):
# Return None or flag message # Return None or flag message
return report return report
async def load_rcm_messages(self, data):
if "Messages" in data.keys(): async def load_rcm_messages_helper(self, msg):
self.logger.warning(f'helper msg: {msg}')
report = None report = None
for key in data['Messages'].keys(): if 'Category' in msg.keys():
message = data['Messages'][key] category = msg['Category']
if 'Category' in message.keys():
category = message["Category"]
category = category.upper() category = category.upper()
if category == "FLAG": if category == "FLAG":
report = await self.load_flag_message(message) report = await self.load_flag_message(msg)
elif category == "OTHER": elif category == "OTHER":
if self.session_type == "RACE" and "DELETED" in message['Message']:
if self.session_type == "RACE" and "DELETED" in msg['Message']:
pass pass
elif "SLIPPERY" in message['Message'] and \
elif "SLIPPERY" in msg['Message'] and \
not self.is_slippery_reported: not self.is_slippery_reported:
self.is_slippery_reported = True self.is_slippery_reported = True
report = "It's slippery out there!" report = "It's slippery out there!"
else: else:
report = message['Message'] report = msg['Message']
elif category == "DRS": elif category == "DRS":
report = message['Message'] report = msg['Message']
elif category == "CAREVENT" and \ elif category == "CAREVENT" and \
(self.session_type == "PRACTICE" or (self.session_type == "PRACTICE" or
self.session_type == "QUALI"): self.session_type == "QUALI"):
report = message['Message'] report = msg['Message']
elif category == "SAFETYCAR": elif category == "SAFETYCAR":
if message["Mode"] == 'VIRTUAL SAFETY CAR': if msg["Mode"] == 'VIRTUAL SAFETY CAR':
report = f"{self.flag_dict['VSC']}" + \ report = f"{self.flag_dict['VSC']}" + \
f"{message['Message']}" + \ f"{msg['Message']}" + \
f"{self.flag_dict['VSC']}" f"{self.flag_dict['VSC']}"
elif msg["Mode"] == 'SAFETY CAR':
elif message["Mode"] == 'SAFETY CAR':
report = f"{self.flag_dict['SC']}" + \ report = f"{self.flag_dict['SC']}" + \
f"{message['Message']}" + \ f"{msg['Message']}" + \
f"{self.flag_dict['SC']}" f"{self.flag_dict['SC']}"
self.logger.warning(f'rcm helper - report: {report}')
return report
async def load_rcm_messages(self, data):
if "Messages" in data.keys():
report = None
self.logger.warning(f'load_rcm_messages - data: {data}')
# Value of 'Messages' can be scalar or array.
# If scalar, pass to helper. If array, loop and send to helper
msgs_value = data['Messages']
self.logger.warning(f'load_rcm_messages - msgs_value: {msgs_value} type: {type(msgs_value)}')
# If value is a list
if isinstance(msgs_value, list):
for msg in msgs_value:
self.logger.warning(f'load_rcm_messages array msg: {msg}')
report = await self.load_rcm_messages_helper(msg)
if report is not None:
await self.send_status_report(report)
else:
self.logger.warning(f'load_rcm_messages scalar msg: {msgs_value}')
for msg in msgs_value.values():
report = await self.load_rcm_messages_helper(msg)
if report is not None: if report is not None:
await self.send_status_report(report) await self.send_status_report(report)
@@ -338,6 +364,7 @@ class Robottas(commands.Bot):
self.weather = weather_txt self.weather = weather_txt
def load_timing_stats_data(self, data): def load_timing_stats_data(self, data):
self.logger.warning(f'load_timing_stats_data: {data}')
if "Lines" in data.keys(): if "Lines" in data.keys():
lines = data["Lines"] lines = data["Lines"]
for driver_num in lines.keys(): for driver_num in lines.keys():
@@ -433,7 +460,7 @@ class Robottas(commands.Bot):
async def process_message(self, message): async def process_message(self, message):
try: try:
if isinstance(message, collections.abc.Sequence): if isinstance(message, collections.abc.Sequence):
#self.logger.warning(f"in process_message {message}") self.logger.warning(f"in process_message {message[:50]}")
if message[0] == 'Heartbeat': if message[0] == 'Heartbeat':
return return
@@ -465,7 +492,7 @@ class Robottas(commands.Bot):
self.load_initial(message) self.load_initial(message)
except Exception as e: except Exception as e:
pass self.logger.warning(f"Error in process_message: {e}")
def get_messages_from_db(self): def get_messages_from_db(self):
try: try:
@@ -475,8 +502,9 @@ class Robottas(commands.Bot):
with closing(con.cursor()) as cur: with closing(con.cursor()) as cur:
with closing(con.cursor()) as cur2: with closing(con.cursor()) as cur2:
for row in cur.execute('select id, message from messages order by id asc'): for row in cur.execute('select id, message from messages order by id asc'):
messages.append(self.convert_message(row[1], self.logger)) msg = row[1]
self.logger.warn(f"get_messages_from_db: {row[1]}") messages.append(self.convert_message(msg, self.logger))
self.logger.warning(f"get_messages_from_db: {msg[:50]}")
# Now that we have the message, delete this row from the dbfile # Now that we have the message, delete this row from the dbfile
cur2.execute(f"delete from messages where id = {row[0]}") cur2.execute(f"delete from messages where id = {row[0]}")
@@ -500,12 +528,18 @@ class Robottas(commands.Bot):
self.logger.warning(f"error in clear_messages_from_db: {e}") self.logger.warning(f"error in clear_messages_from_db: {e}")
async def _race_report(self, ctx): async def _race_report(self, ctx):
if self.is_reporting:
await self.rbstop()
self.clear_messages_from_db() self.clear_messages_from_db()
self.report_deleted_lap = False self.report_deleted_lap = False
self.session_type = 'RACE' self.session_type = 'RACE'
await self._report(ctx) await self._report(ctx)
async def _quali_report(self, ctx): async def _quali_report(self, ctx):
if self.is_reporting:
await self.rbstop()
self.clear_messages_from_db() self.clear_messages_from_db()
self.report_deleted_lap = True self.report_deleted_lap = True
self.session_type = 'QUALI' self.session_type = 'QUALI'
@@ -513,6 +547,9 @@ class Robottas(commands.Bot):
await self._report(ctx) await self._report(ctx)
async def _practice_report(self, ctx): async def _practice_report(self, ctx):
if self.is_reporting:
await self.rbstop()
self.clear_messages_from_db() self.clear_messages_from_db()
self.report_deleted_lap = True self.report_deleted_lap = True
self.session_type = 'PRACTICE' self.session_type = 'PRACTICE'
@@ -525,6 +562,7 @@ class Robottas(commands.Bot):
while self.is_reporting: while self.is_reporting:
# Do processing # Do processing
await asyncio.sleep(5)
#self.logger.warning("in is_reporting") #self.logger.warning("in is_reporting")
@@ -532,12 +570,11 @@ class Robottas(commands.Bot):
messages = self.get_messages_from_db() messages = self.get_messages_from_db()
try: try:
for message in messages: for message in messages:
#self.logger.warning(f"processing message: {message}") self.logger.warning(f"processing message: {message}")
await self.process_message(message) await self.process_message(message)
await asyncio.sleep(2)
except: except Exception as e:
pass self.logger.warning(f'Error in _report: {e}')
# process any messages in the delay queue # process any messages in the delay queue
await self.process_delay_messages() await self.process_delay_messages()
@@ -631,14 +668,55 @@ class Robottas(commands.Bot):
command_txt += self.collector_params command_txt += self.collector_params
self.collector_proc = Popen(command_txt.split()) self.collector_proc = Popen(command_txt.split())
async def start_test_collect(self, ctx):
await self.stop_test_collect()
self.is_test_collecting = True
self.channel = ctx.channel
dir_path = os.path.dirname(os.path.realpath(__file__))
command_txt = os.path.join(dir_path, self.test_collector_command)
self.collector_proc = Popen(command_txt.split())
while self.is_test_collecting:
await asyncio.sleep(10)
messages = self.get_messages_from_db()
try:
for msg in messages:
self.logger.warning(f"processing message{msg}")
await self.process_message(msg)
except Exception as e:
self.logger.warning(f"Error in start_test_collect: {e}")
await self.process_delay_messages()
async def stop_collect(self): async def stop_collect(self):
self.is_collecting = False self.is_collecting = False
self.is_test_collecting = False
try: try:
if self.collector_proc != None: if self.collector_proc != None:
self.collector_proc.kill() self.collector_proc.kill()
self.clear_messages_from_db() self.clear_messages_from_db()
except: except:
self.logger.warn("error in stop_collect.") self.logger.warning("error in stop_collect.")
try:
if self.test_collector_proc != None:
self.collector_proc.kill()
self.clear_messages_from_db()
except:
self.logger.warning("error in stop_collect for test.")
async def stop_test_collect(self):
self.is_test_collecting = False
try:
if self.test_collector_proc != None:
self.test_collector_proc.kill()
self.clear_messages_from_db()
except Exception as e:
self.logger.warning(f"Error in stop_test_collect: {e}")
def decode_watched(self, w): def decode_watched(self, w):
if w == 0: if w == 0:
@@ -876,9 +954,12 @@ class Robottas(commands.Bot):
# Discord authentication token # Discord authentication token
self.token = self.get_token("token.txt") self.token = self.get_token("token.txt")
self.collector_command = "robottas_collector.py" self.collector_command = "robottas_collector.py"
self.test_collector_command = "test_messager.py"
self.collector_params = " save dummy.txt" self.collector_params = " save dummy.txt"
self.collector_proc = None self.collector_proc = None
self.test_collector_proc = None
self.is_collecting = False self.is_collecting = False
self.test_collecting = False
# Preface messages with the following # Preface messages with the following
self.report_preamble = ':robot::peach: Alert!' self.report_preamble = ':robot::peach: Alert!'
@@ -1003,7 +1084,7 @@ class Robottas(commands.Bot):
self.started = False self.started = False
# Hold message delay # Hold message delay
self.delay = 25 self.delay = 20
self.message_queue = [] self.message_queue = []
# Hold whether to report deleted lap messages # Hold whether to report deleted lap messages
@@ -1052,6 +1133,8 @@ class Robottas(commands.Bot):
" !calm - keep calm but come on.\n" + " !calm - keep calm but come on.\n" +
" !censored\n" + " !censored\n" +
" !ciao\n" + " !ciao\n" +
" !danger\n" +
" !dangerbull\n" +
" !encouragement\n" + " !encouragement\n" +
" !forecast - what happened to that podium...\n" + " !forecast - what happened to that podium...\n" +
" !grandma\n" + " !grandma\n" +
@@ -1189,6 +1272,10 @@ class Robottas(commands.Bot):
async def danger(ctx): async def danger(ctx):
await ctx.send("That's some dangerous driving! " + self.name_dict["HAM"]) await ctx.send("That's some dangerous driving! " + self.name_dict["HAM"])
@self.command()
async def dangerbull(ctx):
await self.send_image(ctx, "images/dangerbull.png")
@self.command() @self.command()
async def dotd(ctx): async def dotd(ctx):
await ctx.send("I don't know, probably " + self.name_dict["ALB"]) await ctx.send("I don't know, probably " + self.name_dict["ALB"])
@@ -1458,6 +1545,12 @@ class Robottas(commands.Bot):
await self.show_bing(ctx) await self.show_bing(ctx)
# test processing messages
@self.command()
async def test_message_processing(ctx):
await self.start_test_collect(ctx)
if __name__ == '__main__': if __name__ == '__main__':
rb = Robottas() rb = Robottas()
rb.run_robottas() rb.run_robottas()