Search code examples
javainfraredremote-control

How to IR remote control to IPTV with javaFx application?


I am trying to develop a JavaFx application for testing an IPTV. And my task is checking of channel changing successfully. There is no any component or device at the moment. But I am searching for this task, after that I will buy.

My application will send some remote control command over the IR device.

Here is an IR device, but It doesn't have a Java API.

Is there a way for this solution?


Solution

  • I searched and found a device which name was RedRat. It is usb-infrared device that we can use it linux and windows OS.

    There is a utility for using it with a programming language. Here is a sample java code, may be useful for somebody. But you should have a redrat device.

    First step, you have to download this redRatHub and paste a direction secondly, run main class which has same path with redrathub folders.

    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();
            }
        }
    }
    

    this class is communicate with redrat device over the serial ports.

    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;
            }
        }