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.rumblepad.RumblepadDevice;
import pm.device.jintellitype.JIntellitypeDevice;
import pm.device.lirc.LircDevice;
import pm.device.network.NetworkDevice;
import pm.device.panel.PanelDevice;
import pm.device.player.PlayerDevice;
@@ -30,6 +31,7 @@ public class Client {
public Client(String ip, int port) throws GlobalRouterException {
eventRouter = new GlobalRouter(ip, port);
deviceArray = new Device[] {
new LircDevice(),
new WiimoteDevice(),
new PanelDevice(),
new JIntellitypeDevice(),

View File

@@ -12,6 +12,7 @@ import pm.application.vlc.VLCApplication;
import pm.device.javainput.extreme3d.Extreme3DDevice;
import pm.device.javainput.rumblepad.RumblepadDevice;
import pm.device.jintellitype.JIntellitypeDevice;
import pm.device.lirc.LircDevice;
import pm.device.network.NetworkDevice;
import pm.device.panel.PanelDevice;
import pm.device.player.PlayerDevice;
@@ -36,6 +37,7 @@ public class Main {
new VLCApplication(),
new WinampApplication()};
deviceArray = new Device[] {
new LircDevice(),
new WiimoteDevice(),
new PanelDevice(),
new JIntellitypeDevice(),
@@ -48,7 +50,7 @@ public class Main {
public void start() {
log.debug("Main");
Mimis mimis = new Mimis(eventRouter, applicationArray, deviceArray);
mimis.start();
mimis.activate();
}
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 {
protected Log log = LogFactory.getLog(getClass());
protected static final long serialVersionUID = 1L;
protected static final int INTERVAL = 5000;
protected static final int INTERVAL = 100;
protected T[] manageableArray;
protected Map<T, SelectButton<T>> buttonMap;

View File

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

View File

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

View File

@@ -4,6 +4,7 @@ import java.io.IOException;
import pm.Application;
import pm.util.Native;
import pm.util.VBScript;
public abstract class CMDApplication extends Application {
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;
}
protected void initialise() {
public void activate() {
String key = String.format("%s\\%s", REGISTRY, program);
// Check of naam is gevonden in register
String path = Native.getValue(key);
@@ -30,13 +31,15 @@ public abstract class CMDApplication extends Application {
e.printStackTrace();
//throw new ApplicationInitialiseException();
}
super.activate();
}
public void activate() {
if (!active) {
initialise();
public boolean active() {
try {
return active = VBScript.isRunning(program);
} catch (IOException e) {
return false;
}
super.activate();
}
public void deactivate() {

View File

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

View File

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

View File

@@ -8,7 +8,6 @@ import pm.event.task.Continuous;
import pm.event.task.Dynamic;
import pm.exception.button.UnknownButtonException;
import pm.exception.button.UnknownDirectionException;
import pm.exception.device.DeviceInitialiseException;
import pm.macro.state.Hold;
import pm.macro.state.Press;
import pm.value.Action;
@@ -24,11 +23,6 @@ public class RumblepadDevice extends JavaInputDevice {
}
public void initialise(){
try {
super.initialise(NAME);
} catch (DeviceInitialiseException e) {
e.printStackTrace();
}
add(
new Press(RumblepadButton.ONE),
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;
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 Multiplexer multiplexer;
protected LircService lircService;
public LircDevice(String title) {
super(title);
lircService = new LircService();
public LircDevice() {
super(TITLE);
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() {
lircService.activate();
lircService.start();
super.activate();
}
@@ -21,4 +35,8 @@ public class LircDevice extends Device {
lircService.deactivate();
super.deactivate();
}
public void add(LircButton lircButton) {
}
}

View File

@@ -8,13 +8,19 @@ import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
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.exception.button.UnknownButtonException;
public class LircService extends Worker {
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 int port;
protected Socket socket;
@@ -22,19 +28,30 @@ public class LircService extends Worker {
protected OutputStream outputStream;
protected BufferedReader bufferedReader;
protected PrintWriter printWriter;
protected HashMap<String, LircButton[]> buttonMap;
public LircService() {
this(IP, PORT);
public LircService(HashMap<String, LircButton[]> buttonMap) {
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.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() {
try {
socket = new Socket(ip, port);
socket = new Socket(ip, port);
inputStream = socket.getInputStream();
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
@@ -46,6 +63,7 @@ public class LircService extends Worker {
} catch (IOException e) {
e.printStackTrace();
}
super.activate();
}
public void deactivate() {
@@ -54,7 +72,7 @@ public class LircService extends Worker {
outputStream.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
//e.printStackTrace();
}
super.deactivate();
}
@@ -62,9 +80,41 @@ public class LircService extends Worker {
public void work() {
try {
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) {
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;
import java.awt.event.WindowEvent;
import javax.swing.WindowConstants;
import pm.Device;
import pm.event.Task;
import pm.macro.state.Press;
@@ -16,7 +20,16 @@ public class PanelDevice extends Device implements PanelButtonListener {
}
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.updatePosition(43);
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));
}
public boolean active() {
return active = panel != null && panel.isValid();
}
public void deactivate() {
panel.dispose();
}
protected void processWindowEvent(WindowEvent e) {
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
log.debug("Window closing");
deactivate();
}
}
public void buttonPressed(PanelButton panelButton) {
//Vang hier toggles af om bijvoorbeeld de play/pause en mute knop en veranderen
add(new Press(panelButton));

View File

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

@@ -36,12 +36,12 @@ public class Native {
}
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);
Process process = Runtime.getRuntime().exec(command);
Scanner processScanner = new Scanner(process.getInputStream());
try {
Process process = Runtime.getRuntime().exec(command);
Scanner processScanner = new Scanner(process.getInputStream());
processScanner.nextLine();
processScanner.nextLine();
while (processScanner.hasNextLine()) {
@@ -54,15 +54,13 @@ public class Native {
return lineScanner.nextLine().trim();
}
}
} catch (IOException e) {
} catch (NoSuchElementException e) {}
return null;
}
public static String getValue(String key) {
try {
return getValue(key, "(Default");
} catch (IOException e) {}
return null;
return getValue(key, "(Default");
}
public static String replaceVariables(String string) {