From a1325125a6a8e6034f2935d71ddf1910d1d5db32 Mon Sep 17 00:00:00 2001
From: tomsmeding <tom.smeding@gmail.com>
Date: Wed, 14 Dec 2016 19:03:54 +0100
Subject: Add simple regulator script

---
 regulate.py | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 60 insertions(+)
 create mode 100755 regulate.py

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()
-- 
cgit v1.2.3-70-g09d2