Subversion Repositories HomeAutomation

Rev

Blame | Last modification | View Log | SVN | RSS feed

  1. /*
  2.  * File created by Jimmy
  3.  * at 2004-jan-12 14:17:44
  4.  */
  5. package Macbeth.Modules.modControlMP;
  6.  
  7. import Macbeth.System.MbModule;
  8. import Macbeth.System.MbPacket;
  9. import Macbeth.System.MbLocation;
  10. import Macbeth.System.MbStartupException;
  11. import Macbeth.Net.TcpClient;
  12. import Macbeth.XML.XMLDataHandler;
  13. import Macbeth.Utilities.*;
  14. import java.util.HashMap;
  15. import java.util.Iterator;
  16.  
  17. /**
  18.  * Macbeth module that controls external mediaplayers
  19.  * (only arune's RemoteMedia at this point).
  20.  * @author Jimmy
  21.  */
  22. public class modControlMP extends MbModule {
  23.  
  24.     //used for communication with RemoteMedia
  25.     private TcpClient tcpClient;
  26.     //list of used RemoteMedia functions
  27.     private HashMap functions;
  28.  
  29.     /**
  30.      * Creates a new instance of modControlMP.
  31.      */
  32.     public modControlMP() {
  33.         //construct MbModule
  34.         super();
  35.         //initial values
  36.         options.putField("host", "localhost");
  37.         options.putField("port", "2050");
  38.         tcpClient = new TcpClient(Integer.parseInt(options.getField("port")), options.getField("host"));
  39.         functions = new HashMap(32);
  40.         //we depend on the RemoteControl-module
  41.         addDependency("RemoteControl");
  42.         //register our packet data handler
  43.         setPacketDataHandler(new PacketDataHandler());
  44.     }
  45.  
  46.     public String name() {
  47.         return "ControlMP";
  48.     }
  49.  
  50.     public String description() {
  51.         return "Controls external media players";
  52.     }
  53.  
  54.     /**
  55.      * This method sets default values on all _required_ data
  56.      * fields in the data repository.
  57.      */
  58.     public void initDataFields() {
  59.         //set default values on all REQUIRED option fields before starting up subsystems
  60.         options.putField("host", "localhost");
  61.         options.putField("port", "2050");
  62.     }
  63.  
  64.     public void startup() throws MbStartupException {
  65.         //startup MbModule
  66.         super.startup();
  67.  
  68.         //get all RMP function bindings from the data repository
  69.         DataRepository.DataList list = dataRepository.getList("RMPfunctions");
  70.         Iterator it = list.items();
  71.         while (it.hasNext()) {
  72.             DataRepository.DataListItem item = (DataRepository.DataListItem) it.next();
  73.             String function = item.getField("id");
  74.             String remotebtn = item.getField("remotebutton");
  75.             functions.put(function, new RemoteMediaFunction(function, remotebtn));
  76.         }
  77.  
  78.         //if any RemoteMedia functions are used
  79.         if (!functions.isEmpty()) {
  80.             //lets send requests for their remote control buttons
  81.             MbPacket p = new MbPacket();
  82.             p.setDestination(new MbLocation("self", "RemoteControl"));
  83.             it = functions.keySet().iterator();
  84.             while (it.hasNext()) {
  85.                 String key = (String) it.next();
  86.                 RemoteMediaFunction f = (RemoteMediaFunction) functions.get(key);
  87.                 if (f != null) {
  88.                     p.appendContents("  <buttonrequest button=\"" + f.remoteButton + "\" module=\"" + name() + "\" />");
  89.                 }
  90.             }
  91.             sendPacket(p);
  92.         }
  93.         //create the TCP client
  94.         tcpClient = new TcpClient(Integer.parseInt(options.getField("port")), options.getField("host"));
  95.     }
  96.  
  97.     //sends a string to the Remote Media Player socket server
  98.     private void sendString(String s) {
  99.         //try to open connection to the server and send the string
  100.         try {
  101.             tcpClient.openConnection();
  102.             tcpClient.sendString(s);
  103.             tcpClient.closeConnection();
  104.         } catch (Exception e) {
  105.             _info.println("Cannot connect to Remote Media Player at '" + options.getField("host") + ":" + options.getField("port") + "' ! " +
  106.                     "Exception was: '" + e + "'");
  107.         }
  108.     }
  109.  
  110.     /**
  111.      * Takes care of XML-data found in incoming packets.
  112.      */
  113.     private class PacketDataHandler implements XMLDataHandler {
  114.         public void XMLstartElement(String element, HashMap attributes) {
  115.             /* Ex: <remotecontrol button="NUMERIC4" state="DOWN" />
  116.              * This is a notification from the remote control module
  117.              * about one of our requested remote control buttons have
  118.              * been pressed. */
  119.             if (element.equals("remotecontrol") && attributes.containsKey("button") && attributes.containsKey("state")) {
  120.                 String state = (String)attributes.get("state");
  121.                 String button = (String)attributes.get("button");
  122.                 if (state.equals("DOWN")) {
  123.                     //find all RemoteMedia functions that are associated with this remote button
  124.                     Iterator it = functions.values().iterator();
  125.                     while (it.hasNext()) {
  126.                         RemoteMediaFunction f = (RemoteMediaFunction)it.next();
  127.                         //if this RemoteMedia function uses the remote button,
  128.                         if (f.remoteButton.equals(button)) {
  129.                             //try to send the ID string of this function to RemoteMedia
  130.                             sendString(f.id);
  131.                         }
  132.                     }
  133.                 }
  134.                 else if (state.equals("UP")) {
  135.                     //send btn-release-message to RMP (5000)
  136.                     sendString("5000");
  137.                 }
  138.             }
  139.         }
  140.  
  141.         public void XMLendElement(String element) {}
  142.         public void XMLelementData(String data) {}
  143.         public void XMLdocumentStart() {}
  144.         public void XMLdocumentEnd() {}
  145.     }
  146.  
  147. }