From e574ca32150d365261bbae4965952a7d2cdd2f64 Mon Sep 17 00:00:00 2001 From: cs-powell <142438185+cs-powell@users.noreply.github.com> Date: Sun, 24 Nov 2024 13:32:21 -0500 Subject: [PATCH] restructuring for project (Backlog 3) --- Java/ProjectModels/CognitiveModel/Action.java | 40 +++++ .../CognitiveModel/ActionType.java | 7 + Java/ProjectModels/CognitiveModel/Delay.java | 16 ++ .../CognitiveModel/MindQueue.java | 58 ++++++++ Java/ProjectModels/CognitiveModel/Model.java | 139 ++++++++++++++++++ Java/ProjectModels/CognitiveModel/Motor.java | 39 +++++ .../CognitiveModel}/Process.java | 1 + .../CognitiveModel}/Tests.java | 1 + Java/ProjectModels/CognitiveModel/Vision.java | 22 +++ .../XPCdependencies/Beacon.java | 45 ++++++ .../XPCdependencies/BeaconParser.java | 54 +++++++ .../BeaconReceivedListener.java | 10 ++ .../DiscoveryConnectionCallback.java | 14 ++ .../XPCdependencies/ViewType.java | 60 ++++++++ .../XPCdependencies/WaypointOp.java | 50 +++++++ .../XPlaneConnectDiscovery.java | 94 ++++++++++++ .../CognitiveModel}/XPlaneConnect.java | 8 +- .../Model-1/.idea/compiler.xml | 0 .../.idea/copyright/profiles_settings.xml | 0 .../Model-1/.idea/libraries/Java.xml | 0 .../Model-1/.idea/misc.xml | 0 .../Model-1/.idea/modules.xml | 0 .../Model-1/.idea/vcs.xml | 0 .../Model-1/BasicOperation.iml | 0 .../Model-1/content/Rudder Pedals.png | Bin .../Model-1/content/Yoke Symbol.png | Bin .../Model-1/src/Main.java | 0 .../Model-1/src/XPlaneConnect.java | 0 .../Model-1/src/legacy output | 0 .../Model-1/src/output | 0 .../src}/Main.java | 31 ++-- .../Cognitive_Model/Action.java | 26 ---- .../Cognitive_Model/MindQueue.java | 40 ----- .../Project_Models/Cognitive_Model/Model.java | 83 ----------- .../Cognitive_Model/Vision.java | 15 -- Java/src/XPlaneConnect.java | 2 + Java/src/{ => discovery}/ViewType.java | 2 +- Java/src/{ => discovery}/WaypointOp.java | 2 +- 38 files changed, 680 insertions(+), 179 deletions(-) create mode 100644 Java/ProjectModels/CognitiveModel/Action.java create mode 100644 Java/ProjectModels/CognitiveModel/ActionType.java create mode 100644 Java/ProjectModels/CognitiveModel/Delay.java create mode 100644 Java/ProjectModels/CognitiveModel/MindQueue.java create mode 100644 Java/ProjectModels/CognitiveModel/Model.java create mode 100644 Java/ProjectModels/CognitiveModel/Motor.java rename Java/{Project_Models/Cognitive_Model => ProjectModels/CognitiveModel}/Process.java (62%) rename Java/{Project_Models/Cognitive_Model => ProjectModels/CognitiveModel}/Tests.java (88%) create mode 100644 Java/ProjectModels/CognitiveModel/Vision.java create mode 100644 Java/ProjectModels/CognitiveModel/XPCdependencies/Beacon.java create mode 100644 Java/ProjectModels/CognitiveModel/XPCdependencies/BeaconParser.java create mode 100644 Java/ProjectModels/CognitiveModel/XPCdependencies/BeaconReceivedListener.java create mode 100644 Java/ProjectModels/CognitiveModel/XPCdependencies/DiscoveryConnectionCallback.java create mode 100644 Java/ProjectModels/CognitiveModel/XPCdependencies/ViewType.java create mode 100644 Java/ProjectModels/CognitiveModel/XPCdependencies/WaypointOp.java create mode 100644 Java/ProjectModels/CognitiveModel/XPCdependencies/XPlaneConnectDiscovery.java rename Java/{Project_Models/Cognitive_Model => ProjectModels/CognitiveModel}/XPlaneConnect.java (99%) rename Java/{Project_Models => ProjectModels}/Model-1/.idea/compiler.xml (100%) rename Java/{Project_Models => ProjectModels}/Model-1/.idea/copyright/profiles_settings.xml (100%) rename Java/{Project_Models => ProjectModels}/Model-1/.idea/libraries/Java.xml (100%) rename Java/{Project_Models => ProjectModels}/Model-1/.idea/misc.xml (100%) rename Java/{Project_Models => ProjectModels}/Model-1/.idea/modules.xml (100%) rename Java/{Project_Models => ProjectModels}/Model-1/.idea/vcs.xml (100%) rename Java/{Project_Models => ProjectModels}/Model-1/BasicOperation.iml (100%) rename Java/{Project_Models => ProjectModels}/Model-1/content/Rudder Pedals.png (100%) rename Java/{Project_Models => ProjectModels}/Model-1/content/Yoke Symbol.png (100%) rename Java/{Project_Models => ProjectModels}/Model-1/src/Main.java (100%) rename Java/{Project_Models => ProjectModels}/Model-1/src/XPlaneConnect.java (100%) rename Java/{Project_Models => ProjectModels}/Model-1/src/legacy output (100%) rename Java/{Project_Models => ProjectModels}/Model-1/src/output (100%) rename Java/{Project_Models/Cognitive_Model => ProjectModels/src}/Main.java (69%) delete mode 100644 Java/Project_Models/Cognitive_Model/Action.java delete mode 100644 Java/Project_Models/Cognitive_Model/MindQueue.java delete mode 100644 Java/Project_Models/Cognitive_Model/Model.java delete mode 100644 Java/Project_Models/Cognitive_Model/Vision.java rename Java/src/{ => discovery}/ViewType.java (99%) rename Java/src/{ => discovery}/WaypointOp.java (98%) diff --git a/Java/ProjectModels/CognitiveModel/Action.java b/Java/ProjectModels/CognitiveModel/Action.java new file mode 100644 index 0000000..998b491 --- /dev/null +++ b/Java/ProjectModels/CognitiveModel/Action.java @@ -0,0 +1,40 @@ +package ProjectModels.CognitiveModel; + +public abstract class Action { + + + +Integer priority; //Priority of the Task +Integer delay; //Potential Delay before starting the task +String targetDREF; +Integer taskTime; //How long should the task take +boolean taskComplete; //Is the task completed? +ActionType actionType; + +public Action(ActionType actionType, int delay){ + this.actionType = actionType; + this.delay = delay; + this.targetDREF = null; +} + + +public Action(ActionType actionType, int delay, String target){ + this.actionType = actionType; + this.delay = delay; + targetDREF = target; + } + +public ActionType getType(){ + return actionType; +} + + +public Integer getDelay(){ + return delay; +} + +public String getTarget(){ + return targetDREF; +} + +} diff --git a/Java/ProjectModels/CognitiveModel/ActionType.java b/Java/ProjectModels/CognitiveModel/ActionType.java new file mode 100644 index 0000000..22e9c44 --- /dev/null +++ b/Java/ProjectModels/CognitiveModel/ActionType.java @@ -0,0 +1,7 @@ +package ProjectModels.CognitiveModel; + +public enum ActionType{ + VISION, + MOTOR, + DELAY +} \ No newline at end of file diff --git a/Java/ProjectModels/CognitiveModel/Delay.java b/Java/ProjectModels/CognitiveModel/Delay.java new file mode 100644 index 0000000..b798549 --- /dev/null +++ b/Java/ProjectModels/CognitiveModel/Delay.java @@ -0,0 +1,16 @@ +package ProjectModels.CognitiveModel; +import java.util.Map; +import java.util.TreeMap; + + +public class Delay extends Action { + + public Delay(){ + super(ActionType.DELAY,1000); + } + + public Delay(int delay){ + super(ActionType.DELAY,delay); + } + +} \ No newline at end of file diff --git a/Java/ProjectModels/CognitiveModel/MindQueue.java b/Java/ProjectModels/CognitiveModel/MindQueue.java new file mode 100644 index 0000000..793f900 --- /dev/null +++ b/Java/ProjectModels/CognitiveModel/MindQueue.java @@ -0,0 +1,58 @@ +package ProjectModels.CognitiveModel; + +import java.util.LinkedList; + +public class MindQueue { + + LinkedList q; + + public MindQueue () { + q = new LinkedList(); + } + + public void push(Action e){ + q.add(e); + } + + + public Action removeEvent(Action e){ + Action temp = e; + q.remove(e); + return temp; + } + + public Action pop() { + Vision v = new Vision(); + if(q.isEmpty()) { + return v; + } + return q.remove(); + } + + public boolean isEmpty(){ + return q.isEmpty(); + } + + public String printQueue(){ + String queueTrace = "Next to Execute ==> "; + + for (Action action : q) { + if(action.getType() == ActionType.VISION){ + queueTrace += "[V]"; + } + if(action.getType() == ActionType.MOTOR){ + queueTrace += "[M]"; + } + if(action.getType() == ActionType.DELAY){ + queueTrace += "[D]"; + } + } + return queueTrace; + } + + public int queueLength(){ + return q.size(); + } + + +} diff --git a/Java/ProjectModels/CognitiveModel/Model.java b/Java/ProjectModels/CognitiveModel/Model.java new file mode 100644 index 0000000..4a4360f --- /dev/null +++ b/Java/ProjectModels/CognitiveModel/Model.java @@ -0,0 +1,139 @@ +package ProjectModels.CognitiveModel; +import java.io.IOException; + +public class Model { + + private MindQueue q; //Queue for actions + boolean modelActive; // On/Off Switch for the model execution + XPlaneConnect xpc; //Allows connection to the simulator + + + //Constructors + public Model() { + q = new MindQueue(); + modelActive = false; + } + + public Model(XPlaneConnect xpc) { + q = new MindQueue(); + modelActive = false; + this.xpc = xpc; + } + + + /*Getters*/ + public MindQueue getQueue() { + return q; + } + + /*Setters*/ + + /* + * Setup Methods + * Printing, Empty check, Activation/Deactivation of Model, etc. + */ + public void activateModel() { + modelActive = true; + } + + public void establishConnection(XPlaneConnect newXPC){ + this.xpc = newXPC; + } + + public void deactivateModel(){ + modelActive = false; + } + + public boolean isEmpty(){ + return q.isEmpty(); + } + + public void push(Action a){ + q.push(a); + } + + public boolean isActive(){ + return modelActive; + } + + public void printModelQueue(){ + System.out.println(q.printQueue()); + } + + public int getModelQueueLength(){ + return q.queueLength(); + } + + public void createAction(int actionType,int delay,String target) { + Action newAction = null; + switch(actionType){ + case 1: + newAction = new Vision(delay,target); + this.push(newAction); + case 2: + newAction = new Motor(delay,target); + this.push(newAction); + + case 3: + newAction = new Delay(delay); + this.push(newAction); + } + } + + /* + * Processing Methods + * + */ + + /* + * Process the next event in the model queue + */ + public float[] next() { + float[] returnArray = null; + Action temp = q.pop(); + // System.out.println("Type of Action: " + temp.getType()); + + if(temp.getType() == ActionType.VISION){ // Vision Action (Get Data) + handelVisionAction(temp, returnArray); + } else if (temp.getType() == ActionType.MOTOR){ //Motor Action (Act Upon Data) + handleMotorAction(temp); + } else if (temp.getType() == ActionType.DELAY){//Pure Delays (Do nothing) + handleDelayAction(temp); + } + // q.push(temp); + return returnArray; + + } + + /* Next Helpers */ + private void handelVisionAction(Action temp, float[] returnArray) { + Vision tempV = (Vision) temp; + initiateDelay(tempV.getDelay()); + String dref = tempV.getTarget(); + try { + returnArray = xpc.getDREF(dref); + } catch (IOException e) { + + } + } + + private void handleMotorAction(Action temp){ + Motor tempM = (Motor) temp; + tempM.getDelay(); + } + + private void handleDelayAction(Action temp){ + Delay tempD = (Delay) temp; + initiateDelay(tempD.getDelay()); + } + + public static void initiateDelay(int delay){ + try { + Thread.sleep(delay); // Using the action's Built in Delay + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + +} diff --git a/Java/ProjectModels/CognitiveModel/Motor.java b/Java/ProjectModels/CognitiveModel/Motor.java new file mode 100644 index 0000000..100954d --- /dev/null +++ b/Java/ProjectModels/CognitiveModel/Motor.java @@ -0,0 +1,39 @@ +package ProjectModels.CognitiveModel; +import java.io.IOException; + + +public class Motor extends Action { + + float[] control; + + public Motor() { + super(ActionType.MOTOR,1000); + } + +public Motor(int delay) { + super(ActionType.MOTOR,delay); +} + + +public Motor(int delay, String target) { + super(ActionType.MOTOR,delay,target); +} + + +public void createMotorControl(){ // TODO: Maybe takes in a formatted set of vision inputs? + control = null; +} + +public void sendMotorControl(XPlaneConnect xpc) { + + if(control != null) { + try { + xpc.sendCTRL(control); + } catch (IOException e) { + // TODO Auto-generated catch block + System.out.println("Control failed to send"); + } + } +} + +} \ No newline at end of file diff --git a/Java/Project_Models/Cognitive_Model/Process.java b/Java/ProjectModels/CognitiveModel/Process.java similarity index 62% rename from Java/Project_Models/Cognitive_Model/Process.java rename to Java/ProjectModels/CognitiveModel/Process.java index 4dcf8de..0db50c2 100644 --- a/Java/Project_Models/Cognitive_Model/Process.java +++ b/Java/ProjectModels/CognitiveModel/Process.java @@ -1,3 +1,4 @@ +package ProjectModels.CognitiveModel; public class Process { diff --git a/Java/Project_Models/Cognitive_Model/Tests.java b/Java/ProjectModels/CognitiveModel/Tests.java similarity index 88% rename from Java/Project_Models/Cognitive_Model/Tests.java rename to Java/ProjectModels/CognitiveModel/Tests.java index 17344e0..a8b286e 100644 --- a/Java/Project_Models/Cognitive_Model/Tests.java +++ b/Java/ProjectModels/CognitiveModel/Tests.java @@ -1,3 +1,4 @@ +package ProjectModels.CognitiveModel; import org.junit.Test; import static org.junit.Assert.assertEquals; diff --git a/Java/ProjectModels/CognitiveModel/Vision.java b/Java/ProjectModels/CognitiveModel/Vision.java new file mode 100644 index 0000000..206f016 --- /dev/null +++ b/Java/ProjectModels/CognitiveModel/Vision.java @@ -0,0 +1,22 @@ +package ProjectModels.CognitiveModel; + +public class Vision extends Action { + + + public Vision(){ + super(ActionType.VISION,1000); + } + +public Vision(int delay){ + super(ActionType.VISION,delay); +} + +public Vision(String dref){ + super(ActionType.VISION,1000,dref); +} + +public Vision(int delay, String dref){ + super(ActionType.VISION,delay, dref); +} + +} \ No newline at end of file diff --git a/Java/ProjectModels/CognitiveModel/XPCdependencies/Beacon.java b/Java/ProjectModels/CognitiveModel/XPCdependencies/Beacon.java new file mode 100644 index 0000000..620f719 --- /dev/null +++ b/Java/ProjectModels/CognitiveModel/XPCdependencies/Beacon.java @@ -0,0 +1,45 @@ +package ProjectModels.CognitiveModel.XPCdependencies; + +import java.net.InetAddress; + + +public class Beacon { + + private InetAddress xplaneAddress; + private int pluginPort; + private String pluginVersion; + private int xPlaneVersion; + + + public Beacon(InetAddress xplaneAddress, int pluginPort, String pluginVersion, int xPlaneVersion) { + this.xplaneAddress = xplaneAddress; + this.pluginPort = pluginPort; + this.pluginVersion = pluginVersion; + this.xPlaneVersion = xPlaneVersion; + } + + public String getHost() { + return xplaneAddress.getHostAddress(); + } + + public String getPluginVersion() { + return pluginVersion; + } + + public String getXplaneVersion() { + return String.format("%.2f", xPlaneVersion / 100.0); + } + + public int getPluginPort() { + return pluginPort; + } + + public InetAddress getXplaneAddress() { + return xplaneAddress; + } + + @Override + public String toString() { + return "host: " + getHost() + ":" + getPluginPort() +" version: " + getPluginVersion() + " X-Plane Version: " + getXplaneVersion(); + } +} diff --git a/Java/ProjectModels/CognitiveModel/XPCdependencies/BeaconParser.java b/Java/ProjectModels/CognitiveModel/XPCdependencies/BeaconParser.java new file mode 100644 index 0000000..121f0d7 --- /dev/null +++ b/Java/ProjectModels/CognitiveModel/XPCdependencies/BeaconParser.java @@ -0,0 +1,54 @@ +package ProjectModels.CognitiveModel.XPCdependencies; + +import java.io.IOException; +import java.net.DatagramPacket; +import java.net.InetAddress; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; + +/** + * Parser class that parses the UDP discovery BECN packet. The format of the packet is + * - 4 bytes: BECN + * - 1 byte: 0 + * - 2 bytes: XPlaneConnect server port e.g. '49009' + * - 4 bytes: X-Plane version e.g. '11260' + * - null terminated string: XPlaneConnect plugin version e.g. '1.3-rc.1' + */ +public class BeaconParser { + + private static String BECN = "BECN"; + + private static int XPC_PORT_OFFSET = BECN.length() + 1; + private static int XPC_PORT_LEN = 2; + private static int XPC_VERSION_OFFSET = XPC_PORT_OFFSET + XPC_PORT_LEN; + private static int XPC_VERSION_LEN = 4; + private static int XPC_PLUGIN_VERSION_OFFSET = XPC_VERSION_OFFSET + XPC_VERSION_LEN; + + + public Beacon readBCN(DatagramPacket packet) throws IOException { + if (packet.getLength() < BECN.length()) { + throw new IOException("BECN response too short"); + } + + byte[] data = packet.getData(); + + String command = new String(data, 0, BECN.length()); + if (!command.equals(BECN)) { + throw new IOException("Expected " + BECN + " got '" + command + "'"); + } + + InetAddress address = packet.getAddress(); + ByteBuffer bb = ByteBuffer.wrap(data); + bb.order(ByteOrder.LITTLE_ENDIAN); + // 2 bytes: port as short + converted to an int + int port = bb.getShort(XPC_PORT_OFFSET) & 0xffff; + + // 4 bytes: x plane version as int + int version = bb.getInt(XPC_VERSION_OFFSET); + + // plugin version + String pluginVersion = new String(data, XPC_PLUGIN_VERSION_OFFSET, packet.getLength() - XPC_PLUGIN_VERSION_OFFSET); + + return new Beacon(address, port, pluginVersion.trim(), version); + } +} \ No newline at end of file diff --git a/Java/ProjectModels/CognitiveModel/XPCdependencies/BeaconReceivedListener.java b/Java/ProjectModels/CognitiveModel/XPCdependencies/BeaconReceivedListener.java new file mode 100644 index 0000000..8c123cb --- /dev/null +++ b/Java/ProjectModels/CognitiveModel/XPCdependencies/BeaconReceivedListener.java @@ -0,0 +1,10 @@ +package ProjectModels.CognitiveModel.XPCdependencies; + +public interface BeaconReceivedListener { + + /** + * Called every time a BECN packet is received + * @param beacon The parsed beacon + */ + void onBeaconReceived(Beacon beacon); +} diff --git a/Java/ProjectModels/CognitiveModel/XPCdependencies/DiscoveryConnectionCallback.java b/Java/ProjectModels/CognitiveModel/XPCdependencies/DiscoveryConnectionCallback.java new file mode 100644 index 0000000..3fffd48 --- /dev/null +++ b/Java/ProjectModels/CognitiveModel/XPCdependencies/DiscoveryConnectionCallback.java @@ -0,0 +1,14 @@ +package ProjectModels.CognitiveModel.XPCdependencies; + +import ProjectModels.CognitiveModel.*; + +public interface DiscoveryConnectionCallback { + + /** + * Helper callback called when a 1st XPlanePlugin is discovered. When the 1st packet is received, an XPlaneConnect + * instance is created and the discovery is stopped. + * + * @param xpc The XPlaneConnect instance configured with the discovered + */ + void onConnectionEstablished(XPlaneConnect xpc); +} diff --git a/Java/ProjectModels/CognitiveModel/XPCdependencies/ViewType.java b/Java/ProjectModels/CognitiveModel/XPCdependencies/ViewType.java new file mode 100644 index 0000000..ad02ebb --- /dev/null +++ b/Java/ProjectModels/CognitiveModel/XPCdependencies/ViewType.java @@ -0,0 +1,60 @@ +//NOTICES: +// Copyright (c) 2013-2018 United States Government as represented by the Administrator of the +// National Aeronautics and Space Administration. All Rights Reserved. +// +// DISCLAIMERS +// No Warranty: THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY KIND, +// EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY THAT THE +// SUBJECT SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT THE SUBJECT +// SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT DOCUMENTATION, IF PROVIDED, WILL CONFORM TO +// THE SUBJECT SOFTWARE. THIS AGREEMENT DOES NOT, IN ANY MANNER, CONSTITUTE AN ENDORSEMENT BY +// GOVERNMENT AGENCY OR ANY PRIOR RECIPIENT OF ANY RESULTS, RESULTING DESIGNS, HARDWARE, +// SOFTWARE PRODUCTS OR ANY OTHER APPLICATIONS RESULTING FROM USE OF THE SUBJECT SOFTWARE. +// FURTHER, GOVERNMENT AGENCY DISCLAIMS ALL WARRANTIES AND LIABILITIES REGARDING THIRD-PARTY +// SOFTWARE, IF PRESENT IN THE ORIGINAL SOFTWARE, AND DISTRIBUTES IT "AS IS." +// +// Waiver and Indemnity: RECIPIENT AGREES TO WAIVE ANY AND ALL CLAIMS AGAINST THE UNITED STATES +// GOVERNMENT, ITS CONTRACTORS AND SUBCONTRACTORS, AS WELL AS ANY PRIOR RECIPIENT. IF +// RECIPIENT'S USE OF THE SUBJECT SOFTWARE RESULTS IN ANY LIABILITIES, DEMANDS, DAMAGES, +// EXPENSES OR LOSSES ARISING FROM SUCH USE, INCLUDING ANY DAMAGES FROM PRODUCTS BASED ON, OR +// RESULTING FROM, RECIPIENT'S USE OF THE SUBJECT SOFTWARE, RECIPIENT SHALL INDEMNIFY AND HOLD +// HARMLESS THE UNITED STATES GOVERNMENT, ITS CONTRACTORS AND SUBCONTRACTORS, AS WELL AS ANY +// PRIOR RECIPIENT, TO THE EXTENT PERMITTED BY LAW. RECIPIENT'S SOLE REMEDY FOR ANY SUCH MATTER +// SHALL BE THE IMMEDIATE, UNILATERAL TERMINATION OF THIS AGREEMENT. +package ProjectModels.CognitiveModel.XPCdependencies; + +/** + * Represents a camera view in X-Plane + * + * @author Jason Watkins + * @version 1.1 + * @since 2015-05-08 + */ +public enum ViewType +{ + Forwards(73), + Down(74), + Left(75), + Right(76), + Back(77), + Tower(78), + Runway(79), + Chase(80), + Follow(81), + FollowWithPanel(82), + Spot(83), + FullscreenWithHud(84), + FullscreenNoHud(85); + + private final int value; + private ViewType(int value) + { + this.value = value; + } + + public int getValue() + { + return value; + } +} diff --git a/Java/ProjectModels/CognitiveModel/XPCdependencies/WaypointOp.java b/Java/ProjectModels/CognitiveModel/XPCdependencies/WaypointOp.java new file mode 100644 index 0000000..956c268 --- /dev/null +++ b/Java/ProjectModels/CognitiveModel/XPCdependencies/WaypointOp.java @@ -0,0 +1,50 @@ +//NOTICES: +// Copyright (c) 2013-2018 United States Government as represented by the Administrator of the +// National Aeronautics and Space Administration. All Rights Reserved. +// +// DISCLAIMERS +// No Warranty: THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY KIND, +// EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY THAT THE +// SUBJECT SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT THE SUBJECT +// SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT DOCUMENTATION, IF PROVIDED, WILL CONFORM TO +// THE SUBJECT SOFTWARE. THIS AGREEMENT DOES NOT, IN ANY MANNER, CONSTITUTE AN ENDORSEMENT BY +// GOVERNMENT AGENCY OR ANY PRIOR RECIPIENT OF ANY RESULTS, RESULTING DESIGNS, HARDWARE, +// SOFTWARE PRODUCTS OR ANY OTHER APPLICATIONS RESULTING FROM USE OF THE SUBJECT SOFTWARE. +// FURTHER, GOVERNMENT AGENCY DISCLAIMS ALL WARRANTIES AND LIABILITIES REGARDING THIRD-PARTY +// SOFTWARE, IF PRESENT IN THE ORIGINAL SOFTWARE, AND DISTRIBUTES IT "AS IS." +// +// Waiver and Indemnity: RECIPIENT AGREES TO WAIVE ANY AND ALL CLAIMS AGAINST THE UNITED STATES +// GOVERNMENT, ITS CONTRACTORS AND SUBCONTRACTORS, AS WELL AS ANY PRIOR RECIPIENT. IF +// RECIPIENT'S USE OF THE SUBJECT SOFTWARE RESULTS IN ANY LIABILITIES, DEMANDS, DAMAGES, +// EXPENSES OR LOSSES ARISING FROM SUCH USE, INCLUDING ANY DAMAGES FROM PRODUCTS BASED ON, OR +// RESULTING FROM, RECIPIENT'S USE OF THE SUBJECT SOFTWARE, RECIPIENT SHALL INDEMNIFY AND HOLD +// HARMLESS THE UNITED STATES GOVERNMENT, ITS CONTRACTORS AND SUBCONTRACTORS, AS WELL AS ANY +// PRIOR RECIPIENT, TO THE EXTENT PERMITTED BY LAW. RECIPIENT'S SOLE REMEDY FOR ANY SUCH MATTER +// SHALL BE THE IMMEDIATE, UNILATERAL TERMINATION OF THIS AGREEMENT. +package ProjectModels.CognitiveModel.XPCdependencies; + +/** + * Represents operations that can be performed by the WYPT command. + * + * @author Jason Watkins + * @version 1.0 + * @since 2015-04-09 + */ +public enum WaypointOp +{ + Add(1), + Del(2), + Clr(3); + + private final int value; + private WaypointOp(int value) + { + this.value = value; + } + + public int getValue() + { + return value; + } +} diff --git a/Java/ProjectModels/CognitiveModel/XPCdependencies/XPlaneConnectDiscovery.java b/Java/ProjectModels/CognitiveModel/XPCdependencies/XPlaneConnectDiscovery.java new file mode 100644 index 0000000..6344fb4 --- /dev/null +++ b/Java/ProjectModels/CognitiveModel/XPCdependencies/XPlaneConnectDiscovery.java @@ -0,0 +1,94 @@ +package ProjectModels.CognitiveModel.XPCdependencies; + +import ProjectModels.CognitiveModel.*; + +import java.io.IOException; +import java.net.DatagramPacket; +import java.net.InetAddress; +import java.net.MulticastSocket; +import java.net.SocketException; + + +/** + * The XPlaneConnectDiscovery joins a UDP multi cast group and listens for BECN packets published by + * XPlaneConnect server plugin. It allows to clients to discover XPlane instances and be sure that the XPlanePlugin is + * installed. + */ +public class XPlaneConnectDiscovery implements AutoCloseable { + + private static int DEFAULT_PORT = 49710; + private static String DEFAULT_ADDRESS = "239.255.1.1"; + + private BeaconReceivedListener mListener; + + private MulticastSocket socket = null; + private byte[] buf = new byte[256]; + + private int mPort; + private String mAddress; + private BeaconParser parser = new BeaconParser(); + + private XPlaneConnectDiscovery(int port, String address) { + mPort = port; + mAddress = address; + } + + public XPlaneConnectDiscovery() { + this(DEFAULT_PORT, DEFAULT_ADDRESS); + } + + public void start(DiscoveryConnectionCallback callback) throws IOException { + onBeaconReceived(beacon -> { + this.close(); + + try { + XPlaneConnect xpc = new XPlaneConnect(beacon); + callback.onConnectionEstablished(xpc); + } catch (SocketException e) { + System.err.println("Could not connect to a discovered XplaneConnect " +beacon+ ": " + e.getMessage()); + } + }); + start(); + + } + + public void start() throws IOException { + System.out.println("Starting XPlane Connect discovery"); + socket = new MulticastSocket(mPort); + InetAddress group = InetAddress.getByName(mAddress); + socket.joinGroup(group); + while (socket != null) { + DatagramPacket packet = new DatagramPacket(buf, buf.length); + socket.receive(packet); + if (mListener == null) { + continue; + } + try { + Beacon beacon = parser.readBCN(packet); + mListener.onBeaconReceived(beacon); + } catch (IOException ex) { + System.err.println("Received packet on discovery group but could not parse it: " + ex); + } + } + + close(); + System.out.println("XPlane Connect discovery ended"); + } + + + public void onBeaconReceived(BeaconReceivedListener mListener) { + this.mListener = mListener; + } + + @Override + public void close() { + if (socket != null) { + socket.close(); + socket = null; + } + } + + public void stop() { + close(); + } +} diff --git a/Java/Project_Models/Cognitive_Model/XPlaneConnect.java b/Java/ProjectModels/CognitiveModel/XPlaneConnect.java similarity index 99% rename from Java/Project_Models/Cognitive_Model/XPlaneConnect.java rename to Java/ProjectModels/CognitiveModel/XPlaneConnect.java index fd3d41a..70be915 100644 --- a/Java/Project_Models/Cognitive_Model/XPlaneConnect.java +++ b/Java/ProjectModels/CognitiveModel/XPlaneConnect.java @@ -23,9 +23,11 @@ // PRIOR RECIPIENT, TO THE EXTENT PERMITTED BY LAW. RECIPIENT'S SOLE REMEDY FOR ANY SUCH MATTER // SHALL BE THE IMMEDIATE, UNILATERAL TERMINATION OF THIS AGREEMENT. -// package gov.nasa.xpc; +package ProjectModels.CognitiveModel; -// import gov.nasa.xpc.discovery.Beacon; +import ProjectModels.CognitiveModel.XPCdependencies.*; +import src.discovery.ViewType; +import src.discovery.WaypointOp; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -45,7 +47,7 @@ import java.util.Arrays; */ public class XPlaneConnect implements AutoCloseable { - private static int clientNum; + //private static int clientNum; private DatagramSocket socket; private InetAddress xplaneAddr; private int xplanePort; diff --git a/Java/Project_Models/Model-1/.idea/compiler.xml b/Java/ProjectModels/Model-1/.idea/compiler.xml similarity index 100% rename from Java/Project_Models/Model-1/.idea/compiler.xml rename to Java/ProjectModels/Model-1/.idea/compiler.xml diff --git a/Java/Project_Models/Model-1/.idea/copyright/profiles_settings.xml b/Java/ProjectModels/Model-1/.idea/copyright/profiles_settings.xml similarity index 100% rename from Java/Project_Models/Model-1/.idea/copyright/profiles_settings.xml rename to Java/ProjectModels/Model-1/.idea/copyright/profiles_settings.xml diff --git a/Java/Project_Models/Model-1/.idea/libraries/Java.xml b/Java/ProjectModels/Model-1/.idea/libraries/Java.xml similarity index 100% rename from Java/Project_Models/Model-1/.idea/libraries/Java.xml rename to Java/ProjectModels/Model-1/.idea/libraries/Java.xml diff --git a/Java/Project_Models/Model-1/.idea/misc.xml b/Java/ProjectModels/Model-1/.idea/misc.xml similarity index 100% rename from Java/Project_Models/Model-1/.idea/misc.xml rename to Java/ProjectModels/Model-1/.idea/misc.xml diff --git a/Java/Project_Models/Model-1/.idea/modules.xml b/Java/ProjectModels/Model-1/.idea/modules.xml similarity index 100% rename from Java/Project_Models/Model-1/.idea/modules.xml rename to Java/ProjectModels/Model-1/.idea/modules.xml diff --git a/Java/Project_Models/Model-1/.idea/vcs.xml b/Java/ProjectModels/Model-1/.idea/vcs.xml similarity index 100% rename from Java/Project_Models/Model-1/.idea/vcs.xml rename to Java/ProjectModels/Model-1/.idea/vcs.xml diff --git a/Java/Project_Models/Model-1/BasicOperation.iml b/Java/ProjectModels/Model-1/BasicOperation.iml similarity index 100% rename from Java/Project_Models/Model-1/BasicOperation.iml rename to Java/ProjectModels/Model-1/BasicOperation.iml diff --git a/Java/Project_Models/Model-1/content/Rudder Pedals.png b/Java/ProjectModels/Model-1/content/Rudder Pedals.png similarity index 100% rename from Java/Project_Models/Model-1/content/Rudder Pedals.png rename to Java/ProjectModels/Model-1/content/Rudder Pedals.png diff --git a/Java/Project_Models/Model-1/content/Yoke Symbol.png b/Java/ProjectModels/Model-1/content/Yoke Symbol.png similarity index 100% rename from Java/Project_Models/Model-1/content/Yoke Symbol.png rename to Java/ProjectModels/Model-1/content/Yoke Symbol.png diff --git a/Java/Project_Models/Model-1/src/Main.java b/Java/ProjectModels/Model-1/src/Main.java similarity index 100% rename from Java/Project_Models/Model-1/src/Main.java rename to Java/ProjectModels/Model-1/src/Main.java diff --git a/Java/Project_Models/Model-1/src/XPlaneConnect.java b/Java/ProjectModels/Model-1/src/XPlaneConnect.java similarity index 100% rename from Java/Project_Models/Model-1/src/XPlaneConnect.java rename to Java/ProjectModels/Model-1/src/XPlaneConnect.java diff --git a/Java/Project_Models/Model-1/src/legacy output b/Java/ProjectModels/Model-1/src/legacy output similarity index 100% rename from Java/Project_Models/Model-1/src/legacy output rename to Java/ProjectModels/Model-1/src/legacy output diff --git a/Java/Project_Models/Model-1/src/output b/Java/ProjectModels/Model-1/src/output similarity index 100% rename from Java/Project_Models/Model-1/src/output rename to Java/ProjectModels/Model-1/src/output diff --git a/Java/Project_Models/Cognitive_Model/Main.java b/Java/ProjectModels/src/Main.java similarity index 69% rename from Java/Project_Models/Cognitive_Model/Main.java rename to Java/ProjectModels/src/Main.java index ad362e5..3e5935b 100644 --- a/Java/Project_Models/Cognitive_Model/Main.java +++ b/Java/ProjectModels/src/Main.java @@ -1,3 +1,4 @@ +package ProjectModels.src; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; @@ -12,10 +13,11 @@ import javax.swing.OverlayLayout; import javax.swing.SwingConstants; import javax.swing.border.Border; +import ProjectModels.CognitiveModel.*; + public class Main { + public static void main(String[] args) { - - //Encapsulation (or lack thereof) Test Model m = new Model(); MindQueue q = m.getQueue(); @@ -25,25 +27,35 @@ public class Main { MindQueue q2 = m.getQueue(); System.out.println(q2.pop()); - + //Using actions try(XPlaneConnect xpc = new XPlaneConnect()) { - Action b = new Vision(); + m.createAction(0, 0, null); + Action b = new Vision(10,"sim/cockpit2/gauges/indicators/airspeed_kts_pilot"); + Action d = new Delay(20); // Ensure connection established. m = new Model(xpc); - m.activate(); + m.activateModel(); m.push(b); + m.push(d); + m.printModelQueue(); + // m.deactivateModel(); while(m.isActive() && !m.isEmpty()) { float[] array = m.next(); if(array != null){ - System.out.println(array[0]); + // System.out.println(array[0]); + if(array[0] > 80.0f){ + m.push(d); + } } else { - System.out.println("no dice"); + // System.out.println("no dice"); // System.out.println(xpc.getDREF(null)"); } - + if(m.getModelQueueLength() < 5){ + m.push(b); + } + m.printModelQueue(); } - } catch (SocketException ex) { @@ -52,7 +64,6 @@ public class Main { catch (IOException ex) { System.out.println("Something went wrong with one of the commands. (Error message was '" + ex.getMessage() + "'.)"); - } System.out.println("Exiting"); } diff --git a/Java/Project_Models/Cognitive_Model/Action.java b/Java/Project_Models/Cognitive_Model/Action.java deleted file mode 100644 index 66c778a..0000000 --- a/Java/Project_Models/Cognitive_Model/Action.java +++ /dev/null @@ -1,26 +0,0 @@ -public abstract class Action { - - - -Integer priority; //Priority of the Task -Integer delay; //Potential Delay before starting the task -Integer taskTime; //How long should the task take -boolean taskComplete; //Is the task completed? -int actionType; - -public Action(int actionType, int delay){ - this.actionType = actionType; - this.delay = delay; -} - -public int getType(){ - return actionType; -} - - -public Integer getDelay(){ - return delay; -} - - -} diff --git a/Java/Project_Models/Cognitive_Model/MindQueue.java b/Java/Project_Models/Cognitive_Model/MindQueue.java deleted file mode 100644 index 3cd48cb..0000000 --- a/Java/Project_Models/Cognitive_Model/MindQueue.java +++ /dev/null @@ -1,40 +0,0 @@ - - -import java.util.LinkedList; - -public class MindQueue { - - LinkedList q; - - public MindQueue () { - q = new LinkedList(); - } - - public void push(Action e){ - q.add(e); - } - - - public Action removeEvent(Action e){ - Action temp = e; - q.remove(e); - return temp; - } - - public Action pop() { - Vision v = new Vision(); - if(q.isEmpty()) { - return v; - } - return q.remove(); - } - - public boolean isEmpty(){ - return q.isEmpty(); - } - - - - - -} diff --git a/Java/Project_Models/Cognitive_Model/Model.java b/Java/Project_Models/Cognitive_Model/Model.java deleted file mode 100644 index 224ea6d..0000000 --- a/Java/Project_Models/Cognitive_Model/Model.java +++ /dev/null @@ -1,83 +0,0 @@ -import java.io.IOException; - -// import gov.nasa.xpc.XPlaneConnect; - -public class Model { - - private MindQueue q; - boolean modelActive; - XPlaneConnect xpc; - - public Model() { - q = new MindQueue(); - modelActive = false; - } - - public Model(XPlaneConnect xpc) { - q = new MindQueue(); - modelActive = false; - this.xpc = xpc; - } - - - - public MindQueue getQueue() { - return q; - } - - public float[] next(){ - Action temp = q.pop(); - System.out.println("Type of Action: " + temp.getType()); - float[] returnArray = null; - if(temp.getType() == 1){ - System.out.println("in next if statement"); - Vision tempV = (Vision) temp; - - //execute delay - try { - Thread.sleep(tempV.getDelay()); // Using the action's Built in Delay - } catch (InterruptedException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - - String dref = "sim/cockpit2/gauges/indicators/airspeed_kts_pilot"; - try { - returnArray = xpc.getDREF(dref); - - } catch (IOException e) { - // TODO: handle exception - } - - } - q.push(temp); - return returnArray; - - } - - public boolean isEmpty(){ - return q.isEmpty(); - } - - public void push(Action a){ - q.push(a); - } - - public void activate() { - modelActive = true; - } - - public void establishConnection(XPlaneConnect newXPC){ - this.xpc = newXPC; - } - - public void deactivate(){ - modelActive = false; - } - - public boolean isActive(){ - return modelActive; - } - -} diff --git a/Java/Project_Models/Cognitive_Model/Vision.java b/Java/Project_Models/Cognitive_Model/Vision.java deleted file mode 100644 index 65bfb21..0000000 --- a/Java/Project_Models/Cognitive_Model/Vision.java +++ /dev/null @@ -1,15 +0,0 @@ -import java.util.Map; -import java.util.TreeMap; - -public class Vision extends Action { - - - public Vision(){ - super(1,1000); - } - -public Vision(int delay){ - super(1,delay); -} - -} \ No newline at end of file diff --git a/Java/src/XPlaneConnect.java b/Java/src/XPlaneConnect.java index df32cff..6c669a4 100644 --- a/Java/src/XPlaneConnect.java +++ b/Java/src/XPlaneConnect.java @@ -26,6 +26,8 @@ package gov.nasa.xpc; import gov.nasa.xpc.discovery.Beacon; +import src.discovery.ViewType; +import src.discovery.WaypointOp; import java.io.ByteArrayOutputStream; import java.io.IOException; diff --git a/Java/src/ViewType.java b/Java/src/discovery/ViewType.java similarity index 99% rename from Java/src/ViewType.java rename to Java/src/discovery/ViewType.java index 3cff8c4..206784f 100644 --- a/Java/src/ViewType.java +++ b/Java/src/discovery/ViewType.java @@ -22,7 +22,7 @@ // HARMLESS THE UNITED STATES GOVERNMENT, ITS CONTRACTORS AND SUBCONTRACTORS, AS WELL AS ANY // PRIOR RECIPIENT, TO THE EXTENT PERMITTED BY LAW. RECIPIENT'S SOLE REMEDY FOR ANY SUCH MATTER // SHALL BE THE IMMEDIATE, UNILATERAL TERMINATION OF THIS AGREEMENT. -package gov.nasa.xpc; +package src.discovery; /** * Represents a camera view in X-Plane diff --git a/Java/src/WaypointOp.java b/Java/src/discovery/WaypointOp.java similarity index 98% rename from Java/src/WaypointOp.java rename to Java/src/discovery/WaypointOp.java index 30cf4fc..2873083 100644 --- a/Java/src/WaypointOp.java +++ b/Java/src/discovery/WaypointOp.java @@ -22,7 +22,7 @@ // HARMLESS THE UNITED STATES GOVERNMENT, ITS CONTRACTORS AND SUBCONTRACTORS, AS WELL AS ANY // PRIOR RECIPIENT, TO THE EXTENT PERMITTED BY LAW. RECIPIENT'S SOLE REMEDY FOR ANY SUCH MATTER // SHALL BE THE IMMEDIATE, UNILATERAL TERMINATION OF THIS AGREEMENT. -package gov.nasa.xpc; +package src.discovery; /** * Represents operations that can be performed by the WYPT command.