Instantly share code, notes, and snippets.
Save endolith/02bc3cf6d6deb879c5cbb530a83f15b7 to your computer and use it in GitHub Desktop.
Turn BOINC on or off depending on how warm the room is
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| importsubprocess | |
| importre | |
| importdatetime | |
| importos | |
| # Set the temperature threshold (in Celsius) | |
| TEMP_THRESHOLD=25 | |
| # Set the path to the BOINC data directory containing the gui_rpc_auth.cfg file | |
| boinc_data_dir="/var/lib/boinc-client" | |
| # Get the current temperature from TemperHum | |
| temperhum_output=subprocess.check_output("temper-poll -c",shell=True).decode("utf-8") | |
| current_temperature=float(temperhum_output.strip()) | |
| # Set the absolute path to the log file | |
| log_file="/home/endolith/temperature/boinc_temperature_control.log" | |
| # Change the working directory to the BOINC data directory | |
| os.chdir(boinc_data_dir) | |
| # Get project status | |
| project_status_output=subprocess.check_output("boinccmd --get_project_status",shell=True).decode("utf-8") | |
| # Extract project URLs and their "don't request more work" status | |
| projects=re.findall(r'master URL: (.+?)\n.*?don\'t request more work: (\w+)',project_status_output,re.DOTALL) | |
| # Check if the temperature is above the threshold | |
| ifcurrent_temperature>TEMP_THRESHOLD: | |
| # Disable new tasks for all projects only if they are not already suspended | |
| forproject_url,no_more_workinprojects: | |
| ifno_more_work=="no": | |
| subprocess.run(f"boinccmd --project{project_url} nomorework",shell=True) | |
| withopen(log_file,"a")aslog: | |
| log.write(f"{datetime.datetime.now()} - Temperature:{current_temperature}°C - BOINC tasks suspended for project{project_url}\n") | |
| else: | |
| # Enable new tasks for all projects only if they are not already running | |
| forproject_url,no_more_workinprojects: | |
| ifno_more_work=="yes": | |
| subprocess.run(f"boinccmd --project{project_url} allowmorework",shell=True) | |
| withopen(log_file,"a")aslog: | |
| log.write(f"{datetime.datetime.now()} - Temperature:{current_temperature}°C - BOINC tasks resumed for project{project_url}\n") |
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment