Skip to content

Commit

Permalink
New keywords for connecting and disconnecting
Browse files Browse the repository at this point in the history
  • Loading branch information
joergschultzelutter committed Aug 30, 2021
1 parent 7bd1908 commit a8b2a73
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,4 @@ dmypy.json
log.html
output.xml
report.html

.DS_Store
107 changes: 79 additions & 28 deletions src/AprsLibrary.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,36 @@
from robot.api.deco import library, keyword
from robot.api.logger import librarylogger as logger
import aprslib
import re


@library(scope="GLOBAL", version="0.1", auto_keywords=True)
class AprsLibrary:

# APRS-IS connection parameters
__aprsis_server = None
__aprsis_port = None
__aprsis_callsign = None
__aprsis_passcode = None
__aprsis_filter = ""
__aprsis_filter = None

# the actual APRS-IS connection to the server
__ais = None

def __init__(
self,
aprsis_server: str = "euro.aprs2.net",
aprsis_port: int = 14580,
aprsis_callsign: str = "N0CALL",
aprsis_passcode: int = -1,
aprsis_passcode: int = "-1",
aprsis_filter: str = "",
):
self.__aprsis_server = aprsis_server
self.__aprsis_port = aprsis_port
self.__aprsis_callsign = aprsis_callsign
self.__aprsis_passcode = aprsis_passcode
self.__aprsis_filter = aprsis_filter
self.__ais = None

# Python "Getter" methods
#
Expand All @@ -72,6 +79,10 @@ def aprsis_passcode(self):
def aprsis_filter(self):
return self.__aprsis_filter

@property
def ais(self):
return self.__ais

# Python "Setter" methods
#
# Note that adding an additional Robot decorator (@keyword) will not
Expand All @@ -94,10 +105,10 @@ def aprsis_port(self, aprsis_port: int):
def aprsis_callsign(self, aprsis_callsign: str):
if not aprsis_callsign:
raise ValueError("No value for APRS-IS callsign has been specified")
self.__aprsis_callsign = aprsis_callsign
self.__aprsis_callsign = aprsis_callsign.upper()

@aprsis_passcode.setter
def aprsis_passcode(self, aprsis_passcode: int):
def aprsis_passcode(self, aprsis_passcode: str):
if not aprsis_passcode:
raise ValueError("No value for APRS-IS passcode has been specified")
self.__aprsis_passcode = aprsis_passcode
Expand All @@ -106,8 +117,22 @@ def aprsis_passcode(self, aprsis_passcode: int):
def aprsis_filter(self, aprsis_filter: str):
if not aprsis_filter:
raise ValueError("No value for APRS-IS filter has been specified")

# Apply a crude format filter and check if we have received something valid
aprsis_filter = aprsis_filter.lower()
if aprsis_filter != "":
matches = re.findall(r"^[r|p|b|o|t|s|d|a|e|g|o|q|m|f]\/", string)
if not matches:
raise ValueError("Invalid APRS-IS server filter string")
self.__aprsis_filter = aprsis_filter

@ais.setter
def ais(self, ais: object):
# Value can be "None" if we reset the connection. Therefore,
# we simply accept the value "as is"
self.__ais = ais


#
# Robot-specific "getter" keywords
#
Expand Down Expand Up @@ -147,7 +172,7 @@ def set_aprsis_callsign(self, aprsis_callsign: str = None):
self.aprsis_callsign = aprsis_callsign

@keyword("Set APRS-IS Passcode")
def set_aprsis_passcode(self, aprsis_passcode: int = None):
def set_aprsis_passcode(self, aprsis_passcode: str = None):
self.aprsis_passcode = aprsis_passcode

@keyword("Set APRS-IS Filter")
Expand All @@ -174,29 +199,55 @@ def parse_aprs_packet(self,aprs_packet: str = None):
raise ValueError("Unknown APRS format")
return packet

if __name__ == "__main__":
abcd = AprsLibrary()

print(abcd.aprsis_server)
abcd.aprsis_server = "na2.abcd.de"
print(abcd.aprsis_server)

