Files
ErhaCam/src/main/java/id/co/gtc/erhacam/MainApplication.java
2025-05-19 14:09:39 +07:00

125 lines
4.6 KiB
Java

package id.co.gtc.erhacam;
import Config.SomeCodes;
import Database.PhotoCleaner;
import SecureDongle.SecureDongle;
import SecureDongle.SecureDongleEvent;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.stage.Screen;
import javafx.stage.Stage;
import org.tinylog.Logger;
import java.io.IOException;
import java.util.Map;
import static Config.SomeCodes.ShowAlert;
import static Config.SomeCodes.config;
public class MainApplication extends Application {
PhotoCleaner photoCleaner;
@Override
public void start(Stage stage) throws IOException {
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
System.out.println("ShutdownHook Checking running threads...");
Map<Thread, StackTraceElement[]> allThreads = Thread.getAllStackTraces();
for(Thread thread : allThreads.keySet()){
//System.out.println("Thread: " + thread.getName()+", State: " + thread.getState()+", Daemon: " + thread.isDaemon());
thread.interrupt();
}
if (photoCleaner!=null) photoCleaner.Stop();
}));
SecureDongle sd = new SecureDongle((short)0x4B30, (short)0xA66C, (short)0x3109, (short)0x37B1);
if (sd.Find()){
if (sd.Open()){
String UserID = Integer.toHexString(sd.ReadUserID()) ;
sd.Close();
if (UserID.equals("14022025")){
Logger.info("Secure Dongle UserID valid");
FXMLLoader fxmlLoader = new FXMLLoader(MainApplication.class.getResource("main-view.fxml"));
Screen screen = Screen.getPrimary();
Rectangle2D screenbound = screen.getBounds();
Scene scene = new Scene(fxmlLoader.load(), screenbound.getWidth(), screenbound.getHeight());
stage.setTitle("MultiCam Capture App for ERHA 19052025-001");
stage.setScene(scene);
stage.setResizable(true);
stage.setMaximized(true);
stage.setOnCloseRequest(e->{
sd.StopMonitor();
config.Save();
MainView mainView = fxmlLoader.getController();
mainView.Unload();
Logger.info("Application closed");
});
SomeCodes.LoadQRReader();
Detectors.LoadAllDetectors();
stage.show();
AutoCloseAlert.init();
Logger.info("Application started");
sd.setEvent(new SecureDongleEvent() {
@Override
public void onDongleMissing() {
Logger.error("Secure Dongle Missing");
Platform.runLater(()->{
ShowAlert(Alert.AlertType.ERROR, "Secure Dongle Missing", "Secure Dongle Missing", "Secure Dongle Missing");
Platform.exit();
});
}
@Override
public void onDongleError(String function, int errorCode) {
}
});
sd.StartMonitor();
photoCleaner = new PhotoCleaner(90);
photoCleaner.Start();
} else {
ShowAlert(Alert.AlertType.ERROR, "Secure Dongle UserID not valid", "Secure Dongle UserID not valid", "Secure Dongle UserID not valid");
Logger.error("Secure Dongle UserID not valid");
Platform.exit();
}
} else {
ShowAlert(Alert.AlertType.ERROR, "Secure Dongle cannot be opened", "Secure Dongle cannot be opened", "Secure Dongle cannot be opened");
Logger.error("Secure Dongle cannot be opened");
Platform.exit();
}
} else {
ShowAlert(Alert.AlertType.ERROR, "Secure Dongle not found", "Secure Dongle not found", "Secure Dongle not found");
Logger.error("Secure Dongle not found");
Platform.exit();
}
}
@Override
public void stop() throws Exception {
Logger.info("Application stopped");
super.stop();
}
public static void main(String[] args) {
SomeCodes.ExtractResource("/tinylog.properties");
launch();
}
}