|
| 1 | +from adb_globals import * |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | +""" |
| 7 | +------------------------------------------------- |
| 8 | +----------------- BATTERY USAGE ----------------- |
| 9 | +------------------------------------------------- |
| 10 | +""" |
| 11 | + |
| 12 | +battery_states_final={} |
| 13 | +def battery_state_update(device, format = "Celcius"): |
| 14 | + """ Returns Dictionary with Full Battery Details """ |
| 15 | + battery_states = {"temperature": "", "level": "", "voltage":"", "status":"", "health": "", |
| 16 | + "AC powered": "", "USB powered": "", "Wireless powered":"", "present": ""} |
| 17 | + |
| 18 | + ok=(device.shell('dumpsys battery')) |
| 19 | + buf = StringIO(ok) |
| 20 | + changes = 0 |
| 21 | + for line2 in buf.readlines(): |
| 22 | + line = line2.strip() |
| 23 | + if "Max charging voltage" in line: |
| 24 | + pass |
| 25 | + else: |
| 26 | + for state, value in battery_states.items(): |
| 27 | + m = re.search(r'{}:(.*)'.format(state), line) |
| 28 | + if m: |
| 29 | + if value != m.group(1): |
| 30 | + changes += 1 |
| 31 | + # print("changed: state={} old={} new={}".format(state, value, m.group(1))) |
| 32 | + |
| 33 | + battery_states[state] = m.group(1).lstrip() |
| 34 | + |
| 35 | + # Taking Battery Health/Status and Turning into "result""" |
| 36 | + if "vendor.samsung.hardware" in battery_states['health']: |
| 37 | + extract_health= (battery_states['health'].split("::")[0].replace("vendor.samsung.hardware.health@","")) |
| 38 | + battery_states['health'] = str(round(float(extract_health))) |
| 39 | + |
| 40 | + # Converting Battery Status Number to String |
| 41 | + try: |
| 42 | + battery_states['status'] = bat_state_conver(choice='status',num=int(battery_states['status'])) |
| 43 | + except: |
| 44 | + print("Unable to Convert Status, Check Results") |
| 45 | + |
| 46 | + if changes > 0: |
| 47 | + print("---- {} changes".format(changes)) |
| 48 | + # print(battery_states) |
| 49 | + pass |
| 50 | + |
| 51 | + return battery_states |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | +""" |
| 60 | +------------------------------------------------- |
| 61 | +------------------ SCREEN INFO ------------------ |
| 62 | +------------------------------------------------- |
| 63 | +""" |
| 64 | + |
| 65 | +dupe_device_count=1 |
| 66 | +def get_screen_info(device_choice): |
| 67 | + """Get Device Screen Info |
| 68 | + - This is where the device details are started |
| 69 | + """ |
| 70 | + global dupe_device_count |
| 71 | + density = device_choice.shell('wm density').split(":")[-1].split("\n")[0] |
| 72 | + size = device_choice.shell('wm size').split(":")[-1].split("\n")[0] |
| 73 | + model = device_choice.shell('getprop ro.product.model').split("\n")[0] |
| 74 | + manufac = device_choice.shell('getprop ro.product.manufacturer').split("\n")[0] |
| 75 | + board = device_choice.shell('getprop ro.product.board').split("\n")[0] |
| 76 | + hardware = device_choice.shell('getprop ro.hardware').split("\n")[0] |
| 77 | + android_version = device_choice.shell('getprop ro.build.version.release').split("\n")[0] |
| 78 | + |
| 79 | + |
| 80 | + ### Incase of duplicate devices lets add 1 number... |
| 81 | + if model in device_names: |
| 82 | + model = f"{model}({dupe_device_count})" |
| 83 | + dupe_device_count = dupe_device_count +1 |
| 84 | + device_names.append(model) |
| 85 | + return {"Version": android_version, "Hardware": hardware, "Board": board, "Manufacturer": manufac.capitalize(), "Model": model, "Screen Size": size, "Screen Density":density} |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | +""" |
| 95 | +------------------------------------------------- |
| 96 | +------------------- MEM USAGE ------------------- |
| 97 | +------------------------------------------------- |
| 98 | +""" |
| 99 | + |
| 100 | +def get_mem_info(device): |
| 101 | + """ Returns Dictionary with Memory Details""" |
| 102 | + memavail = 0 |
| 103 | + memfree = 0 |
| 104 | + memtotal = 0 |
| 105 | + |
| 106 | + try: |
| 107 | + mem_check = device.shell("cat /proc/meminfo") |
| 108 | + split = mem_check.splitlines(-1) |
| 109 | + memdict ={} |
| 110 | + for line in split: |
| 111 | + if 'MemTotal:' in line: |
| 112 | + size = kb2mb(int(line.split(" ")[-2])) |
| 113 | + memdict['MemTotal'] = f"{size}" |
| 114 | + memtotal = memdict['MemTotal'] |
| 115 | + |
| 116 | + if 'MemFree:' in line: |
| 117 | + size = kb2mb(line.split(" ")[-2]) |
| 118 | + memdict['MemFree'] = f"{size}" |
| 119 | + memfree = memdict['MemFree'] |
| 120 | + |
| 121 | + if 'MemAvailable:' in line: |
| 122 | + size = kb2mb(line.split(" ")[-2]) |
| 123 | + memdict['MemAvailable'] = f"{size}" |
| 124 | + memavail = memdict['MemAvailable'] |
| 125 | + |
| 126 | + |
| 127 | + # print(memdict) |
| 128 | + """Had to do this to get it to round?""" |
| 129 | + perce= int(memavail) / int(memtotal) * 100 |
| 130 | + memdict['Percentage']= round(perce) |
| 131 | + |
| 132 | + for x in device_list: |
| 133 | + if device_list[x]['ID'] == device: |
| 134 | + device_list[x].update({'MemAvailable':memavail}) |
| 135 | + device_list[x].update({'MemFree':memfree}) |
| 136 | + device_list[x].update({'Percentage':perce}) |
| 137 | + # print(f"{x} Dictionary Updated with Memory Usage") |
| 138 | + |
| 139 | + return memdict |
| 140 | + |
| 141 | + except Exception as e: |
| 142 | + print(f"Error Getting Memory Info: {e}") |
| 143 | + return None |
| 144 | + |
| 145 | + |
| 146 | + |
| 147 | + |
| 148 | + |
| 149 | +""" |
| 150 | +------------------------------------------------- |
| 151 | +------------------- CPU USAGE ------------------- |
| 152 | +------------------------------------------------- |
| 153 | +""" |
| 154 | + |
| 155 | +def get_cpu_usge(device): |
| 156 | + """ |
| 157 | + Get the CPU Usage of the Device(s) |
| 158 | + """ |
| 159 | + |
| 160 | + build = {} |
| 161 | + thelist = {"cpu", "user", "nice", "sys", "idle"} |
| 162 | + ok = device.shell("top -n1") |
| 163 | + split = ok.splitlines(-1) |
| 164 | + complete = False |
| 165 | + for line in split: |
| 166 | + if not complete: |
| 167 | + if "cpu" in line: |
| 168 | + newline = line.split(" ") |
| 169 | + complete = True |
| 170 | + for x in newline: |
| 171 | + if x: |
| 172 | + final_split = x.split("%") |
| 173 | + if final_split[-1] in thelist: |
| 174 | + build[final_split[-1]] = final_split[0] |
| 175 | + |
| 176 | + result= int(build['user']) + int(build['sys']) |
| 177 | + result = result / int(build['cpu']) |
| 178 | + result=result*100 |
| 179 | + result = str(result)[0:2] |
| 180 | + |
| 181 | + if "." in result: |
| 182 | + result = result.replace(".", "") |
| 183 | + |
| 184 | + """ Updating CPU Usage for Devices""" |
| 185 | + for x in device_list: |
| 186 | + if device_list[x]['ID'] == device: |
| 187 | + device_list[x].update({'CPU USAGE':result}) |
| 188 | + # print(f"{x} Dictionary Updated with CPU Usage") |
| 189 | + |
| 190 | + return result |
0 commit comments