2024-09-20 11:13:04 +01:00

55 lines
1.8 KiB
Python

import os
import sys
import json
from dotenv import load_dotenv
from lib.mqtt import create_client, update_disc, control_player, create_config, check_current_disc
# Load the .env file
load_dotenv()
# Define the MQTT server details
broker = os.environ.get("broker")
port = int(os.environ.get("port"))
# MQTT username and password
username = os.environ.get("username")
password = os.environ.get("password")
print(f"Broker: {broker}, Port: {port}, Username: {username}")
client = create_client(broker, port, username, password)
create_config(client)
# Check if any arguments are passed
if len(sys.argv) > 1:
# Get the JSON argument from the command line
json_str = sys.argv[1]
try:
# Convert the JSON string into a dictionary
disc_data = json.loads(json_str.replace("'",'"'))
# Check if disc is the same as last inserted
if (disc_data == json.loads(check_current_disc(client))):
# If disc is the same, PLAY
control_player(client, "PLAY")
else:
# Pass the parsed data to the update_disc function
control_player(client, "PLAY")
update_disc(client, disc_data)
except json.JSONDecodeError as e:
try:
if json_str == "EJECT":
# PAUSE the current playing item
control_player(client, "PAUSE")
elif json_str == "PLAY":
# PLAY the current playing item
control_player(client, "PLAY")
elif json_str == "PAUSE":
# PAUSE the current playing item
control_player(client, "PAUSE")
else:
print(f"Can't process {json_str}")
except Exception:
print(f"Invalid JSON format: {e}")
else:
print("No Command or Track Info Passed")