print(abcd.aprsis_filter)
abcd.aprsis_filter = "HALLO"
print(abcd.aprsis_filter)

print(abcd.aprsis_passcode)
abcd.aprsis_passcode = 5678
print(abcd.aprsis_passcode)
@keyword("Connect To APRS-IS")
def connect_aprsis(self):
# Enforce default passcode if we're dealing with a read-only request
if self.aprsis_callsign == "N0CALL":
logger.debug(msg="Callsign is N0CALL; resetting APRS-IS passcode to default value")
self.aprsis_passcode = "-1"

if self.ais:
raise ValueError("An APRS-IS connection is still open; please close it first")

# Create the connection
self.ais = aprslib.IS(callsign=self.aprsis_callsign,passwd=self.aprsis_passcode,host=self.aprsis_server,port=self.aprsis_port)

# Set the filter if the string is not empty
if self.aprsis_filter != "":
self.ais.set_filter(self.aprsis_filter)

# Finally, connect to APRS-IS
self.ais.connect(blocking=True)

if not self.ais._connected:
disconnect_aprsis(self)
raise ConnectionError(f"Cannot connect to APRS-IS with server {self.aprsis_server} port {self.aprsis_port} callsign {self.aprsis_callsign}")

logger.debug(msg="Successfully connected to APRS-IS")

return self.ais

@keyword("Disconnect from APRS-IS")
def disconnect_aprsis(self):
if self.ais:
self.ais.close()
self.ais = None

@keyword("Get Current APRS-IS Configuration")
def get_aprs_configuration(self):
myvalues = {
"server": self.aprsis_server,
"port": self.aprsis_port,
"user": self.aprsis_callsign,
"passcode": self.aprsis_passcode,
"filter": self.aprsis_filter,
"ais": self.ais
}
return myvalues

print(abcd.aprsis_port)
abcd.aprsis_port = 1234
print(abcd.aprsis_port)

print(abcd.aprsis_callsign)
abcd.aprsis_callsign = "HELLOWORLD"
print(abcd.aprsis_callsign)

print (abcd.calculate_aprsis_passcode("MPAD"))
if __name__ == "__main__":
abcd = AprsLibrary()
abcd.calculate_aprsis_passcode("MPAD")
print(abcd.connect_aprsis())

print (abcd.parse_aprs_packet(r"M0XER-4>APRS64,TF3RPF,WIDE2*,qAR,TF3SUT-2:!/.(M4I^C,O `DXa/A=040849|#B>@\"v90!+|"))
27 changes: 12 additions & 15 deletions src/libtest.robot
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
*** Settings ***

Library AprsLibrary.py aprsis_port=666 aprsis_server=www.domino.de
Library AprsLibrary.py

*** Test Cases ***
Mein Erster Testfall
${a}= Get APRS-IS Port
Log To Console ${a}

Set APRS-IS Port 42
${b}= Get APRS-IS Port
${b}= Calculate APRS-IS Passcode MPAD
Log To Console ${b}

${b}= Parse APRS Packet M0XER-4>APRS64,TF3RPF,WIDE2*,qAR,TF3SUT-2:!/.(M4I^C,O `DXa/A=040849|#B>@\"v90!+|
Log To Console ${b}

${a}= Get APRS-IS Server
Log To Console ${a}
Set APRS-IS Server 94.33.51.3

Set APRS-IS server www.spiegel.de
${b}= Get APRS-IS Server
Log To Console ${b}
${b}= Connect To APRS-IS

${b}= Calculate APRS-IS Passcode MPAD
Log To Console ${b}
${c}= Get Current APRS-IS Configuration
Log To Console ${c}
Log Variables

${b}= Parse APRS Packet M0XER-4>APRS64,TF3RPF,WIDE2*,qAR,TF3SUT-2:!/.(M4I^C,O `DXa/A=040849|#B>@\"v90!+|
Log To Console ${b}
Disconnect from APRS-IS
${c}= Get Current APRS-IS Configuration
Log To Console ${c}

0 comments on commit a8b2a73

Please sign in to comment.