diff options
| -rwxr-xr-x | regulate.py | 60 | 
1 files changed, 60 insertions, 0 deletions
diff --git a/regulate.py b/regulate.py new file mode 100755 index 0000000..8081e6c --- /dev/null +++ b/regulate.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python3 + +import subprocess, re, time, sys + +# lowtemp,lowtrigger,hightrigger,hightemp=19.0,19.5,20.5,21.0 +lowtemp,lowtrigger,hightrigger,hightemp=18.0,18.5,19.5,20.0 + +assert lowtemp < lowtrigger < hightrigger < hightemp + +def gettemp(): +	output=subprocess.check_output("date").decode("ascii") +	print(output.replace("\n",""),end="") +	output=subprocess.check_output("./gettemp.sh").decode("ascii") +	print("  "+output.replace("\n","")) +	match=re.match(r"^current: ([0-9]+\.[0-9]+); target: ([0-9]+\.[0-9]+)\s*$",output) +	if match is None: +		return None +	return (float(match.group(1)),float(match.group(2))) + +def inctemp(amount): +	print("Adjusting temperature: "+str(amount)) +	subprocess.check_call(["./inctemp.sh",str(amount)]) + +def alert(msg): +	sys.stdout.write("\x07") +	sys.stdout.flush() +	try: +		escmsg=msg.replace("\\","\\\\").replace("\"","\\\"") +		subprocess.check_call(["osascript","-e","display dialog \""+escmsg+"\""]) +	except CalledProcessError: +		pass + +def main(): +	targettemp=None +	while True: +		temp=gettemp() +		if temp is None: +			alert("No valid output got from gettemp.sh!") +			break + +		if targettemp is None: +			targettemp=temp[1] +			if targettemp not in (lowtemp,hightemp): +				alert("Target temperature not equal to "+str(lowtemp)+" or "+str(hightemp)+"!") +				break +		elif temp[1]!=targettemp: +			alert("Someone pushed some buttons!") +			break + +		if temp[1]==lowtemp and temp[0]<=lowtrigger: +			inctemp(hightemp-lowtemp) +			targettemp=hightemp +		elif temp[1]==hightemp and temp[0]>=hightrigger: +			inctemp(lowtemp-hightemp) +			targettemp=lowtemp + +		time.sleep(120) + +if __name__=="__main__": +	main()  | 
