|
| 1 | +#!/usr/bin/env -S python3 -B |
| 2 | + |
| 3 | +# Copyright (c) 2024 Project CHIP Authors |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +import os |
| 18 | +import subprocess |
| 19 | +import sys |
| 20 | +import time |
| 21 | + |
| 22 | +def read_logs(log_file): |
| 23 | + |
| 24 | + with open(log_file, 'r') as file: |
| 25 | + lines = file.readlines() |
| 26 | + |
| 27 | + discovered_commissioners = [] |
| 28 | + |
| 29 | + print('Reading from Linux-tv-casting-logs.txt') |
| 30 | + |
| 31 | + found_cast_request_0 = False |
| 32 | + |
| 33 | + # Read through the Linux-tv-casting-logs.txt line by line |
| 34 | + for i, line in enumerate(lines): |
| 35 | + |
| 36 | + if "commissioner(s) discovered" in line: |
| 37 | + print('----------------------------------------------------------------') |
| 38 | + print(line) |
| 39 | + |
| 40 | + if "cast request 0" in line: |
| 41 | + print(line) |
| 42 | + found_cast_request_0 = True |
| 43 | + # sys.exit(0) |
| 44 | + print('DONE!!') |
| 45 | + |
| 46 | + if "No commissioner discovered" in line: |
| 47 | + print(line) |
| 48 | + sys.exit(1) |
| 49 | + break |
| 50 | + |
| 51 | + # Look for "Discovered Commissioner" |
| 52 | + if "Discovered Commissioner" in line: |
| 53 | + print('----------------------------------------------------------------') |
| 54 | + print(line) |
| 55 | + |
| 56 | + # Extract the relevant part of the string |
| 57 | + commissioner = line.split("Discovered Commissioner")[-1].strip() |
| 58 | + commissioner = commissioner.replace('\x1b[0m', '') |
| 59 | + |
| 60 | + # Initialize variables for Vendor ID, Product ID, and Device Type |
| 61 | + vendor_id = None |
| 62 | + product_id = None |
| 63 | + device_type = None |
| 64 | + |
| 65 | + # Iterate through the subsequent lines to find product id, vendor id, and device type |
| 66 | + for next_line in lines[i+1:]: |
| 67 | + # Check if the line contains the required information |
| 68 | + if "Vendor ID:" in next_line: |
| 69 | + print(next_line) |
| 70 | + vendor_id = next_line.split(":")[-1].strip() |
| 71 | + vendor_id = vendor_id.replace('\x1b[0m', '') |
| 72 | + |
| 73 | + elif "Product ID:" in next_line: |
| 74 | + print(next_line) |
| 75 | + product_id = next_line.split(":")[-1].strip() |
| 76 | + product_id = product_id.replace('\x1b[0m', '') |
| 77 | + |
| 78 | + elif "Device Type:" in next_line: |
| 79 | + print(next_line) |
| 80 | + device_type = next_line.split(":")[-1].strip() |
| 81 | + device_type = device_type.replace('\x1b[0m', '') |
| 82 | + |
| 83 | + elif "commissioner(s) discovered" in next_line: |
| 84 | + parsing_allowed = False |
| 85 | + break |
| 86 | + |
| 87 | + # If the next line starts with "Discovered Commissioner", break the loop |
| 88 | + if "Discovered Commissioner" in next_line: |
| 89 | + break |
| 90 | + |
| 91 | + # Append the extracted information to the devices list |
| 92 | + discovered_commissioners.append({ |
| 93 | + "discovered_commissioner": commissioner, |
| 94 | + "vendor_id": vendor_id, |
| 95 | + "product_id": product_id, |
| 96 | + "device_type": device_type |
| 97 | + }) |
| 98 | + |
| 99 | + if len(discovered_commissioners) == 0: |
| 100 | + print('No Commissioner(s) Discovered! The list of discovered commissioners is empty!') |
| 101 | + sys.exit(1) |
| 102 | + |
| 103 | + # Print the list of discovered commissioners |
| 104 | + print("Discovered Commissioners:") |
| 105 | + for discovered_commissioner in discovered_commissioners: |
| 106 | + print(discovered_commissioner) |
| 107 | + |
| 108 | + if not found_cast_request_0: |
| 109 | + print('Discovery did not finish successfully! No user prompt for "cast request 0" displayed!') |
| 110 | + sys.exit(1) |
| 111 | + |
| 112 | +if __name__ == '__main__': |
| 113 | + |
| 114 | + with open('Linux-tv-app-logs.txt', 'w') as fd1, open('Linux-tv-casting-app-logs.txt', 'w') as fd2: |
| 115 | + |
| 116 | + # Run the Linux tv-app and write the output to file |
| 117 | + tv_app_rel_path = '../../out/tv-app/chip-tv-app' |
| 118 | + tv_app_abs_path = os.path.abspath(tv_app_rel_path) |
| 119 | + p1 = subprocess.Popen(tv_app_abs_path, stdout=fd1, text=True) |
| 120 | + |
| 121 | + time.sleep(5) |
| 122 | + |
| 123 | + # Run the Linux tv-casting-app and write the output to file |
| 124 | + tv_casting_app_rel_path = '../../out/tv-casting-app/chip-tv-casting-app' |
| 125 | + tv_casting_app_abs_path = os.path.abspath(tv_casting_app_rel_path) |
| 126 | + p2 = subprocess.Popen(tv_casting_app_abs_path, stdout=fd2, text=True) |
| 127 | + |
| 128 | + # Wait for the processes to finish writing before attempting to read |
| 129 | + time.sleep(15) |
| 130 | + |
| 131 | + read_logs('Linux-tv-casting-app-logs.txt') |
| 132 | + # print('Finished reading') |
0 commit comments