python 监控cpu实现高温警报
import winreg,winsound,pyttsx3,os,functools,wmi,subprocess,time
from ctypes import cast, POINTER
from comtypes import CLSCTX_ALL
from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
engine = pyttsx3.init()
engine.setProperty('volume', 1)# 设置音量(0到1之间)
rate = engine.getProperty('rate')# 设置语速
engine.setProperty('rate', rate-50)
r_path = os.path.join(os.environ['USERPROFILE'])+"\AppData\Local\Temp"
#设置最大声音
devices = AudioUtilities.GetSpeakers()
interface = devices.Activate(IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
volume = cast(interface, POINTER(IAudioEndpointVolume))
volume.SetMasterVolumeLevel(0.0, None)
def ApplicationInstance(func):
@functools.wraps(func)
def fun(*args,**kwargs):
import socket
try:
global s
s = socket.socket()
host = socket.gethostname()
s.bind((host, 60124))
except:
return print('程序已打开')
return func(*args,**kwargs)
return fun
@ApplicationInstance
def cpu_Monitor():
c = wmi.WMI()
processor = c.Win32_Processor()[0]
#cpu型号
cpu= processor.Name.strip()
if 'AMD' in cpu:
# 使用subprocess.STARTUPINFO来隐藏窗口
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
# 隐藏窗口
startupinfo.wShowWindow = subprocess.SW_HIDE
subprocess.Popen('OpenHardwareMonitor/OpenHardwareMonitor.exe',startupinfo=startupinfo)
time.sleep(3)
while True:
try:
w= wmi.WMI(namespace="root\OpenHardwareMonitor")
temperature_infos = w.Sensor()
for sensor in temperature_infos:
if sensor.SensorType == 'Temperature' and sensor.Name == 'CPU Package':
if int(sensor.Value)> 80:
filename = r_path+r'\Countdown.wav'
winsound.PlaySound(filename, winsound.SND_FILENAME)
engine.say('cpu当前温度%s度'%round(sensor.Value))
engine.runAndWait()
except:
pass
if 'Intel' in cpu:
while True:
try:
registry_path = r'SOFTWARE\\FinalWire\AIDA64\\SensorValues'
# 获取注册表该位置的所有键值
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, registry_path)
value = winreg.QueryValueEx(key,'Value.TCC-1-1')[0]
if int(value)> 80:
filename = r_path+r'\Countdown.wav'
winsound.PlaySound(filename, winsound.SND_FILENAME)
# winsound.Beep(frequency=2500,duration=1000)
engine.say('cpu当前温度%s度'%value)
engine.runAndWait()
except:
pass
if __name__ == '__main__':
cpu_Monitor()