Skip to content

Commit

Permalink
Desktop: Extract Clock and Handler implementations to new classes
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurbenemann committed Oct 13, 2014
1 parent ce1b124 commit 24cd3bb
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void fit(int[] magVector) {
}


if (!fitComplete && ellipsoidFit.getFitness() > 0.95 && points.size() > 100) {
if (!fitComplete && ellipsoidFit.getFitness() > 0.98 && points.size() > 100) {
fitComplete = true;
if (listner!=null) {
listner.finished(ellipsoidFit);
Expand Down
8 changes: 8 additions & 0 deletions Desktop/src/org/droidplanner/desktop/logic/Clock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.droidplanner.desktop.logic;

public class Clock implements org.droidplanner.core.drone.DroneInterfaces.Clock {
@Override
public long elapsedRealtime() {
return System.currentTimeMillis();
}
};
48 changes: 0 additions & 48 deletions Desktop/src/org/droidplanner/desktop/logic/FakeFactory.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
package org.droidplanner.desktop.logic;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import org.droidplanner.core.drone.DroneInterfaces.Clock;
import org.droidplanner.core.drone.DroneInterfaces.Handler;
import org.droidplanner.core.drone.Preferences;
import org.droidplanner.core.drone.profiles.VehicleProfile;
import org.droidplanner.core.firmware.FirmwareType;
Expand Down Expand Up @@ -38,42 +28,4 @@ public Rates getRates() {
};
}

static Handler fakeHandler() {
return new Handler() {

private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
private final Map<Runnable, ScheduledFuture<?>> futureThreads = new HashMap<Runnable, ScheduledFuture<?>>();

@Override
public void removeCallbacks(Runnable thread) {
if (futureThreads.containsKey(thread)) {
boolean mayInterruptIfRunning = false;
futureThreads.get(thread).cancel(mayInterruptIfRunning);
System.out.println("Canceled");
}
}

@Override
public void post(Runnable thread){
scheduler.execute(thread);
}

@Override
public void postDelayed(Runnable thread, long timeout) {
ScheduledFuture<?> future = scheduler.schedule(thread, timeout, TimeUnit.MILLISECONDS);
futureThreads.put(thread, future);
System.out.println("PostDelayed");
}
};
}

static Clock fakeClock() {
return new Clock() {
@Override
public long elapsedRealtime() {
return System.currentTimeMillis();
}
};
}

}
36 changes: 36 additions & 0 deletions Desktop/src/org/droidplanner/desktop/logic/Handler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.droidplanner.desktop.logic;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

public class Handler implements
org.droidplanner.core.drone.DroneInterfaces.Handler {

private final ScheduledExecutorService scheduler = Executors
.newSingleThreadScheduledExecutor();
private final Map<Runnable, ScheduledFuture<?>> futureThreads = new HashMap<Runnable, ScheduledFuture<?>>();

@Override
public void removeCallbacks(Runnable thread) {
if (futureThreads.containsKey(thread)) {
boolean mayInterruptIfRunning = false;
futureThreads.get(thread).cancel(mayInterruptIfRunning);
}
}

@Override
public void post(Runnable thread) {
scheduler.execute(thread);
}

@Override
public void postDelayed(Runnable thread, long timeout) {
ScheduledFuture<?> future = scheduler.schedule(thread, timeout,
TimeUnit.MILLISECONDS);
futureThreads.put(thread, future);
}
}
8 changes: 4 additions & 4 deletions Desktop/src/org/droidplanner/desktop/logic/Logic.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import org.droidplanner.core.MAVLink.MavLinkMsgHandler;
import org.droidplanner.core.drone.DroneImpl;
import org.droidplanner.core.drone.variables.helpers.MagnetometerCalibration;
import org.droidplanner.core.drone.DroneInterfaces.Handler;
import org.droidplanner.core.gcs.follow.Follow;
import org.droidplanner.core.model.Drone;
import org.droidplanner.desktop.communication.Connection;
Expand All @@ -22,10 +22,10 @@ public class Logic implements Runnable {


public Logic() {
drone = new DroneImpl(link, FakeFactory.fakeClock(), FakeFactory.fakeHandler(),
FakeFactory.fakePreferences());
Handler handler = new org.droidplanner.desktop.logic.Handler();
drone = new DroneImpl(link, new Clock(), handler,FakeFactory.fakePreferences());
mavlinkHandler = new MavLinkMsgHandler(drone);
follow = new Follow(drone, FakeFactory.fakeHandler(), new FakeLocation());
follow = new Follow(drone, handler, new FakeLocation());
}

@Override
Expand Down

0 comments on commit 24cd3bb

Please sign in to comment.