Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -983,47 +983,47 @@ if __name__ == "__main__":
|
|
983 |
@app.route('/calculation', methods=['POST'])
|
984 |
def handle_calculation():
|
985 |
try:
|
986 |
-
#
|
987 |
data = request.get_json()
|
988 |
-
|
989 |
-
|
990 |
-
|
991 |
-
|
992 |
-
|
993 |
-
|
994 |
-
|
995 |
-
|
996 |
-
|
997 |
-
|
998 |
-
|
999 |
-
|
1000 |
-
|
1001 |
-
|
1002 |
-
|
1003 |
-
|
1004 |
-
|
1005 |
-
|
1006 |
-
|
1007 |
-
|
1008 |
-
|
1009 |
-
|
1010 |
-
}
|
1011 |
-
except (ValueError, KeyError) as e:
|
1012 |
-
return jsonify({"error": f"Invalid data format: {str(e)}"}), 400
|
1013 |
-
|
1014 |
-
# 4. Расчет
|
1015 |
-
calculator = NutrientCalculator(volume_liters=server_data['profileSettings']['liters'])
|
1016 |
-
calculator.target_profile.update(server_data['profileSettings'])
|
1017 |
-
calculator.fertilizers = server_data['fertilizerConstants']
|
1018 |
calculator.calculate()
|
1019 |
|
1020 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1021 |
|
1022 |
except Exception as e:
|
1023 |
-
# Логируем ошибку для диагностики
|
1024 |
-
print(f"Calculation error: {str(e)}")
|
1025 |
return jsonify({
|
1026 |
-
"error": "
|
1027 |
"details": str(e)
|
1028 |
}), 500
|
1029 |
|
|
|
983 |
@app.route('/calculation', methods=['POST'])
|
984 |
def handle_calculation():
|
985 |
try:
|
986 |
+
# Получаем данные запроса
|
987 |
data = request.get_json()
|
988 |
+
|
989 |
+
# Проверяем наличие необходимых данных
|
990 |
+
if not data or 'profileSettings' not in data:
|
991 |
+
return jsonify({"error": "Invalid request data"}), 400
|
992 |
+
|
993 |
+
# Получаем значения из запроса
|
994 |
+
profile_settings = data['profileSettings']
|
995 |
+
no3_ratio = float(profile_settings.get('NO3_RATIO', NO3_RATIO))
|
996 |
+
nh4_ratio = float(profile_settings.get('NH4_RATIO', NH4_RATIO))
|
997 |
+
total_nitrogen = float(profile_settings.get('TOTAL_NITROGEN', TOTAL_NITROGEN))
|
998 |
+
liters = int(profile_settings.get('liters', VOLUME_LITERS))
|
999 |
+
|
1000 |
+
# Создаем калькулятор
|
1001 |
+
calculator = NutrientCalculator(volume_liters=liters)
|
1002 |
+
|
1003 |
+
# Обновляем целевый профиль с учетом новых значений
|
1004 |
+
calculator.target_profile.update({
|
1005 |
+
'N (NO3-)': total_nitrogen * (no3_ratio / (no3_ratio + nh4_ratio)),
|
1006 |
+
'N (NH4+)': total_nitrogen * (nh4_ratio / (no3_ratio + nh4_ratio))
|
1007 |
+
})
|
1008 |
+
|
1009 |
+
# Выполняем расчет
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1010 |
calculator.calculate()
|
1011 |
|
1012 |
+
# Возвращаем результаты
|
1013 |
+
return jsonify({
|
1014 |
+
"fertilizers": calculator._format_fertilizers(),
|
1015 |
+
"profile": calculator._format_profile(),
|
1016 |
+
"ec": calculator.calculate_ec(),
|
1017 |
+
"deficits": calculator.calculate_deficits(),
|
1018 |
+
"used_ratios": {
|
1019 |
+
"NO3_RATIO": no3_ratio,
|
1020 |
+
"NH4_RATIO": nh4_ratio
|
1021 |
+
}
|
1022 |
+
})
|
1023 |
|
1024 |
except Exception as e:
|
|
|
|
|
1025 |
return jsonify({
|
1026 |
+
"error": "Calculation failed",
|
1027 |
"details": str(e)
|
1028 |
}), 500
|
1029 |
|