Aanpassing in worker: starten activeert niet automatisch, activeren start nu wel automatisch. Bij activeren kan worden opgegeven of als een thread moet worden gestart. Register lezen IOException genegeerd. LircDevice naar knop herkenning toegevoegd en class Multiplexer geintroduceerd. Verder kleine aanpassingen.

This commit is contained in:
2011-06-02 11:00:32 +00:00
parent fb9c871a80
commit 075e058ef2
19 changed files with 253 additions and 50 deletions

View File

@@ -6,6 +6,7 @@ import org.apache.commons.logging.LogFactory;
import pm.device.javainput.extreme3d.Extreme3DDevice; import pm.device.javainput.extreme3d.Extreme3DDevice;
import pm.device.javainput.rumblepad.RumblepadDevice; import pm.device.javainput.rumblepad.RumblepadDevice;
import pm.device.jintellitype.JIntellitypeDevice; import pm.device.jintellitype.JIntellitypeDevice;
import pm.device.lirc.LircDevice;
import pm.device.network.NetworkDevice; import pm.device.network.NetworkDevice;
import pm.device.panel.PanelDevice; import pm.device.panel.PanelDevice;
import pm.device.player.PlayerDevice; import pm.device.player.PlayerDevice;
@@ -30,6 +31,7 @@ public class Client {
public Client(String ip, int port) throws GlobalRouterException { public Client(String ip, int port) throws GlobalRouterException {
eventRouter = new GlobalRouter(ip, port); eventRouter = new GlobalRouter(ip, port);
deviceArray = new Device[] { deviceArray = new Device[] {
new LircDevice(),
new WiimoteDevice(), new WiimoteDevice(),
new PanelDevice(), new PanelDevice(),
new JIntellitypeDevice(), new JIntellitypeDevice(),

View File

@@ -12,6 +12,7 @@ import pm.application.vlc.VLCApplication;
import pm.device.javainput.extreme3d.Extreme3DDevice; import pm.device.javainput.extreme3d.Extreme3DDevice;
import pm.device.javainput.rumblepad.RumblepadDevice; import pm.device.javainput.rumblepad.RumblepadDevice;
import pm.device.jintellitype.JIntellitypeDevice; import pm.device.jintellitype.JIntellitypeDevice;
import pm.device.lirc.LircDevice;
import pm.device.network.NetworkDevice; import pm.device.network.NetworkDevice;
import pm.device.panel.PanelDevice; import pm.device.panel.PanelDevice;
import pm.device.player.PlayerDevice; import pm.device.player.PlayerDevice;
@@ -36,6 +37,7 @@ public class Main {
new VLCApplication(), new VLCApplication(),
new WinampApplication()}; new WinampApplication()};
deviceArray = new Device[] { deviceArray = new Device[] {
new LircDevice(),
new WiimoteDevice(), new WiimoteDevice(),
new PanelDevice(), new PanelDevice(),
new JIntellitypeDevice(), new JIntellitypeDevice(),
@@ -48,7 +50,7 @@ public class Main {
public void start() { public void start() {
log.debug("Main"); log.debug("Main");
Mimis mimis = new Mimis(eventRouter, applicationArray, deviceArray); Mimis mimis = new Mimis(eventRouter, applicationArray, deviceArray);
mimis.start(); mimis.activate();
} }
public static void main(String[] args) { public static void main(String[] args) {

View File

@@ -14,7 +14,7 @@ import pm.manager.Titled;
public class Manager<T extends Worker & Titled & Exitable> extends Worker { public class Manager<T extends Worker & Titled & Exitable> extends Worker {
protected Log log = LogFactory.getLog(getClass()); protected Log log = LogFactory.getLog(getClass());
protected static final long serialVersionUID = 1L; protected static final long serialVersionUID = 1L;
protected static final int INTERVAL = 5000; protected static final int INTERVAL = 100;
protected T[] manageableArray; protected T[] manageableArray;
protected Map<T, SelectButton<T>> buttonMap; protected Map<T, SelectButton<T>> buttonMap;

View File

@@ -41,9 +41,9 @@ public class Mimis extends EventHandler {
applicationCycle = new ArrayCycle<Application>(applicationArray); applicationCycle = new ArrayCycle<Application>(applicationArray);
} }
public void start() { public void activate() {
log.debug("Start managers"); log.debug("Activate managers");
applicationManager.start(); applicationManager.activate();
deviceManager.start(); deviceManager.start();
log.debug("Create gui"); log.debug("Create gui");
@@ -53,7 +53,7 @@ public class Mimis extends EventHandler {
log.debug("Initialise application cycle"); log.debug("Initialise application cycle");
eventRouter.set(applicationCycle.current()); eventRouter.set(applicationCycle.current());
} }
super.start(false); super.activate(false);
} }
public void exit() { public void exit() {

View File

@@ -15,7 +15,6 @@ public abstract class Worker implements Runnable {
public void start(boolean thread) { public void start(boolean thread) {
log.debug("Start"); log.debug("Start");
running = true; running = true;
activate();
if (thread) { if (thread) {
log.debug("Start thread"); log.debug("Start thread");
new Thread(this).start(); new Thread(this).start();
@@ -59,6 +58,13 @@ public abstract class Worker implements Runnable {
} }
public void activate() { public void activate() {
activate(THREAD);
}
public void activate(boolean thread) {
if (!running) {
start(thread);
}
active = true; active = true;
} }

View File

@@ -4,6 +4,7 @@ import java.io.IOException;
import pm.Application; import pm.Application;
import pm.util.Native; import pm.util.Native;
import pm.util.VBScript;
public abstract class CMDApplication extends Application { public abstract class CMDApplication extends Application {
protected final static String REGISTRY = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths"; protected final static String REGISTRY = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths";
@@ -18,7 +19,7 @@ public abstract class CMDApplication extends Application {
this.title = title; this.title = title;
} }
protected void initialise() { public void activate() {
String key = String.format("%s\\%s", REGISTRY, program); String key = String.format("%s\\%s", REGISTRY, program);
// Check of naam is gevonden in register // Check of naam is gevonden in register
String path = Native.getValue(key); String path = Native.getValue(key);
@@ -30,13 +31,15 @@ public abstract class CMDApplication extends Application {
e.printStackTrace(); e.printStackTrace();
//throw new ApplicationInitialiseException(); //throw new ApplicationInitialiseException();
} }
super.activate();
} }
public void activate() { public boolean active() {
if (!active) { try {
initialise(); return active = VBScript.isRunning(program);
} catch (IOException e) {
return false;
} }
super.activate();
} }
public void deactivate() { public void deactivate() {

View File

@@ -37,15 +37,15 @@ public class iTunesApplication extends Application implements iTunesEventsInterf
} }
public boolean active() { public boolean active() {
log.info("Check iTunes"); //log.info("Check iTunes");
try { try {
iTunes.getCurrentTrack(); iTunes.getCurrentTrack();
active = true; active = true;
} catch (Exception e) { } catch (Exception e) {
log.fatal(e); //log.fatal(e);
active = false; active = false;
} }
log.info(active); //log.info(active);
return active; return active;
} }

View File

@@ -8,7 +8,6 @@ import pm.event.Task;
import pm.exception.MacroException; import pm.exception.MacroException;
import pm.exception.button.UnknownButtonException; import pm.exception.button.UnknownButtonException;
import pm.exception.button.UnknownDirectionException; import pm.exception.button.UnknownDirectionException;
import pm.exception.device.DeviceInitialiseException;
import pm.macro.state.Hold; import pm.macro.state.Hold;
import pm.macro.state.Press; import pm.macro.state.Press;
import pm.macro.state.Release; import pm.macro.state.Release;
@@ -25,11 +24,6 @@ public class Extreme3DDevice extends JavaInputDevice {
} }
public void initialise() { public void initialise() {
try {
super.initialise(NAME);
} catch (DeviceInitialiseException e) {
e.printStackTrace();
}
try { try {
add( add(
new Press(Extreme3DButton.TWELVE), new Press(Extreme3DButton.TWELVE),

View File

@@ -8,7 +8,6 @@ import pm.event.task.Continuous;
import pm.event.task.Dynamic; import pm.event.task.Dynamic;
import pm.exception.button.UnknownButtonException; import pm.exception.button.UnknownButtonException;
import pm.exception.button.UnknownDirectionException; import pm.exception.button.UnknownDirectionException;
import pm.exception.device.DeviceInitialiseException;
import pm.macro.state.Hold; import pm.macro.state.Hold;
import pm.macro.state.Press; import pm.macro.state.Press;
import pm.value.Action; import pm.value.Action;
@@ -24,11 +23,6 @@ public class RumblepadDevice extends JavaInputDevice {
} }
public void initialise(){ public void initialise(){
try {
super.initialise(NAME);
} catch (DeviceInitialiseException e) {
e.printStackTrace();
}
add( add(
new Press(RumblepadButton.ONE), new Press(RumblepadButton.ONE),
new Task(Target.APPLICATION, Action.PLAY)); new Task(Target.APPLICATION, Action.PLAY));

View File

@@ -0,0 +1,7 @@
package pm.device.lirc;
import pm.Button;
public interface LircButton extends Button {
public String getCode();
}

View File

@@ -0,0 +1,5 @@
package pm.device.lirc;
public interface LircButtonListener {
public void add(LircButton lircButton);
}

View File

@@ -1,19 +1,33 @@
package pm.device.lirc; package pm.device.lirc;
import pm.Device; import java.util.HashMap;
public class LircDevice extends Device { import pm.Device;
import pm.device.lirc.button.DenonRC176;
import pm.device.lirc.button.PhiliphsRCLE011Button;
import pm.util.Multiplexer;
public class LircDevice extends Device implements LircButtonListener {
protected static final String TITLE = "Lirc"; protected static final String TITLE = "Lirc";
protected Multiplexer multiplexer;
protected LircService lircService; protected LircService lircService;
public LircDevice(String title) { public LircDevice() {
super(title); super(TITLE);
lircService = new LircService();
HashMap<String, LircButton[]> buttonMap = new HashMap<String, LircButton[]>();
buttonMap.put(PhiliphsRCLE011Button.NAME, PhiliphsRCLE011Button.values());
buttonMap.put(DenonRC176.NAME, DenonRC176.values());
multiplexer = new Multiplexer();
lircService = new LircService(buttonMap);
lircService.add(this);
} }
public void activate() { public void activate() {
lircService.activate(); lircService.start();
super.activate(); super.activate();
} }
@@ -21,4 +35,8 @@ public class LircDevice extends Device {
lircService.deactivate(); lircService.deactivate();
super.deactivate(); super.deactivate();
} }
public void add(LircButton lircButton) {
}
} }

View File

@@ -8,13 +8,19 @@ import java.io.OutputStream;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.net.Socket; import java.net.Socket;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.Scanner;
import pm.Worker; import pm.Worker;
import pm.exception.button.UnknownButtonException;
public class LircService extends Worker { public class LircService extends Worker {
public static final String IP = "127.0.0.1"; public static final String IP = "127.0.0.1";
public static final int PORT = 6789; public static final int PORT = 8765;
protected ArrayList<LircButtonListener> lircButtonListenerList;
protected String ip; protected String ip;
protected int port; protected int port;
protected Socket socket; protected Socket socket;
@@ -22,14 +28,25 @@ public class LircService extends Worker {
protected OutputStream outputStream; protected OutputStream outputStream;
protected BufferedReader bufferedReader; protected BufferedReader bufferedReader;
protected PrintWriter printWriter; protected PrintWriter printWriter;
protected HashMap<String, LircButton[]> buttonMap;
public LircService() { public LircService(HashMap<String, LircButton[]> buttonMap) {
this(IP, PORT); this(buttonMap, IP, PORT);
} }
public LircService(String ip, int port) { public LircService(HashMap<String, LircButton[]> buttonMap, String ip, int port) {
this.buttonMap = buttonMap;
this.ip = ip; this.ip = ip;
this.port = port; this.port = port;
lircButtonListenerList = new ArrayList<LircButtonListener>();
}
public void add(LircButtonListener lircButtonListener) {
lircButtonListenerList.add(lircButtonListener);
}
public void remove(LircButtonListener lircButtonListener) {
lircButtonListenerList.remove(lircButtonListener);
} }
public void activate() { public void activate() {
@@ -46,6 +63,7 @@ public class LircService extends Worker {
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
super.activate();
} }
public void deactivate() { public void deactivate() {
@@ -54,7 +72,7 @@ public class LircService extends Worker {
outputStream.close(); outputStream.close();
socket.close(); socket.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); //e.printStackTrace();
} }
super.deactivate(); super.deactivate();
} }
@@ -62,9 +80,41 @@ public class LircService extends Worker {
public void work() { public void work() {
try { try {
String string = bufferedReader.readLine(); String string = bufferedReader.readLine();
System.out.println(string); try {
LircButton lircButton = parseButton(new Scanner(string));
log.debug(String.format("Lirc button: %s", lircButton));
for (LircButtonListener lircbuttonListener : lircButtonListenerList) {
lircbuttonListener.add(lircButton);
}
} catch (UnknownButtonException e) {}
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
public LircButton parseButton(Scanner scanner) throws UnknownButtonException {
try {
scanner.next();
scanner.next();
String code = scanner.next();
String remote = scanner.next();
//log.debug(String.format("%s: %s", remote, code));
LircButton[] buttonArray = buttonMap.get(remote);
if (buttonArray != null) {
for (LircButton button : buttonArray) {
if (button.getCode().equals(code)) {
return button;
}
}
}
} catch (InputMismatchException e) {}
throw new UnknownButtonException();
}
public static void main(String[] args) {
System.out.println("hey");
//String send = Native.getValue("HKEY_CURRENT_USER\\Software\\LIRC", "password");
new LircDevice().start();
while (true);
}
} }

View File

@@ -0,0 +1,21 @@
package pm.device.lirc.button;
import pm.device.lirc.LircButton;
public enum DenonRC176 implements LircButton {
;
public static final String NAME = "DENON_RC-176";
protected String code;
private DenonRC176(String code) {
this.code = code;
}
public String getCode() {
return code;
}
}

View File

@@ -0,0 +1,63 @@
package pm.device.lirc.button;
import pm.device.lirc.LircButton;
import pm.exception.button.UnknownButtonException;
public enum PhiliphsRCLE011Button implements LircButton {
POWER ("Standby"),
RED ("Red"),
GREEN ("Green"),
YELLOW ("Yellow"),
BLUE ("Blue"),
TUNE ("Tune"),
RADIO ("Radio"),
SQUARE ("Square"),
MENU ("Menu"),
TEXT ("Text"),
UP ("Up"),
DOWN ("Down"),
LEFT ("Left"),
RIGHT ("Right"),
VOLUME_UP ("Volume+"),
VOLUME_DOWN ("Volume-");
/*Mute,
Program+,
Program-,
1,
2,
3,
4,
5,
6
7,
8,
9,
0,
Clock,
Out,
i+,
screenup,
screendown,
question;*/
public static final String NAME = "Philips_RCLE011";
protected String code;
private PhiliphsRCLE011Button(String code) {
this.code = code;
}
public String getCode() {
return code;
}
public static PhiliphsRCLE011Button create(String code) throws UnknownButtonException {
for (PhiliphsRCLE011Button button : PhiliphsRCLE011Button.values()) {
if (button.getCode().equals(code)) {
return button;
}
}
throw new UnknownButtonException();
}
}

View File

@@ -1,5 +1,9 @@
package pm.device.panel; package pm.device.panel;
import java.awt.event.WindowEvent;
import javax.swing.WindowConstants;
import pm.Device; import pm.Device;
import pm.event.Task; import pm.event.Task;
import pm.macro.state.Press; import pm.macro.state.Press;
@@ -16,7 +20,16 @@ public class PanelDevice extends Device implements PanelButtonListener {
} }
public void activate() { public void activate() {
panel = new Panel(this); panel = new Panel(this) {
protected static final long serialVersionUID = 1L;
protected void processWindowEvent(WindowEvent e) {
log.debug("Window closing");
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
deactivate();
}
}
};
panel.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
//panel.updateTime(12342398); //panel.updateTime(12342398);
//panel.updatePosition(43); //panel.updatePosition(43);
add(new Press(PanelButton.PREVIOUS), new Task(Target.APPLICATION, Action.PREVIOUS)); add(new Press(PanelButton.PREVIOUS), new Task(Target.APPLICATION, Action.PREVIOUS));
@@ -31,10 +44,21 @@ public class PanelDevice extends Device implements PanelButtonListener {
add(new Press(PanelButton.VOLUME_UP), new Task(Target.APPLICATION, Action.VOLUME_UP)); add(new Press(PanelButton.VOLUME_UP), new Task(Target.APPLICATION, Action.VOLUME_UP));
} }
public boolean active() {
return active = panel != null && panel.isValid();
}
public void deactivate() { public void deactivate() {
panel.dispose(); panel.dispose();
} }
protected void processWindowEvent(WindowEvent e) {
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
log.debug("Window closing");
deactivate();
}
}
public void buttonPressed(PanelButton panelButton) { public void buttonPressed(PanelButton panelButton) {
//Vang hier toggles af om bijvoorbeeld de play/pause en mute knop en veranderen //Vang hier toggles af om bijvoorbeeld de play/pause en mute knop en veranderen
add(new Press(panelButton)); add(new Press(panelButton));

View File

@@ -20,11 +20,14 @@ public class SelectButton<T extends Worker & Titled> extends JToggleButton imple
public SelectButton(T activatable) { public SelectButton(T activatable) {
this.activatable = activatable; this.activatable = activatable;
setText(activatable.title()); setText(activatable.title());
setRolloverEnabled(false);
addItemListener(this); addItemListener(this);
//getModel().setPressed(true); //setFocusable(false);
//getModel().setRollover(true);
} }
public void itemStateChanged(ItemEvent itemEvent) { public void itemStateChanged(ItemEvent itemEvent) {
//setSelected();
int state = itemEvent.getStateChange(); int state = itemEvent.getStateChange();
if (state == ItemEvent.SELECTED) { if (state == ItemEvent.SELECTED) {
System.out.println("Selected"); System.out.println("Selected");

View File

@@ -0,0 +1,13 @@
package pm.util;
import pm.Worker;
public class Multiplexer extends Worker {
public static final int THRESHOLD = 500;
protected Object object;
protected void work() {
// TODO Auto-generated method stub
}
}

View File

@@ -37,11 +37,11 @@ public class Native {
return count > 0 ? handleList.get(0) : -1; return count > 0 ? handleList.get(0) : -1;
} }
public static String getValue(String key, String name) throws IOException { public static String getValue(String key, String name) {
String command = String.format("reg query \"%s\"", key); String command = String.format("reg query \"%s\"", key);
Process process = Runtime.getRuntime().exec(command);
Scanner processScanner = new Scanner(process.getInputStream());
try { try {
Process process = Runtime.getRuntime().exec(command);
Scanner processScanner = new Scanner(process.getInputStream());
processScanner.nextLine(); processScanner.nextLine();
processScanner.nextLine(); processScanner.nextLine();
while (processScanner.hasNextLine()) { while (processScanner.hasNextLine()) {
@@ -54,15 +54,13 @@ public class Native {
return lineScanner.nextLine().trim(); return lineScanner.nextLine().trim();
} }
} }
} catch (IOException e) {
} catch (NoSuchElementException e) {} } catch (NoSuchElementException e) {}
return null; return null;
} }
public static String getValue(String key) { public static String getValue(String key) {
try { return getValue(key, "(Default");
return getValue(key, "(Default");
} catch (IOException e) {}
return null;
} }
public static String replaceVariables(String string) { public static String replaceVariables(String string) {