Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assignment 5 - Redo Pull Request (headSimulatorTwo) #15

Merged
merged 16 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
6 changes: 5 additions & 1 deletion .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ The dashboard displays the affective states and eye gaze of the user in real-tim
- [ ] :two: **Implement a Common Subscriber**
- Ensure the subscriber class works across all components.

- [ ] :three: **Create a app.library.Blackboard with Delegates**
- [ ] :three: **Create a app.Blackboard with Delegates**
- Implement a set of blackboards following the singleton pattern for shared communication.

- [ ] :four: **Implement Standard Robot with Animation**
Expand All @@ -47,7 +47,7 @@ The dashboard displays the affective states and eye gaze of the user in real-tim
- Ensure all panels observe the blackboard, displaying a default drawing and updating whenever an event is fired.

- [ ] :six: **Add Menus to Applications**
- Include a menu in all applications with options to start/stop sending or receiving data, i.e., all app.library.Main are "similar"
- Include a menu in all applications with options to start/stop sending or receiving data, i.e., all app.Main are "similar"

- [ ] :seven: **Code Review**
- Ensure all classes adhere to the Single Responsibility Principle (SRP). Make recomendations
Expand Down
Empty file.
Binary file modified headSimulatorTwo/.DS_Store
Binary file not shown.
7 changes: 7 additions & 0 deletions headSimulatorTwo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@
</dependency>

<!-- Additional library (if available in a repository) -->
<!-- <dependency>-->
<!-- <groupId>javiergs</groupId>-->
<!-- <artifactId>library/headSimTwo</artifactId>-->
<!-- <version>1.0-SNAPSHOT</version>-->
<!-- <scope>compile</scope>-->
<!-- </dependency>-->

<dependency>
<groupId>javiergs</groupId>
<artifactId>library</artifactId>
Expand Down
Binary file modified headSimulatorTwo/src/.DS_Store
Binary file not shown.
36 changes: 36 additions & 0 deletions headSimulatorTwo/src/main/java/app/DataPointListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package app;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

/**
* The `DataPointListener` class listens for property change events from the
* `Blackboard` and updates the `TrackArea` with the latest data point.
*/
public class DataPointListener implements PropertyChangeListener {

private final TrackArea trackArea;

/**
* Constructs a `DataPointListener`.
*
* @param trackArea The `TrackArea` to update with new data points.
*/
public DataPointListener(TrackArea trackArea) {
this.trackArea = trackArea;
}

/**
* Called when a property change event is fired.
* Updates the `TrackArea` if the event is a "newPoint" event.
*
* @param evt The `PropertyChangeEvent` object.
*/
@Override
public void propertyChange(PropertyChangeEvent evt) {
if ("newPoint".equals(evt.getPropertyName())) {
int[] point = (int[]) evt.getNewValue();
trackArea.updateLatestPoint(point[0], point[1]);
}
}
}
91 changes: 91 additions & 0 deletions headSimulatorTwo/src/main/java/app/EyeController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package app;


import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import headSim.Blackboard;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import headSim.Publisher;


