Skip to content

Commit

Permalink
Gui updates - make it easier to see what's happening
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-xo committed Apr 23, 2020
1 parent 765825a commit 607ca13
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 47 deletions.
2 changes: 1 addition & 1 deletion src/main/java/am/xo/cdjscrobbler/CDJScrobbler.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ public void run() {
oconfig.setIsGui(true);
EventQueue.invokeLater(() -> {
CDJScrobblerGui gui = new CDJScrobblerGui(o);
o.addCDJScrobblerReadyListener(gui);
o.addMessageListener(gui);
gui.setVisible(true);
});
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/am/xo/cdjscrobbler/CDJScrobblerGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import java.awt.*;
import java.awt.event.ActionEvent;

public class CDJScrobblerGui extends JFrame implements CDJScrobblerReadyListener {
public class CDJScrobblerGui extends JFrame implements OrchestratorListener {
static final Logger logger = LoggerFactory.getLogger(CDJScrobblerGui.class);

private final Orchestrator o;
Expand Down Expand Up @@ -119,8 +119,8 @@ private void createLayout(JComponent... arg) {
}

@Override
public void cdjScrobblerReady() {
readyLabel.setText("CDJ Scrobbler Ready");
public void cdjScrobblerMessage(String message) {
readyLabel.setText(message);
}

private class LfmCheckBoxAction extends AbstractAction {
Expand Down
96 changes: 60 additions & 36 deletions src/main/java/am/xo/cdjscrobbler/Orchestrator.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.deepsymmetry.beatlink.dbserver.ConnectionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.helpers.MessageFormatter;

import java.io.IOException;
import java.net.UnknownHostException;
Expand Down Expand Up @@ -92,8 +93,8 @@ public OrchestratorConfig getConfig() {
@Override
public void run() {

logger.info("💿📀💿📀 CDJ Scrobbler v{} by Ben XO", config.getVersion());
logger.info("💿📀💿📀 https://github.com/ben-xo/cdjscrobbler");
info("💿📀💿📀 CDJ Scrobbler v{} by Ben XO", config.getVersion());
info("💿📀💿📀 https://github.com/ben-xo/cdjscrobbler");

try {

Expand All @@ -110,14 +111,14 @@ public void run() {

// start two threads with a shared queue
// TODO: dynamically add and remove UpdateListeners as devices are announced
logger.info("Starting UpdateListener…");
info("Starting UpdateListener…");
updateListener = new UpdateListener(songEventQueue);
VirtualCdj.getInstance().addUpdateListener(updateListener);

startVirtualCdj();


logger.info("Starting QueueProcessor…");
info("Starting QueueProcessor…");

// this must happen after startVirtualCdj() because ArtFinder starts MetadataFinder
// final ArtworkPopup artworkPopup = new ArtworkPopup();
Expand All @@ -139,12 +140,14 @@ public void run() {
startTwitterClient();
}

info("CDJ Scrobbler ready!");

queueProcessor.start(); // this doesn't return until shutdown (or exception)

// TODO: queue processor should probably have its own thread.
} catch(ConfigException e) {
// config exceptions should be user friendly, so we don't print out a stack trace
logger.error("\nThere was a problem with the configuration file {}\n{}", confFile, e.getMessage());
error("\nThere was a problem with the configuration file {}\n{}", confFile, e.getMessage());
System.exit(-2);
} catch(Exception e) {
e.printStackTrace();
Expand Down Expand Up @@ -172,15 +175,15 @@ public boolean isCsvLoggerEnabled() {

public void startTwitterClient() throws IOException, ConfigException {
if(twitter == null) {
logger.info("Starting Twitter bot…");
info("Starting Twitter bot…");
twitter = getTwitterClient();
queueProcessor.addNowPlayingListener(twitter);
}
}

public void stopTwitterClient() throws IOException {
if(twitter != null) {
logger.info("Stopping Twitter bot…");
info("Stopping Twitter bot…");
queueProcessor.removeNowPlayingListener(twitter);
twitter = null;
}
Expand All @@ -190,7 +193,7 @@ public void stopTwitterClient() throws IOException {

public void startLastFmClient() throws IOException, ConfigException {
if(lfm == null) {
logger.info("Starting Last.fm Scrobbler…");
info("Starting Last.fm Scrobbler…");
lfm = getLfmClient();
queueProcessor.addNowPlayingListener(lfm);
queueProcessor.addScrobbleListener(lfm);
Expand All @@ -199,7 +202,7 @@ public void startLastFmClient() throws IOException, ConfigException {

public void stopLastFmClient() throws IOException {
if(lfm != null) {
logger.info("Stopping Last.fm Scrobbler…");
info("Stopping Last.fm Scrobbler…");
queueProcessor.removeNowPlayingListener(lfm);
queueProcessor.removeScrobbleListener(lfm);
lfm = null;
Expand All @@ -209,15 +212,15 @@ public void stopLastFmClient() throws IOException {
private CsvLogger csvLogger = null;
public void startCsvLogger() throws IOException {
if(csvLogger == null) {
logger.info("Logging the tracklist to {}", config.getCsvLoggerFilename());
info("Logging the tracklist to {}", config.getCsvLoggerFilename());
csvLogger = getCsvLogger();
queueProcessor.addScrobbleListener(csvLogger);
}
}

public void stopCsvLogger() {
if(csvLogger != null) {
logger.info("Stopped logging tracklist");
info("Stopped logging tracklist");
queueProcessor.removeScrobbleListener(csvLogger);
csvLogger = null;
}
Expand All @@ -227,7 +230,7 @@ public void stopCsvLogger() {

public void startDmcaAccountant() throws IOException {
if(dmcaAccountant == null) {
logger.info("Starting DMCA Accountant…");
info("Starting DMCA Accountant…");
dmcaAccountant = getDmcaAccountant();

// start the on air warning
Expand All @@ -239,7 +242,7 @@ public void startDmcaAccountant() throws IOException {

public void stopDmcaAccountant() {
if(dmcaAccountant != null) {
logger.info("Stopping DMCA Accountant…");
info("Stopping DMCA Accountant…");

// stop the on air warning
dmcaAccountant.interrupt();
Expand All @@ -249,27 +252,27 @@ public void stopDmcaAccountant() {
}
}

public static LastFmClient getLfmClient() throws IOException, ConfigException {
public LastFmClient getLfmClient() throws IOException, ConfigException {
LastFmClient lfm = getLastFmClientImpl(config.getLastFmClientConfig());
try {
lfm.ensureUserIsConnected();
} catch (CallException e) {
if (e.getCause() instanceof UnknownHostException) {
logger.warn("** Looks like we're offline. Scrobbling disabled. **");
warn("** Looks like we're offline. Scrobbling disabled. **");
} else {
throw e;
}
}
return lfm;
}

public static TwitterClient getTwitterClient() throws IOException, ConfigException {
public TwitterClient getTwitterClient() throws IOException, ConfigException {
TwitterClient twitter = getTwitterClientImpl(config.getTwitterClientConfig());
try {
twitter.ensureUserIsConnected();
} catch (OAuthException e) {
if (e.getCause() instanceof UnknownHostException) {
logger.warn("** Looks like we're offline. Tweeting disabled. **");
warn("** Looks like we're offline. Tweeting disabled. **");
} else {
throw e;
}
Expand All @@ -291,11 +294,11 @@ private static LastFmClient getLastFmClientImpl(LastFmClientConfig lastFmClientC
return new LastFmClient(lastFmClientConfig);
}

public static DmcaAccountant getDmcaAccountant() throws IOException {
public static DmcaAccountant getDmcaAccountant() {
return new DmcaAccountant();
}

public static CsvLogger getCsvLogger() throws IOException {
public static CsvLogger getCsvLogger() {
return new CsvLogger(config.getCsvLoggerFilename());
}

Expand All @@ -314,17 +317,17 @@ private void startVirtualCdj() throws InterruptedException {

boolean started;

logger.info("Starting VirtualCDJ…");
info("Starting VirtualCDJ…");
started = false;
do {
try {
started = virtualCdj.start();
oldDeviceNumber = virtualCdj.getDeviceNumber();
} catch (Exception e) {
logger.warn("Failed to start.", e);
warn("Failed to start VirtualCdj.", e);
}
if (!started) {
logger.info("Retrying VirtualCdj");
info("Retrying VirtualCdj … (close RekordBox?)");
Thread.sleep(config.getRetryDelay());
}
} while (!started);
Expand All @@ -333,49 +336,70 @@ private void startVirtualCdj() throws InterruptedException {
updateVirtualCdjNumber();
deviceFinder.addDeviceAnnouncementListener(this);

logger.info("Starting MetadataFinder…");
info("Starting MetadataFinder…");
started = false;
do {
try {
metadataFinder.start();
started = metadataFinder.isRunning();
} catch (Exception e) {
logger.warn("Failed to start.", e);
warn("Failed to start MetadataFinder.", e);
}
if (!started) {
logger.info("Retrying MetadataFinder…");
info("Retrying MetadataFinder…");
Thread.sleep(config.getRetryDelay());
}
} while (!started);

logger.info("Starting CrateDigger…");
info("Starting CrateDigger…");
do {
try {
crateDigger.start();
} catch(Exception e) {
logger.error("CrateDigger error (retrying):", e);
error("CrateDigger error (retrying):", e);
Thread.sleep(config.getRetryDelay());
}
} while(!crateDigger.isRunning());

logger.info("Starting ArtFinder for tweeting cover art…");
info("Starting ArtFinder for tweeting cover art…");
do {
try {
artFinder.start();
} catch (Exception e) {
logger.error("ArtFinder error (retrying):", e);
error("ArtFinder error (retrying):", e);
Thread.sleep(config.getRetryDelay());
}
} while (!artFinder.isRunning());
}

private final List<OrchestratorListener> orchestratorListeners = new ArrayList<>();
public void addMessageListener(OrchestratorListener l) {
orchestratorListeners.add(l);
queueProcessor.addMessageListener(l);
}

cdjScrobblerReadyListeners.forEach((listener) -> listener.cdjScrobblerReady());
protected void info(String m, Object... args) {
logger.info(m, args);
orchestratorListeners.forEach((listener) -> listener.cdjScrobblerMessage(
MessageFormatter.arrayFormat(m, args).getMessage()
));
}

private final List<CDJScrobblerReadyListener> cdjScrobblerReadyListeners = new ArrayList<>();
public void addCDJScrobblerReadyListener(CDJScrobblerReadyListener l) {
cdjScrobblerReadyListeners.add(l);
public void warn(String m, Object... args) {
logger.warn(m, args);
orchestratorListeners.forEach((listener) -> listener.cdjScrobblerMessage(
MessageFormatter.arrayFormat(m, args).getMessage()
));
}

protected void error(String m, Object... args) {
logger.error(m, args);
orchestratorListeners.forEach((listener) -> listener.cdjScrobblerMessage(
MessageFormatter.arrayFormat(m, args).getMessage()
));
}


/**
* Looks for a device number <= 4 that we can use for the MetadataFinder.
* <p>
Expand Down Expand Up @@ -412,7 +436,7 @@ public void started(LifecycleParticipant sender) {

@Override
public void stopped(LifecycleParticipant sender) {
logger.info("Attempting to restart because {} stopped", sender);
info("Attempting to restart because {} stopped", sender);
try {
startVirtualCdj();
} catch (InterruptedException e) {
Expand All @@ -439,9 +463,9 @@ void updateVirtualCdjNumber() {
try {
byte newDeviceNumber = getFreeLowDeviceNumber();
VirtualCdj.getInstance().setDeviceNumber(newDeviceNumber);
logger.info("Set virtual CDJ device number to {}", newDeviceNumber);
info("Set virtual CDJ device number to {}", newDeviceNumber);
} catch (IllegalStateException e) {
logger.error("only 1 device no free devices?");
error("only 1 device no free devices?");
}
} else {
VirtualCdj.getInstance().setDeviceNumber(oldDeviceNumber);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@

package am.xo.cdjscrobbler;

public interface CDJScrobblerReadyListener {
void cdjScrobblerReady();
public interface OrchestratorListener {
void cdjScrobblerMessage(String message);
}
Loading

0 comments on commit 607ca13

Please sign in to comment.