Skip to content

Commit

Permalink
Merge pull request #42 from UniversityRadioYork/docs-and-logging
Browse files Browse the repository at this point in the history
Comments and reordering.
  • Loading branch information
mstratford authored Mar 18, 2022
2 parents 358b5bb + 5d4c722 commit 7912b2c
Show file tree
Hide file tree
Showing 9 changed files with 427 additions and 342 deletions.
57 changes: 30 additions & 27 deletions controllers/mattchbox_usb.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def __init__(
self, player_to_q: List[Queue], player_from_q: Queue, state: StateManager
):

process_title = "ControllerHandler"
process_title = "BAPSicle - ControllerHandler"
setproctitle(process_title)
current_process().name = process_title

Expand Down Expand Up @@ -68,16 +68,18 @@ def connect(self, port: Optional[str]):
if port:
# connect to serial port
self.ser = serial.serial_for_url(port, do_not_open=True)
self.ser.baudrate = 2400
try:
self.ser.open()
self.logger.log.info("Connected to serial port {}".format(port))
except (FileNotFoundError, serial.SerialException) as e:
self.logger.log.error(
"Could not open serial port {}:\n{}".format(port, e)
)
self._disconnected()
self.ser = None
if self.ser:
self.ser.baudrate = 2400
self.ser.timeout = 0.1 # Speed up waiting for a byte.
try:
self.ser.open()
self.logger.log.info("Connected to serial port {}".format(port))
except (FileNotFoundError, serial.SerialException) as e:
self.logger.log.error(
"Could not open serial port {}:\n{}".format(port, e)
)
self._disconnected()
self.ser = None
else:
self.ser = None

Expand All @@ -88,22 +90,23 @@ def handler(self):
self.ser and self.ser.is_open and self.port
): # If self.port is changing (via state_handler), we should stop.
try:
line = int.from_bytes(
self.ser.read(1), "big"
) # Endianness doesn't matter for 1 byte.
self.logger.log.info("Received from controller: " + str(line))
if line == 255:
self.ser.write(b"\xff") # Send 255 back, this is a keepalive.
elif line in [51, 52, 53]:
# We've received a status update about fader live status, fader is down.
self.sendToPlayer(line - 51, "SETLIVE:False")
elif line in [61, 62, 63]:
# We've received a status update about fader live status, fader is up.
self.sendToPlayer(line - 61, "SETLIVE:True")
elif line in [1, 3, 5]:
self.sendToPlayer(int(line / 2), "PLAYPAUSE")
elif line in [2, 4, 6]:
self.sendToPlayer(int(line / 2) - 1, "STOP")
if self.ser.in_waiting > 0:
line = int.from_bytes(
self.ser.read(1), "big"
) # Endianness doesn't matter for 1 byte.
self.logger.log.info("Received from controller: " + str(line))
if line == 255:
self.ser.write(b"\xff") # Send 255 back, this is a keepalive.
elif line in [51, 52, 53]:
# We've received a status update about fader live status, fader is down.
self.sendToPlayer(line - 51, "SETLIVE:False")
elif line in [61, 62, 63]:
# We've received a status update about fader live status, fader is up.
self.sendToPlayer(line - 61, "SETLIVE:True")
elif line in [1, 3, 5]:
self.sendToPlayer(int(line / 2), "PLAYPAUSE")
elif line in [2, 4, 6]:
self.sendToPlayer(int(line / 2) - 1, "STOP")
except Exception:
time.sleep(5)
self.connect(self.port)
Expand Down
2 changes: 1 addition & 1 deletion file_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def __init__(self, channel_from_q: Queue, server_config: StateManager):
self.logger = LoggingManager("FileManager")
self.api = MyRadioAPI(self.logger, server_config)

process_title = "File Manager"
process_title = "BAPSicle - File Manager"
setproctitle(process_title)
current_process().name = process_title

Expand Down
2 changes: 1 addition & 1 deletion helpers/state_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def __init__(
rate_limit_period_s=5,
):
# When a StateManager is shared via proxy to other processes, it has a thread itself.
process_title = "StateManager Proxy"
process_title = "BAPSicle - StateManager Proxy"
setproctitle(process_title)
current_process().name = process_title

Expand Down
16 changes: 10 additions & 6 deletions launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ def startServer(notifications=False):
time.sleep(1)
if server and server.is_alive():
if notifications and not sent_start_notif:
print("NOTIFICATION:Welcome to BAPSicle!")
notif("Welcome to BAPSicle!")
sent_start_notif = True
pass
else:
print("Server dead. Exiting.")
printer("Server dead. Exiting.")
if notifications:
print("NOTIFICATION:BAPSicle Server Stopped!")
notif("BAPSicle Server Stopped!")
sys.exit(0)

if server and server.is_alive():
Expand All @@ -50,6 +50,10 @@ def printer(msg: Any):
print("LAUNCHER:{}".format(msg))


def notif(msg: str):
print("NOTIFICATION:{}".format(msg))


if __name__ == "__main__":
# On Windows, calling this function is necessary.
# Causes all kinds of loops if not present.
Expand All @@ -58,20 +62,20 @@ def printer(msg: Any):
# If it's not here, multiprocessing just doesn't run in the package.
# Freeze support refers to being packaged with Pyinstaller.
multiprocessing.freeze_support()
setproctitle("BAPSicle Launcher")
setproctitle("BAPSicle - Launcher")
if len(sys.argv) > 1:
# We got an argument! It's probably Platypus's UI.
try:
if (sys.argv[1]) == "Start Server":
print("NOTIFICATION:BAPSicle is starting, please wait...")
notif("BAPSicle is starting, please wait...")
webbrowser.open("http://localhost:13500/")
startServer(notifications=True)
if sys.argv[1] == "Server":
webbrowser.open("http://localhost:13500/")
if sys.argv[1] == "Presenter":
webbrowser.open("http://localhost:13500/presenter/")
except Exception as e:
print(
printer(
"ALERT:BAPSicle failed with exception of type {}:{}".format(
type(e).__name__, e
)
Expand Down
Loading

0 comments on commit 7912b2c

Please sign in to comment.