如何使用javaFx应用程序对IPTV进行IR远程控制?

时间:2017-06-13 09:04:29

标签: java infrared remote-control

我正在尝试开发用于测试IPTV的JavaFx应用程序。我的任务是成功检查频道更改。目前没有任何组件或设备。但我正在寻找这个任务,之后我会买。

我的应用程序将通过IR设备发送一些远程控制命令。

Here是一个IR设备,但它没有Java API。

这种解决方案有办法吗?

1 个答案:

答案 0 :(得分:0)

我搜索并找到了一个名为RedRat的设备。它是usb-infrared设备,我们可以使用linux和windows OS。

使用编程语言有一个utility。 这是一个示例java代码,可能对某些人有用。但你应该有一个redrat设备。

首先,您必须下载this redRatHub并粘贴方向 其次,运行与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();
        }
    }
}

此类通过串口与redrat设备通信。

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