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
|
2024-09-20 12:41:25 +01:00
|
|
|
input_str = sys.argv[1]
|
2024-09-20 07:49:08 +01:00
|
|
|
|
2024-09-20 12:52:23 +01:00
|
|
|
# Convert the JSON string into a dictionary
|
|
|
|
disc_data = input_str.split(":")
|
|
|
|
if disc_data[0] == "ytmusic":
|
|
|
|
disc_object = {
|
2024-09-20 14:13:21 +01:00
|
|
|
"name":disc_data[1],
|
2024-09-20 12:52:23 +01:00
|
|
|
"type":disc_data[2],
|
|
|
|
"id":disc_data[3]
|
|
|
|
}
|
|
|
|
# Check if disc is the same as last inserted
|
2024-09-20 14:15:53 +01:00
|
|
|
if (disc_object == check_current_disc(client)):
|
2024-09-20 12:52:23 +01:00
|
|
|
# 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)
|
|
|
|
else:
|
|
|
|
if input_str == "EJECT":
|
|
|
|
# PAUSE the current playing item
|
|
|
|
control_player(client, "PAUSE")
|
|
|
|
elif input_str == "PLAY":
|
|
|
|
# PLAY the current playing item
|
|
|
|
control_player(client, "PLAY")
|
|
|
|
elif input_str == "PAUSE":
|
|
|
|
# PAUSE the current playing item
|
|
|
|
control_player(client, "PAUSE")
|
|
|
|
else:
|
|
|
|
print(f"Can't process {input_str}")
|