forked from j62/ctbrec
1
0
Fork 0

Fire events from LocalRecorder

This commit is contained in:
0xboobface 2018-12-01 21:56:23 +01:00
parent 5b8cfc02d6
commit 80381c0d49
3 changed files with 62 additions and 28 deletions

View File

@ -48,6 +48,7 @@ import javafx.scene.control.Alert;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.image.Image;
import javafx.scene.media.AudioClip;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import okhttp3.Request;
@ -93,14 +94,7 @@ public class CamrecApplication extends Application {
createGui(primaryStage);
checkForUpdates();
new Thread(() -> {
try {
Thread.sleep(TimeUnit.SECONDS.toMillis(10));
} catch (InterruptedException e) {
e.printStackTrace();
}
Platform.runLater(() -> registerAlertSystem());
}).start();
registerAlertSystem();
}
private void logEnvironment() {
@ -211,28 +205,41 @@ public class CamrecApplication extends Application {
}
private void registerAlertSystem() {
Notification.Notifier.setNotificationOwner(primaryStage);
EventBusHolder.BUS.register(new Object() {
@Subscribe
public void modelEvent(Map<String, Object> e) {
try {
if (Objects.equals("model.status", e.get("event"))) {
String status = (String) e.get("status");
Model model = (Model) e.get("model");
LOG.debug("Alert: {} is {}", model.getName(), status);
if (Objects.equals("online", status)) {
Platform.runLater(() -> {
notifier.notifyInfo("Model Online", model.getName() + " is now online");
//AudioClip clip = new AudioClip("file:///tmp/Oxygen-Im-Highlight-Msg.mp3");
//clip.play();
});
new Thread(() -> {
try {
// don't register before 1 minute has passed, because directly after
// the start of ctbrec, an event for every online model would be fired,
// which is annoying as f
Thread.sleep(TimeUnit.MINUTES.toMillis(1));
} catch (InterruptedException e) {
e.printStackTrace();
}
LOG.debug("Alert System registered");
Platform.runLater(() -> {
Notification.Notifier.setNotificationOwner(primaryStage);
EventBusHolder.BUS.register(new Object() {
@Subscribe
public void modelEvent(Map<String, Object> e) {
LOG.debug("Alert: {}", e);
try {
if (Objects.equals("model.status", e.get("event"))) {
String status = (String) e.get("status");
Model model = (Model) e.get("model");
if (Objects.equals("online", status)) {
Platform.runLater(() -> {
notifier.notifyInfo("Model Online", model.getName() + " is now online");
AudioClip clip = new AudioClip(getClass().getResource("/Oxygen-Im-Highlight-Msg.mp3").toString());
clip.play();
});
}
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
});
});
}).start();
}
private void writeColorSchemeStyleSheet(Stage primaryStage) {

Binary file not shown.

View File

@ -36,6 +36,7 @@ import com.iheartradio.m3u8.ParseException;
import com.iheartradio.m3u8.PlaylistException;
import ctbrec.Config;
import ctbrec.EventBusHolder;
import ctbrec.Model;
import ctbrec.OS;
import ctbrec.Recording;
@ -179,6 +180,7 @@ public class LocalRecorder implements Recorder {
}
}
}.start();
fireRecordingStateChanged(model, true);
}
private void stopRecordingProcess(Model model) {
@ -188,6 +190,7 @@ public class LocalRecorder implements Recorder {
if(!Config.isServerMode()) {
postprocess(download);
}
fireRecordingStateChanged(model, false);
}
private void postprocess(Download download) {
@ -368,6 +371,7 @@ public class LocalRecorder implements Recorder {
} else {
postprocess(d);
}
fireRecordingStateChanged(m, false);
}
}
for (Model m : restart) {
@ -434,7 +438,11 @@ public class LocalRecorder implements Recorder {
List<Model> models = getModelsRecording();
for (Model model : models) {
try {
boolean wasOnline = model.isOnline();
boolean isOnline = model.isOnline(IGNORE_CACHE);
if(wasOnline != isOnline) {
fireModelOnlineStateChanged(model, isOnline);
}
LOG.trace("Checking online state for {}: {}", model, (isOnline ? "online" : "offline"));
if (isOnline && !isSuspended(model) && !recordingProcesses.containsKey(model)) {
LOG.info("Model {}'s room back to public", model);
@ -468,6 +476,25 @@ public class LocalRecorder implements Recorder {
}
LOG.debug(getName() + " terminated");
}
}
private void fireModelOnlineStateChanged(Model model, boolean online) {
Map<String, Object> evt = new HashMap<>();
evt.put("event", "model.status");
evt.put("status", online ? "online" : "offline");
evt.put("model", model);
EventBusHolder.BUS.post(evt);
LOG.debug("Event fired {}", evt);
}
private void fireRecordingStateChanged(Model model, boolean recording) {
Map<String, Object> evt = new HashMap<>();
evt.put("event", "recording.status");
evt.put("status", recording ? "started" : "stopped");
evt.put("model", model);
EventBusHolder.BUS.post(evt);
LOG.debug("Event fired {}", evt);
}
private class PostProcessingTrigger extends Thread {