|
| 1 | +import com.fazecast.jSerialComm.SerialPort; |
| 2 | +import com.fazecast.jSerialComm.SerialPortTimeoutException; |
| 3 | +import java.net.HttpURLConnection; |
| 4 | +import java.net.URL; |
| 5 | +import java.io.IOException; |
| 6 | +import java.io.InputStream; |
| 7 | +import java.util.regex.Matcher; |
| 8 | +import java.util.regex.Pattern; |
| 9 | +import java.io.FileInputStream; |
| 10 | +import java.util.Properties; |
| 11 | +public class Main { |
| 12 | + |
| 13 | + public static void main(String[] args) { |
| 14 | + Properties properties = new Properties(); |
| 15 | + try (FileInputStream input = new FileInputStream("Config.txt")) { |
| 16 | + properties.load(input); |
| 17 | + } catch (IOException e) { |
| 18 | + e.printStackTrace(); |
| 19 | + } |
| 20 | + |
| 21 | + // Accessing configuration values |
| 22 | + String comPort1 = properties.getProperty("port.name"); |
| 23 | + int BaudRate = Integer.parseInt(properties.getProperty("port.baud")); |
| 24 | + String vmixUrl = properties.getProperty("vmix.url"); |
| 25 | + int startCamNumber = Integer.parseInt(properties.getProperty("vmix.StartCamNumber")); |
| 26 | + int WideNumber = Integer.parseInt(properties.getProperty("vmix.WideCamNumber")); |
| 27 | + String testdebug = properties.getProperty("Debug.enabled"); |
| 28 | + |
| 29 | + boolean debug = false; |
| 30 | + if(testdebug.equals("true")){ |
| 31 | + debug=true; |
| 32 | + } |
| 33 | + else { |
| 34 | + debug=false; |
| 35 | + } |
| 36 | + |
| 37 | + if (debug) { |
| 38 | + // Printing configuration values |
| 39 | + System.out.println("Listed below is the list of values pulled form the Config:"); |
| 40 | + System.out.println("Com Port: " + comPort1); |
| 41 | + System.out.println("Baud Rate: " + BaudRate); |
| 42 | + System.out.println("Vmix Ip And Port: " + vmixUrl); |
| 43 | + System.out.println("The Input number of your wide shot: " + startCamNumber); |
| 44 | + System.out.println("The Input number of your start shot: " + WideNumber); |
| 45 | + } |
| 46 | + |
| 47 | + String comPortName = comPort1; // Replace with the actual COM port name |
| 48 | + |
| 49 | + int inputNumber = 1; |
| 50 | + SerialPort comPort = SerialPort.getCommPort(comPortName); |
| 51 | + comPort.setBaudRate(BaudRate); |
| 52 | + |
| 53 | + if (comPort.openPort()) { |
| 54 | + System.out.println("Port opened successfully."); |
| 55 | + comPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 100, 0); // Adjust timeout as needed |
| 56 | + InputStream inputStream = comPort.getInputStream(); |
| 57 | + |
| 58 | + StringBuilder messageBuilder = new StringBuilder(); |
| 59 | + boolean receivingData = false; |
| 60 | + |
| 61 | + try { |
| 62 | + while (true) { |
| 63 | + String lane = ""; |
| 64 | + try { |
| 65 | + int data = inputStream.read(); |
| 66 | + if (data == -1) { |
| 67 | + if(debug) { |
| 68 | + System.out.println("No more data to read."); |
| 69 | + } |
| 70 | + break; // End of stream |
| 71 | + } |
| 72 | + |
| 73 | + char c = (char) data; |
| 74 | + |
| 75 | + if (c == 1) { // <SOH> |
| 76 | + messageBuilder.setLength(0); |
| 77 | + receivingData = true; // Start receiving data |
| 78 | + } else if (c == 4 && receivingData) { // <EOT> when receiving data |
| 79 | + String message = messageBuilder.toString().trim(); |
| 80 | + //System.out.println("Received Data: " + message); |
| 81 | + |
| 82 | + if (message.startsWith("SR") && message.length() >= 7) { |
| 83 | + String mm_ss_t = message.substring(2,9); // Extract mm:ss.t including the decimal part |
| 84 | + if(debug) { |
| 85 | + System.out.println(mm_ss_t); |
| 86 | + } |
| 87 | + if(mm_ss_t.equals(" : .0")) { |
| 88 | + cut(vmixUrl, startCamNumber, debug); |
| 89 | + CutWithDelay(5000,vmixUrl, WideNumber,debug); |
| 90 | + } |
| 91 | + |
| 92 | + } else { |
| 93 | + // Use a regular expression to extract relevant information |
| 94 | + Pattern pattern = Pattern.compile("FT\\s*(\\d+)\\s*(\\d+)\\s*((?:\\d+:)?\\d{2}\\.\\d{2})"); |
| 95 | + Matcher matcher = pattern.matcher(message); |
| 96 | + |
| 97 | + if (matcher.find()) { |
| 98 | + lane = matcher.group(1); |
| 99 | + String place = matcher.group(2); |
| 100 | + String mm_ss_tt = matcher.group(3); |
| 101 | + cut(vmixUrl, startCamNumber, debug ); |
| 102 | + if(debug) { |
| 103 | + System.out.println("Score Data: Lane " + lane + ", Place " + place + ", Time " + mm_ss_tt); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + receivingData = false; // Stop receiving after processing |
| 109 | + } else if (receivingData) { |
| 110 | + messageBuilder.append(c); |
| 111 | + } |
| 112 | + } catch (SerialPortTimeoutException timeoutException) { |
| 113 | + // Handle timeout exception (no data received) |
| 114 | + //System.out.println("Timeout: No data received."); |
| 115 | + } |
| 116 | + } |
| 117 | + } catch (IOException e) { |
| 118 | + e.printStackTrace(); |
| 119 | + } finally { |
| 120 | + comPort.closePort(); |
| 121 | + System.out.println("Port closed."); |
| 122 | + } |
| 123 | + } else { |
| 124 | + System.err.println("Failed to open the port."); |
| 125 | + } |
| 126 | + } |
| 127 | + // Method to cut to a specific input in vMix |
| 128 | + public static void cut (String vmixApiUrl, int Input,boolean debug){ |
| 129 | + try { |
| 130 | + cutToInput(vmixApiUrl, Input, debug ); |
| 131 | + } catch (Exception e){ |
| 132 | + e.printStackTrace(); |
| 133 | + } |
| 134 | + } |
| 135 | + public static void cutToInput(String vmixApiUrl, int inputNumber, boolean debug) throws Exception { |
| 136 | + try { |
| 137 | + // Create a URL object for the vMix API endpoint |
| 138 | + URL url = new URL("http://"+vmixApiUrl + "/API/?Function=CutDirect&input=" + inputNumber); |
| 139 | + |
| 140 | + // Open a connection to the URL |
| 141 | + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
| 142 | + connection.setRequestMethod("GET"); |
| 143 | + |
| 144 | + // Get the response from the vMix API |
| 145 | + int responseCode = connection.getResponseCode(); |
| 146 | + if(debug) { |
| 147 | + System.out.println(connection.getResponseCode()); |
| 148 | + } |
| 149 | + // Close the connection |
| 150 | + connection.disconnect(); |
| 151 | + |
| 152 | + // Return true if the request was successful (HTTP status code 200) |
| 153 | + |
| 154 | + } catch (Exception e) { |
| 155 | + e.printStackTrace(); |
| 156 | + // Return false if an exception occurred |
| 157 | + |
| 158 | + } |
| 159 | + } |
| 160 | + private static void CutWithDelay(int delay, String vmixUrl, int cam, boolean debug) { |
| 161 | + // Create a thread to print "test the test2" after 1 second |
| 162 | + Thread delayedThread = new Thread(() -> { |
| 163 | + try { |
| 164 | + Thread.sleep(delay); // Delay for 1 second |
| 165 | + cut(vmixUrl, cam, debug); |
| 166 | + } catch (InterruptedException e) { |
| 167 | + e.printStackTrace(); |
| 168 | + } |
| 169 | + }); |
| 170 | + |
| 171 | + // Start the delayed thread |
| 172 | + delayedThread.start(); |
| 173 | + } |
| 174 | + |
| 175 | +} |
0 commit comments