DmitrMakeev commited on
Commit
31a7d72
·
verified ·
1 Parent(s): 738b34b

Update app.py

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