From 0d3a8596ce5c5802c37559845b1aea5a6c2d3047 Mon Sep 17 00:00:00 2001 From: Charles Henville Date: Tue, 23 Jan 2024 09:55:11 -0500 Subject: [PATCH] serv changes --- backend/serv.py | 80 ++++++++++++++++++++++++++++--------------------- 1 file changed, 46 insertions(+), 34 deletions(-) diff --git a/backend/serv.py b/backend/serv.py index 490967f..9e0d8a7 100644 --- a/backend/serv.py +++ b/backend/serv.py @@ -14,30 +14,34 @@ sse = int(cdt.timestamp()) max_points = 4096 dry_threshold = 25.0 -write_interval = 1800 ## How often data gets written to drive +write_interval = 1800 # How often data gets written to drive -df = pd.DataFrame(columns=['time', 'moisture', 'moisture2', 'moisture3', 'sunlight']) +df = pd.DataFrame(columns=['time', 'moisture', + 'moisture2', 'moisture3', 'sunlight']) df.loc[0] = [sse, 0.0, 0.0, 0.0, 0.0] try: df = pd.read_csv(log_path, index_col=0) + if df.empty: + raise FileNotFoundError() + filtered_df = df[['time', 'moisture']] - if filtered_df.empty: raise FileNotFoundError() + moist_data = filtered_df.rename(columns={'moisture': 'value'}).to_dict(orient='records') - # TODO: Change the moist_data instances here to a larger list of lists. - moist_data = filtered_df.to_dict(orient='records') filtered_df = df[['time', 'moisture2']] - moist_data_2 = filtered_df.to_dict(orient='records') + moist_data_2 = filtered_df.rename(columns={'moisture2': 'value'}).to_dict(orient='records') + filtered_df = df[['time', 'moisture3']] - moist_data_3 = filtered_df.to_dict(orient='records') - filtered_df = df[['time', 'sunlight']] + moist_data_3 = filtered_df.rename(columns={'moisture3': 'value'}).to_dict(orient='records') - sun_data = filtered_df.to_dict(orient='records') + filtered_df = df[['time', 'sunlight']] + sunlight_data = filtered_df.rename(columns={'sunlight': 'value'}).to_dict(orient='records') except FileNotFoundError: - moist_data=[{"time": sse, "value": 0}] - moist_data_2=[{"time": sse, "value": 0}] - moist_data_3=[{"time": sse, "value": 0}] - sun_data=[{"time": sse, "value": 0}] + moist_data = [{"time": sse, "value": 0}] + moist_data_2 = [{"time": sse, "value": 0}] + moist_data_3 = [{"time": sse, "value": 0}] + sun_data = [{"time": sse, "value": 0}] + def write_to_global_data(moist, moist2, moist3, sun): global moist_data, moist_data_2, sun_data, cdt, max_points, lw_time @@ -53,14 +57,14 @@ def write_to_global_data(moist, moist2, moist3, sun): if (sse - lw_time) >= write_interval: lw_time = sse df.to_csv(log_path, encoding='utf-8') - filtered_df = df[['time', 'moisture']] - moist_data = filtered_df.to_dict(orient='records') + filtered_df = df[['time', 'moisture']] + moist_data = filtered_df.rename(columns={'moisture': 'value'}).to_dict(orient='records') filtered_df = df[['time', 'moisture2']] - moist_data_2 = filtered_df.to_dict(orient='records') + moist_data_2 = filtered_df.rename(columns={'moisture2': 'value'}).to_dict(orient='records') filtered_df = df[['time', 'moisture3']] - moist_data_3 = filtered_df.to_dict(orient='records') + moist_data_3 = filtered_df.rename(columns={'moisture3': 'value'}).to_dict(orient='records') filtered_df = df[['time', 'sunlight']] - sun_data = filtered_df.to_dict(orient='records') + sun_data = filtered_df.rename(columns={'sunlight': 'value'}).to_dict(orient='records') else: moist_data.append({"time": sse, "value": moist}) moist_data_2.append({"time": sse, "value": moist2}) @@ -76,63 +80,71 @@ def write_to_global_data(moist, moist2, moist3, sun): if len(sun_data) > max_points: sun_data.pop(0) + def check_activation(): global moist_data, moist_data_2, moist_data_3 - total=0 - seconds=0 - seconds2=0 + total = 0 + seconds = 0 + seconds2 = 0 lm = float(moist_data[len(moist_data)-1]['value']) lm2 = float(moist_data_2[len(moist_data_2)-1]['value']) lm3 = float(moist_data_3[len(moist_data_3)-1]['value']) - if lm