From 15691a5d4a7ee2860d4e9b9b4662ed2ea38bba7c Mon Sep 17 00:00:00 2001 From: Jiacheng Huang <chonpcaacpnohc@gmail.com> Date: Thu, 12 Oct 2023 06:31:40 +0000 Subject: [PATCH] Upload New File --- Software/Database/Flask_Database.py | 1065 +++++++++++++++++++++++++++ 1 file changed, 1065 insertions(+) create mode 100644 Software/Database/Flask_Database.py diff --git a/Software/Database/Flask_Database.py b/Software/Database/Flask_Database.py new file mode 100644 index 0000000..272fde3 --- /dev/null +++ b/Software/Database/Flask_Database.py @@ -0,0 +1,1065 @@ +# -*- coding: utf-8 -*- +""" +Created on Mon Jul 17 16:53:42 2023 + +@author: 30517 +""" + +from flask import Flask, request, jsonify +import os +import time + +app = Flask(__name__) + +path = os.getcwd() + + +databases = ['Property', + 'Calibration-pH','Calibration-pumps', + 'Pump1-S','Pump1-V', + 'Pump2-S','Pump2-V', + 'Pump3-S','Pump3-V', + 'Pump4-S','Pump4-V', + 'Pump5-S','Pump5-V', + 'Pump6-S','Pump6-V', + 'Pump7-S','Pump7-V', + 'Air_S', 'Air_V', + 'pH', 'Temperature', + 'OD_G', 'OD_R', 'OD_all', + 'd_pH'] + + + +propers = ['p_Build_Time', + 'p_Work','p_start_time', + + 'p_Pump1-S','p_Pump1-V', + 'p_Pump2-S','p_Pump2-V', + 'p_Pump3-S','p_Pump3-V', + 'p_Pump4-S','p_Pump4-V', + 'p_Pump5-S','p_Pump5-V', + 'p_Pump6-S','p_Pump6-V', + 'p_Pump7-S','p_Pump7-V', + 'p_Air_S', 'p_Air_V', + 'p_pH', 'p_Temperature', + 'p_OD_G', 'p_OD_R', 'p_OD_all', + + 'g_pH', 'g_Temperature', + + + 'p_device_1', 'p_device_2', + + + 'f_ph', + 'c_pH', + + 'c_Pump1', + 'c_Pump2', + 'c_Pump3', + 'c_Pump4', + 'c_Pump5', + 'c_Pump6', + 'c_Pump7'] + + +Light = ['L_G', 'L_R', 'L_all'] + + +data_require_num = 3000 + +@app.route('/get_previous_experiment',methods=['POST']) +def get_previous_experiment(): + + os.chdir('History') + files = os.listdir() + os.chdir(path) + tmp_files = [] + for i in files: + if i.isalnum(): + tmp_files.append(i) + tmp_files.sort() + files = tmp_files + + return jsonify(msg = files) + + +@app.route('/creat_new_process',methods=['POST']) +def creat_new_process(): + + my_json = request.get_json() + + create_time = str(my_json.get('time')) + + os.chdir('History') + + work = 0 + + try: + + os.mkdir(create_time) + os.chdir(create_time) + + for i in databases: + f = open( i +'.txt','a+') + f.write('X') + f.write('\t') + f.write('Y') + f.close() + + for i in propers: + f = open( i +'.txt','a+') + f.write('0') + f.close() + + + + for i in Light: + f = open( i +'.txt','a+') + f.write('X') + f.write('\t') + f.write('S') + f.write('\t') + f.write('Y') + f.close() + + + + + f = open('p_Build_Time' + '.txt', 'w') + f.write(create_time) + f.close() + + f = open('p_Work' + '.txt', 'w') + f.write('0') + f.close() + + #æ ¡å‡† + + for i in ['f_ph', + 'c_pH', + 'c_Pump1', + 'c_Pump2', + 'c_Pump3', + 'c_Pump4', + 'c_Pump5', + 'c_Pump6', + 'c_Pump7']: + + f = open(i + '.txt', 'w') + f.write('0') + f.close() + + + f = open('g_pH' + '.txt', 'w') + f.write('7.0') + f.close() + + f = open('g_Temperature' + '.txt', 'w') + f.write('37.0') + f.close() + + f = open('g_Air' + '.txt', 'w') + f.write('0.0') + f.close() + + + os.chdir(path) + + work = 1 + + except: + + os.chdir(path) + + return jsonify(msg = work) + +@app.route('/update_database',methods=['POST']) +def update_database(): + + my_json = request.get_json() + + create_time = str(my_json.get('time')) + + index_counter = str(my_json.get('index')) + + data_update_x = str(my_json.get('x')) + + data_update_y = str(my_json.get('y')) + + f = open('./History/' + create_time + '/' + index_counter + '.txt', 'a+') + + f.write('\n') + + f.write(data_update_x) + + f.write('\t') + + f.write(data_update_y) + + f.close() + + return jsonify(msg = 1) + + + + +@app.route('/update_index',methods=['POST']) +def update_index(): + + my_json = request.get_json() + + create_time = str(my_json.get('time')) + + index_counter = str(my_json.get('index')) + + data_update = str(my_json.get('data')) + + f = open('./History/' + create_time + '/' + index_counter + '.txt', 'w') + + f.write(data_update) + + f.close() + + return jsonify(msg = 1) + + +@app.route('/update_work',methods=['POST']) +def update_work(): + + my_json = request.get_json() + + create_time = str(my_json.get('time')) + + f = open('./History/work_time.txt', 'w') + + f.write(create_time) + + f.close() + + return jsonify(msg = 1) + + +@app.route('/update_start',methods=['POST']) +def update_start(): + + my_json = request.get_json() + + create_time = str(my_json.get('time')) + + now_time = str(my_json.get('now_time')) + + + f = open('./History/' + create_time + '/' + 'p_Work' + '.txt', 'w') + + f.write(str(1)) + + f.close() + + + f = open('./History/' + create_time + '/' + 'p_start_time' + '.txt', 'w') + + f.write(now_time) + + f.close() + + for i in ['Pump1-S','Pump1-V', + 'Pump2-S','Pump2-V', + 'Pump3-S','Pump3-V', + 'Pump4-S','Pump4-V', + 'Pump5-S','Pump5-V', + 'Pump6-S','Pump6-V', + 'Pump7-S','Pump7-V', + 'Air_S', 'Air_V']: + + f = open('./History/' + create_time + '/' + i + '.txt', 'a+') + + f.write('\n') + + f.write(now_time) + + f.write('\t') + + f.write(str(0)) + + f.close() + + return jsonify(msg = 1) + + +@app.route('/lookup_database',methods=['POST']) +def lookup_database(): + + my_json = request.get_json() + + create_time = str(my_json.get('time')) + + index_counter = str(my_json.get('index')) + + cur_x = float(my_json.get('cur')) + + f = open('./History/' + create_time + '/' + index_counter + '.txt', 'r') + + f = f.readlines() + + X = [] + + Y = [] + + + for i in f[1::]: + + i = i.replace('\n','').split('\t') + + if (float(i[0]) > cur_x) and (len(X)<data_require_num): + + X.append(float(i[0])) + + Y.append(float(i[1])) + + return jsonify(x=X,y=Y) + + +@app.route('/lookup_index',methods=['POST']) +def lookup_index(): + + my_json = request.get_json() + + create_time = str(my_json.get('time')) + + index_counter = str(my_json.get('index')) + + f = open('./History/' + create_time + '/' + index_counter + '.txt', 'r') + + f = f.readlines() + + data = float(f[0].replace('\n', '')) + + return jsonify(data = data) + + +@app.route('/lookup_work',methods=['POST']) +def lookup_work(): + + f = open('./History/work_time.txt', 'r') + + f = f.readlines() + + data = int(f[0].replace('\n', '')) + + return jsonify(data = data) + + +@app.route('/lookup_cab',methods=['POST']) +def lookup_cab(): + + my_json = request.get_json() + + create_time = str(my_json.get('time')) + + data = [] + + for i in ['c_Pump7', + 'c_Pump6', + 'c_Pump5', + 'c_Pump4', + 'c_Pump3', + 'c_Pump2', + 'c_Pump1', + 'c_pH', + 'f_ph']: + + f = open('./History/' + create_time + '/' + i + '.txt', 'r') + + f = f.readlines() + + data.append(int(f[0].replace('\n', ''))) + + data = data[::-1] + + return jsonify(data = data) + + +@app.route('/lookup_all_index',methods=['POST']) +def lookup_all_index(): + + my_json = request.get_json() + + create_time = str(my_json.get('time')) + + data = [] + + for i in ['p_Work','p_start_time', + 'p_Pump1-S','p_Pump1-V', + 'p_Pump2-S','p_Pump2-V', + 'p_Pump3-S','p_Pump3-V', + 'p_Pump4-S','p_Pump4-V', + 'p_Pump5-S','p_Pump5-V', + 'p_Pump6-S','p_Pump6-V', + 'p_Pump7-S','p_Pump7-V', + 'p_pH', 'p_Temperature', + 'p_OD_G', 'p_OD_R', + 'g_pH', 'g_Temperature', + 'p_device_1', 'p_device_2', + 'p_Air_S', 'p_Air_V']: + + f = open('./History/' + create_time + '/' + i + '.txt', 'r') + + f = f.readlines() + + data.append(float(f[0].replace('\n', ''))) + + + return jsonify(data = data) + + + + +@app.route('/pump_lookup_all_database',methods=['POST']) +def pump_lookup_all_database(): + + my_json = request.get_json() + + create_time = str(my_json.get('time')) + + index_counter = eval(my_json.get('index')) + + + f = open('./History/' + create_time + '/p_' + index_counter[0] + '.txt', 'r') + + f = f.readlines() + + data_get_1 = float(f[0].replace('\n', '')) + + + f = open('./History/' + create_time + '/p_' + index_counter[1] + '.txt', 'r') + + f = f.readlines() + + data_get_2 = float(f[0].replace('\n', '')) + + + cur_x = float(my_json.get('cur')) + + + + f = open('./History/' + create_time + '/' + index_counter[0] + '.txt', 'r') + + f = f.readlines() + + X = [] + + Y = [] + + for i in f[1::]: + + i = i.replace('\n','').split('\t') + + if (float(i[0]) > cur_x) and (len(X) < data_require_num): + + X.append(float(i[0])) + + Y.append(float(i[1])) + + data_get1 = [X, Y] + + + + f = open('./History/' + create_time + '/' + index_counter[1] + '.txt', 'r') + + f = f.readlines() + + X = [] + + Y = [] + + for i in f[1::]: + + i = i.replace('\n','').split('\t') + + if (float(i[0]) > cur_x) and (len(X) < data_require_num): + + X.append(float(i[0])) + + Y.append(float(i[1])) + + data_get2 = [X, Y] + + + return jsonify(data_get_1 = data_get_1, data_get_2 = data_get_2, data_get1 = data_get1, data_get2 = data_get2) + + + + +@app.route('/pump_update_all_database',methods=['POST']) +def pump_update_all_database(): + + my_json = request.get_json() + + create_time = str(my_json.get('time')) + + index_counter = eval(my_json.get('index')) + + number = str(my_json.get('number')) + + # now_time = str(my_json.get('now_time')) + + # now_v = str(my_json.get('now_v')) + + + + + # f = open('./History/' + create_time + '/' + databases[int(index_counter[0])] + '.txt', 'a+') + + # f.write('\n') + + # f.write(now_time) + + # f.write('\t') + + # f.write(number) + + # f.close() + + + # f = open('./History/' + create_time + '/' + databases[int(index_counter[1])] + '.txt', 'a+') + + # f.write('\n') + + # f.write(now_time) + + # f.write('\t') + + # f.write(now_v) + + # f.close() + + + f = open('./History/' + create_time + '/p_' + index_counter[0] + '.txt', 'w') + + f.write(number) + + f.close() + + + # f = open('./History/' + create_time + '/' + propers[int(index_counter[1])] + '.txt', 'w') + + # f.write(now_v) + + # f.close() + + + return jsonify(msg = 1) + + + +@app.route('/L',methods=['POST']) +def L(): + + my_json = request.get_json() + + create_time = str(my_json.get('time')) + + sensor_data = eval(my_json.get('sensor_data')) + + now_time = int(time.time()) + + os.chdir('History') + + work = 0 + + try: + + + os.chdir(create_time) + + + f = open('./History/' + create_time + '/' + 'L_G' + '.txt', 'a+') + + for i in range(100): + + f.write('\n') + + f.write(create_time) + + f.write('\t') + + f.write(str(i)) + + f.write('\t') + + f.write(sensor_data[i]) + + f.close() + + + f = open('./History/' + create_time + '/' + 'L_R' + '.txt', 'a+') + + for i in range(100): + + f.write('\n') + + f.write(create_time) + + f.write('\t') + + f.write(str(i)) + + f.write('\t') + + f.write(sensor_data[i+100]) + + f.close() + + os.chdir(path) + + work = 1 + + except: + + os.chdir(path) + + return jsonify(msg = work) + + + + + + + +@app.route('/F',methods=['POST']) +def F(): + + my_json = request.get_json() + + create_time = str(my_json.get('time')) + + control_data = eval(my_json.get('control_data')) + + sensor_data = eval(my_json.get('sensor_data')) + + now_time = int(time.time()) + + cur_x = -1 + + pump_s = ['Pump1-S', 'Pump2-S', 'Pump3-S', 'Pump4-S', 'Pump5-S', 'Pump6-S', 'Pump7-S', 'Air_S'] + + pump_v = ['Pump1-V', 'Pump2-V', 'Pump3-V', 'Pump4-V', 'Pump5-V', 'Pump6-V', 'Pump7-V', 'Air_V'] + + send_control_data = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] + + + f = open('./History/' + create_time + '/' + 'p_device_1' + '.txt', 'w') + + f.write(str(now_time)) + + f.close() + + + for ii in range(2): + + + # f = open('./History/' + create_time + '/' + propers[int(pump_s[i])] + '.txt', 'r') + + # f = f.readlines() + + # data_get_1 = float(f[0].replace('\n', '')) + + + # f = open('./History/' + create_time + '/' + propers[int(pump_v[i])] + '.txt', 'r') + + # f = f.readlines() + + # data_get_2 = float(f[0].replace('\n', '')) + + + + + f = open('./History/' + create_time + '/' + pump_s[ii] + '.txt', 'r') + + f = f.readlines() + + X = [] + + Y = [] + + for i in f[1::]: + + i = i.replace('\n','').split('\t') + + if float(i[0]) > cur_x: + + X.append(float(i[0])) + + Y.append(float(i[1])) + + data_get1 = [X, Y] + + + f = open('./History/' + create_time + '/' + pump_v[ii] + '.txt', 'r') + + f = f.readlines() + + X = [] + + Y = [] + + for i in f[1::]: + + i = i.replace('\n','').split('\t') + + if float(i[0]) > cur_x: + + X.append(float(i[0])) + + Y.append(float(i[1])) + + data_get2 = [X, Y] + + + + + now_v = (now_time-data_get2[0][-1])/5*data_get1[1][-1]/100 + data_get2[1][-1] + + + + + f = open('./History/' + create_time + '/' + pump_s[ii] + '.txt', 'a+') + + f.write('\n') + + f.write(str(now_time-5)) + + f.write('\t') + + f.write(str(control_data[ii])) + + f.close() + + + f = open('./History/' + create_time + '/' + pump_v[ii] + '.txt', 'a+') + + f.write('\n') + + f.write(str(now_time-5)) + + f.write('\t') + + f.write(str(now_v)) + + f.close() + + + f = open('./History/' + create_time + '/p_' + pump_s[ii] + '.txt', 'w') + + f.write(str(control_data[ii])) + + f.close() + + + f = open('./History/' + create_time + '/p_' + pump_v[ii] + '.txt', 'w') + + f.write(str(now_v)) + + f.close() + + send_control_data[ii] = control_data[ii] + + + + + for ii in range(2,7): + + + f = open('./History/' + create_time + '/p_' + pump_s[ii] + '.txt', 'r') + + f = f.readlines() + + data_get_1 = float(f[0].replace('\n', '')) + + + # f = open('./History/' + create_time + '/' + propers[int(pump_v[i])] + '.txt', 'r') + + # f = f.readlines() + + # data_get_2 = float(f[0].replace('\n', '')) + + + + + f = open('./History/' + create_time + '/' + pump_s[ii] + '.txt', 'r') + + f = f.readlines() + + X = [] + + Y = [] + + for i in f[1::]: + + i = i.replace('\n','').split('\t') + + if float(i[0]) > cur_x: + + X.append(float(i[0])) + + Y.append(float(i[1])) + + data_get1 = [X, Y] + + + + f = open('./History/' + create_time + '/' + pump_v[ii] + '.txt', 'r') + + f = f.readlines() + + X = [] + + Y = [] + + for i in f[1::]: + + i = i.replace('\n','').split('\t') + + if float(i[0]) > cur_x: + + X.append(float(i[0])) + + Y.append(float(i[1])) + + data_get2 = [X, Y] + + + + + now_v = (now_time-data_get2[0][-1])/5*data_get1[1][-1]/100 + data_get2[1][-1] + + + + + f = open('./History/' + create_time + '/' + pump_s[ii] + '.txt', 'a+') + + f.write('\n') + + f.write(str(now_time)) + + f.write('\t') + + f.write(str(data_get_1)) + + f.close() + + + f = open('./History/' + create_time + '/' + pump_v[ii] + '.txt', 'a+') + + f.write('\n') + + f.write(str(now_time)) + + f.write('\t') + + f.write(str(now_v)) + + f.close() + + + f = open('./History/' + create_time + '/p_' + pump_s[ii] + '.txt', 'w') + + f.write(str(data_get_1)) + + f.close() + + + f = open('./History/' + create_time + '/p_' + pump_v[ii] + '.txt', 'w') + + f.write(str(now_v)) + + f.close() + + send_control_data[ii] = data_get_1 + + + + + send_control_data[7] = control_data[7] + + + f = open('./History/' + create_time + '/' + 'g_pH' + '.txt', 'r') + + f = f.readlines() + + data_get_1 = float(f[0].replace('\n', '')) + + send_control_data[8] = data_get_1 + + + f = open('./History/' + create_time + '/' + 'g_Temperature' + '.txt', 'r') + + f = f.readlines() + + data_get_1 = float(f[0].replace('\n', '')) + + send_control_data[9] = data_get_1 + + + + for ii in range(7,8): + + #Air + + + f = open('./History/' + create_time + '/p_' + pump_s[ii] + '.txt', 'r') + + f = f.readlines() + + data_get_1 = float(f[0].replace('\n', '')) + + + # f = open('./History/' + create_time + '/' + propers[int(pump_v[i])] + '.txt', 'r') + + # f = f.readlines() + + # data_get_2 = float(f[0].replace('\n', '')) + + + + + f = open('./History/' + create_time + '/' + pump_s[ii] + '.txt', 'r') + + f = f.readlines() + + X = [] + + Y = [] + + for i in f[1::]: + + i = i.replace('\n','').split('\t') + + if float(i[0]) > cur_x: + + X.append(float(i[0])) + + Y.append(float(i[1])) + + data_get1 = [X, Y] + + + + f = open('./History/' + create_time + '/' + pump_v[ii] + '.txt', 'r') + + f = f.readlines() + + X = [] + + Y = [] + + for i in f[1::]: + + i = i.replace('\n','').split('\t') + + if float(i[0]) > cur_x: + + X.append(float(i[0])) + + Y.append(float(i[1])) + + data_get2 = [X, Y] + + + + + now_v = (now_time-data_get2[0][-1])/5*data_get1[1][-1]/100 + data_get2[1][-1] + + + + + f = open('./History/' + create_time + '/' + pump_s[ii] + '.txt', 'a+') + + f.write('\n') + + f.write(str(now_time)) + + f.write('\t') + + f.write(str(data_get_1)) + + f.close() + + + f = open('./History/' + create_time + '/' + pump_v[ii] + '.txt', 'a+') + + f.write('\n') + + f.write(str(now_time)) + + f.write('\t') + + f.write(str(now_v)) + + f.close() + + + f = open('./History/' + create_time + '/p_' + pump_s[ii] + '.txt', 'w') + + f.write(str(data_get_1)) + + f.close() + + + f = open('./History/' + create_time + '/p_' + pump_v[ii] + '.txt', 'w') + + f.write(str(now_v)) + + f.close() + + send_control_data[ii+3] = data_get_1 + + + + + f = open('./History/' + create_time + '/' + 'pH' + '.txt', 'a+') + + f.write('\n') + + f.write(str(now_time-5)) + + f.write('\t') + + f.write(str(sensor_data[0])) + + f.close() + + + f = open('./History/' + create_time + '/' + 'Temperature' + '.txt', 'a+') + + f.write('\n') + + f.write(str(now_time-5)) + + f.write('\t') + + f.write(str(sensor_data[1])) + + f.close() + + + f = open('./History/' + create_time + '/' + 'p_pH' + '.txt', 'w') + + f.write(str(sensor_data[0])) + + f.close() + + + f = open('./History/' + create_time + '/' + 'p_Temperature' + '.txt', 'w') + + f.write(str(sensor_data[1])) + + f.close() + + + return jsonify(send_control_data = send_control_data) + + + +app.run(host='0.0.0.0', debug = False ,port=2000) + -- GitLab