diff --git a/java_console/io/src/main/java/com/rusefi/binaryprotocol/BinaryProtocol.java b/java_console/io/src/main/java/com/rusefi/binaryprotocol/BinaryProtocol.java index c367ba56be..58dc40c7b7 100644 --- a/java_console/io/src/main/java/com/rusefi/binaryprotocol/BinaryProtocol.java +++ b/java_console/io/src/main/java/com/rusefi/binaryprotocol/BinaryProtocol.java @@ -264,7 +264,8 @@ public class BinaryProtocol { String text = requestPendingMessages(); if (text != null) listener.onDataArrived((text + "\r\n").getBytes()); - LiveDocsRegistry.INSTANCE.refresh(BinaryProtocol.this); + LiveDocsRegistry.LiveDataProvider liveDataProvider = LiveDocsRegistry.getLiveDataProvider(BinaryProtocol.this); + LiveDocsRegistry.INSTANCE.refresh(liveDataProvider); } }); } diff --git a/java_console/io/src/main/java/com/rusefi/ui/livedocs/LiveDocHolder.java b/java_console/io/src/main/java/com/rusefi/ui/livedocs/LiveDocHolder.java index d269691033..428e3f5c23 100644 --- a/java_console/io/src/main/java/com/rusefi/ui/livedocs/LiveDocHolder.java +++ b/java_console/io/src/main/java/com/rusefi/ui/livedocs/LiveDocHolder.java @@ -15,8 +15,8 @@ public abstract class LiveDocHolder { this.action = action; } - public void update(BinaryProtocol binaryProtocol, byte[] response) { - action.refresh(binaryProtocol, response); + public void update(byte[] response) { + action.refresh(response); } public abstract boolean isVisible(); diff --git a/java_console/io/src/main/java/com/rusefi/ui/livedocs/LiveDocsRegistry.java b/java_console/io/src/main/java/com/rusefi/ui/livedocs/LiveDocsRegistry.java index a61d1ed0b3..25bba55d76 100644 --- a/java_console/io/src/main/java/com/rusefi/ui/livedocs/LiveDocsRegistry.java +++ b/java_console/io/src/main/java/com/rusefi/ui/livedocs/LiveDocsRegistry.java @@ -5,6 +5,7 @@ import com.rusefi.config.Field; import com.rusefi.config.generated.Fields; import com.rusefi.enums.live_data_e; import com.rusefi.ldmp.StateDictionary; +import org.jetbrains.annotations.NotNull; import java.util.ArrayList; import java.util.List; @@ -25,33 +26,48 @@ public enum LiveDocsRegistry { liveDocs.add(holder); } - public void refresh(BinaryProtocol binaryProtocol) { + public void refresh(LiveDataProvider liveDataProvider) { for (LiveDocHolder holder : liveDocs) { boolean visible = holder.isVisible(); if (visible) { live_data_e context = holder.getId(); - refresh(binaryProtocol, holder, context); + refresh(holder, context, liveDataProvider); } } } - private void refresh(BinaryProtocol binaryProtocol, LiveDocHolder holder, live_data_e context) { - Field[] values = StateDictionary.INSTANCE.getFields(context); - int size = Field.getStructureSize(values); + private void refresh(LiveDocHolder holder, live_data_e context, LiveDataProvider liveDataProvider) { - byte[] packet = new byte[5]; - packet[0] = Fields.TS_GET_STRUCT; - putShort(packet, 1, swap16(context.ordinal())); // offset - putShort(packet, 3, swap16(size)); - - byte[] responseWithCode = binaryProtocol.executeCommand(packet, "get LiveDoc"); - if (responseWithCode == null || responseWithCode.length != (size + 1) || responseWithCode[0] != Fields.TS_RESPONSE_OK) + byte[] response = liveDataProvider.provide(context); + if (response == null) return; - byte[] response = new byte[size]; - - System.arraycopy(responseWithCode, 1, response, 0, size); - - holder.update(binaryProtocol, response); + holder.update(response); } + + @NotNull + public static LiveDataProvider getLiveDataProvider(BinaryProtocol binaryProtocol) { + return context -> { + Field[] values = StateDictionary.INSTANCE.getFields(context); + int size = Field.getStructureSize(values); + byte[] packet = new byte[5]; + packet[0] = Fields.TS_GET_STRUCT; + putShort(packet, 1, swap16(context.ordinal())); // offset + putShort(packet, 3, swap16(size)); + + byte[] responseWithCode = binaryProtocol.executeCommand(packet, "get LiveDoc"); + if (responseWithCode == null || responseWithCode.length != (size + 1) || responseWithCode[0] != Fields.TS_RESPONSE_OK) + return null; + + byte[] response = new byte[size]; + + System.arraycopy(responseWithCode, 1, response, 0, size); + return response; + }; + } + + public interface LiveDataProvider { + byte[] provide(live_data_e context); + } + } diff --git a/java_console/io/src/main/java/com/rusefi/ui/livedocs/RefreshActions.java b/java_console/io/src/main/java/com/rusefi/ui/livedocs/RefreshActions.java index 836c0b5fab..8601181244 100644 --- a/java_console/io/src/main/java/com/rusefi/ui/livedocs/RefreshActions.java +++ b/java_console/io/src/main/java/com/rusefi/ui/livedocs/RefreshActions.java @@ -1,7 +1,5 @@ package com.rusefi.ui.livedocs; -import com.rusefi.binaryprotocol.BinaryProtocol; - public abstract class RefreshActions { - public abstract void refresh(BinaryProtocol bp, byte[] response); + public abstract void refresh(byte[] response); } diff --git a/java_console/ui/src/main/java/com/rusefi/livedata/LiveDataParserPanel.java b/java_console/ui/src/main/java/com/rusefi/livedata/LiveDataParserPanel.java index 66430b41f4..e49f3b4e3f 100644 --- a/java_console/ui/src/main/java/com/rusefi/livedata/LiveDataParserPanel.java +++ b/java_console/ui/src/main/java/com/rusefi/livedata/LiveDataParserPanel.java @@ -37,6 +37,7 @@ import static javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED; /** * this panel shows a live view of rusEFI firmware C/C++ code + * @see LiveDataParserPanelSandbox */ public class LiveDataParserPanel { private static final String CONFIG_MAGIC_PREFIX = "engineConfiguration"; @@ -263,7 +264,7 @@ public class LiveDataParserPanel { }, fileName); RefreshActions refreshAction = new RefreshActions() { @Override - public void refresh(BinaryProtocol bp, byte[] response) { + public void refresh(byte[] response) { if (log.debugEnabled()) log.debug("Got data " + response.length + " bytes"); reference.set(response); diff --git a/java_console/ui/src/test/java/com/rusefi/livedata/LiveDataParserPanelSandbox.java b/java_console/ui/src/test/java/com/rusefi/livedata/LiveDataParserPanelSandbox.java new file mode 100644 index 0000000000..0cc2a62081 --- /dev/null +++ b/java_console/ui/src/test/java/com/rusefi/livedata/LiveDataParserPanelSandbox.java @@ -0,0 +1,29 @@ +package com.rusefi.livedata; + +import com.rusefi.enums.live_data_e; +import com.rusefi.ui.UIContext; +import com.rusefi.ui.livedocs.LiveDocsRegistry; +import com.rusefi.ui.util.FrameHelper; + +import javax.swing.*; + +public class LiveDataParserPanelSandbox { + public static void main(String[] args) { + + UIContext context = new UIContext(); + + JPanel panel = LiveDataParserPanel.createLiveDataParserContent(context, + LiveDataView.BOOST_CONTROL); + + LiveDocsRegistry.INSTANCE.refresh(new LiveDocsRegistry.LiveDataProvider() { + @Override + public byte[] provide(live_data_e context) { + System.out.println("provide"); + return new byte[0]; + } + } + ); + + new FrameHelper().showFrame(panel); + } +} diff --git a/java_console/ui/src/test/java/com/rusefi/ui/livedata/LiveDataParserTest.java b/java_console/ui/src/test/java/com/rusefi/ui/livedata/LiveDataParserTest.java index 16b5344470..8b67a8259e 100644 --- a/java_console/ui/src/test/java/com/rusefi/ui/livedata/LiveDataParserTest.java +++ b/java_console/ui/src/test/java/com/rusefi/ui/livedata/LiveDataParserTest.java @@ -13,6 +13,7 @@ import java.net.URISyntaxException; import java.util.Map; import java.util.TreeMap; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.mockito.Matchers.any; import static org.mockito.Matchers.eq; @@ -63,6 +64,6 @@ public class LiveDataParserTest { ParseTree tree = LiveDataParserPanel.getParseTree(sourceCode); ParseResult parseResult = LiveDataParserPanel.applyVariables(VariableValueSource.VOID, sourceCode, SourceCodePainter.VOID, tree); - assertTrue(!parseResult.getConfigTokens().isEmpty()); + assertFalse(parseResult.getConfigTokens().isEmpty()); } }