import clr from datetime import datetime import os from threading import Thread import time import database class Monitor(Thread): def __init__(self): clr.AddReference(r'OpenHardwareMonitorLib') from OpenHardwareMonitor import Hardware self.handle = Hardware.Computer() self.handle.MainboardEnabled = True self.handle.CPUEnabled = True self.handle.RAMEnabled = True self.handle.GPUEnabled = True self.handle.HDDEnabled = True self.handle.Open() super().__init__() def run(self): def parse_sensor(sensor): sql = ''' insert into sensor_types ( name ) values ( ? ) on conflict (name) do update set name = sensor_types.name returning sensor_types.id; ''' cursor.execute( sql, ( str(sensor.SensorType), ), ) sensor_type_id, = cursor.fetchone() sql = ''' insert into sensors ( hardware_id, sensor_type_id, identifier, name, 'index', is_default_hidden, enabled ) values ( ?, ?, ?, ?, ?, ?, false ) on conflict (identifier, sensor_type_id) do update set hardware_id = sensors.hardware_id, name = sensors.name, 'index' = sensors.'index', is_default_hidden = sensors.is_default_hidden, enabled = sensors.enabled returning sensors.id, sensors.enabled; ''' cursor.execute( sql, ( str(hardware_id), sensor_type_id, str(sensor.Identifier), str(sensor.Name), int(sensor.Index), bool(sensor.IsDefaultHidden), ), ) sensor_id, sensor_enabled, = cursor.fetchone() if not sensor_enabled: return sql = ''' insert into sensor_values ( sensor_id, timestamp, value ) values ( ?, ?, ? ); ''' cursor.execute( sql, ( sensor_id, datetime.now(), sensor.Value, ), ) while True: for hardware in self.handle.Hardware: with database.connect() as connection: cursor = connection.cursor() sql = ''' insert into hardware_types ( name ) values ( ? ) on conflict (name) do update set name = hardware_types.name returning hardware_types.id; ''' cursor.execute( sql, ( str(hardware.HardwareType), ), ) hardware_type_id, = cursor.fetchone() sql = ''' insert into hardware ( identifier, name, hardware_type_id ) values ( ?, ?, ? ) on conflict (identifier) do update set name = hardware.name, hardware_type_id = hardware.hardware_type_id returning hardware.id; ''' cursor.execute( sql, ( str(hardware.Identifier), str(hardware.Name), hardware_type_id, ), ) hardware_id, = cursor.fetchone() hardware.Update() for sensor in hardware.Sensors: parse_sensor(sensor) for subhardware in hardware.SubHardware: subhardware.Update() for subsensor in subhardware.Sensors: parse_sensor(subsensor) time.sleep(1)