Guest User

IRControlepc

a guest
Aug 27th, 2017
532
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 20.77 KB | None | 0 0
  1. /*
  2.  bem10jfx.blogspot.com
  3.  */
  4. package jfxcontrolearduinopcvolume;
  5.  
  6. import java.awt.AWTException;
  7. import java.awt.Robot;
  8. import java.awt.event.InputEvent;
  9. import java.awt.event.KeyEvent;
  10. import java.util.Scanner;
  11. import java.util.logging.Level;
  12. import java.util.logging.Logger;
  13. import javafx.application.Application;
  14. import javafx.application.Platform;
  15. import javafx.event.ActionEvent;
  16. import javafx.event.EventHandler;
  17. import javafx.scene.Group;
  18. import javafx.scene.Scene;
  19. import javafx.scene.control.Button;
  20. import javafx.scene.control.ChoiceBox;
  21. import javafx.scene.control.Label;
  22. import javafx.scene.paint.Color;
  23. import javafx.scene.text.Text;
  24. import javafx.stage.Stage;
  25. import javafx.stage.WindowEvent;
  26. import jssc.SerialPort;
  27. import jssc.SerialPortEvent;
  28. import jssc.SerialPortEventListener;
  29. import jssc.SerialPortException;
  30. import jssc.SerialPortList;
  31.  
  32. /**
  33.  *
  34.  * @author kmls
  35.  */
  36. public class JFXControleArduinoPCvolume extends Application {
  37.  
  38.     String vlCode;
  39.     String vlactions;
  40.     Label lbaction = new Label("Ação: " + vlactions);
  41.     Text lbcode = new Text("Code IR:" + vlCode);
  42.     SerialPort serialport;
  43.     String input;
  44.     ChoiceBox cbport = new ChoiceBox();
  45.     Scene scene;
  46.     Robot robot;
  47.     //1024/2 de resoluçao
  48.     int cima = 550;
  49.     int direita = 550;
  50.  
  51.     public void arranqdeconexao() {
  52.  
  53.         final String[] serialPortasNomes = SerialPortList.getPortNames();
  54.         for (String nomeportas : serialPortasNomes) {
  55.             cbport.getItems().addAll(nomeportas);
  56.             System.out.println("iniciando comunicaçaoa de portas\n" + nomeportas);
  57.         }
  58.  
  59.     }
  60.  
  61.     @Override
  62.     public void start(Stage stage) {
  63.         Group gp = new Group();
  64.         scene = new Scene(gp, 400, 400, Color.SILVER);
  65.         stage.setTitle("" + vlCode);
  66.         stage.setScene(scene);
  67.  
  68.         arranqdeconexao();
  69.  
  70.         lbaction.layoutXProperty().bind(scene.widthProperty().divide(2.5));
  71.         lbaction.layoutYProperty().bind(scene.heightProperty().divide(2.5));
  72.         lbaction.setText("Açao :" + vlactions);
  73.         lbcode.layoutXProperty().bind(lbaction.layoutXProperty());
  74.         lbcode.layoutYProperty().bind(lbaction.layoutYProperty().add(30));
  75.         lbcode.setText("code IR:" + vlCode);
  76.         Button btport = new Button("conectar");
  77.         btport.layoutXProperty().bind(lbcode.layoutXProperty().subtract(1));
  78.         btport.layoutYProperty().bind(lbcode.layoutYProperty().add(30));
  79.         cbport.layoutXProperty().bind(btport.layoutXProperty().add(70));
  80.         cbport.layoutYProperty().bind(btport.layoutYProperty());
  81.  
  82.         gp.getChildren().addAll(lbaction, lbcode, btport, cbport);
  83.  
  84.         btport.setOnAction(new EventHandler<ActionEvent>() {
  85.             @Override
  86.             public void handle(ActionEvent event) {
  87.                 try {
  88.  
  89.                     serialport = new SerialPort(cbport.getValue().toString());
  90.                     try {
  91.                         serialport.openPort();
  92.                         serialport.setParams(9600, 8, 1, 0);
  93.                         serialport.setEventsMask(SerialPort.MASK_RXCHAR);
  94.  
  95.                     } catch (SerialPortException ex) {
  96.                         Logger.getLogger(jfxcontrolearduinopcvolume.JFXControleArduinoPCvolume.class.getName()).log(Level.SEVERE, null, ex);
  97.                     }
  98.  
  99.                     if (serialport.isOpened() == true) {
  100.                         stage.setTitle("Conectado");
  101.  
  102.                     } else {
  103.                         stage.setTitle("Desconectando");
  104.  
  105.                     }
  106.  
  107.                     serialport.addEventListener(new SerialPortEventListener() {
  108.                         @Override
  109.                         public void serialEvent(SerialPortEvent serialPortEvent) {
  110.                             try {
  111.  
  112.                                 input = serialport.readString(serialPortEvent.getEventValue());
  113.                                 if (serialPortEvent.isRXCHAR()) {
  114.                                     if (input.endsWith("\r\n")) {
  115.                                         System.out.print("Code:");
  116.                                         System.out.println("" + input.substring(0, input.indexOf("\r\n")));
  117.                                         vlCode = input.substring(0, input.indexOf("\r\n"));
  118.                                         lbcode.setText("Code IR:" + vlCode);
  119.  
  120.                                         if (vlCode.toString().equals("551502015".toString())) {
  121.                                             vlactions = "POWER OFF";
  122.                                             //   lbaction.setText(""+vlactions.toString());
  123.                                             System.out.println("Action :" + vlactions.toString());
  124.  
  125.                                             try {
  126.                                                 robot = new Robot();
  127.  
  128.                                             } catch (AWTException ex) {
  129.                                                 Logger.getLogger(JFXControleArduinoPCvolume.class.getName()).log(Level.SEVERE, null, ex);
  130.                                             }
  131.  
  132.                                         }
  133.                                         ///retorno de code ir
  134.                                         if (vlCode.toString().equals("4294967295".toString()) && vlactions.toString() == "Cima") {
  135.                                             System.out.println("retorno de caracter:" + vlactions.toString());
  136.                                             cima = cima - 15;
  137.                                             robot = new Robot();
  138.  
  139.                                             robot.mouseMove(direita, cima);
  140.                                         }
  141.                                         if (vlCode.toString().equals("4294967295".toString()) && vlactions.toString() == "Baixo") {
  142.                                             System.out.println("retorno de caracter:" + vlactions.toString());
  143.                                             cima = cima + 15;
  144.                                             robot = new Robot();
  145.                                             robot.mouseMove(direita, cima);
  146.  
  147.                                         }
  148.  
  149.                                         if (vlCode.toString().equals("4294967295".toString()) && vlactions.toString() == "Esquerda") {
  150.                                             System.out.println("retorno de caracter:" + vlactions.toString());
  151.                                             direita = direita - 15;
  152.                                             robot = new Robot();
  153.                                             robot.mouseMove(direita, cima);
  154.                                         }
  155.                                         if (vlCode.toString().equals("4294967295".toString()) && vlactions.toString() == "Direita") {
  156.                                             System.out.println("retorno de caracter:" + vlactions.toString());
  157.                                             direita = direita + 15;
  158.                                             robot = new Robot();
  159.                                             robot.mouseMove(direita, cima);
  160.                                         }
  161.                                         //////////fim de retorno
  162.                                         if (vlCode.toString().equals("551502015".toString())) {
  163.                                             vlactions = "Volume +";
  164.                                             //   lbaction.setText(""+vlactions.toString());
  165.                                             System.out.println("Action :" + vlactions.toString());
  166.                                             Robot robot;
  167.  
  168.                                             try {
  169.                                                 robot = new Robot();
  170.  
  171.                                             } catch (AWTException ex) {
  172.                                                 Logger.getLogger(JFXControleArduinoPCvolume.class.getName()).log(Level.SEVERE, null, ex);
  173.                                             }
  174.  
  175.                                         } else if (vlCode.toString().equals("551534655".toString())) {
  176.                                             vlactions = "Volume -";
  177.                                             // lbaction.setText("volume"+vlactions);
  178.                                             System.out.println("Action :" + vlactions);
  179.  
  180.                                         } else if (vlCode.toString().equals("551486205".toString())) {
  181.                                             vlactions = "Cima";
  182.                                             // lbaction.setText(""+vlactions.toString());
  183.                                             System.out.println("Action :" + vlactions.toString());
  184.                                             cima = cima - 10;
  185.                                             try {
  186.  
  187.                                                 Robot rb = new Robot();
  188.                                                 rb.mouseMove(direita, cima);
  189.  
  190.                                             } catch (AWTException ex) {
  191.                                                 Logger.getLogger(JFXControleArduinoPCvolume.class.getName()).log(Level.SEVERE, null, ex);
  192.                                             }
  193.  
  194.                                         } else if (vlCode.toString().equals("551518845".toString())) {
  195.                                             vlactions = "Baixo";
  196.                                             // lbaction.setText("Ação :"+vlactions.toString());
  197.                                             System.out.println("Action :" + vlactions.toString());
  198.                                             cima = cima + 10;
  199.                                             try {
  200.  
  201.                                                 Robot rb = new Robot();
  202.                                                 rb.mouseMove(direita, cima);
  203.  
  204.                                             } catch (AWTException ex) {
  205.                                                 Logger.getLogger(JFXControleArduinoPCvolume.class.getName()).log(Level.SEVERE, null, ex);
  206.                                             }
  207.  
  208.                                         } else if (vlCode.toString().equals("551510175".toString())) {
  209.                                             vlactions = "Direita";
  210.                                             // lbaction.setText(  "Ação :"+vlactions);
  211.                                             System.out.println("Action :" + vlactions.toString());
  212.                                             direita = direita + 10;
  213.                                             try {
  214.                                                 Robot rb = new Robot();
  215.                                                 rb.mouseMove(direita, cima);  //layouttx ,layoutY
  216.  
  217.                                             } catch (AWTException ex) {
  218.                                                 Logger.getLogger(JFXControleArduinoPCvolume.class.getName()).log(Level.SEVERE, null, ex);
  219.                                             }
  220.  
  221.                                         } else if (vlCode.toString().equals("551542815".toString())) {
  222.                                             vlactions = "Esquerda";
  223.                                             System.out.println("Action :" + vlactions.toString());
  224.                                             direita = direita - 10;
  225.                                             try {
  226.                                                 Robot rb = new Robot();
  227.                                                 rb.mouseMove(direita, cima);
  228.  
  229.                                             } catch (AWTException ex) {
  230.                                                 Logger.getLogger(JFXControleArduinoPCvolume.class.getName()).log(Level.SEVERE, null, ex);
  231.                                             }
  232.  
  233.                                         } else if (vlCode.toString().equals("551494365".toString())) {
  234.                                             vlactions = "Mouse click esquerda";
  235.                                             // lbaction.setText(""+vlactions.toString());
  236.                                             System.out.println("Action :" + vlactions.toString());
  237.                                             //click =click +1 direita
  238.                                             try {
  239.                                                 Robot rb = new Robot();
  240.                                                 rb.mousePress(InputEvent.BUTTON1_MASK);
  241.                                                 rb.mouseRelease(InputEvent.BUTTON1_MASK);
  242.                                             } catch (AWTException ex) {
  243.                                                 Logger.getLogger(JFXControleArduinoPCvolume.class.getName()).log(Level.SEVERE, null, ex);
  244.                                             }
  245.  
  246.                                         }else if (vlCode.toString().equals("551494365".toString())) {
  247.                                             vlactions = "Mouse click esquerda";
  248.                                             // lbaction.setText(""+vlactions.toString());
  249.                                             System.out.println("Action :" + vlactions.toString());
  250.                                           //click=clik+1 esquerda
  251.                                             try {
  252.                                                 Robot rb = new Robot();
  253.                                                 rb.mousePress(InputEvent.BUTTON1_MASK);
  254.                                                 rb.mouseRelease(InputEvent.BUTTON1_MASK);
  255.                                             } catch (AWTException ex) {
  256.                                                 Logger.getLogger(JFXControleArduinoPCvolume.class.getName()).log(Level.SEVERE, null, ex);
  257.                                             }
  258.  
  259.                                         } else if (vlCode.toString().equals("551535165".toString())) {
  260.                                             vlactions = "Mouse Presscionado Esquerda";
  261.                                             // lbaction.setText(""+vlactions.toString());
  262.                                             System.out.println("Action :" + vlactions.toString());
  263.                                             //prescionado direita  ;pres=(pres 1x=1 or pres 2x=0)
  264.                                             try {
  265.                                                 Robot rb = new Robot();
  266.                                                 rb.mousePress(InputEvent.BUTTON1_DOWN_MASK);
  267.                                                  rb.mousePress(InputEvent.BUTTON1_DOWN_MASK);
  268.                                             } catch (AWTException ex) {
  269.                                                 Logger.getLogger(JFXControleArduinoPCvolume.class.getName()).log(Level.SEVERE, null, ex);
  270.                                             }
  271.  
  272.                                         }else if (vlCode.toString().equals("551527005".toString())) {
  273.                                             vlactions = "Mouse click direita";
  274.                                             // lbaction.setText(""+vlactions.toString());
  275.                                             System.out.println("Action :" + vlactions.toString());
  276.                                           //click=clik+1 esquerda
  277.                                             try {
  278.                                                 Robot rb = new Robot();
  279.                                                 rb.mousePress(InputEvent.BUTTON3_MASK);
  280.                                                 rb.mouseRelease(InputEvent.BUTTON3_MASK);
  281.                                             } catch (AWTException ex) {
  282.                                                 Logger.getLogger(JFXControleArduinoPCvolume.class.getName()).log(Level.SEVERE, null, ex);
  283.                                             }
  284.  
  285.  
  286.                                         }else if (vlCode.toString().equals("551490795".toString())) {
  287.                                             vlactions = "Windows Desktop";
  288.                                             // lbaction.setText(""+vlactions.toString());
  289.                                             System.out.println("Action :" + vlactions.toString());
  290.                                            
  291.                                             try {
  292.                                                 Robot rb = new Robot();
  293.                                                 rb.keyPress(KeyEvent.VK_WINDOWS);
  294.                                                 rb.keyPress(KeyEvent.VK_D);  
  295.                                                 rb.keyRelease(KeyEvent.VK_D);
  296.                                                 rb.keyRelease(KeyEvent.VK_WINDOWS);  
  297.                                                        
  298.                                             } catch (AWTException ex) {
  299.                                                 Logger.getLogger(JFXControleArduinoPCvolume.class.getName()).log(Level.SEVERE, null, ex);
  300.                                             }
  301.  
  302.                                         }else if (vlCode.toString().equals("551541285".toString())) {
  303.                                             vlactions = "Exit";
  304.                                             // lbaction.setText(""+vlactions.toString());
  305.                                             System.out.println("Action :" + vlactions.toString());
  306.                                            
  307.                                             try {
  308.                                                 Robot rb = new Robot();
  309.                                                 rb.keyPress(KeyEvent.VK_ALT);
  310.                                                 rb.keyPress(KeyEvent.VK_F4);  
  311.                                                 rb.keyRelease(KeyEvent.VK_F4);
  312.                                                 rb.keyRelease(KeyEvent.VK_ALT);  
  313.                                                        
  314.                                             } catch (AWTException ex) {
  315.                                                 Logger.getLogger(JFXControleArduinoPCvolume.class.getName()).log(Level.SEVERE, null, ex);
  316.                                             }
  317.  
  318.                                         }else if (vlCode.toString().equals("551517825".toString())) {
  319.                                             vlactions = "Exit";
  320.                                             // lbaction.setText(""+vlactions.toString());
  321.                                             System.out.println("Action :" + vlactions.toString());
  322.                                            
  323.                                             try {
  324.                                                 Robot rb = new Robot();
  325.                                                 rb.keyPress(KeyEvent.VK_WINDOWS);
  326.                                                 rb.keyPress(KeyEvent.VK_UP);  
  327.                                                 rb.keyRelease(KeyEvent.VK_UP);
  328.                                                 rb.keyRelease(KeyEvent.VK_WINDOWS);  
  329.                                                        
  330.                                             } catch (AWTException ex) {
  331.                                                 Logger.getLogger(JFXControleArduinoPCvolume.class.getName()).log(Level.SEVERE, null, ex);
  332.                                             }
  333.  
  334.                                         }
  335.  
  336.                                     }
  337.  
  338.                                 }
  339.                             } catch (SerialPortException ex) {
  340.                                 Logger.getLogger(JFXControleArduinoPCvolume.class.getName()).log(Level.SEVERE, null, ex);
  341.                                 System.err.println("error : Sem porta de conexao escolhida");
  342.                             } catch (AWTException ex) {
  343.                                 Logger.getLogger(JFXControleArduinoPCvolume.class.getName()).log(Level.SEVERE, null, ex);
  344.                             }
  345.                         }
  346.                     });
  347.  
  348.                 } catch (Exception e) {
  349.                     e.getMessage();
  350.                 }
  351.             }
  352.         });
  353.         stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
  354.             @Override
  355.             public void handle(WindowEvent event) {
  356.                 try {
  357.                     if (serialport.isOpened()) {
  358.  
  359.                         serialport.closePort();
  360.                          Platform.exit();
  361.                     } else if (serialport.closePort() == true) {
  362.                         Platform.exit();
  363.                     }
  364.                 } catch (SerialPortException ex) {
  365.                     Logger.getLogger(JFXControleArduinoPCvolume.class.getName()).log(Level.SEVERE, null, ex);
  366.                 }
  367.  
  368.             }
  369.         });
  370.  
  371.         stage.show();
  372.     }
  373.  
  374.     /**
  375.      * bem10jfx.blogspot.com
  376.      */
  377.     public static void main(String[] args) {
  378.         launch(args);
  379.     }
  380.  
  381. }
  382.  
  383. ///bem10jfx.blogspot.com
Advertisement
Add Comment
Please, Sign In to add comment