/**
* The `EyeController` class manages the eye tracking simulation by responding to mouse
* movements and user actions from a dropdown menu. It updates the eye position in the
* `TrackArea` and sends data to the `Server` for processing.
*
* @author Ashton
* @author David H.
* @author Anthony C.
* @version 1.0
*/
public class EyeController implements MouseMotionListener, ActionListener {

private TrackArea trackArea;
private Publisher server;
private JComboBox<String> menu;
private boolean init_connected = false;
private static final Logger logger = LoggerFactory.getLogger(EyeController.class);

/**
* Constructs a `EyeController`.
*
* @param trackArea The `TrackArea` where eye movements are visualized.
* @param server The `Server` that manages the WebSocket connection.
* @param menu The dropdown menu for controlling the server (Start/Stop).
*/
public EyeController(TrackArea trackArea, Publisher server, JComboBox<String> menu) {
this.trackArea = trackArea;
this.server = server;
this.menu = menu;
Blackboard.getInstance().addPropertyChangeListener(new DataPointListener(trackArea));
}

/**
* Updates the eye position and sends data to the `Blackboard` when the mouse is moved.
*
* @param e The mouse movement event.
*/
@Override
public void mouseMoved(MouseEvent e) {
if (server.isRunning()) {
int x = e.getX();
int y = e.getY();
trackArea.moveEyes(x, y);
int[] newPoint = {x, y};
Blackboard.getInstance().addPoint(newPoint);
logger.info("Mouse moved to: ({}, {})", x, y);
}
}

/**
* Handles mouse drag events (currently not used).
*
* @param e The mouse drag event.
*/
@Override
public void mouseDragged(MouseEvent e) {
}

/**
* Handles actions from the dropdown menu (e.g., starting or stopping the server).
*
* @param e The action event.
*/
@Override
public void actionPerformed(ActionEvent e) {
String selectedAction = (String) menu.getSelectedItem();
if (selectedAction.equals("Start")) {
server.start();
logger.info("app.library.Server started by user action.");
} else if (selectedAction.equals("Stop")) {
server.stop();
logger.info("app.library.Server stopped by user action.");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
package app.library;
package app;

import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import headSim.Blackboard;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import headSim.Publisher;
import headSim.TheSubscriber;
import headSim.TheSubscriberMQTT;

/**
* app.library.Main class is responsible for controlling the eye tracking simulation.
* Authors as listed in your README.md file.
* The `Main` class is the entry point of the eye tracking simulation application. It sets up
* the main window, initializes key components (server, subscriber, UI elements), and manages
* their interaction.
*
* @author Ashton
* @author David H.
Expand All @@ -21,13 +27,16 @@
*/
public class Main extends JFrame {

private Server server;
private Publisher server;
private TheSubscriber subscriber;
private static final Logger logger = LoggerFactory.getLogger(Main.class);
private String subscriberType;

/**
* Constructs the main application window and initializes components.
*/
public Main() {
server = new Server();
server = new Publisher();
setLayout(new BorderLayout());

JPanel dropdownPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
Expand All @@ -36,16 +45,15 @@ public Main() {
dropdownPanel.add(dropdownMenu);
add(dropdownPanel, BorderLayout.NORTH);

Blackboard blackboard = new Blackboard();
TrackArea area = new TrackArea(server, dropdownMenu, blackboard);
TrackArea area = new TrackArea(server, dropdownMenu, ScreenController.getInstance());
add(area, BorderLayout.CENTER);

Controller c = new Controller(area, server, dropdownMenu);
EyeController c = new EyeController(area, server, dropdownMenu);
dropdownMenu.addActionListener(c);

blackboard.setDrawingState("Updated TrackArea");
ScreenController.getInstance().setDrawingState("Updated TrackArea");

DataRepository destination = DataRepository.getInstance();
Blackboard destination = Blackboard.getInstance();

// Default for now
subscriberType = "tcp";
Expand All @@ -57,7 +65,7 @@ public Main() {
String ipHost = "127.0.0.1"; // Example IP, replace with actual if needed
int port = 9090; // Example port, replace with actual if needed
String dataPrefix = "PointData";
subscriber = new TheSubscriber(ipHost, port, dataPrefix, DataRepository.getInstance());
subscriber = new TheSubscriber(ipHost, port, dataPrefix, Blackboard.getInstance());
new Thread(subscriber).start();
logger.info("TheSubscriber initialized and started.");
} catch (IOException e) {
Expand All @@ -71,7 +79,6 @@ public Main() {
// Define topic and prefix pairs
Map<String, String> topicAndPrefixPairs = new HashMap<>();
topicAndPrefixPairs.put("device/coords", "XY");
// topicAndPrefixPairs.put("device/direction", "HUMID");
try {
// Instantiate the subscriber
TheSubscriberMQTT subscriber = new TheSubscriberMQTT(broker, clientID, topicAndPrefixPairs, destination);
Expand All @@ -81,19 +88,20 @@ public Main() {
subscriberThread.start();
logger.info("TheSubscriber initialized and started.");

// Optionally, stop the subscriber after a certain condition
// subscriber.stopSubscriber();
// subscriberThread.join();

} catch (MqttException e) {
System.out.println("An error occurred while initializing the subscriber: " + e.getMessage());
}
}else{
}else{
logger.error("Unknown subscriber type");
}

}

/**
* Main method to launch the application.
*
* @param args Command line arguments.
*/
public static void main(String[] args) {
Main main = new Main();
main.setTitle("Eye Tracker Simulator");
Expand All @@ -103,6 +111,9 @@ public static void main(String[] args) {
logger.info("Eye Tracker Simulator application started.");
}

/**
* Called when the application window is closed.
*/
@Override
public void dispose() {
super.dispose();
Expand Down
Loading