Add stdout thread to capture commands from the server
This commit is contained in:
parent
9692132e75
commit
f1723ff5bd
|
@ -3,6 +3,7 @@ import numpy as np
|
|||
import json
|
||||
import serial
|
||||
import subprocess
|
||||
import threading
|
||||
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)
|
||||
|
@ -13,7 +14,14 @@ def send_update(msg):
|
|||
process.stdin.write(bytes(json.dumps(msg) + "\n", "utf8"))
|
||||
process.stdin.flush()
|
||||
|
||||
# TODO run thread to process data from process's stdout
|
||||
exiting = False
|
||||
# run thread to process data from process's stdout
|
||||
def stdout_loop():
|
||||
while not exiting:
|
||||
msg = process.stdout.readline()
|
||||
print("got message ", msg)
|
||||
stdout_thread = threading.Thread(target=stdout_loop)
|
||||
stdout_thread.start()
|
||||
|
||||
class MockSerial:
|
||||
def __init__(self):
|
||||
|
@ -44,7 +52,7 @@ class MockSerial:
|
|||
self.humidity[1:-1] += 0.2*(avg - self.humidity[1:-1])
|
||||
#print(self.humidity)
|
||||
|
||||
humidity = self.humidity[60]
|
||||
humidity = self.humidity[60] + np.random.random()*0.003
|
||||
if self.humidifier_on:
|
||||
hv = 3.3
|
||||
else:
|
||||
|
@ -93,6 +101,9 @@ class Humidifier:
|
|||
humidifier = Humidifier()
|
||||
target_lower = 0.85
|
||||
target_upper = 0.90
|
||||
|
||||
humidifier_history = np.zeros(30)
|
||||
first_sample = False
|
||||
try:
|
||||
last_sample = 0
|
||||
while True:
|
||||
|
@ -113,15 +124,24 @@ try:
|
|||
temp = float(parts[1])
|
||||
volts = float(parts[2])
|
||||
print(humidity, temp, volts)
|
||||
if first_sample:
|
||||
humidifier_history[:] = humidity
|
||||
first_sample = False
|
||||
else:
|
||||
humidifier_history[:-1] = humidifier_history[1:]
|
||||
humidifier_history[-1] = humidity
|
||||
|
||||
humidifier.update(volts)
|
||||
if humidity < target_lower and humidifier.off:
|
||||
print("try toggle")
|
||||
humidifier.toggle(s)
|
||||
elif humidity > target_upper and humidifier.on:
|
||||
humidifier.toggle(s)
|
||||
# compensate for the slow response time
|
||||
slope = (humidifier_history[-1] - humidifier_history[0])/humidifier_history.shape[0]
|
||||
comp_humidity = humidity + 50*slope
|
||||
|
||||
try:
|
||||
humidifier.update(volts)
|
||||
if comp_humidity < target_lower and humidifier.off:
|
||||
humidifier.toggle(s)
|
||||
elif comp_humidity > target_upper and humidifier.on:
|
||||
humidifier.toggle(s)
|
||||
|
||||
update = {
|
||||
"time": int(now*1000),
|
||||
"temp": temp,
|
||||
|
@ -132,11 +152,19 @@ try:
|
|||
except Exception as e:
|
||||
print("pipe errored out, restarting: ", e)
|
||||
# restart the process I guess
|
||||
exiting = True
|
||||
process.kill()
|
||||
time.sleep(0.1)
|
||||
stdout_thread.join()
|
||||
exiting = False
|
||||
|
||||
process = subprocess.Popen(["/usr/bin/env", "python", "/home/kelvin/src/shroom-server/shroom_pipe.py"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
|
||||
|
||||
stdout_thread = threading.Thread(target=stdout_loop)
|
||||
stdout_thread.start()
|
||||
|
||||
finally:
|
||||
# kill ssh connection
|
||||
exiting = True
|
||||
process.kill()
|
||||
pass
|
||||
stdout_thread.join()
|
||||
|
|
Loading…
Reference in New Issue