summaryrefslogtreecommitdiff
path: root/regulate.py
blob: 8081e6c107803205f8ae25630bb242e40f5ec715 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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()