Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Ward authored and Ward committed May 21, 2018
1 parent 6e67ba3 commit a351496
Show file tree
Hide file tree
Showing 13 changed files with 1,116 additions and 73 deletions.
1 change: 1 addition & 0 deletions nbproject/build-impl.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ is divided into following sections:
-->
<project xmlns:j2seproject1="http://www.netbeans.org/ns/j2se-project/1" xmlns:j2seproject3="http://www.netbeans.org/ns/j2se-project/3" xmlns:jaxrpc="http://www.netbeans.org/ns/j2se-project/jax-rpc" basedir=".." default="default" name="Formula1GUI-impl">
<import file="build-native.xml"/>
<fail message="Please build using Ant 1.8.0 or higher.">
<condition>
<not>
Expand Down
953 changes: 953 additions & 0 deletions nbproject/build-native.xml

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions nbproject/genfiles.properties
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
build.xml.data.CRC32=1909cdc9
build.xml.data.CRC32=af02e548
build.xml.script.CRC32=6d7de45a
build.xml.stylesheet.CRC32=8064a381@1.74.1.48
# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
nbproject/build-impl.xml.data.CRC32=1909cdc9
nbproject/build-impl.xml.script.CRC32=1b987ef3
nbproject/build-impl.xml.data.CRC32=af02e548
nbproject/build-impl.xml.script.CRC32=c8d92b36
nbproject/build-impl.xml.stylesheet.CRC32=876e7a8f@1.74.1.48
1 change: 1 addition & 0 deletions nbproject/project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ javadoc.windowtitle=
main.class=main.MainFrame
meta.inf.dir=${src.dir}/META-INF
mkdist.disabled=true
native.bundling.enabled=true
platform.active=default_platform
run.classpath=\
${javac.classpath}:\
Expand Down
3 changes: 3 additions & 0 deletions nbproject/project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
<project xmlns="http://www.netbeans.org/ns/project/1">
<type>org.netbeans.modules.java.j2seproject</type>
<configuration>
<buildExtensions xmlns="http://www.netbeans.org/ns/ant-build-extender/1">
<extension file="build-native.xml" id="j2sedeploy"/>
</buildExtensions>
<data xmlns="http://www.netbeans.org/ns/j2se-project/3">
<name>Formula1GUI</name>
<source-roots>
Expand Down
Binary file added resources/icon.ico
Binary file not shown.
Binary file added resources/splash.bmp
Binary file not shown.
10 changes: 8 additions & 2 deletions src/main/CSVReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,20 @@ public List<String> getNextRecordLine() throws IOException {
}

public String getNextRecordString(){
return currentLine.stream()
return currentLine == null ? "" :
currentLine.stream()
.map((item) -> item + " ")
.reduce("", String::concat)
.trim();
}

public Stream<String> getAllLines(){
return lines;
}

public void done() throws IOException{
fr.close();
if(fr != null)
fr.close();
}
private List<String> currentLine;
private final FileReader fr;
Expand Down
54 changes: 31 additions & 23 deletions src/main/DataReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,36 @@
import java.util.List;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.Arrays;


