auto-sync

This commit is contained in:
rusEfi 2015-03-07 18:07:55 -06:00
parent b76a8ac551
commit 215bfb64e2
7 changed files with 104 additions and 58 deletions

View File

@ -0,0 +1,30 @@
package com.rusefi;
/**
* (c) Andrey Belomutskiy
* 3/7/2015
*/
public interface Logger {
Logger STDOUT = new Logger() {
@Override
public void trace(String msg) {
System.out.println(msg);
}
@Override
public void info(String msg) {
System.out.println(msg);
}
@Override
public void error(String msg) {
System.err.println(msg);
}
};
void trace(String msg);
void info(String msg);
void error(String msg);
}

View File

@ -1,6 +1,7 @@
package com.rusefi.binaryprotocol;
import com.rusefi.ConfigurationImage;
import com.rusefi.Logger;
import com.rusefi.io.DataListener;
import com.rusefi.io.serial.SerialPortReader;
import etch.util.CircularByteBuffer;
@ -17,21 +18,23 @@ import java.util.Arrays;
public class BinaryProtocol {
private static final int BLOCKING_FACTOR = 256;
private static final byte RESPONSE_OK = 0;
private final Logger logger;
private final SerialPort serialPort;
private static final int BUFFER_SIZE = 10000;
final CircularByteBuffer cbb;
public BinaryProtocol(SerialPort serialPort) throws SerialPortException {
public BinaryProtocol(final Logger logger, SerialPort serialPort) throws SerialPortException {
this.logger = logger;
this.serialPort = serialPort;
cbb = new CircularByteBuffer(BUFFER_SIZE);
DataListener listener = new DataListener() {
@Override
public void onDataArrived(byte[] freshData) {
System.out.println(freshData.length + " byte(s) arrived");
logger.trace(freshData.length + " byte(s) arrived");
synchronized (cbb) {
if (cbb.size() - cbb.length() < freshData.length) {
System.out.println("buffer overflow not expected");
logger.error("buffer overflow not expected");
cbb.clear();
}
cbb.put(freshData);
@ -43,7 +46,7 @@ public class BinaryProtocol {
}
private void waitForBytes(int count) throws InterruptedException {
System.out.println("Waiting for " + count + " byte(s)");
logger.info("Waiting for " + count + " byte(s)");
synchronized (cbb) {
while (cbb.length() < count)
cbb.wait();
@ -90,7 +93,7 @@ public class BinaryProtocol {
public void sendCrcPacket(byte[] command) throws SerialPortException {
byte[] packet = makePacket(command);
System.out.println("Sending " + Arrays.toString(packet));
logger.info("Sending " + Arrays.toString(packet));
serialPort.writeBytes(packet);
}
@ -107,7 +110,7 @@ public class BinaryProtocol {
waitForBytes(2);
int packetSize = BinaryProtocol.swap16(cbb.getShort());
System.out.println("Got packet size " + packetSize);
logger.trace("Got packet size " + packetSize);
if (packetSize < 0 || packetSize > 300) {
// invalid packet size
return null;
@ -124,10 +127,10 @@ public class BinaryProtocol {
boolean isCrcOk = actualCrc == packetCrc;
if (!isCrcOk) {
System.out.println(String.format("%x", actualCrc) + " vs " + String.format("%x", packetCrc));
logger.trace(String.format("%x", actualCrc) + " vs " + String.format("%x", packetCrc));
return null;
}
System.out.println("packet " + Arrays.toString(packet) + ": crc OK");
logger.trace("packet " + Arrays.toString(packet) + ": crc OK");
return packet;
}
@ -150,7 +153,7 @@ public class BinaryProtocol {
byte[] response = receivePacket();
if (response == null || response.length == 0 || response[0] != RESPONSE_OK || response.length != requestSize + 1) {
System.out.println("Something is wrong, retrying...");
logger.error("Something is wrong, retrying...");
continue;
}
@ -159,6 +162,6 @@ public class BinaryProtocol {
offset += requestSize;
}
System.out.println("Got image!");
logger.info("Got image!");
}
}

View File

@ -1,6 +1,7 @@
package com.rusefi.binaryprotocol;
import com.rusefi.ConfigurationImage;
import com.rusefi.Logger;
import com.rusefi.io.DataListener;
import com.rusefi.io.serial.PortHolder;
import com.rusefi.io.serial.SerialPortReader;
@ -27,17 +28,18 @@ public class BinaryProtocolCmd {
String port = args[0];
Logger logger = Logger.STDOUT;
serialPort = new SerialPort(port);
boolean opened = serialPort.openPort();
if (!opened) {
System.out.println("failed to open " + port);
logger.error("failed to open " + port);
}
BinaryProtocol bp = new BinaryProtocol(serialPort);
BinaryProtocol bp = new BinaryProtocol(logger, serialPort);
PortHolder.setupPort(serialPort, 38400);
System.out.println("Looks good");
logger.info("Looks good");
bp.sendQueryCommand();
bp.receivePacket();

View File

@ -17,7 +17,7 @@ public class EraseChip extends ProcessStatusWindow {
"Are you sure?", JOptionPane.YES_NO_OPTION);
if (dialogResult != JOptionPane.YES_OPTION)
return;
showFrame();
wnd.showFrame("rusEfi Firmware Flasher");
submitAction(new Runnable() {
@Override
public void run() {

View File

@ -1,7 +1,6 @@
package com.rusefi.maintenance;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
@ -25,7 +24,7 @@ public class FirmwareFlasher extends ProcessStatusWindow {
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
showFrame();
wnd.showFrame("rusEfi Firmware Flasher");
Runnable runnable = new Runnable() {
@Override
@ -40,14 +39,14 @@ public class FirmwareFlasher extends ProcessStatusWindow {
private void doFlashFirmware() {
if (!new File(IMAGE_FILE).exists()) {
appendMsg(IMAGE_FILE + " not found, cannot proceed !!!");
wnd.appendMsg(IMAGE_FILE + " not found, cannot proceed !!!");
return;
}
StringBuffer error = executeCommand(OPEN_OCD_COMMAND);
if (error.toString().contains(SUCCESS_MESSAGE_TAG) && !error.toString().contains(FAILED_MESSAGE_TAG)) {
appendMsg("!!! Looks good!!!");
wnd.appendMsg("!!! Looks good!!!");
} else {
appendMsg("!!! FIRMWARE FLASH: DOES NOT LOOK RIGHT !!!");
wnd.appendMsg("!!! FIRMWARE FLASH: DOES NOT LOOK RIGHT !!!");
}
}

View File

@ -1,32 +1,19 @@
package com.rusefi.maintenance;
import com.rusefi.ui.util.FrameHelper;
import com.rusefi.ui.util.UiUtils;
import com.rusefi.ui.StatusWindow;
import javax.swing.*;
import java.awt.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class ProcessStatusWindow {
protected final JTextArea log = new JTextArea();
private final JScrollPane messagesScroll = new JScrollPane(log, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED) {
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
};
public ProcessStatusWindow() {
log.setLineWrap(true);
}
public static boolean isWindows() {
return System.getProperty("os.name").toLowerCase().contains("win");
}
protected final StatusWindow wnd = new StatusWindow();
protected static boolean isRunning(Process p) {
try {
p.exitValue();
@ -36,24 +23,6 @@ public class ProcessStatusWindow {
}
}
protected void showFrame() {
FrameHelper f = new FrameHelper();
f.getFrame().setTitle("rusEfi Firmware Flasher");
f.showFrame(messagesScroll, false);
UiUtils.centerWindow(f.getFrame());
log.setText(""); // let's remove stuff from previous invocation
}
protected void appendMsg(final String s) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
log.append(s + "\r\n");
UiUtils.trueLayout(log);
}
});
}
/**
* This method listens to a data stream from the process, appends messages to UI
* and accumulates output in a buffer
@ -68,11 +37,11 @@ public class ProcessStatusWindow {
String line = bis.readLine();
if (line == null)
break;
appendMsg(line);
wnd.appendMsg(line);
buffer.append(line);
}
} catch (IOException e) {
appendMsg("Stream " + e);
wnd.appendMsg("Stream " + e);
}
}
});
@ -81,7 +50,7 @@ public class ProcessStatusWindow {
}
protected StringBuffer executeCommand(String command) {
appendMsg("Executing " + command);
wnd.appendMsg("Executing " + command);
StringBuffer output = new StringBuffer();
StringBuffer error = new StringBuffer();
try {
@ -90,9 +59,9 @@ public class ProcessStatusWindow {
startStreamThread(p, p.getErrorStream(), error);
p.waitFor();
} catch (IOException e) {
appendMsg("IOError: " + e);
wnd.appendMsg("IOError: " + e);
} catch (InterruptedException e) {
appendMsg("WaitError: " + e);
wnd.appendMsg("WaitError: " + e);
}
return error;
}

View File

@ -0,0 +1,43 @@
package com.rusefi.ui;
import com.rusefi.ui.util.FrameHelper;
import com.rusefi.ui.util.UiUtils;
import javax.swing.*;
import java.awt.*;
/**
* Create(c) Andrey Belomutskiy 2013-2015
* 3/7/2015
*/
public class StatusWindow {
private final JTextArea log = new JTextArea();
private final JScrollPane messagesScroll = new JScrollPane(log, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED) {
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
};
public StatusWindow() {
log.setLineWrap(true);
}
public void showFrame(String title) {
FrameHelper f = new FrameHelper();
f.getFrame().setTitle(title);
f.showFrame(messagesScroll, false);
UiUtils.centerWindow(f.getFrame());
log.setText(""); // let's remove stuff from previous invocation
}
public void appendMsg(final String s) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
log.append(s + "\r\n");
UiUtils.trueLayout(log);
}
});
}
}