Go to the documentation of this file.00001
00009 package org.rosmultimedia.player.onkyo.eiscp;
00010
00011 import java.io.IOException;
00012 import java.util.HashMap;
00013 import java.util.Map;
00014
00015 import org.joda.time.DateTime;
00016 import org.joda.time.Period;
00017
00018 import com.google.common.base.Strings;
00019
00020 import de.csmp.jeiscp.EiscpConnector;
00021 import de.csmp.jeiscp.EiscpListener;
00022 import de.csmp.jeiscp.eiscp.EiscpCommmandsConstants;
00023
00029 public class OnkyoEiscp {
00030
00031 private final String host;
00032 private final int port;
00033 private EiscpConnector eiscpConnector;
00034 private Map<String, String> iscpResult;
00035
00036 private EiscpListener messageListener = new EiscpListener() {
00037
00038 @Override
00039 public void receivedIscpMessage(String message) {
00040 iscpResult.put(message.substring(0, 3), message);
00041 }
00042
00043 };
00044
00045 public OnkyoEiscp(String host, int port) {
00046 this.host = host;
00047 this.port = port;
00048
00049 this.iscpResult = new HashMap<String, String>();
00050
00051 this.initialize();
00052 }
00053
00054 private void initialize() {
00055 try {
00056 this.iscpResult.clear();
00057 this.eiscpConnector = new EiscpConnector(this.host, this.port);
00058 this.eiscpConnector.attachListener(this.messageListener);
00059 } catch (IOException e) {
00060
00061 }
00062 }
00063
00064 public String getIscpResponse(String prefix) {
00065 String result = null;
00066
00067 DateTime startTime = DateTime.now();
00068
00069 while(true) {
00070 prefix = this.getCommandPrefix(prefix);
00071
00072 if (this.iscpResult.containsKey(prefix)) {
00073 result = this.iscpResult.get(prefix);
00074 break;
00075 }
00076
00077 if (new Period(startTime, DateTime.now()).getMillis() > 50) {
00078 break;
00079 }
00080
00081 try {
00082 Thread.sleep(0, 10);
00083 } catch (InterruptedException e) {
00084 e.printStackTrace();
00085 }
00086 }
00087
00088 return result;
00089 }
00090
00091 private String getCommandPrefix(String command) {
00092 String result = command;
00093
00094 if (result.length() > 3) {
00095 result = result.substring(0, 3);
00096 }
00097
00098 return result;
00099 }
00100
00101 public String sendCommand(String command) {
00102 return this.sendCommand(command, null);
00103 }
00104
00105 public String sendCommand(String command, String args) {
00106 String result = null;
00107
00108 if (!this.isConnected()) {
00109 this.initialize();
00110 }
00111
00112 if (!Strings.isNullOrEmpty(args)
00113 && command == EiscpCommmandsConstants.MASTER_VOLUME_ISCP) {
00114 String hexVol = Integer.toHexString(Integer.valueOf(args));
00115
00116 if (hexVol.length() == 1) {
00117 hexVol = "0" + hexVol;
00118 }
00119
00120 command += hexVol;
00121 }
00122
00123 if (this.isConnected()) {
00124 try {
00125 if (!this.iscpResult.containsKey(this.getCommandPrefix(command))
00126 || !command.endsWith("QSTN")) {
00127 this.eiscpConnector.sendIscpCommand(command);
00128 }
00129
00130 result = this.getIscpResponse(command);
00131
00132 if (result == null) {
00133 result = "";
00134 }
00135 } catch (IOException e) {
00136 this.eiscpConnector = null;
00137 }
00138 }
00139
00140 return result;
00141 }
00142
00143 public boolean isConnected() {
00144 return this.eiscpConnector != null && this.eiscpConnector.isConnected()
00145 && !this.eiscpConnector.isClosed();
00146 }
00147 }