import psutil
import serial
import time
import os
ser = serial.Serial('/dev/ttyUSB0') #define serial port.
line = "nothing"; #create the variable where incoming communication is gonna be saved.
while True:
sep = ','
sep1 = '.'
t = os.popen('uptime -p').read()[:-1] #open the file /proc/uptime and read it
cpu = psutil.cpu_percent(interval=1) #reads the cpu åercetange every second
if cpu <= 10: #if cpu percetange is less than 10, add a zero, eg. 5% -> 05%. This lessens the propability of a bug happening where ram has two "%" icons
cpu = '0',str(cpu)
cpu = str(cpu)
cpu = str(cpu).replace('"', "")# Remove unecessary characters from string
cpu = str(cpu).replace("'", "")# --||--
cpu = str(cpu).replace(' ', "")# --||--
cpu = 'CPU', str(cpu).split(sep1, 1)[0] #split the string at the first dot and remove anything behind it
amount_of_ram = str(psutil.virtual_memory()[2]).split(sep1, 1)[0] # --||--
ram = 'RAM', str(amount_of_ram) # Convert the variable to a string. (Why??)
uptime = t #more spaghetti for the sake of it
updisk = (str(uptime).split(sep, 1)[0],'/',psutil.disk_usage('/') [3], '%') #generate a string that contains the uptime and how much of the / drive is used.
downdisk = str(cpu) + '% ' + str(ram) + '%' #generate a string that contains the info regarding the RAM and the CPU. I know my naming scheme is stupid.
downdisk = str(downdisk) #convert the variable to a string??
upstr = str(updisk) #--||--
upstr = upstr.replace("(", "") #replace all unecessary characters from the strings
upstr = upstr.replace(",", "")
upstr = upstr.replace("'", "")
upstr = upstr.replace(")", "")
upstr = upstr.replace("minutes", "m")
upstr = upstr.replace("minute", "m")
upstr = upstr.replace("hours", "h")
upstr = upstr.replace("hour", "h")
upstr = upstr.replace("days", "d")
upstr = upstr.replace("day", "d")
downdisk = downdisk.replace("(", "")
downdisk = downdisk.replace(",", "")
downdisk = downdisk.replace("'", "")
downdisk = downdisk.replace(")", "")
ser.write(b"*") # Let the arduino know we're about to write onto the second row
ser.write(upstr.encode()) # Write the data
ser.write(b"#") # Let the arduino know we're about to write onto the first row
ser.write(downdisk.encode())
line = ser.readline() # Read the serial output
if (not line == b'nothing\r\n'): # if false any buttons were'nt pressed, do nothing.
if (line == b'reboot\r\n'): # A reboot was ordered by pressing a button
os.system("reboot") #reboot the host system, replace the command if you wish to do something else when a button is pressed.
if (line == b'shutdown\r\n'): # A shutdown was ordered
os.system("shutdown now")
time.sleep(2) # Wait two seconds
# I'm sorry for the spaghetti and the excessive use of the commenting feature :)
Comments
Please log in or sign up to comment.