Skip to content

Commit

Permalink
Hue-Emulator V0.2. Many changes. See CHANGELOG.md for details.
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveyO committed Dec 1, 2014
1 parent 7b198d1 commit 0045230
Show file tree
Hide file tree
Showing 26 changed files with 1,069 additions and 354 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Hue-Emulator Changelog
============

v0.2 (01-December-2014)

New Features:
---
* UPNP Server. 1st attempt. If you run the Emulator on Port 80 (if not blocked) some hue apps now work out of the box :-)
* Added Create User + Pushlink functionality (you must click the bridge icon to simulate pushlinking)
* Started Scenes implementation. Get Scenes and Create Scenes functionality added + recall scenes. Not 100% tested.
* Can now configure which JSON Strings to show in console (i.e. Requests, Responses, Full Config).
* Re-ordered some of the JSON to be more consistent with a hue bridge

Bug Fixes:
---
* Software version is now a string (not an integer).
* Clear console should now work.
* Added transition time (attribute only, not bulb transitions).
File renamed without changes.
Binary file added HueEmulator-v0.2.jar
Binary file not shown.
4 changes: 3 additions & 1 deletion resources/config-2bulbs.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"lights": {
"1": {
"state": {
"on": false,
"on": true,
"bri": 254,
"hue": 4444,
"sat": 254,
Expand Down Expand Up @@ -141,5 +141,7 @@
},
"time": "2012-10-29T12:00:00"
}
},
"scenes": {
}
}
92 changes: 60 additions & 32 deletions src/com/hueemulator/emulator/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class Controller {

private MutableAttributeSet sas;
private StyleContext context;
public boolean hasBridgeBeenPushLinked=false;

public Controller(Model model, View view){
this.model = model;
Expand All @@ -51,7 +52,7 @@ public Controller(Model model, View view){
emulator = new Emulator(this);

String introText = "Welcome to the Hue Emulator. Choose a port and click the Start Button";
addTextToConsole(introText, Color.YELLOW);
addTextToConsole(introText, Color.YELLOW, true);
}

public void addPropertiesListeners() {
Expand All @@ -66,40 +67,40 @@ public void actionPerformed(ActionEvent e) {
public void addMenuListeners(){
// Add Listeners For Stop and Start Buttons.
view.getMenuBar().getStartButton().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
emulator.startServer();
public void actionPerformed(ActionEvent e)
{
emulator.startServers();
view.getMenuBar().getStartButton().setEnabled(false);
view.getMenuBar().getStopButton().setEnabled(true);
}
view.getMenuBar().getStopButton().setEnabled(true);
}
});

view.getMenuBar().getStopButton().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
public void actionPerformed(ActionEvent e)
{
emulator.stopServer();
view.getMenuBar().getStartButton().setEnabled(true);
view.getMenuBar().getStopButton().setEnabled(false);
}
view.getMenuBar().getStopButton().setEnabled(false);
}
});
view.getMenuBar().getClearConsoleMenuItem().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
consoleText="";
addTextToConsole("Clear Console", Color.WHITE);
clearConsole();
addTextToConsole("Console Cleared", Color.WHITE, true);
}
});


view.getMenuBar().getViewGraphicsMenuItems().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
if (view.getMenuBar().getViewGraphicsMenuItems().isSelected()) {
view.getGraphicsPanel().setVisible(true);
}
else {
view.getGraphicsPanel().setVisible(false);
}
if (view.getMenuBar().getViewGraphicsMenuItems().isSelected()) {
view.getGraphicsPanel().setVisible(true);
}
else {
view.getGraphicsPanel().setVisible(false);
}
}
});
view.getMenuBar().getLoadConfigMenuItem().addActionListener(new ActionListener() {
Expand All @@ -109,14 +110,7 @@ public void actionPerformed(ActionEvent e)
}
});

view.getMenuBar().getPropertiesMenuItem().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{

view.getPropertiesFrame().setLocation(300,300);
view.getPropertiesFrame().setVisible(true);
}
});

view.getMenuBar().getAboutMenuItem().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
Expand Down Expand Up @@ -144,7 +138,7 @@ public Controller() {
}


