diff --git a/serv/file.csv b/serv/file.csv
deleted file mode 100644
index 93a5bbd..0000000
--- a/serv/file.csv
+++ /dev/null
@@ -1,7 +0,0 @@
-date, time, expected temp, actual temp, expected hygro, actual hygro
-09/01/2023, 11:31:28, 24.0, 20.83, 95.0, 94.86
-09/01/2023, 11:31:34, 24.0, 20.81, 95.0, 94.7
-09/01/2023, 11:31:40, 24.0, 20.83, 95.0, 94.55
-09/01/2023, 11:31:46, 24.0, 20.8, 95.0, 94.39
-09/01/2023, 11:31:51, 24.0, 20.8, 95.0, 94.17
-09/01/2023, 11:31:57, 24.0, 20.8, 95.0, 93.95
diff --git a/serv/main.py b/serv/main.py
index 876f7bf..155d9f9 100644
--- a/serv/main.py
+++ b/serv/main.py
@@ -1,22 +1,92 @@
import os
import csv
import poller
+import json
import mushroom_controller
-from flask import Flask, render_template
+from datetime import datetime
+from flask import Flask, render_template, request
+
+from threading import Thread
+from threading import Lock
+
+
+# create a shared lock
+lock = Lock()
conn = mushroom_controller.get_connection()
+print(">>>", conn is None)
filename = "file.csv"
-if os.fork() == 0:
- poller.run(conn, filename)
- os._exit(1) # unreachable
+Thread(target=poller.run, args=(conn, filename, lock)).start()
+# if os.fork() == 0:
+# poller.run(conn, filename)
+# os._exit(1) # unreachable
app = Flask(__name__)
@app.route("/")
-def hello_world():
+def homepage():
+ return render_template('index.html')
+
+
+@app.route("/data")
+def get_data():
with open(filename, "r") as file:
- data = [x for i, x in enumerate(csv.reader(file)) if i > 0]
- return "
".join([str(x) for x in data])
+ raw = [v for v in csv.reader(file)]
+ data = [
+ [raw[i][j] for i in range(len(raw))]
+ for j in range(len(raw[0]))]
+ return {
+ "data": [
+ {
+ "name": line[0],
+ "x": data[0][1:],
+ "y": line[1:],
+ }
+ for line in data[1:]],
+ "time": datetime.now().strftime("%Y-%m-%dT%H:%M:%S")
+ }
+
+
+@app.route("/set_settings", methods=['POST'])
+def send_settings():
+ data = request.get_json()
+ with lock:
+ mushroom_controller.set_hygro(conn, data["hygro"])
+ print("set hydro")
+ mushroom_controller.set_temperature(conn, data["temp"])
+ print("set temp")
+ return "Ok"
+
+
+@app.route("/program", methods=['POST'])
+def program():
+ data = request.get_json()
+ try:
+ with open("prog.json", "r") as file:
+ jsondata = json.load(file)
+ except Exception:
+ jsondata = []
+ for val in data:
+ try:
+ date = datetime.strptime(val["date"], '%Y-%m-%dT%H:%M:%S')
+ except Exception:
+ return "Bad program data", 400
+ if type(val["hygro"]) != float or type(val["temp"]) != float:
+ return "Bad program data", 400
+ jsondata.append([
+ date.strftime("%Y-%m-%dT%H:%M:%S"),
+ {"hygro": val["hygro"], "temp": val["temp"]}])
+ jsondata.sort(reverse=True)
+ with open("prog.json", "w+") as file:
+ json.dump(jsondata, file)
+ return "Ok"
+
+
+@app.route("/program", methods=['DELETE'])
+def clear_program():
+ with open("prog.json", "w+") as file:
+ file.write("[]")
+ return "Ok"
diff --git a/serv/mushroom_controller.py b/serv/mushroom_controller.py
index 1ffe86e..83376fd 100644
--- a/serv/mushroom_controller.py
+++ b/serv/mushroom_controller.py
@@ -10,47 +10,68 @@ def get_connection():
try:
ser = serial.Serial(PORT, baudrate=9600, timeout=10)
time.sleep(5)
+ if ser is None:
+ raise Exception("Could not connect to hardware")
return ser
except Exception as e:
print(e)
+ raise Exception("Could not connect to hardware")
def set_temperature(conn, temp):
to_send = '1 '+str(temp)+"\r\n"
conn.write(to_send.encode())
c = conn.readline().rstrip()
- return float(c)
+ try:
+ return float(c)
+ except Exception:
+ return None
def set_hygro(conn, hygro):
to_send = '2 '+str(hygro)+"\r\n"
conn.write(to_send.encode())
c = conn.readline().rstrip()
- return float(c)
+ try:
+ return float(c)
+ except Exception:
+ return None
def get_expected_tmp(conn):
conn.write(b"3\n")
c = conn.readline().rstrip()
- return float(c)
+ try:
+ return float(c)
+ except Exception:
+ return None
def get_expected_hygro(conn):
conn.write(b"4\n")
c = conn.readline().rstrip()
- return float(c)
+ try:
+ return float(c)
+ except Exception:
+ return None
def get_actual_tmp(conn):
conn.write(b"5\n")
c = conn.readline().rstrip()
- return float(c)
+ try:
+ return float(c)
+ except Exception:
+ return None
def get_actual_hygro(conn):
conn.write(b"6\n")
c = conn.readline().rstrip()
- return float(c)
+ try:
+ return float(c)
+ except Exception:
+ return None
# Testing connection
diff --git a/serv/poller.py b/serv/poller.py
index f719562..d3c595d 100644
--- a/serv/poller.py
+++ b/serv/poller.py
@@ -1,21 +1,39 @@
from datetime import datetime
import time
+import json
import mushroom_controller
-def run(conn, filename):
+def run(conn, filename, lock):
with open(filename, "w") as file:
print("erasing file")
- file.write("date, time, expected temp, actual temp, expected hygro, actual hygro\n")
+ file.write("date time, Temp. attendue, Temp. effective, Humidité attendue, Humidité effective\n")
while True:
data = []
- exp_tmp = str(mushroom_controller.get_expected_tmp(conn))
- exp_hygro = str(mushroom_controller.get_expected_hygro(conn))
- actual_tmp = str(mushroom_controller.get_actual_tmp(conn))
- actual_hygro = str(mushroom_controller.get_actual_hygro(conn))
- stamp = datetime.now().strftime("%d/%m/%Y, %H:%M:%S")
+ with lock:
+ exp_tmp = str(mushroom_controller.get_expected_tmp(conn))
+ exp_hygro = str(mushroom_controller.get_expected_hygro(conn))
+ actual_tmp = str(mushroom_controller.get_actual_tmp(conn))
+ actual_hygro = str(mushroom_controller.get_actual_hygro(conn))
+ stamp = datetime.now().strftime("%Y-%m-%dT%H:%M:%S")
data = ", ".join((stamp, exp_tmp, actual_tmp, exp_hygro, actual_hygro))
with open(filename, "a") as file:
file.write(data + "\n")
- print("writing", data)
+
+ try:
+ with open("prog.json", "r") as file:
+ jsondata = json.load(file)
+ except Exception as e:
+ pass
+ else:
+ to_do = None
+ while len(jsondata) > 0 and jsondata[-1][0] < stamp: # if should be applied
+ to_do = jsondata.pop()
+ with open("prog.json", "w") as file:
+ json.dump(jsondata, file)
+ if to_do is not None:
+ with lock:
+ mushroom_controller.set_hygro(conn, to_do[1]["hygro"])
+ mushroom_controller.set_temperature(conn, to_do[1]["temp"])
+
time.sleep(1)
diff --git a/serv/static/style.css b/serv/static/style.css
new file mode 100644
index 0000000..a71b6ee
--- /dev/null
+++ b/serv/static/style.css
@@ -0,0 +1,5 @@
+body {
+ display: flex;
+ flex-direction: column;
+ align-items: center;
+}
\ No newline at end of file
diff --git a/serv/templates/index.html b/serv/templates/index.html
new file mode 100644
index 0000000..7a03343
--- /dev/null
+++ b/serv/templates/index.html
@@ -0,0 +1,57 @@
+
+