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 json
|
||||||
import serial
|
import serial
|
||||||
import subprocess
|
import subprocess
|
||||||
|
import threading
|
||||||
import time
|
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(["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.write(bytes(json.dumps(msg) + "\n", "utf8"))
|
||||||
process.stdin.flush()
|
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:
|
class MockSerial:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
@ -44,7 +52,7 @@ class MockSerial:
|
||||||
self.humidity[1:-1] += 0.2*(avg - self.humidity[1:-1])
|
self.humidity[1:-1] += 0.2*(avg - self.humidity[1:-1])
|
||||||
#print(self.humidity)
|
#print(self.humidity)
|
||||||
|
|
||||||
humidity = self.humidity[60]
|
humidity = self.humidity[60] + np.random.random()*0.003
|
||||||
if self.humidifier_on:
|
if self.humidifier_on:
|
||||||
hv = 3.3
|
hv = 3.3
|
||||||
else:
|
else:
|
||||||
|
@ -93,6 +101,9 @@ class Humidifier:
|
||||||
humidifier = Humidifier()
|
humidifier = Humidifier()
|
||||||
target_lower = 0.85
|
target_lower = 0.85
|
||||||
target_upper = 0.90
|
target_upper = 0.90
|
||||||
|
|
||||||
|
humidifier_history = np.zeros(30)
|
||||||
|
first_sample = False
|
||||||
try:
|
try:
|
||||||
last_sample = 0
|
last_sample = 0
|
||||||
while True:
|
while True:
|
||||||
|
@ -113,15 +124,24 @@ try:
|
||||||
temp = float(parts[1])
|
temp = float(parts[1])
|
||||||
volts = float(parts[2])
|
volts = float(parts[2])
|
||||||
print(humidity, temp, volts)
|
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)
|
# compensate for the slow response time
|
||||||
if humidity < target_lower and humidifier.off:
|
slope = (humidifier_history[-1] - humidifier_history[0])/humidifier_history.shape[0]
|
||||||
print("try toggle")
|
comp_humidity = humidity + 50*slope
|
||||||
humidifier.toggle(s)
|
|
||||||
elif humidity > target_upper and humidifier.on:
|
|
||||||
humidifier.toggle(s)
|
|
||||||
|
|
||||||
try:
|
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 = {
|
update = {
|
||||||
"time": int(now*1000),
|
"time": int(now*1000),
|
||||||
"temp": temp,
|
"temp": temp,
|
||||||
|
@ -132,11 +152,19 @@ try:
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("pipe errored out, restarting: ", e)
|
print("pipe errored out, restarting: ", e)
|
||||||
# restart the process I guess
|
# restart the process I guess
|
||||||
|
exiting = True
|
||||||
process.kill()
|
process.kill()
|
||||||
time.sleep(0.1)
|
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)
|
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:
|
finally:
|
||||||
# kill ssh connection
|
# kill ssh connection
|
||||||
|
exiting = True
|
||||||
process.kill()
|
process.kill()
|
||||||
pass
|
stdout_thread.join()
|
||||||
|
|
Loading…
Reference in New Issue