/*
* @since 04/11/2017
* @author wardm@campus.technion.ac.il
*/
public class DataReader {
public static final String DELIM = ",";
public DataReader(String fileName) throws InterruptedException, SerialReaderError {
FileWriter temp = null;
try {
temp = new FileWriter(fileName);
System.out.printf("File %s succesfuly opened\n", fileName);
} catch(IOException ex){
System.out.printf("Could not open file %s for logging\n", fileName);
System.out.printf("Exception: \n%s\n", ex.getMessage());
Logger.getLogger(DataReader.class.getName()).log(Level.SEVERE, null, ex);
}
fw = temp;
dr = new SerialReader();
currentLine = new LinkedList<>();

public DataReader(String fileName) throws InterruptedException, SerialReaderException {
FileWriter temp = null;
try {
temp = new FileWriter(fileName);
System.out.printf("File %s successfully opened for data loging\r\n", fileName);
} catch(IOException ex){
System.out.printf("Could not open file %s for logging\r\n", fileName);
}
fw = temp;
dr = new SerialReader();
currentLine = new LinkedList<>();
}

public List<String> getNextLineList(){
List<String> next = new LinkedList<>();
currentLine = next;
String line = dr.readLine();
if (line == null)
return next;
for(String item : line.split(DELIM)){
next.add(item);
}

return next;
next.addAll(Arrays.asList(line.split(DELIM)));
return next;
}

Expand All @@ -46,14 +42,26 @@ public String getNextLineString(){
result = currentLine.stream().map((item) -> item + " ").reduce(result, String::concat);
return result.trim();
}


public void log(final String m) {
if (fw == null)
return;
try {
fw.write(m);
fw.flush();
} catch (IOException ex) {
}
}
public void done() throws IOException{
fw.close();
fw.flush();
fw.close();
}
public List<String> getCurrentLine(){
return currentLine;
}

public String getPortName(){
return dr != null ? dr.getWindowsPortName() : "";
}
private List<String> currentLine;
private final FileWriter fw;
private final SerialReader dr;
Expand Down
131 changes: 99 additions & 32 deletions src/main/MainFrame.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
package main;

import static java.awt.EventQueue.invokeLater;
import java.awt.event.WindowEvent;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import static java.lang.Double.parseDouble;
import static java.lang.System.exit;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.StringTokenizer;
import static java.util.logging.Logger.getLogger;
import javax.swing.Timer;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;
import static javax.swing.JOptionPane.showMessageDialog;
import static javax.swing.JOptionPane.showMessageDialog;
import javax.swing.JPanel;
import javax.swing.Timer;
import static javax.swing.UIManager.getInstalledLookAndFeels;
import static javax.swing.UIManager.setLookAndFeel;
import static javax.swing.UIManager.setLookAndFeel;


public final class MainFrame extends javax.swing.JFrame {
Expand All @@ -24,49 +30,75 @@ public final class MainFrame extends javax.swing.JFrame {
* Creates new form MainFrame
*/
public MainFrame() {
setUpOutputStream();
initComponents();
inialize();
}

public void issueErrorMessage(final String msg){
System.err.print(msg);
showMessageDialog(errorPanel, msg, "Error", JOptionPane.OK_OPTION);
}

public void setUpOutputStream(){
try {
PrintStream s = makeStream(LOG_PATH);
System.setOut(s);
System.setErr(s);
} catch (FileNotFoundException ex) {
System.out.printf("Could not open %s for logging\r\n", LOG_PATH);
}
}

public void inialize(){
setSize(1360,830);
setResizable(false);

try {
// Uncomment following line to simulate reading data from a file
// presentationReader = new CSVReader("presentation.csv");
dataReader = new DataReader("log.txt");
foundCOM = true;
} catch (InterruptedException e){
e.printStackTrace();
exit(1);
} catch (SerialReaderError e){
System.out.println("Serial COM port initialization failed - terminating GUI");
showMessageDialog(errorPanel, "Could not find COM device", "Error", JOptionPane.OK_OPTION);
if (foundCOM == false) {
exit(1);
}
dataReader = new DataReader(DATA_LOG_PATH);
} catch (SerialReaderException | InterruptedException e){
issueErrorMessage("Could not detect COM port!\r\n" + e.toString());
exit(0);
} catch(Exception e){
issueErrorMessage("Unexpected exception caught!\r\n" + e.toString());
exit(0);
} catch (Error er){
issueErrorMessage("Error: " + er.toString());
exit(0);
}

System.out.printf("Initializing COM port was successful, using: %s\r\n", dataReader.getPortName());
System.out.printf("File %s used for logging data captured\r\n", DATA_LOG_PATH);
System.out.printf("Started listening to data\r\n");

timer = new Timer(DELAY, (ActionEvent e) -> {
List<String> currentInfo = null;
List<String> currentInfo;
if (presentationReader != null){
try {
currentInfo = presentationReader.getNextRecordLine();
} catch (IOException ex) {
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
issueErrorMessage("Could not read from presentation file!\r\n" + ex.getMessage());
timer.stop();
return;
}
} else if (dataReader != null){
currentInfo = dataReader.getNextLineList();
if (currentInfo == null || currentInfo.isEmpty())
return;
System.out.println(dataReader.getNextLineString());
} else {
timer.stop();
return;
}

if(currentInfo == null || currentInfo.size() != FIELDS_NUMBER)
if(currentInfo == null)
return;

if(currentInfo.size() != FIELDS_NUMBER){
System.out.println(dataReader.getNextLineString());
return;
}

dataReader.log(dataReader.getNextLineString());

susRF.setText(currentInfo.get(0));
susRR.setText(currentInfo.get(1));
susLF.setText(currentInfo.get(2));
Expand All @@ -86,6 +118,9 @@ public void inialize(){
});
timer.start();
}

private static final String LOG_PATH = "log.txt";
private static final String DATA_LOG_PATH = "data.txt";
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
Expand All @@ -94,6 +129,39 @@ public void inialize(){
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
// checking that the SteelSeries, Lightbulb and RXTX classes are found
try {
Class.forName("eu.hansolo.steelseries.gauges.Radial1Vertical");
} catch(ClassNotFoundException e) {
issueErrorMessage("Could not find SteelSeries library!\r\n" + e.getMessage());
exit(0);
}
try {
Class.forName("eu.hansolo.lightbulb.LightBulb");
} catch(ClassNotFoundException e) {
issueErrorMessage("Could not find LightBulb library!\r\n" + e.getMessage());
exit(0);
}

try {
Class.forName("gnu.io.RXTXVersion");
} catch(ClassNotFoundException e) {
issueErrorMessage("Could not find RXTX library!\r\n" + e.getMessage());
exit(0);
}

try {
System.loadLibrary("rxtxSerial");
} catch (UnsatisfiedLinkError e) {
if(!e.getMessage().contains("already")) {
issueErrorMessage("Native code for rxtxSerial.dll failed to load!\r\n" + e +
"\r\nPlease make sure rxtxSerial.dll is found in environmental variable PATH:"
+ Pattern.compile(";").splitAsStream(System.getProperty("java.library.path"))
.map(l -> l +"\r\n")
.reduce("", String::concat));
exit(0);
}
}

sWTToolkitHandler1 = new org.pushingpixels.trident.swt.SWTToolkitHandler();
velocity = new eu.hansolo.steelseries.gauges.Radial4Lcd();
Expand All @@ -118,7 +186,7 @@ private void initComponents() {
label3 = new java.awt.Label();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

velocity.setDoubleBuffered(true);
velocity.setName("velocity"); // NOI18N
velocity.setTitle("Avg Speed");
Expand Down Expand Up @@ -157,7 +225,6 @@ private void initComponents() {
gear.setHorizontalAlignment(javax.swing.JTextField.CENTER);
gear.setText("1");
gear.setName("gear"); // NOI18N
gear.addActionListener(this::gearActionPerformed);

oilAlert.setGlowColor(new java.awt.Color(255, 0, 0));
oilAlert.setName("oilAlert"); // NOI18N
Expand Down Expand Up @@ -315,10 +382,7 @@ private void initComponents() {

pack();
}// </editor-fold>

private void gearActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}


/**
* @param args the command line arguments
Expand All @@ -345,18 +409,21 @@ public static void main(String args[]) {
invokeLater(() -> {
new MainFrame().setVisible(true);
});
}
}
protected PrintStream makeStream(String name) throws FileNotFoundException {
return new PrintStream(new BufferedOutputStream(new FileOutputStream(name)), true);
}
public void windowClosed(WindowEvent e) {
try {
dataReader.done();
} catch(IOException e2){

}

exit(0);
}
final JPanel errorPanel = new JPanel();
private CSVReader presentationReader = null;
private DataReader dataReader = null;
private CSVReader presentationReader;
private DataReader dataReader;
private Timer timer;
public final static int FIELDS_NUMBER = 13;
final static int DELAY = 500;
Expand Down
Loading

0 comments on commit a351496

Please sign in to comment.