public void addTextToConsole(String text, Color textColour) {
public void addTextToConsole(String text, Color textColour, boolean appendText) {

if (view==null) {
return; // No View for JUnit tests, so this is null.
Expand All @@ -157,7 +151,9 @@ public void addTextToConsole(String text, Color textColour) {
dateString="";
}

append(dateString, text, textColour, view.getConsole());
if (appendText) { // Can now be disabled in menus
append(dateString, text, textColour, view.getConsole());
}

// Repaint the Light Bulbs after Every Command.
view.getGraphicsPanel().repaint();
Expand Down Expand Up @@ -218,19 +214,24 @@ public void addNewBulb() {
lightState.setOn(true);
lightState.setReachable(true);
lightState.setEffect("none");
lightState.setAlert("none");


lightState.setAlert("none");
light.setState(lightState);

bridgeConfiguration.getLights().put(newLightId, light);
addTextToConsole("New Bulb Created: " + newLightId, Color.ORANGE);
addTextToConsole("New Bulb Created: " + newLightId, Color.ORANGE, true);
}

public String getIpAddress() {
return ipAddress;
}

public void clearConsole() {
// System.out.println("Clear Console " + int docLength = document.getLength(););
try {
view.getConsole().getDocument().remove(0, view.getConsole().getDocument().getLength());
} catch (BadLocationException e) {}
}

public void append(final String dateString, final String s, final Color color, final JEditorPane console) {
if (!SwingUtilities.isEventDispatchThread()) {
SwingUtilities.invokeLater(new Runnable() {
Expand Down Expand Up @@ -263,4 +264,31 @@ public void run() {
}
}

public boolean isHasBridgeBeenPushLinked() {
return hasBridgeBeenPushLinked;
}

public void setHasBridgeBeenPushLinked(boolean hasBridgeBeenPushLinked) {
this.hasBridgeBeenPushLinked = hasBridgeBeenPushLinked;
}

public boolean showRequestJson() {
return model.isShowRequestJSON();
}

public void setShowRequestJson(boolean showRequestJSON) {
model.setShowRequestJSON(showRequestJSON);
}

public boolean showResponseJson() {
return model.isShowResponseJSON();
}

public void setShowResponseJson(boolean showResponseJSON) {
model.setShowResponseJSON(showResponseJSON);
}

public void setShowFullConfigJson(boolean showFullConfigJSON) {
model.setShowFullConfigJSON(showFullConfigJSON);
}
}
124 changes: 67 additions & 57 deletions src/com/hueemulator/emulator/Emulator.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,76 +10,86 @@

import com.hueemulator.model.PHBridgeConfiguration;
import com.hueemulator.server.Server;
import com.hueemulator.server.UPNPServer;



// Taken from here: http://www.java2s.com/Code/Java/JDK-6/LightweightHTTPServer.htm
// Here is another one: http://www.sourcestream.com/programming-stuff/java-http-server

public class Emulator {

private Server server;
private Controller controller;

public Emulator(Controller controller) {
this.controller = controller;
String fileName = "/config-2bulbs.json";
controller.addTextToConsole("Loading in Configuration, Filename: " + fileName, Color.WHITE);

loadConfiguration(fileName);
controller.addTextToConsole("Starting Emulator...", Color.GREEN);

private Server server;
private UPNPServer upnpServer;
private Controller controller;

public Emulator(Controller controller) {
this.controller = controller;
String fileName = "/config-2bulbs.json";
controller.addTextToConsole("Loading in Configuration, Filename: " + fileName, Color.WHITE, true);

loadConfiguration(fileName);
controller.addTextToConsole("Starting Emulator...", Color.GREEN, true);
}

public void startServer() {

try {
server = new Server(controller.getModel().getBridgeConfiguration(), controller, controller.getPort());
controller.setIPAddress();
} catch (java.net.BindException e) {
controller.addTextToConsole(" **NOT STARTED ** Server already running. " + e.getMessage(), Color.RED);
} catch (IOException e) {
e.printStackTrace();
}
server.getHttpServer().start();
controller.addTextToConsole("**STARTED** Emulator is listening on port: " + controller.getPort(), Color.WHITE);

}


public void startServers() {
try {
server = new Server(controller.getModel().getBridgeConfiguration(), controller, controller.getPort());
controller.setIPAddress();
} catch (java.net.BindException e) {
controller.addTextToConsole(" **NOT STARTED ** Server already running. " + e.getMessage(), Color.RED, true);
} catch (IOException e) {
e.printStackTrace();
}
server.getHttpServer().start();
controller.addTextToConsole("**STARTED** Emulator is listening on port: " + controller.getPort(), Color.WHITE, true);

upnpServer = new UPNPServer(controller);
upnpServer.startUPNPServer();

controller.addTextToConsole("UPnP Server Started" , Color.WHITE, true);

if (!controller.getPort().equals("80")) {
controller.addTextToConsole("UPnP works best with the Emulator running on port 80. Apps written with the Java/iOS SDK's wont connect to the Emulator." , Color.RED, true);
}

}

public void stopServer() {
if (server!=null && server.getHttpServer() !=null) {
server.getHttpServer().stop(0);
server.getHttpServer().removeContext("/api");
controller.addTextToConsole("Stopping the Server on port: " + server.getHttpServer().getAddress().getPort(), Color.RED);
}
if (server!=null && server.getHttpServer() !=null) {
server.getHttpServer().stop(0);
server.getHttpServer().removeContext("/api");
controller.addTextToConsole("Stopping the Server on port: " + server.getHttpServer().getAddress().getPort(), Color.RED, true);
}
if (upnpServer !=null) {
upnpServer.stopUPNPServer();
}
}

public void loadConfiguration(String fileName) {
//2. Convert JSON to Java object
ObjectMapper mapper = new ObjectMapper();
try {
InputStream is = getClass().getResourceAsStream(fileName);
controller.getModel().setBridgeConfiguration(mapper.readValue(is, PHBridgeConfiguration.class));
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();

}


//2. Convert JSON to Java object
ObjectMapper mapper = new ObjectMapper();
try {
InputStream is = getClass().getResourceAsStream(fileName);
controller.getModel().setBridgeConfiguration(mapper.readValue(is, PHBridgeConfiguration.class));
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();

}
}
public Server getServer() {
return server;
}

public void setServer(Server server) {
this.server = server;
}

public Server getServer() {
return server;
}

public void setServer(Server server) {
this.server = server;
}

}


Expand Down
1 change: 1 addition & 0 deletions src/com/hueemulator/emulator/HueEmulator.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public HueEmulator() {
// Bind the Model and View
Controller controller = new Controller(model,view);
view.getMenuBar().setController(controller);
view.getGraphicsPanel().setController(controller);

// Add all the Menu Listeners.
controller.addMenuListeners();
Expand Down
Loading

0 comments on commit 0045230

Please sign in to comment.