2017-06-13 4 views
0

J'essaie de développer une application JavaFx pour tester un IPTV. Et ma tâche est de vérifier le changement de canal avec succès. Il n'y a aucun composant ou dispositif pour le moment. Mais je cherche cette tâche, après que je vais acheter.Comment la télécommande IR vers IPTV avec l'application javaFx?

Mon application envoie une commande de contrôle à distance sur le périphérique IR.

Here est un périphérique IR, mais il ne possède pas d'API Java.

Y a-t-il un moyen pour cette solution?

Répondre

0

J'ai recherché et trouvé un appareil dont le nom était RedRat. C'est USB-périphérique infrarouge que nous pouvons utiliser linux et Windows OS. Il existe un utility pour l'utiliser avec un langage de programmation. Voici un exemple de code java, peut être utile pour quelqu'un. Mais vous devriez avoir un dispositif de redrat.

Première étape, vous devez télécharger this redRatHub et collez une direction d'autre part, exécutez la classe principale qui a le même chemin avec les dossiers redrathub.

public class MyDemo { 

    private static Client client; 
    private static String DEVICE_NAME = ""; 
    private static String DATA_SET = ""; 

    public static void main(String[] args) { 

     try { 

      startRedRat(); 

      client = new Client(); 
      client.openSocket("localhost", 40000); 

      DEVICE_NAME = client.readData("hubquery=\"list redrats\"").split("]")[1].split("\n")[0].trim(); 
      DATA_SET = client.readData("hubquery=\"list datasets\"").split("\n")[1]; 

      sendCommand("power"); 
      TimeUnit.SECONDS.sleep(5); 

      sendCommand("btn1", "btn1", "btn1", "btn1"); 
      sendCommand("btnOK"); 
      TimeUnit.SECONDS.sleep(30); 
      sendCommand("btnBACK"); 
      sendCommand("channel+"); 
      sendCommand("btn6", "btn1"); 
      sendCommand("channel+"); 
      sendCommand("channel-"); 
      sendCommand("volume+"); 
      sendCommand("volume-"); 
      sendCommand("power"); 

      client.closeSocket(); 

      p.destroy(); 

     } catch (Exception ex) { 
      System.err.println(ex.getMessage()); 
     } finally { 
      System.out.println("Finished. Hit <RETURN> to exit..."); 
     } 
    } 


    private static void sendCommand(String... command) { 

     try { 
      for (String cmd : command) { 
       client.sendMessage("name=\"" + DEVICE_NAME + "\" dataset=\"" + DATA_SET + "\" signal=\"" + cmd + "\""); 
       TimeUnit.MILLISECONDS.sleep(500); 
       System.out.println(cmd + " signal send"); 
      } 
      TimeUnit.SECONDS.sleep(3); 

     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    private static void startRedRat() { 
     try { 
      SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() { 
       @Override 
       protected Void doInBackground() throws Exception { 
        p = Runtime.getRuntime().exec("cmd /C C:\\RedRatHub\\RedRatHubCmd.exe C:\\RedRatHub\\TivibuDB.xml"); 
        return null; 
       } 
      }; 
      worker.run(); 
      TimeUnit.SECONDS.sleep(5); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

Cette classe communique avec le périphérique redrat par les ports série.

public class Client { 

     private Socket socket; 
     private DataOutputStream out; 
     private DataInputStream in; 

     /* 
     * Opens the socket to RedRatHubCmd. 
     */ 
     public void openSocket(String host, int port) throws UnknownHostException, IOException { 
      if (socket != null && socket.isConnected()) return; 

      socket = new Socket(host, port); 
      out = new DataOutputStream(socket.getOutputStream()); 
      in = new DataInputStream(socket.getInputStream()); 
     } 

     /* 
     * Closes the RedRatHubCmd socket. 
     */ 
     public void closeSocket() throws IOException { 
      socket.close(); 
     } 

     /* 
     * Sends a message to the readData() method. Use when returned data from RedRatHub is not needed. 
     */ 
     public void sendMessage(String message) throws Exception { 
      String res = readData(message); 
      if (!res.trim().equals("OK")) { 
       throw new Exception("Error sending message: " + res); 
      } 
     } 

     /* 
     * Reads data back from RedRatHub. Use when returned data is needed to be output. 
     */ 
     public String readData(String message) throws IOException { 
      if (socket == null || !socket.isConnected()) { 
       System.out.println("\tSocket has not been opened. Call 'openSocket()' first."); 
       return null; 
      } 

      // Send message 
      out.write((message + "\n").getBytes("UTF-8")); 

      // Check response. This is either a single line, e.g. "OK\n", or a multi-line response with 
      // '{' and '}' start/end delimiters. 
      String received = ""; 
      byte[] inBuf = new byte[256]; 
      while (true) { 
       // Read data... 
       int inLength = in.read(inBuf); 
       //byte[] thisMSg = new byte[inLength]; 
       String msg = new String(Arrays.copyOfRange(inBuf, 0, inLength), "UTF-8"); 
       received += msg; 
       if (checkEom(received)) return received; 
      } 
     } 

     /* 
     * Checks for the end of a message 
     */ 
     public boolean checkEom(String message) { 
      // Multi-line message 
      if (message.trim().endsWith("}")) { 
       return message.startsWith("{"); 
      } 

      // Single line message 
      return message.endsWith("\n"); 
      //return true; 
     } 
    }