包括:CPU温度,系统运行时间,网卡名称,上下行网速

from PCD8544 import PCD8544 as lcd
import time
import os
import threading

cpu_info=""
time_info=""

speed_down=[]
speed_up=[]
speed_name=[]

# 显示cpu温度
def cpu_t():
    global cpu_info
    while(True):
        info=open('/sys/class/thermal/thermal_zone0/temp','r').read()
        info=(int)(info)/1000
        info=(str)(info)+'C'
        cpu_info=info
        time.sleep(1)

# 显示开机运行时间
def time_local():
    global time_info
    while(True):
        info=os.popen('cat /proc/uptime| awk -F. \'{run_days=$1 / 86400;run_hour=($1 % 86400)/3600;run_minute=($1 % 3600)/60;run_second=$1 % 60;printf(\"%dD %d:%d:%d\",run_days,run_hour,run_minute,run_second)}\'')
        info=info.read()
        time_info=info
        time.sleep(1)
        
# 显示网卡速度        
def net_speed():
    global speed_name
    global speed_down
    global speed_up

    speed_down_1={}
    speed_up_1={}

    speed_net_time={}       #记录各网卡有网速的时间

    while True:
        info=open('/proc/net/dev','r').readlines()
        for p in info:
            p=p.split( )
            if p[1].isdigit() and p[1] != 0:
                index = 0
                if p[0] in speed_name:
                    index = speed_name.index(p[0])

                    #若10秒后网速仍然为0就取消显示该网卡速度
                    if (int)(time.time()) - speed_net_time.get(p[0],0) > 10 and speed_down_1.get(p[0]) == p[1] and speed_up_1.get(p[0]) == p[9]:
                        del speed_name[index]
                        del speed_down[index]
                        del speed_up[index]
                        speed_net_time[p[0]] = (int)(time.time())
                    else:
                        speed_down[index]=(float)((int)(p[1])-(int)(speed_down_1.get(p[0])))/1000
                        speed_up[index]=(float)((int)(p[9])-(int)(speed_up_1.get(p[0])))/1000
                        if speed_down[index] != 0 and speed_up[index] != 0:
                            speed_net_time[p[0]] = (int)(time.time())
                    speed_down_1[p[0]]=p[1]
                    speed_up_1[p[0]]=p[9]
                else:
                    if speed_down_1.get(p[0],0) != p[1] and speed_up_1.get(p[0],0) != p[9]:
                        speed_name.append(p[0])
                        speed_down.append(0)
                        speed_up.append(0)
                        speed_down_1[p[0]] = p[1]
                        speed_up_1[p[0]] = p[9]
                    continue
        time.sleep(1)



def main():
    a = lcd(dc=13, rst=5, sclk=26, din=19, cs=6, vcc=20, bl=21, bk=0)
    a.begin(contrast=60)        #屏幕对比度

    #各任务使用多线程运行
    tmp=threading.Thread(target=time_local)
    tmp.setDaemon(True)
    tmp.start()
    tmp=threading.Thread(target=cpu_t)
    tmp.setDaemon(True)
    tmp.start()
    tmp=threading.Thread(target=net_speed)
    tmp.setDaemon(True)
    tmp.start()

    t1=(int)(time.time())
    index=0
    while(True):
        try:
            t2=(int)(time.time())
            a.clear()
            a.draw_str(0,'CPU: '+cpu_info)
            a.draw_str(1,time_info)
            try:
                # 网卡显示,10秒切换网卡信息
                if t2-t1>10:
                    t1=t2
                    if index < len(speed_name)-1:
                        index=index+1
                    else:
                        index=0
                a.draw_str(2,speed_name[index]+" ("+(str)(index+1)+"/"+(str)(len(speed_name))+")")
                a.draw_str(3,"D: "+(str)(speed_down[index])+"k/s")
                a.draw_str(4,"U: "+(str)(speed_up[index])+"k/s")
            except Exception as e:
                pass
            a.display()
            time.sleep(1)
        except KeyboardInterrupt:
            a.quit()
            exit(0)

if __name__ == '__main__':
    main()
Last modification:October 24th, 2019 at 02:48 pm
如果觉得我的文章对你有用,请随意赞赏