Refactored the main process_packet method

This patch refactored the process_packet method
and adjusted the logic for determining if we got
a message to filter on.  We now look at the format
to make a determination.  Also isolated the processing
of message packets, ack packets and mic-e packets into
their own functions.
This commit is contained in:
Hemna 2020-12-21 11:57:54 -05:00
parent 8161719697
commit 1d898ea20f
3 changed files with 82 additions and 60 deletions

View File

@ -4,6 +4,7 @@ CHANGES
v1.1.0
------
* Refactored the main process\_packet method
* Update README with version 1.1.0 related info
* Added fix for an unknown packet type
* Ensure fortune is installed

View File

@ -6,7 +6,7 @@ ENV VERSION=1.0.0
ENV APRS_USER=aprs
ENV HOME=/home/aprs
ENV APRSD=http://github.com/craigerl/aprsd.git
ENV APRSD_BRANCH="master"
ENV APRSD_BRANCH="v1.1.0"
ENV VIRTUAL_ENV=$HOME/.venv3
ENV INSTALL=$HOME/install

View File

@ -172,6 +172,71 @@ def setup_logging(config, loglevel, quiet):
LOG.addHandler(sh)
def process_ack_packet(packet):
ack_num = packet.get("msgNo")
LOG.info("Got ack for message {}".format(ack_num))
messaging.log_message(
"ACK", packet["raw"], None, ack=ack_num, fromcall=packet["from"]
)
messaging.ack_dict.update({int(ack_num): 1})
return
def process_mic_e_packet(packet):
LOG.info("Mic-E Packet detected. Currenlty unsupported.")
messaging.log_packet(packet)
return
def process_message_packet(packet):
LOG.info("Got a message packet")
fromcall = packet["from"]
message = packet.get("message_text", None)
msg_number = packet.get("msgNo", None)
if msg_number:
ack = msg_number
else:
ack = "0"
messaging.log_message(
"Received Message", packet["raw"], message, fromcall=fromcall, ack=ack
)
found_command = False
# Get singleton of the PM
pm = plugin.PluginManager()
try:
results = pm.run(fromcall=fromcall, message=message, ack=ack)
for reply in results:
found_command = True
# A plugin can return a null message flag which signals
# us that they processed the message correctly, but have
# nothing to reply with, so we avoid replying with a usage string
if reply is not messaging.NULL_MESSAGE:
LOG.debug("Sending '{}'".format(reply))
messaging.send_message(fromcall, reply)
else:
LOG.debug("Got NULL MESSAGE from plugin")
if not found_command:
plugins = pm.get_plugins()
names = [x.command_name for x in plugins]
names.sort()
reply = "Usage: {}".format(", ".join(names))
messaging.send_message(fromcall, reply)
except Exception as ex:
LOG.exception("Plugin failed!!!", ex)
reply = "A Plugin failed! try again?"
messaging.send_message(fromcall, reply)
# let any threads do their thing, then ack
# send an ack last
messaging.send_ack(fromcall, ack)
LOG.debug("Packet processing complete")
def process_packet(packet):
"""Process a packet recieved from aprs-is server."""
@ -179,66 +244,22 @@ def process_packet(packet):
try:
LOG.debug("Got message: {}".format(packet))
fromcall = packet["from"]
message = packet.get("message_text", None)
if not message:
LOG.debug("Didn't get a message, could be an ack?")
if packet.get("response", None) == "ack":
# looks like an ACKa
ack_num = packet.get("msgNo")
LOG.info("Got ack for message {}".format(ack_num))
messaging.log_message(
"ACK", packet["raw"], None, ack=ack_num, fromcall=packet["from"]
)
messaging.ack_dict.update({int(ack_num): 1})
return
else:
LOG.info("Don't know what to do with this message. Ignoring")
messaging.log_packet(packet)
return
msg = packet.get("message_text", None)
msg_format = packet.get("format", None)
msg_response = packet.get("response", None)
if msg_format == "message" and msg:
# we want to send the message through the
# plugins
process_message_packet(packet)
return
elif msg_response == "ack":
process_ack_packet(packet)
return
msg_number = packet.get("msgNo", None)
if msg_number:
ack = msg_number
else:
ack = "0"
messaging.log_message(
"Received Message", packet["raw"], message, fromcall=fromcall, ack=ack
)
found_command = False
# Get singleton of the PM
pm = plugin.PluginManager()
try:
results = pm.run(fromcall=fromcall, message=message, ack=ack)
for reply in results:
found_command = True
# A plugin can return a null message flag which signals
# us that they processed the message correctly, but have
# nothing to reply with, so we avoid replying with a usage string
if reply is not messaging.NULL_MESSAGE:
LOG.debug("Sending '{}'".format(reply))
messaging.send_message(fromcall, reply)
else:
LOG.debug("Got NULL MESSAGE from plugin")
if not found_command:
plugins = pm.get_plugins()
names = [x.command_name for x in plugins]
names.sort()
reply = "Usage: {}".format(", ".join(names))
messaging.send_message(fromcall, reply)
except Exception as ex:
LOG.exception("Plugin failed!!!", ex)
reply = "A Plugin failed! try again?"
messaging.send_message(fromcall, reply)
# let any threads do their thing, then ack
# send an ack last
messaging.send_ack(fromcall, ack)
LOG.debug("Packet processing complete")
if msg_format == "mic-e":
# process a mic-e packet
process_mic_e_packet(packet)
return
except (aprslib.ParseError, aprslib.UnknownFormat) as exp:
LOG.exception("Failed to parse packet from aprs-is", exp)