2024-09-19 11:00:07 +01:00
|
|
|
import os
|
2024-09-19 11:00:35 +01:00
|
|
|
import sys
|
|
|
|
import json
|
2024-09-19 11:43:33 +01:00
|
|
|
from dotenv import load_dotenv
|
2024-09-20 11:13:04 +01:00
|
|
|
from lib.mqtt import create_client, update_disc, control_player, create_config, check_current_disc
|
2024-09-19 11:00:07 +01:00
|
|
|
|
2024-09-20 07:41:26 +01:00
|
|
|
# Load the .env file
|
2024-09-20 07:49:08 +01:00
|
|
|
load_dotenv()
|
2024-09-19 11:43:33 +01:00
|
|
|
|
2024-09-19 11:00:07 +01:00
|
|
|
# Define the MQTT server details
|
|
|
|
broker = os.environ.get("broker")
|
|
|
|
port = int(os.environ.get("port"))
|
|
|
|
# MQTT username and password
|
2024-09-20 07:49:08 +01:00
|
|
|
username = os.environ.get("username")
|
|
|
|
password = os.environ.get("password")
|
2024-09-19 11:00:07 +01:00
|
|
|
|
2024-09-20 07:41:26 +01:00
|
|
|
print(f"Broker: {broker}, Port: {port}, Username: {username}")
|
|
|
|
|
2024-09-19 11:00:07 +01:00
|
|
|
client = create_client(broker, port, username, password)
|
|
|
|
|
|
|
|
create_config(client)
|
|
|
|
|
2024-09-19 11:00:35 +01:00
|
|
|
# Check if any arguments are passed
|
|
|
|
if len(sys.argv) > 1:
|
|
|
|
# Get the JSON argument from the command line
|
|
|
|
json_str = sys.argv[1]
|
2024-09-20 07:49:08 +01:00
|
|
|
|
2024-09-19 11:00:35 +01:00
|
|
|
try:
|
|
|
|
# Convert the JSON string into a dictionary
|
2024-09-20 11:13:04 +01:00
|
|
|
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)
|
2024-09-19 11:00:35 +01:00
|
|
|
except json.JSONDecodeError as e:
|
2024-09-20 07:49:08 +01:00
|
|
|
try:
|
|
|
|
if json_str == "EJECT":
|
2024-09-20 11:13:04 +01:00
|
|
|
# PAUSE the current playing item
|
|
|
|
control_player(client, "PAUSE")
|
2024-09-20 07:49:08 +01:00
|
|
|
elif json_str == "PLAY":
|
2024-09-20 11:13:04 +01:00
|
|
|
# PLAY the current playing item
|
2024-09-20 07:49:08 +01:00
|
|
|
control_player(client, "PLAY")
|
2024-09-20 11:13:04 +01:00
|
|
|
elif json_str == "PAUSE":
|
|
|
|
# PAUSE the current playing item
|
|
|
|
control_player(client, "PAUSE")
|
2024-09-20 10:09:56 +01:00
|
|
|
else:
|
|
|
|
print(f"Can't process {json_str}")
|
2024-09-20 07:49:08 +01:00
|
|
|
except Exception:
|
|
|
|
print(f"Invalid JSON format: {e}")
|
2024-09-19 11:00:35 +01:00
|
|
|
else:
|
2024-09-20 10:09:56 +01:00
|
|
|
print("No Command or Track Info Passed")
|