Refact:Devkit change msg format
This commit is contained in:
@@ -2,13 +2,10 @@ package pub.doric.devkit;
|
||||
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import org.greenrobot.eventbus.EventBus;
|
||||
import org.greenrobot.eventbus.Subscribe;
|
||||
import org.greenrobot.eventbus.ThreadMode;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import pub.doric.Doric;
|
||||
import pub.doric.DoricContext;
|
||||
@@ -16,17 +13,14 @@ import pub.doric.DoricContextManager;
|
||||
import pub.doric.DoricNativeDriver;
|
||||
import pub.doric.devkit.event.ConnectExceptionEvent;
|
||||
import pub.doric.devkit.event.EOFExceptionEvent;
|
||||
import pub.doric.devkit.event.EnterDebugEvent;
|
||||
import pub.doric.devkit.event.OpenEvent;
|
||||
import pub.doric.devkit.event.ReloadEvent;
|
||||
import pub.doric.devkit.event.StartDebugEvent;
|
||||
import pub.doric.devkit.event.StopDebugEvent;
|
||||
|
||||
public class DevKit implements IDevKit {
|
||||
|
||||
public static boolean isRunningInEmulator = false;
|
||||
public static String ip = "";
|
||||
private static Gson gson = new Gson();
|
||||
|
||||
|
||||
private static class Inner {
|
||||
private static final DevKit sInstance = new DevKit();
|
||||
@@ -50,11 +44,8 @@ public class DevKit implements IDevKit {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendDevCommand(IDevKit.Command command, JsonObject jsonObject) {
|
||||
JsonObject result = new JsonObject();
|
||||
result.addProperty("cmd", command.toString());
|
||||
result.add("data", jsonObject);
|
||||
wsClient.send(gson.toJson(result));
|
||||
public void sendDevCommand(IDevKit.Command command, JSONObject jsonObject) {
|
||||
wsClient.sendToServer(command.toString(), jsonObject);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -85,31 +76,29 @@ public class DevKit implements IDevKit {
|
||||
Toast.makeText(Doric.application(), "dev kit connection exception", Toast.LENGTH_LONG).show();
|
||||
}
|
||||
|
||||
@Subscribe(threadMode = ThreadMode.MAIN)
|
||||
public void onStartDebugEvent(StartDebugEvent startDebugEvent) {
|
||||
doricContextDebuggable = new DoricContextDebuggable(startDebugEvent.getContextId());
|
||||
}
|
||||
|
||||
@Subscribe(threadMode = ThreadMode.MAIN)
|
||||
public void onEnterDebugEvent(EnterDebugEvent enterDebugEvent) {
|
||||
doricContextDebuggable.startDebug();
|
||||
}
|
||||
|
||||
@Subscribe(threadMode = ThreadMode.MAIN)
|
||||
public void onQuitDebugEvent(StopDebugEvent quitDebugEvent) {
|
||||
doricContextDebuggable.stopDebug();
|
||||
}
|
||||
|
||||
@Subscribe(threadMode = ThreadMode.MAIN)
|
||||
public void onReloadEvent(ReloadEvent reloadEvent) {
|
||||
public DoricContext requestDebugContext(String source) {
|
||||
for (DoricContext context : DoricContextManager.aliveContexts()) {
|
||||
if (source.contains(context.getSource()) || context.getSource().equals("__dev__")) {
|
||||
return context;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void reload(String source, String script) {
|
||||
for (DoricContext context : DoricContextManager.aliveContexts()) {
|
||||
if (doricContextDebuggable != null &&
|
||||
doricContextDebuggable.isDebugging &&
|
||||
doricContextDebuggable.getContext().getContextId().equals(context.getContextId())) {
|
||||
System.out.println("is debugging context id: " + context.getContextId());
|
||||
} else {
|
||||
if (reloadEvent.source.contains(context.getSource()) || context.getSource().equals("__dev__")) {
|
||||
context.reload(reloadEvent.script);
|
||||
if (source.contains(context.getSource()) || context.getSource().equals("__dev__")) {
|
||||
context.reload(script);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,43 +1,35 @@
|
||||
package pub.doric.devkit;
|
||||
|
||||
import pub.doric.DoricContext;
|
||||
import pub.doric.DoricContextManager;
|
||||
import pub.doric.DoricNativeDriver;
|
||||
import pub.doric.utils.DoricConstant;
|
||||
import pub.doric.IDoricDriver;
|
||||
|
||||
public class DoricContextDebuggable {
|
||||
private DoricContext doricContext;
|
||||
private DoricDebugDriver doricDebugDriver;
|
||||
private final DoricContext doricContext;
|
||||
private DoricDebugDriver debugDriver;
|
||||
private final IDoricDriver nativeDriver;
|
||||
public boolean isDebugging = false;
|
||||
|
||||
public DoricContextDebuggable(String contextId) {
|
||||
this.doricContext = DoricContextManager.getContext(contextId);
|
||||
isDebugging = true;
|
||||
public DoricContextDebuggable(DoricContext doricContext) {
|
||||
this.doricContext = doricContext;
|
||||
this.isDebugging = true;
|
||||
this.nativeDriver = this.doricContext.getDriver();
|
||||
}
|
||||
|
||||
public void startDebug() {
|
||||
doricDebugDriver = new DoricDebugDriver(new IStatusCallback() {
|
||||
debugDriver = new DoricDebugDriver(new IStatusCallback() {
|
||||
@Override
|
||||
public void start() {
|
||||
doricContext.setDriver(doricDebugDriver);
|
||||
|
||||
doricContext.getRootNode().setId("");
|
||||
doricContext.callEntity(DoricConstant.DORIC_ENTITY_INIT, doricContext.getExtra());
|
||||
doricContext.callEntity(DoricConstant.DORIC_ENTITY_CREATE);
|
||||
doricContext.callEntity(DoricConstant.DORIC_ENTITY_BUILD, doricContext.getInitParams());
|
||||
doricContext.setDriver(debugDriver);
|
||||
doricContext.reload(doricContext.getScript());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void stopDebug() {
|
||||
isDebugging = false;
|
||||
doricDebugDriver.destroy();
|
||||
doricContext.setDriver(DoricNativeDriver.getInstance());
|
||||
|
||||
doricContext.getRootNode().setId("");
|
||||
doricContext.callEntity(DoricConstant.DORIC_ENTITY_INIT, doricContext.getExtra());
|
||||
doricContext.callEntity(DoricConstant.DORIC_ENTITY_CREATE);
|
||||
doricContext.callEntity(DoricConstant.DORIC_ENTITY_BUILD, doricContext.getInitParams());
|
||||
doricContext.setDriver(nativeDriver);
|
||||
debugDriver.destroy();
|
||||
doricContext.reload(doricContext.getScript());
|
||||
}
|
||||
|
||||
public DoricContext getContext() {
|
||||
|
@@ -1,18 +1,7 @@
|
||||
package pub.doric.devkit;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
|
||||
import pub.doric.DoricContext;
|
||||
import pub.doric.devkit.remote.DoricRemoteJSExecutor;
|
||||
import pub.doric.engine.DoricJSEngine;
|
||||
import pub.doric.utils.DoricLog;
|
||||
|
||||
public class DoricDebugJSEngine extends DoricJSEngine {
|
||||
|
||||
@@ -27,41 +16,4 @@ public class DoricDebugJSEngine extends DoricJSEngine {
|
||||
protected void initJSEngine() {
|
||||
mDoricJSE = new DoricRemoteJSExecutor(statusCallback);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onException(@Nullable DoricContext context, Exception e) {
|
||||
Log.e(DoricJSEngine.class.getSimpleName(), "In source file: " + (context != null ? context.getSource() : "Unknown"));
|
||||
e.printStackTrace();
|
||||
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
jsonObject.addProperty("source", "In source file: " + (context != null ? context.getSource() : "Unknown"));
|
||||
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
e.printStackTrace(new PrintWriter(stringWriter));
|
||||
jsonObject.addProperty("exception", stringWriter.toString());
|
||||
DevKit.getInstance().sendDevCommand(IDevKit.Command.EXCEPTION, jsonObject);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLog(int type, String message) {
|
||||
String typeString = "DEFAULT";
|
||||
switch (type) {
|
||||
case Log.ERROR:
|
||||
DoricLog.suffix_e("_js", message);
|
||||
typeString = "ERROR";
|
||||
break;
|
||||
case Log.WARN:
|
||||
DoricLog.suffix_w("_js", message);
|
||||
typeString = "WARN";
|
||||
break;
|
||||
default:
|
||||
DoricLog.suffix_d("_js", message);
|
||||
break;
|
||||
}
|
||||
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
jsonObject.addProperty("type", typeString);
|
||||
jsonObject.addProperty("message", message);
|
||||
DevKit.getInstance().sendDevCommand(IDevKit.Command.LOG, jsonObject);
|
||||
}
|
||||
}
|
||||
|
@@ -17,7 +17,7 @@ package pub.doric.devkit;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import com.github.pengfeizhou.jscore.JSONBuilder;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
@@ -37,12 +37,14 @@ public class DoricDevMonitor implements IDoricMonitor {
|
||||
if (!DoricDev.getInstance().isInDevMode()) {
|
||||
return;
|
||||
}
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
jsonObject.addProperty("source", "In source file: " + (context != null ? context.getSource() : "Unknown"));
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
e.printStackTrace(new PrintWriter(stringWriter));
|
||||
jsonObject.addProperty("exception", stringWriter.toString());
|
||||
DevKit.getInstance().sendDevCommand(IDevKit.Command.EXCEPTION, jsonObject);
|
||||
DevKit.getInstance().sendDevCommand(
|
||||
IDevKit.Command.EXCEPTION,
|
||||
new JSONBuilder()
|
||||
.put("source", "In source file: " + (context != null ? context.getSource() : "Unknown"))
|
||||
.put("exception", stringWriter.toString())
|
||||
.toJSONObject());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -64,9 +66,11 @@ public class DoricDevMonitor implements IDoricMonitor {
|
||||
DoricLog.suffix_d("_js", message);
|
||||
break;
|
||||
}
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
jsonObject.addProperty("type", typeString);
|
||||
jsonObject.addProperty("message", message);
|
||||
DevKit.getInstance().sendDevCommand(IDevKit.Command.LOG, jsonObject);
|
||||
DevKit.getInstance().sendDevCommand(
|
||||
IDevKit.Command.LOG,
|
||||
new JSONBuilder()
|
||||
.put("type", typeString)
|
||||
.put("message", message)
|
||||
.toJSONObject());
|
||||
}
|
||||
}
|
||||
|
@@ -1,16 +1,17 @@
|
||||
package pub.doric.devkit;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
public interface IDevKit {
|
||||
|
||||
enum Command {
|
||||
DEBUG, HOT_RELOAD, EXCEPTION, LOG
|
||||
HOT_RELOAD, EXCEPTION, LOG
|
||||
}
|
||||
|
||||
void connectDevKit(String url);
|
||||
|
||||
void sendDevCommand(IDevKit.Command command, JsonObject jsonObject);
|
||||
void sendDevCommand(IDevKit.Command command, JSONObject jsonObject);
|
||||
|
||||
void disconnectDevKit();
|
||||
}
|
||||
|
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package pub.doric.devkit;
|
||||
|
||||
import com.github.pengfeizhou.jscore.JSONBuilder;
|
||||
|
||||
import org.greenrobot.eventbus.EventBus;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
@@ -28,11 +30,10 @@ import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
import okhttp3.WebSocket;
|
||||
import okhttp3.WebSocketListener;
|
||||
import pub.doric.DoricContext;
|
||||
import pub.doric.devkit.event.ConnectExceptionEvent;
|
||||
import pub.doric.devkit.event.EOFExceptionEvent;
|
||||
import pub.doric.devkit.event.EnterDebugEvent;
|
||||
import pub.doric.devkit.event.OpenEvent;
|
||||
import pub.doric.devkit.event.ReloadEvent;
|
||||
|
||||
/**
|
||||
* @Description: com.github.penfeizhou.doric.dev
|
||||
@@ -68,19 +69,24 @@ public class WSClient extends WebSocketListener {
|
||||
super.onMessage(webSocket, text);
|
||||
try {
|
||||
JSONObject jsonObject = new JSONObject(text);
|
||||
String type = jsonObject.optString("type");
|
||||
String cmd = jsonObject.optString("cmd");
|
||||
switch (cmd) {
|
||||
case "RELOAD": {
|
||||
String source = jsonObject.optString("source");
|
||||
String script = jsonObject.optString("script");
|
||||
EventBus.getDefault().post(new ReloadEvent(source, script));
|
||||
JSONObject payload = jsonObject.optJSONObject("payload");
|
||||
if ("D2C".equals(type)) {
|
||||
if ("DEBUG_REQ".equals(cmd)) {
|
||||
String source = payload.optString("source");
|
||||
DoricContext context = DevKit.getInstance().requestDebugContext(source);
|
||||
sendToDebugger("DEBUG_RES", new JSONBuilder()
|
||||
.put("contextId", context == null ? "" : context.getContextId())
|
||||
.toJSONObject());
|
||||
}
|
||||
break;
|
||||
case "SWITCH_TO_DEBUG": {
|
||||
String contextId = jsonObject.optString("contextId");
|
||||
EventBus.getDefault().post(new EnterDebugEvent());
|
||||
|
||||
} else if ("S2C".equals(type)) {
|
||||
if ("RELOAD".equals(cmd)) {
|
||||
String source = payload.optString("source");
|
||||
String script = payload.optString("script");
|
||||
DevKit.getInstance().reload(source, script);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
} catch (JSONException e) {
|
||||
@@ -111,7 +117,20 @@ public class WSClient extends WebSocketListener {
|
||||
}
|
||||
}
|
||||
|
||||
public void send(String command) {
|
||||
webSocket.send(command);
|
||||
public void sendToDebugger(String command, JSONObject payload) {
|
||||
webSocket.send(new JSONBuilder()
|
||||
.put("type", "C2D")
|
||||
.put("cmd", command)
|
||||
.put("payload", payload)
|
||||
.toString());
|
||||
}
|
||||
|
||||
public void sendToServer(String command, JSONObject payload) {
|
||||
webSocket.send(new JSONBuilder()
|
||||
.put("type", "C2S")
|
||||
.put("cmd", command)
|
||||
.put("payload", payload)
|
||||
.toString());
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,5 +0,0 @@
|
||||
package pub.doric.devkit.event;
|
||||
|
||||
public class EnterDebugEvent {
|
||||
|
||||
}
|
@@ -1,4 +0,0 @@
|
||||
package pub.doric.devkit.event;
|
||||
|
||||
public class QuitDebugEvent {
|
||||
}
|
@@ -1,11 +0,0 @@
|
||||
package pub.doric.devkit.event;
|
||||
|
||||
public class ReloadEvent {
|
||||
public String source;
|
||||
public String script;
|
||||
|
||||
public ReloadEvent(String source, String script) {
|
||||
this.source = source;
|
||||
this.script = script;
|
||||
}
|
||||
}
|
@@ -1,13 +0,0 @@
|
||||
package pub.doric.devkit.event;
|
||||
|
||||
public class StartDebugEvent {
|
||||
private String contextId;
|
||||
|
||||
public StartDebugEvent(String contextId) {
|
||||
this.contextId = contextId;
|
||||
}
|
||||
|
||||
public String getContextId() {
|
||||
return contextId;
|
||||
}
|
||||
}
|
@@ -17,8 +17,6 @@ import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.core.app.ActivityCompat;
|
||||
import androidx.core.content.ContextCompat;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import org.greenrobot.eventbus.EventBus;
|
||||
import org.greenrobot.eventbus.Subscribe;
|
||||
import org.greenrobot.eventbus.ThreadMode;
|
||||
@@ -27,12 +25,10 @@ import pub.doric.DoricContext;
|
||||
import pub.doric.DoricContextManager;
|
||||
import pub.doric.devkit.DevKit;
|
||||
import pub.doric.devkit.DoricDev;
|
||||
import pub.doric.devkit.IDevKit;
|
||||
import pub.doric.devkit.R;
|
||||
import pub.doric.devkit.event.ConnectExceptionEvent;
|
||||
import pub.doric.devkit.event.EOFExceptionEvent;
|
||||
import pub.doric.devkit.event.OpenEvent;
|
||||
import pub.doric.devkit.event.StartDebugEvent;
|
||||
import pub.doric.devkit.qrcode.DisplayUtil;
|
||||
import pub.doric.devkit.qrcode.activity.CaptureActivity;
|
||||
import pub.doric.devkit.qrcode.activity.CodeUtils;
|
||||
@@ -154,11 +150,7 @@ public class DoricDevActivity extends AppCompatActivity {
|
||||
cell.findViewById(R.id.debug_text_view).setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
EventBus.getDefault().post(new StartDebugEvent(doricContext.getContextId()));
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
jsonObject.addProperty("contextId", doricContext.getContextId());
|
||||
jsonObject.addProperty("source", doricContext.getSource().replace(".js", ".ts"));
|
||||
DevKit.getInstance().sendDevCommand(IDevKit.Command.DEBUG, jsonObject);
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
|
Reference in New Issue
Block a user