/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package mxgstest.egse; import asim.envsim.dhpu.PacketToolIf; import asim.envsim.dhpu.Tc; import asim.envsim.dhpu.Tm; import asim.envsim.dhpu.tmutils.ObservationCollector; import asim.envsim.dhpu.tmutils.ParameterValueEvent; import asim.envsim.dhpu.tmutils.ParameterValueFilter; import common.BooleanHandler; import common.ConditionalEvent; import common.EnvironmentVariables; import egse.common.EGSECommon; import egse.common.time.Time; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.SimpleTimeZone; import java.util.Properties; import java.util.logging.Level; import java.util.logging.Logger; import mxgsegse.MXGSEGSE; import mxgstest.MxgsTestException; import mxgstest.mxgs.Hk; import mxgstest.Output; import mxgstest.mxgs.Dpu; import ptbridge.PacketTool; /** * * @author stet */ public class Basic { /** * Loads the EGSE and its properties. This should only be done once for every run. * @return instance of the egse with properties loaded. * @throws Throwable */ Output output = new Output(); Dpu dpu = new Dpu(); static final String ANSI_RESET = "\u001B[0m"; static final String ANSI_BLACK = "\u001B[30m"; static final String ANSI_RED = "\u001B[31m"; static final String ANSI_GREEN = "\u001B[32m"; static final String ANSI_YELLOW = "\u001B[33m"; static final String ANSI_BLUE = "\u001B[34m"; static final String ANSI_PURPLE = "\u001B[35m"; static final String ANSI_CYAN = "\u001B[36m"; static final String ANSI_WHITE = "\u001B[37m"; static String stepResult; static String stepComment; static boolean stepErr; // public static ObservationCollector auroralObsCol; // public static ObservationCollector backgroundObsCol; // public static ObservationCollector pulseheightObsCol; // public static ObservationCollector sampledObsCol; // public static ObservationCollector tgfObsCol; public MXGSEGSE loadEGSE() throws Throwable { // Load Properties file and create an instance of MXGSEGSE Properties properties = new Properties(); try { properties.load(new FileInputStream(new File("./config/mxgsegse.properties"))); } catch (IOException ex) { Logger.getLogger(MXGSEGSE.class.getName()).log(Level.SEVERE, "Could not load configuration", ex); } MXGSEGSE egse = new MXGSEGSE(properties); return egse; } private static class BootStatusEvent extends ConditionalEvent { private static class BootStatus implements ConditionalEvent.Condition { EGSECommon egse; BootStatus(EGSECommon svf) { this.egse = svf; } @Override public boolean check() { Tm tm = egse.dhpu.getMonitoredHkTm().getLast(); Byte bootStatus = 0; try { bootStatus = Byte.valueOf(egse.packetTool.getTmParameter(tm, "Packet Data.Boot Status")); } catch (PacketToolIf.PacketToolException ex) { //Logger.getLogger(Bvtc01Test.class.getName()).log(Level.SEVERE, null, ex); } return (bootStatus & 0x40) != 0; } } public BootStatusEvent(EGSECommon svf) { super(svf.dhpu.getMonitoredHkEvent(), new BootStatus(svf), null); } } /** * Wait for BootStatusEvent. * @param egse * @throws Throwable */ public void waitForBoot(MXGSEGSE egse) throws Throwable{ BootStatusEvent bsEvent = new BootStatusEvent(egse); egse.control.executeUntil(bsEvent, new Time(30)); } /** * Sends a RebootTC package. * @param egse * @throws Throwable */ public void sendRebootTc(MXGSEGSE egse) throws Throwable { egse.dhpu.sendNormalTc(egse.packetTool.getTc("RebootTC")); } /** * * @param egse * @param command * @param parameter * @param value * @throws Throwable */ public void setTcParameter(MXGSEGSE egse, String command, String parameter, int value) throws Throwable { egse.packetTool.setTcParameter(command, parameter, Integer.toString(value)); } /** * Sends a command and verifies the result code from a commmand verification packet. * @param egse * @param wait maximum wait time for response (seconds) * @param command TC command to send. * @param result expected result code. * @throws Throwable */ public void sendCommandAndVerify(MXGSEGSE egse, double wait, String command, int result) throws Throwable { //Create the Nonscience queue: ParameterValueEvent hkEvent = new ParameterValueEvent(egse.dhpu.getNonscienceTm(), egse.packetTool, "Private Header.Packet Identification.TM Type", String.valueOf(0x10), Logger.global); ParameterValueFilter hkTm = new ParameterValueFilter(hkEvent, Logger.global); egse.dhpu.sendNormalTc(egse.packetTool.getTc(command)); while((hkTm.size() < 1) && (wait > 0)) { Thread.sleep(100); wait = wait - 0.1; } if(wait <= 0){ throw new MxgsTestException("No response received from sendCommandAndVerify: " + command + " (Timeout)."); } int resultCode = Integer.valueOf(egse.packetTool.getTmParameter(hkTm.removeLast(), "Packet Data.Result")); if(result != resultCode){ throw new MxgsTestException("Failed comparing result in sendCommandAndVerify. Expected: "+result+". Recieved: "+resultCode); } } /** * * @param egse * @throws Throwable */ public void verifyEmptyEDLF(MXGSEGSE egse) throws Throwable { BooleanHandler emptyEdlfMonitor = new BooleanHandler(Logger.global, "Empty edlf"); egse.dhpu.getEmptyEDLFEvent().addHandler(emptyEdlfMonitor); emptyEdlfMonitor.reset(); egse.control.executeUntil(egse.dhpu.getEmptyEDLFEvent(), new Time(10)); if(!emptyEdlfMonitor.wasRaised()) { output.printStepOut("Not Received", "Failed", "Did not receive empty EDLF", false); throw new MxgsTestException("Did not receive empty EDLF."); } else { System.out.println(" Received empty EDLF frame: "+ANSI_GREEN+"OK"+ANSI_RESET); output.printStepOut("Received", "OK", "Verified at EGSE level", true); } } /** * Perform a RebootTC and verify the housekeeping parameters. * @param egse * @param wait maximum wait time for response (seconds) * @throws Throwable */ public void rebootAndVerify(MXGSEGSE egse, double wait) throws Throwable{ System.out.println(" Performing: RebootTC and verify HK params"); Hk hke = new Hk(); Basic bas = new Basic(); sendRebootTc(egse); output.printStepStart("", "Empty EDLF Frame - Reboot"); bas.verifyEmptyEDLF(egse); waitForBoot(egse); hke.protocolResync(egse, 2*wait); output.printStepStart("", "Start Cycle Request"); // dpu.sendStartCycleRequestTc(egse); output.printStepStart("", "Monitored HK"); hke.verifyMonHKReceiving(egse, wait); Thread.sleep(1000); String[][] MonHK = { {"Alive Counter", "0", "0"}, {"Boot Status", "67", "0"}, {"SW Mode", "0", "0"}, {"SW Submode", "0", "0"}, {"Errors and Warnings", "0", "0"} }; hke.verifyMonHKarr(egse, 20, MonHK, 4); } /** * * @throws Throwable */ public void notImplementedYet() throws Throwable { throw new MxgsTestException("Test failed becuase it is not implemented yet."); } /** * * @throws Throwable */ public void dontThrowAnything() throws Throwable { //This function is a dummy throw -> not throwing anything. } /** * */ public void resetTcParameters() { Properties properties = new Properties(); EnvironmentVariables environment = new EnvironmentVariables(); if (PacketTool.Load_TC_Packet_Value_Description(new File(environment.expand(properties.getProperty("TC.values"))).getAbsolutePath()) != 0) { //logger.log(Level.SEVERE, "Could not load default TC Values"); System.out.println(" Could not reset TC Parameters."); } System.out.println(" TC Parameters successfully reset to default values."); } /** * Calculates UTC year, seconds within year and millis witin second from gettime * @return time * @author frec */ public long[] time() { Date date = new Date(); SimpleDateFormat ft = new SimpleDateFormat("yyyy"); ft.setTimeZone(new SimpleTimeZone(SimpleTimeZone.UTC_TIME, "UTC")); String year=ft.format(date); SimpleDateFormat ft1 = new SimpleDateFormat("yyyy MM dd"); ft1.setTimeZone(new SimpleTimeZone(SimpleTimeZone.UTC_TIME, "UTC")); Date yearBegin = new Date(0); try { yearBegin = ft1.parse(year+" 1 1"); } catch (ParseException ex) { Logger.getLogger(Hk.class.getName()).log(Level.SEVERE, null, ex); } long millisWithinYear=date.getTime()-yearBegin.getTime(); long secondsWithinYear=millisWithinYear/1000; long millis=millisWithinYear % 1000; long time[]={Long.valueOf(year),secondsWithinYear,millis}; return time; } public static long tcp_start=0; public static long sec_start=0; public static long year_start=0; /** * Send a custom TC (array of chars) * @param egse * @param tcCharArray array of chars creating the TC * @throws Throwable */ public void sendCustomTc(MXGSEGSE egse, char[] tcCharArray) throws Throwable { //Create Byte array with same lenth as char array: byte[] tcByteArray = new byte[tcCharArray.length]; //cast CHAR to BYTE for full TC array: for (int i = 0; i < tcCharArray.length; i++) { tcByteArray[i] = (byte)tcCharArray[i]; } //Send TC: Tc customTc = new Tc(tcByteArray, "Custom TC" ); egse.dhpu.sendNormalTc(customTc); System.out.println(" Custom TC send."); } /** * Send a custom TC and wait for verification report. * @param egse * @param wait maximum wait time for verification report (seconds) * @param tcCharArray array of chars creating the TC * @param result expected result code * @throws Throwable */ public void verifyCustomTc(MXGSEGSE egse, double wait, char[] tcCharArray, int result) throws Throwable { System.out.println(" Verifying custom TC"); //Create the Nonscience queue and filter Command verification report (0x10): ParameterValueEvent hkEvent = new ParameterValueEvent(egse.dhpu.getNonscienceTm(), egse.packetTool, "Private Header.Packet Identification.TM Type", String.valueOf(0x10), Logger.global); ParameterValueFilter hkTm = new ParameterValueFilter(hkEvent, Logger.global); //Create Byte array with same lenth as char array: byte[] tcByteArray = new byte[tcCharArray.length]; //cast CHAR to BYTE for full TC array: for (int i = 0; i < tcCharArray.length; i++) { tcByteArray[i] = (byte)tcCharArray[i]; } egse.dhpu.getNonscienceTm().clear(); hkTm.clear(); Tc customTc = new Tc(tcByteArray, "Custom TC" ); egse.dhpu.sendNormalTc(customTc); while((hkTm.size() < 1) && (wait > 0)) { Thread.sleep(100); wait = wait - 0.1; } int resultCode = 0; if(wait <= 0){ if(egse.dhpu.getNonscienceTm().size()==0) { stepErr=false; stepResult="Failed"; stepComment="No response (Timeout)"; System.out.println(ANSI_RED+"No response received from Custom TC (Timeout)."+ANSI_RESET); } else { String hexStr = egse.dhpu.getNonscienceTm().getLast().hex(); int lengthStr = hexStr.length(); resultCode=Integer.parseInt(hexStr.substring(lengthStr-6, lengthStr-4)+hexStr.substring(lengthStr-3, lengthStr-1),16); if(result != resultCode){ stepErr=false; stepResult="Failed"; stepComment = egse.dhpu.getNonscienceTm().getLast().toString(); System.out.println("Failed comparing Result. "+ANSI_RED+"Expected: "+result+". Received: "+resultCode+ANSI_RESET); } else { stepErr=true; stepResult="OK"; stepComment = egse.dhpu.getNonscienceTm().getLast().toString(); System.out.println(" Custom TC: "+ANSI_GREEN+"OK"+ANSI_RESET); } stepComment = egse.dhpu.getNonscienceTm().getLast().toString(); } } else { resultCode = Integer.valueOf(egse.packetTool.getTmParameter(hkTm.removeLast(), "Packet Data.Result")); if(result != resultCode){ stepErr=false; stepResult="Failed"; stepComment=""; System.out.println("Failed comparing Result. "+ANSI_RED+"Expected: "+result+". Recieved: "+resultCode+ANSI_RESET); } else { stepErr=true; stepResult="OK"; stepComment=""; System.out.println(" Custom TC: "+ANSI_GREEN+"OK"+ANSI_RESET); } } output.printStepOut("Result", result, resultCode, stepResult, stepComment, stepErr); } public int[] readDHPU() throws Throwable { int[] ret={0,0,0,0,0}; try { String s = null; Process p = Runtime.getRuntime().exec("/home/egse/SVN-Projects/ASIM-SW/MXGS/Scripts/tempMonRaw.py"); BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream())); BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream())); // System.out.println("Here is the standard output of the command:\n"); int i=0; while ((s = stdInput.readLine()) != null) { // System.out.println(s); if(i<5) { ret[i]=(int)Math.round(Double.valueOf(s)); // System.out.println(ret[i]+" "+(int)Math.round(Double.valueOf(s))); } i++; } // System.out.println("Here is the standard error of the command (if any):\n"); while ((s = stdError.readLine()) != null) { System.out.println(s); } } catch (IOException e) { System.out.println("exception happened - here's what I know: "); System.exit(-1); } return ret; } }