Skip to content

Commit

Permalink
Added option to the disable data store
Browse files Browse the repository at this point in the history
  • Loading branch information
ImBonana committed Sep 2, 2024
1 parent c14fcb5 commit ed8cf07
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import top.symple.symplegraphdisplay.managers.data.DataListener;
import top.symple.symplegraphdisplay.managers.data.DataListenerGroup;

@TeleOp(name = "test")
//@TeleOp(name = "test")
public class TestOpMode extends LinearOpMode implements DataListenerGroup {

@DataListener(color = @Color(red = 0, green = 0, blue = 200), fillColor = @Color(red = 0, green = 0, blue = 200, alpha = 0.1f))
Expand Down
3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ android.enableJetifier=false
# Allow Gradle to use up to 1 GB of RAM
org.gradle.jvmargs=-Xmx1024M

version=1.0.0
# SympleGraphDisplay version
version=1.1.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package top.symple.symplegraphdisplay;

public class GraphSettings {
private static final double DEFAULT_UPDATE_INTERVAL = 1;
private static final boolean DEFAULT_STORE_DATA = false;

private double updateInterval;
private boolean storeData;

public GraphSettings() {
this.updateInterval = DEFAULT_UPDATE_INTERVAL;
this.storeData = DEFAULT_STORE_DATA;
}

/**
* Set the delay between graph updates
* @param updateInterval time in sec
*/
public GraphSettings setUpdateInterval(double updateInterval) {
this.updateInterval = updateInterval;
return this;
}

/**
* When `true` it will store the data in the memory
* @param storeData should store data in memory?
*/
public GraphSettings setStoreData(boolean storeData) {
this.storeData = storeData;
return this;
}

/**
* Get the delay between graph updates
* @return delay between graph updates in sec
*/
public double getUpdateInterval() {
return updateInterval;
}

/**
* @return `true` if storing data in memory
*/
public boolean isStoreData() {
return storeData;
}

/**
* Reset the current settings to default
*/
public void reset() {
this.updateInterval = DEFAULT_UPDATE_INTERVAL;
this.storeData = DEFAULT_STORE_DATA;
}

/**
* <Strong>NOTE:</Strong> This method will copy the settings from the settings parameter to this {@link GraphSettings} instance
*/
public void fromSettings(GraphSettings settings) {
this.updateInterval = settings.updateInterval;
this.storeData = settings.storeData;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,27 @@
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.AssetManager;
import android.util.Log;

import com.qualcomm.ftccommon.FtcEventLoop;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.eventloop.opmode.OpMode;
import com.qualcomm.robotcore.eventloop.opmode.OpModeManager;
import com.qualcomm.robotcore.eventloop.opmode.OpModeManagerImpl;
import com.qualcomm.robotcore.eventloop.opmode.OpModeManagerNotifier;
import com.qualcomm.robotcore.eventloop.opmode.OpModeRegistrar;
import com.qualcomm.robotcore.util.WebHandlerManager;
import com.qualcomm.robotcore.util.WebServer;

import org.firstinspires.ftc.ftccommon.external.OnCreate;
import org.firstinspires.ftc.ftccommon.external.OnCreateEventLoop;
import org.firstinspires.ftc.ftccommon.external.OnDestroy;
import org.firstinspires.ftc.ftccommon.external.WebHandlerRegistrar;
import org.firstinspires.ftc.robotcore.internal.opmode.OpModeMeta;
import org.firstinspires.ftc.robotcore.internal.system.AppUtil;
import org.firstinspires.ftc.robotcore.internal.system.Misc;
import org.firstinspires.ftc.robotcore.internal.webserver.WebHandler;
import org.firstinspires.ftc.robotserver.internal.webserver.MimeTypesUtil;
import org.java_websocket.WebSocket;

import java.io.IOException;
import java.util.Collection;

import fi.iki.elonen.NanoHTTPD;
import top.symple.symplegraphdisplay.apps.WebsocketServer;
import top.symple.symplegraphdisplay.managers.data.DataListenerGroup;
import top.symple.symplegraphdisplay.managers.data.DataManager;
import top.symple.symplegraphdisplay.packets.Packet;
import top.symple.symplegraphdisplay.packets.ResetGraphDataPacket;
import top.symple.symplegraphdisplay.util.Timer;

public class SympleGraphDisplay {
private static final String PREFS_NAME = "SympleGraphDisplay";
Expand All @@ -51,7 +39,7 @@ public class SympleGraphDisplay {
// register the op mode
@OpModeRegistrar
public static void registerOpMode(OpModeManager manager) {
if(instance != null) {
if (instance != null) {
instance.internalRegisterOpMode(manager);
}
}
Expand All @@ -75,7 +63,7 @@ public static void attachWebServer(Context context, WebHandlerManager manager) {
// close everything when the robot is turned off
@OnDestroy
public static void destroy(Context context) throws IOException, InterruptedException {
if(instance != null) {
if (instance != null) {
instance.close();
instance = null;
}
Expand All @@ -87,20 +75,20 @@ private boolean getAutoEnable() {

private void setAutoEnable(boolean autoEnable) {
prefs.edit()
.putBoolean(PREFS_AUTO_ENABLE_KEY, autoEnable)
.apply();
.putBoolean(PREFS_AUTO_ENABLE_KEY, autoEnable)
.apply();
}

private void enable() {
if(isEnabled) return;
if (isEnabled) return;

setAutoEnable(true);

isEnabled = true;
}

private void disable() {
if(!isEnabled) return;
if (!isEnabled) return;

setAutoEnable(false);
isEnabled = false;
Expand All @@ -114,28 +102,28 @@ private void close() throws IOException, InterruptedException {

private void internalRegisterOpMode(OpModeManager manager) {
manager.register(
new OpModeMeta.Builder()
.setName("Enable/Disable Graph Display")
.setFlavor(OpModeMeta.Flavor.TELEOP)
.setGroup("graph-display")
.build(),
new LinearOpMode() {
@Override
public void runOpMode() throws InterruptedException {
telemetry.log().add(Misc.formatInvariant("Status: %s, Start to %s it.", isEnabled ? "enabled" : "disabled", isEnabled ? "disable" : "enable"));
telemetry.update();

waitForStart();

if(isStopRequested()) return;

if(isEnabled) {
disable();
} else {
enable();
new OpModeMeta.Builder()
.setName("Enable/Disable Graph Display")
.setFlavor(OpModeMeta.Flavor.TELEOP)
.setGroup("graph-display")
.build(),
new LinearOpMode() {
@Override
public void runOpMode() throws InterruptedException {
telemetry.log().add(Misc.formatInvariant("Status: %s, Start to %s it.", isEnabled ? "enabled" : "disabled", isEnabled ? "disable" : "enable"));
telemetry.update();

waitForStart();

if (isStopRequested()) return;

if (isEnabled) {
disable();
} else {
enable();
}
}
}
}
);
}

Expand All @@ -153,7 +141,7 @@ private void internalAttachWebServer(WebServer webServer) {
webHandlerManager.register("/graph/",
newStaticAssetHandler(assetManager, "graph/html/index.html"));

String[] staticAssets = new String[] {
String[] staticAssets = new String[]{
"chart.min.js",
"chartjs-plugin-autocolors.js",
"hammerjs.js",
Expand All @@ -163,8 +151,8 @@ private void internalAttachWebServer(WebServer webServer) {
};

for (String path : staticAssets) {
webHandlerManager.register("/graph/assets/"+path,
newStaticAssetHandler(assetManager, "graph/assets/"+path));
webHandlerManager.register("/graph/assets/" + path,
newStaticAssetHandler(assetManager, "graph/assets/" + path));
}
}

Expand All @@ -185,13 +173,12 @@ public NanoHTTPD.Response getResponse(NanoHTTPD.IHTTPSession session)
};
}


private SympleGraphDisplay() {
this.core = new SympleGraphDisplayCore();
this.core = new SympleGraphDisplayCore(new GraphSettings());

Activity activity = AppUtil.getInstance().getActivity();
prefs = activity.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
if(getAutoEnable()) {
if (getAutoEnable()) {
enable();
}
}
Expand All @@ -208,7 +195,7 @@ public void reset() {
}

/**
* use this to update the graph
* use this to update the graph
*/
public void run() {
this.core.run();
Expand All @@ -224,6 +211,7 @@ public void update() {

/**
* Add data listener
*
* @param dataListenerGroup data listener
*/
public void registerDataListenerGroup(DataListenerGroup... dataListenerGroup) {
Expand All @@ -234,19 +222,14 @@ public DataManager getDataManager() {
return this.core.getDataManager();
}

/**
* Get the delay between graph updates
* @return delay between graph updates in sec
*/
public double getUpdateTime() {
return this.core.getUpdateTime();
public GraphSettings getSettings() {
return this.core.getSettings();
}

/**
* Set the delay between graph updates
* @param updateTime time in sec
*@see GraphSettings#fromSettings(GraphSettings)
*/
public void setUpdateTime(double updateTime) {
this.core.setUpdateTime(updateTime);
public void setSetting(GraphSettings settings) {
this.core.getSettings().fromSettings(settings);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
import top.symple.symplegraphdisplay.util.Timer;

public class SympleGraphDisplayCore {
private static final double DEFAULT_UPDATE_TIME = 0.1;

private final WebsocketServer websocketServer;
private final DataManager dataManager;

private double updateTime = DEFAULT_UPDATE_TIME;
private final GraphSettings settings;
private final Timer timer = new Timer();

protected SympleGraphDisplayCore() {
protected SympleGraphDisplayCore(GraphSettings settings) {
this.settings = settings;

this.websocketServer = new WebsocketServer(3334, this);
this.websocketServer.start();

Expand All @@ -34,13 +34,13 @@ public void init() {
public void reset() {
this.timer.reset();
this.dataManager.reset();
this.updateTime = DEFAULT_UPDATE_TIME;
this.settings.reset();
this.sendPacket(new ResetGraphDataPacket());
}

public void run() {
timer.run();
if(timer.getCurrentTime() >= updateTime) {
if (timer.getCurrentTime() >= this.getUpdateInterval()) {
update();
timer.reset();
}
Expand Down Expand Up @@ -72,11 +72,15 @@ public WebsocketServer getWebsocketServer() {
return websocketServer;
}

public double getUpdateTime() {
return updateTime;
public double getUpdateInterval() {
return this.settings.getUpdateInterval();
}

public boolean isStoreData() {
return this.settings.isStoreData();
}

public void setUpdateTime(double updateTime) {
this.updateTime = updateTime;
public GraphSettings getSettings() {
return settings;
}
}
Loading

0 comments on commit ed8cf07

Please sign in to comment.