DmitrMakeev commited on
Commit
dbc6aa8
·
verified ·
1 Parent(s): 55e0a69

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -56
app.py CHANGED
@@ -859,67 +859,51 @@ print(calculator.generate_report(results))
859
 
860
  @app.route('/calculation', methods=['POST'])
861
  def handle_calculation():
862
- # 1. Выводим в консоль ВСЕ полученные данные
863
- print("\n=== ВХОДЯЩИЕ ДАННЫЕ ===")
864
  try:
865
- raw_data = request.get_data(as_text=True)
866
- print(raw_data)
867
- data = json.loads(raw_data)
868
- except Exception as e:
869
- print(f"Ошибка парсинга JSON: {str(e)}")
870
- data = {}
871
-
872
- # 2. Дефолтный ответ
873
- response = {
874
- "actual_profile": {
875
- "P": 0, "K": 0, "Mg": 0, "Ca": 0, "S": 0,
876
- "N (NO3-)": 0, "N (NH4+)": 0
877
- },
878
- "fertilizers": {},
879
- "nitrogen_ratios": {
880
- "NH4_RATIO": 0,
881
- "NO3_RATIO": 0,
882
- "TOTAL_NITROGEN": 0
883
- },
884
- "total_ppm": 0,
885
- "deficits": {},
886
- "error": None,
887
- "received_data": data # Возвращаем обратно полученные данные
888
- }
889
-
890
- # 3. Примитивный "расчет" без проверок
891
- try:
892
- if data:
893
- # Просто копируем часть данных в ответ
894
- response["actual_profile"].update({
895
- "P": data.get("profileSettings", {}).get("P", 0),
896
- "K": data.get("profileSettings", {}).get("K", 0),
897
- "Mg": data.get("profileSettings", {}).get("Mg", 0),
898
- "Ca": data.get("profileSettings", {}).get("Ca", 0),
899
- "S": data.get("profileSettings", {}).get("S", 0)
900
- })
901
-
902
- # Фиктивные расчеты азота
903
- total_n = data.get("profileSettings", {}).get("TOTAL_NITROG", 0) or 0
904
- no3_ratio = data.get("profileSettings", {}).get("NO3_RAT", 0) or 0
905
- response["nitrogen_ratios"] = {
906
  "NH4_RATIO": 1,
907
- "NO3_RATIO": no3_ratio,
908
- "TOTAL_NITROGEN": total_n
909
  }
910
-
911
- # Фиктивные удобрения
912
- if "fertilizerConstants" in data:
913
- response["fertilizers"] = {
914
- name: {"граммы": round(required_ppm / content, 3)}
915
- for name, content in data["fertilizerConstants"].items()
916
- }
917
-
918
  except Exception as e:
919
- response["error"] = f"Calculation error: {str(e)}"
 
 
 
 
 
 
 
 
920
 
921
- # 4. Всегда возвращаем 200 OK
922
- return jsonify(response), 200
923
 
924
 
925
 
 
859
 
860
  @app.route('/calculation', methods=['POST'])
861
  def handle_calculation():
 
 
862
  try:
863
+ # 1. Получаем и парсим данные
864
+ data = request.get_json()
865
+ if not data:
866
+ return jsonify({"error": "No JSON data received"}), 400
867
+
868
+ # 2. Логируем входящие данные (для отладки)
869
+ print("\n=== ВХОДЯЩИЕ ДАННЫЕ ===")
870
+ print(json.dumps(data, indent=2, ensure_ascii=False))
871
+
872
+ # 3. Создаем и запускаем калькулятор
873
+ calculator = NutrientCalculator(data)
874
+ results = calculator.calculate()
875
+
876
+ # 4. Формируем полный ответ
877
+ response = {
878
+ "fertilizers": results['fertilizers'],
879
+ "actual_profile": results['actual_profile'],
880
+ "deficits": results['deficits'],
881
+ "total_ppm": results['total_ppm'],
882
+ "error": None,
883
+ "nitrogen_ratios": {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
884
  "NH4_RATIO": 1,
885
+ "NO3_RATIO": data.get("profileSettings", {}).get("NO3_RAT", 0),
886
+ "TOTAL_NITROGEN": data.get("profileSettings", {}).get("TOTAL_NITROG", 0)
887
  }
888
+ }
889
+
890
+ # 5. Логируем результаты (для отладки)
891
+ print("\n=== РЕЗУЛЬТАТЫ ===")
892
+ print(calculator.generate_report(results))
893
+
894
+ return jsonify(response), 200
895
+
896
  except Exception as e:
897
+ error_msg = f"Server error: {str(e)}"
898
+ print(error_msg)
899
+ return jsonify({
900
+ "error": error_msg,
901
+ "fertilizers": {},
902
+ "actual_profile": {},
903
+ "deficits": {},
904
+ "total_ppm": 0
905
+ }), 500
906
 
 
 
907
 
908
 
909