Implement a simple mushroom controller mock
This commit is contained in:
parent
18ffe01347
commit
00868284bd
|
@ -0,0 +1,112 @@
|
|||
import numpy as np
|
||||
|
||||
import json
|
||||
import serial
|
||||
import subprocess
|
||||
import time
|
||||
|
||||
#process = subprocess.Popen(["ssh", "shrooms@localhost", "/usr/bin/env", "python", "/home/shrooms/go/src/shroom-server/shroom-pipe.py"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
|
||||
process = subprocess.Popen(["/usr/bin/env", "python", "/home/kelvin/src/shroom-server/shroom_pipe.py"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
|
||||
|
||||
# TODO run thread to process data from process's stdout
|
||||
|
||||
class MockSerial:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def write(self, _):
|
||||
pass
|
||||
|
||||
def read(self, _):
|
||||
t = time.time()
|
||||
temp = 25 + np.sin(0.01*2*np.pi*t) + 0.5*np.sin(0.0001*2*np.pi*t + 7)
|
||||
humidity = 0.90 + 0.01*np.sin(0.05*2*np.pi*t) + 0.03*np.sin(0.1*2*np.pi*t + 7)
|
||||
hv = 3.3*int(1 + np.sin(0.2*np.pi*t))
|
||||
return bytes("{},{},{}\n".format(humidity, temp, hv), "utf8")
|
||||
|
||||
s = MockSerial()
|
||||
|
||||
def reset_serial():
|
||||
pass
|
||||
time.sleep(10)
|
||||
|
||||
class Humidifier:
|
||||
def __init__(self):
|
||||
self.on = False
|
||||
self.history = np.zeros(30)
|
||||
self.switch_timeout = 0
|
||||
|
||||
@property
|
||||
def off(self):
|
||||
return not self.on
|
||||
|
||||
def update(self, volts):
|
||||
self.history[1:] = self.history[:-1]
|
||||
self.history[0] = volts
|
||||
avg = np.sum(self.history)/self.history.shape[0]
|
||||
if self.on:
|
||||
if avg < 0.2:
|
||||
self.on = False
|
||||
else:
|
||||
if avg > 2.6:
|
||||
self.on = True
|
||||
|
||||
def toggle(self, s):
|
||||
if time.time() > self.switch_timeout:
|
||||
s.write(b"h")
|
||||
self.switch_timeout = time.time() + 7
|
||||
|
||||
def send_update(msg):
|
||||
global process
|
||||
process.stdin.write(bytes(json.dumps(update) + "\n", "utf8"))
|
||||
process.stdin.flush()
|
||||
|
||||
humidifier = Humidifier()
|
||||
target_lower = 0.85
|
||||
target_upper = 0.90
|
||||
try:
|
||||
last_sample = 0
|
||||
while True:
|
||||
now = time.time()
|
||||
if now - last_sample < 0.5:
|
||||
time.sleep(0.5 - (now - last_sample))
|
||||
continue
|
||||
last_sample = now
|
||||
|
||||
s.write(b"s")
|
||||
resp = s.read(120)
|
||||
if len(resp) == 0:
|
||||
reset_serial()
|
||||
time.sleep(5)
|
||||
continue
|
||||
parts = resp.split(b",")
|
||||
humidity = float(parts[0])
|
||||
temp = float(parts[1])
|
||||
volts = float(parts[2])
|
||||
print(humidity, temp, volts)
|
||||
|
||||
humidifier.update(volts)
|
||||
if humidity < target_lower and humidifier.off:
|
||||
humidifier.toggle(s)
|
||||
elif humidity > target_upper and humidifier.on:
|
||||
humidifier.toggle(s)
|
||||
|
||||
try:
|
||||
update = {
|
||||
"time": int(now*1000),
|
||||
"temp": temp,
|
||||
"hum": humidity,
|
||||
"hv": volts
|
||||
}
|
||||
send_update(update)
|
||||
except Exception as e:
|
||||
print("pipe errored out, restarting: ", e)
|
||||
# restart the process I guess
|
||||
process.kill()
|
||||
time.sleep(0.1)
|
||||
process = subprocess.Popen(["/usr/bin/env", "python", "/home/kelvin/src/shroom-server/shroom_pipe.py"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
|
||||
|
||||
finally:
|
||||
# kill ssh connection
|
||||
process.kill()
|
||||
pass
|
Loading…
Reference in New Issue