From a72beda043e11233ba144eccafc9615f5097d8b9 Mon Sep 17 00:00:00 2001 From: gac-S_Changer Date: Tue, 2 Jul 2019 10:44:48 +0200 Subject: [PATCH] Generation of events file --- src/main/java/ch/psi/mxsc/Controller.java | 66 ++++++++++++++++++++--- 1 file changed, 60 insertions(+), 6 deletions(-) diff --git a/src/main/java/ch/psi/mxsc/Controller.java b/src/main/java/ch/psi/mxsc/Controller.java index 4fd7efa..872e129 100644 --- a/src/main/java/ch/psi/mxsc/Controller.java +++ b/src/main/java/ch/psi/mxsc/Controller.java @@ -4,6 +4,7 @@ package ch.psi.mxsc; import ch.psi.mxsc.Puck.PuckType; +import ch.psi.pshell.core.CommandInfo; import ch.psi.pshell.core.Context; import ch.psi.pshell.core.DevicePool; import ch.psi.pshell.core.DevicePoolListener; @@ -15,13 +16,19 @@ import ch.psi.pshell.device.GenericDevice; import ch.psi.pshell.device.ReadbackDevice; import ch.psi.pshell.ui.App; import ch.psi.pshell.ui.Panel; +import ch.psi.utils.Arr; import ch.psi.utils.Audio; +import ch.psi.utils.Chrono; import ch.psi.utils.State; import ch.psi.utils.swing.SwingUtils; import java.awt.BorderLayout; import java.awt.Dimension; import java.io.File; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardOpenOption; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -228,19 +235,28 @@ public class Controller { } public Sample getMountedSample() throws Exception{ - currentMountedSample = (String) Context.getInstance().evalLineBackground("get_setting('mounted_sample_position')"); + String mountedSample = (String) Context.getInstance().evalLineBackground("get_setting('mounted_sample_position')"); + checkMountedSampleChange(currentMountedSample, mountedSample); + currentMountedSample = mountedSample; Sample sample = basePlate.getSampleByName(currentMountedSample); return sample; } public void resetMountedSample() throws Exception{ + String mountedSample = (String) Context.getInstance().evalLineBackground("get_setting('mounted_sample_position')"); + checkMountedSampleChange(currentMountedSample, null); Context.getInstance().evalLineBackground("set_setting('mounted_sample_position', None)"); } - + void checkMountedSampleChange(String former, String current){ + if (((current == null) && (former!=null)) || ((current != null) && (!current.equals(former)))){ + logSampleMounted(former, current); + } + } + public void onStateChange(State state, State former) { if (state == State.Initializing) { - getMainFrame().removeDevice(basePlate); + getMainFrame().removeDevice(basePlate); } else if (state == State.Ready) { refreshSamplesTable(); try { @@ -258,8 +274,8 @@ public class Controller { basePlate.resetLoadedSample(); } getMainFrame().refresh(); - } + logStateChange(state); } void onTimer() { @@ -765,13 +781,15 @@ public class Controller { public void onPuckInserted(Puck puck) { playSound("mounted"); String datamatrix = getMainFrame().getPuckDatamatrix(); + logPuckDetectionChange(puck, true); onPuckScanned(null); - onPuckMounted(puck, datamatrix); + onPuckMounted(puck, datamatrix); } public void onPuckRemoved(Puck puck) { playSound("unmounted"); - onPuckUnmounted(puck); + logPuckDetectionChange(puck, false); + onPuckUnmounted(puck); } void linkPuckDatamatrix(Puck puck, String datamatrix, boolean showMessage) { @@ -953,4 +971,40 @@ public class Controller { } } } + + void logSampleMounted(String former, String current){ + logEvent((current!=null) ? "Sample Mounted" : "Sample Unmounted", former, current); + } + + void logPuckDetectionChange(Puck puck, boolean mounted){ + logEvent(mounted ? "Puck Mounted" : "Puck Unmounted", puck.getName(), isPuckLoading() ? "Puck Loading" : ""); + } + + void logStateChange(State state){ + String command = ""; + if (state==State.Busy){ + CommandInfo info = Context.getInstance().getCommandManager().getCurrentCommand(true); + if (info != null){ + command = (info.script != null) ? info.script : info.command; + } + } + logEvent("State Change", state.toString(), command); + } + + public void logEvent(String... event){ + try{ + long now = System.currentTimeMillis(); + Path path = Paths.get(Context.getInstance().getSetup().getOutputPath(), "events", Chrono.getTimeStr(now,"YYYYMMdd") + ".txt"); + path.toFile().getParentFile().mkdirs(); + + String[] data = new String[]{ + Chrono.getTimeStr(now, "dd/MM/YY HH:mm:ss.SSS"), + }; + data = Arr.append(data, event); + Files.write(path, String.join("\t", data).getBytes(), path.toFile().exists() ? StandardOpenOption.APPEND : StandardOpenOption.CREATE); + Files.write(path, System.lineSeparator().getBytes(), StandardOpenOption.APPEND); + } catch(Exception ex){ + Logger.getLogger(Controller.class.getName()).log(Level.WARNING, null, ex); + } + } }