DmitrMakeev commited on
Commit
fb78679
·
verified ·
1 Parent(s): 073cced

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -35
app.py CHANGED
@@ -983,47 +983,47 @@ if __name__ == "__main__":
983
  @app.route('/calculation', methods=['POST'])
984
  def handle_calculation():
985
  try:
986
- # 1. Жесткая проверка данных
987
  data = request.get_json()
988
- if not data:
989
- return jsonify({"error": "No JSON data"}), 400
990
-
991
- # 2. Валидация структуры
992
- required = {'fertilizerConstants', 'profileSettings'}
993
- if not required.issubset(data.keys()):
994
- return jsonify({"error": f"Required fields: {required}"}), 400
995
-
996
- # 3. Конвертация данных
997
- try:
998
- server_data = {
999
- 'fertilizerConstants': convert_client_data(data['fertilizerConstants']),
1000
- 'profileSettings': {
1001
- 'P': float(data['profileSettings']['P']),
1002
- 'K': float(data['profileSettings']['K']),
1003
- 'Mg': float(data['profileSettings']['Mg']),
1004
- 'Ca': float(data['profileSettings']['Ca']),
1005
- 'S': float(data['profileSettings']['S']),
1006
- 'N (NO3-)': float(data['profileSettings']['N (NO3-)']),
1007
- 'N (NH4+)': float(data['profileSettings']['N (NH4+)']),
1008
- 'liters': int(data['profileSettings']['liters'])
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
- return jsonify(calculator.get_web_results())
 
 
 
 
 
 
 
 
 
 
1021
 
1022
  except Exception as e:
1023
- # Логируем ошибку для диагностики
1024
- print(f"Calculation error: {str(e)}")
1025
  return jsonify({
1026
- "error": "Internal server 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