DmitrMakeev commited on
Commit
7c2533c
·
verified ·
1 Parent(s): ba69c34

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -38
app.py CHANGED
@@ -982,65 +982,97 @@ if __name__ == "__main__":
982
  @app.route('/calculation', methods=['POST'])
983
  def handle_calculation():
984
  try:
985
- # Получаем данные как есть
986
  data = request.json
987
-
988
- # Создаем калькулятор
989
- calculator = NutrientCalculator(volume_liters=data['profileSettings']['liters'])
990
-
991
- # Устанавливаем целевой профиль из полученных данных
992
- calculator.target_profile = {
993
- 'P': float(data['profileSettings']['P']),
994
- 'K': float(data['profileSettings']['K']),
995
- 'Mg': float(data['profileSettings']['Mg']),
996
- 'Ca': float(data['profileSettings']['Ca']),
997
- 'S': float(data['profileSettings']['S']),
998
- 'N (NO3-)': float(data['profileSettings']['NO3']),
999
- 'N (NH4+)': float(data['profileSettings']['NH4'])
1000
- }
1001
-
1002
- # Конвертируем удобрения в нужный формат
1003
- calculator.fertilizers = {
1004
  "Кальциевая селитра": {
1005
- "N (NO3-)": data['fertilizerConstants']['CaN2O6']['NO3'] / 100,
1006
- "Ca": data['fertilizerConstants']['CaN2O6']['Ca'] / 100
1007
  },
1008
  "Калий азотнокислый": {
1009
- "N (NO3-)": data['fertilizerConstants']['KNO3']['NO3'] / 100,
1010
- "K": data['fertilizerConstants']['KNO3']['K'] / 100
1011
  },
1012
  "Аммоний азотнокислый": {
1013
- "N (NO3-)": data['fertilizerConstants']['NH4NO3']['NO3'] / 100,
1014
- "N (NH4+)": data['fertilizerConstants']['NH4NO3']['NH4'] / 100
1015
  },
1016
  "Сульфат магния": {
1017
- "Mg": data['fertilizerConstants']['MgSO4']['Mg'] / 100,
1018
- "S": data['fertilizerConstants']['MgSO4']['S'] / 100
1019
  },
1020
  "Монофосфат калия": {
1021
- "P": data['fertilizerConstants']['KH2PO4']['P'] / 100,
1022
- "K": data['fertilizerConstants']['KH2PO4']['K'] / 100
1023
  },
1024
  "Калий сернокислый": {
1025
- "K": data['fertilizerConstants']['K2SO4']['K'] / 100,
1026
- "S": data['fertilizerConstants']['K2SO4']['S'] / 100
1027
  }
1028
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1029
 
1030
  # Выполняем расчет
1031
  calculator.calculate()
1032
 
1033
- # Возвращаем результаты
1034
- return jsonify({
1035
- "fertilizers": calculator._format_fertilizers(),
1036
- "profile": calculator._format_profile(),
1037
  "ec": calculator.calculate_ec(),
1038
  "deficits": calculator.calculate_deficits()
1039
- })
1040
-
1041
- except Exception as e:
1042
- return jsonify({"error": str(e)}), 500
1043
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1044
 
1045
 
1046
 
 
982
  @app.route('/calculation', methods=['POST'])
983
  def handle_calculation():
984
  try:
 
985
  data = request.json
986
+ print("Полученные данные:", data) # Логирование входящих данных
987
+
988
+ # Проверка обязательных полей
989
+ if not data or 'fertilizerConstants' not in data or 'profileSettings' not in data:
990
+ return jsonify({"error": "Не хватает обязательных данных"}), 400
991
+
992
+ # Конвертация удобрений
993
+ fertilizers = {
 
 
 
 
 
 
 
 
 
994
  "Кальциевая селитра": {
995
+ "N (NO3-)": float(data['fertilizerConstants']['Кальциевая селитра']["N (NO3-)"]),
996
+ "Ca": float(data['fertilizerConstants']['Кальциевая селитра']["Ca"])
997
  },
998
  "Калий азотнокислый": {
999
+ "N (NO3-)": float(data['fertilizerConstants']['Калий азотнокислый']["N (NO3-)"]),
1000
+ "K": float(data['fertilizerConstants']['Калий азотнокислый']["K"])
1001
  },
1002
  "Аммоний азотнокислый": {
1003
+ "N (NO3-)": float(data['fertilizerConstants']['Аммоний азотнокислый']["N (NO3-)"]),
1004
+ "N (NH4+)": float(data['fertilizerConstants']['Аммоний азотнокислый']["N (NH4+)"])
1005
  },
1006
  "Сульфат магния": {
1007
+ "Mg": float(data['fertilizerConstants']['Сульфат магния']["Mg"]),
1008
+ "S": float(data['fertilizerConstants']['Сульфат магния']["S"])
1009
  },
1010
  "Монофосфат калия": {
1011
+ "P": float(data['fertilizerConstants']['Монофосфат калия']["P"]),
1012
+ "K": float(data['fertilizerConstants']['Монофосфат калия']["K"])
1013
  },
1014
  "Калий сернокислый": {
1015
+ "K": float(data['fertilizerConstants']['Калий сернокислый']["K"]),
1016
+ "S": float(data['fertilizerConstants']['Калий сернокислый']["S"])
1017
  }
1018
  }
1019
+
1020
+ # Настройки профиля
1021
+ profile = {
1022
+ 'P': float(data['profileSettings']['P']),
1023
+ 'K': float(data['profileSettings']['K']),
1024
+ 'Mg': float(data['profileSettings']['Mg']),
1025
+ 'Ca': float(data['profileSettings']['Ca']),
1026
+ 'S': float(data['profileSettings']['S']),
1027
+ 'N (NO3-)': float(data['profileSettings']['N (NO3-)']),
1028
+ 'N (NH4+)': float(data['profileSettings']['N (NH4+)']),
1029
+ }
1030
+ liters = int(data['profileSettings']['liters'])
1031
+
1032
+ # Создаем и настраиваем калькулятор
1033
+ calculator = NutrientCalculator(volume_liters=liters)
1034
+ calculator.fertilizers = fertilizers
1035
+ calculator.target_profile.update(profile)
1036
 
1037
  # Выполняем расчет
1038
  calculator.calculate()
1039
 
1040
+ # Формируем ответ
1041
+ response = {
1042
+ "fertilizers": [],
1043
+ "profile": [],
1044
  "ec": calculator.calculate_ec(),
1045
  "deficits": calculator.calculate_deficits()
1046
+ }
 
 
 
1047
 
1048
+ # Заполняем удобрения
1049
+ for fert_name, result in calculator.results.items():
1050
+ response["fertilizers"].append({
1051
+ "name": fert_name,
1052
+ "grams": round(result['граммы'], 3),
1053
+ "adds": {k.replace('внесет ', ''): v
1054
+ for k, v in result.items()
1055
+ if k.startswith('внесет')}
1056
+ })
1057
+
1058
+ # Заполняем профиль
1059
+ for element, value in calculator.actual_profile.items():
1060
+ response["profile"].append({
1061
+ "element": element.replace(' (NO3-)', '-NO3').replace(' (NH4+)', '-NH4'),
1062
+ "ppm": round(value, 1)
1063
+ })
1064
+
1065
+ print("Результат расчета:", response) # Логирование результата
1066
+ return jsonify(response)
1067
+
1068
+ except KeyError as e:
1069
+ error_msg = f"Отсутствует обязательное поле: {str(e)}"
1070
+ print(error_msg)
1071
+ return jsonify({"error": error_msg}), 400
1072
+ except Exception as e:
1073
+ error_msg = f"Ошибка расчета: {str(e)}"
1074
+ print(error_msg)
1075
+ return jsonify({"error": error_msg}), 500
1076
 
1077
 
1078