Advertisement
Guest User

Untitled

a guest
Dec 18th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.58 KB | None | 0 0
  1. import pickle
  2. import os.path
  3.  
  4. def compare_dicts(first, second):
  5. if set(first.keys()) == set(second.keys()):
  6. for k in first:
  7. if first[k] != second[k]:
  8. return False
  9. return True
  10. else:
  11. return False
  12.  
  13.  
  14. class IrrigationProgram:
  15. def __init__(self):
  16. self.program = 0
  17. self.units = 0
  18. self.water_before_1 = 0
  19. self.water_before_2 = 0
  20. self.water_after_1 = 0
  21. self.water_after_2 = 0
  22. self.water_total_1 = 0
  23. self.water_total_2 = 0
  24. self.kicks = 0
  25. self.fertilization_program = 0
  26. self.condition_program = 0
  27. self.time_start_1 = 0
  28. self.time_start_2 = 0
  29. self.time_between_1 = 0
  30. self.time_between_2 = 0
  31. self.days = "0,0,0,0,0,0,0"
  32. self.valves = ""
  33.  
  34. def __eq__(self, other):
  35. return str(self.__dict__) == str(other.__dict__)
  36. def iszero(self):
  37. blank = IrrigationProgram()
  38. blank.program = self.program
  39. return self == blank
  40.  
  41.  
  42. class FertilizationProgram:
  43. def __init__(self):
  44. self.program = 0
  45. self.values_1 = 0
  46. self.values_2 = 0
  47. self.values_3 = 0
  48. self.values_4 = 0
  49. self.values_5 = 0
  50. self.values_6 = 0
  51. self.values_7 = 0
  52. self.values_8 = 0
  53. self.ec = 0.0
  54. self.ph = 0.0
  55.  
  56. def __eq__(self, other):
  57. return str(self.__dict__) == str(other.__dict__)
  58. def iszero(self):
  59. blank = FertilizationProgram()
  60. blank.program = self.program
  61. return self == blank
  62.  
  63.  
  64. class Alarm:
  65. def __init__(self):
  66. self.fertilization_deficit = ""
  67. self.fertilization_desc = ""
  68. self.ec_error = 0
  69. self.ph_error = 0
  70. self.ec_danger = 0
  71. self.ph_danger = 0
  72. self.low_pressure = 0
  73. self.high_pressure = 0
  74. self.flow_error = 0
  75. self.no_water = 0
  76. self.dangerous_flow = 0
  77. self.irrigation_out_of_controls = 0
  78.  
  79. def __eq__(self, other):
  80. return str(self.__dict__) == str(other.__dict__)
  81.  
  82.  
  83. class InyectionProgram:
  84. def __init__(self):
  85. self.program = 0
  86. self.flow = 0
  87. self.function = 0
  88. self.time_on = 0
  89. self.litres_pulse = 0
  90. self.max_deviation = 0
  91. self.simulator = 0
  92.  
  93. def __eq__(self, other):
  94. return str(self.__dict__) == str(other.__dict__)
  95. def iszero(self):
  96. blank = InyectionProgram()
  97. blank.program = self.program
  98. return self == blank
  99.  
  100.  
  101. class AlarmConfig:
  102. def __init__(self):
  103. self.delay_alarm_flow_secs = 5
  104. self.deviation_warning_max_error_flow = 0
  105. self.function_alarm_no_water = 0
  106. self.over_dangerous_flow_percentage = 0
  107. self.function_alarm_dangerous_flow = 0
  108. self.max_seconds_between_water_pulses = 0
  109. self.delay_alarm_low_pressure_secs = 0
  110. self.level_alarm_low_pressure_kg = 0.0
  111. self.delay_alarm_high_pressure_kg = 0
  112. self.level_alarm_high_pressure_kg = 0.0
  113. self.function_alarm_high_pressure = 0
  114. self.function_alarm_no_fertilization = 0
  115. self.pulses_needs_fertilizer = 0
  116. self.function_alarm_fertilizer_discontinued = 0
  117. self.pulses_fertilizer_no_control = 0
  118. self.max_diff_warning_error_ec = 0.0
  119. self.max_diff_warning_error_ph = 0.0
  120. self.delay_alarms_ec_ph_secs = 0
  121. self.function_alarm_ec_ph_dangerous = 0
  122. self.max_deviation_over_ec = 0.0
  123. self.max_deviation_under_ph = 0.0
  124. self.delay_alarm_ec_dangerous_secs = 0
  125. self.delay_alarm_ph_dangerous_secs = 0
  126. self.delay_secs_if_diff_ec_more_1 = 0
  127. self.delay_secs_if_diff_ec_more_05 = 0
  128. self.delay_secs_if_diff_ec_more_03 = 0
  129. self.coefficient_correction_ec_more_1 = 0
  130. self.coefficient_correction_ec_more_05 = 0
  131. self.coefficient_correction_ec_more_03 = 0
  132. self.secs_first_ec_correction = 0
  133. self.delay_secs_if_diff_ph_more_1 = 0
  134. self.delay_secs_if_diff_ph_more_05 = 0
  135. self.delay_secs_if_diff_ph_more_03 = 0
  136. self.coefficient_correction_ph_more_1 = 0
  137. self.coefficient_correction_ph_more_05 = 0
  138. self.coefficient_correction_ph_more_03 = 0
  139. self.secs_first_ph_correction = 0
  140.  
  141. def __eq__(self, other):
  142. return str(self.__dict__) == str(other.__dict__)
  143. def iszero(self):
  144. blank = AlarmConfig()
  145. return self == blank
  146.  
  147.  
  148. class IOConfig():
  149. def __init__(self):
  150. self.inyection = "0,0,0,0,0,0,0,0"
  151. self.filters = "0,0,0,0,0,0,0,0"
  152. self.actuators = "0,0,0,0,0,0,0,0"
  153. self.analog_input_1 = "0,0,0,0"
  154. self.analog_input_2 = "0,0,0,0"
  155. self.analog_input_3 = "0,0,0,0"
  156. self.analog_input_4 = "0,0,0,0"
  157. self.analog_input_5 = "0,0,0,0"
  158. self.valves1to16 = "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0"
  159. self.valves17to32 = "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0"
  160. self.valves33to48 = "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0"
  161. self.valves49to64 = "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0"
  162. self.valves65to80 = "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0"
  163.  
  164. def __eq__(self, other):
  165. return str(self.__dict__) == str(other.__dict__)
  166. def iszero(self):
  167. blank = IOConfig()
  168. return self == blank
  169.  
  170.  
  171. class BackFlushing():
  172. def __init__(self):
  173. self.difference_backflush_kg = 0
  174. self.time_between_flushing_hours = 0
  175. self.time_between_flushing_minutes = 0
  176. self.time_between_station_min = 0
  177. self.time_between_station_sec = 0
  178. self.pause_between_filtering_secs = 0
  179. self.amount_of_filters = 0
  180. self.delay_differential_pressure = 0
  181. self.wait_after_sustain = 0
  182. self.times_wash_before_pd_alarm = 0
  183. def __eq__(self, other):
  184. return str(self.__dict__) == str(other.__dict__)
  185. def iszero(self):
  186. blank = BackFlushing()
  187. return self == blank
  188.  
  189.  
  190. class ValveSolape():
  191. def __init__(self):
  192. self.solape = ""
  193. self.time = ""
  194. self.flow = ""
  195.  
  196. def __eq__(self, other):
  197. return str(self.__dict__) == str(other.__dict__)
  198. def iszero(self):
  199. blank = ValveSolape()
  200. return self == blank
  201.  
  202.  
  203. class OtherConfigs():
  204. def __init__(self):
  205. self.booster_pump = 0
  206. self.flow_meter_1 = 0
  207. self.flow_meter_2 = 0
  208. self.time_restart_program_1 = 0
  209. self.time_restart_program_2 = 0
  210. self.blower_1 = 0
  211. self.blower_2 = 0
  212. self.toff_inyector = 0
  213. self.manual_irrigation_units = 0
  214. self.manual_irrigation_water_1 = 0
  215. self.manual_irrigation_water_2 = 0
  216. self.manual_irrigation_program = 0
  217.  
  218. def __eq__(self, other):
  219. return str(self.__dict__) == str(other.__dict__)
  220. def iszero(self):
  221. blank = OtherConfigs()
  222. return self == blank
  223.  
  224. class InyectorStats:
  225. def __init__(self):
  226. self.inyector = 0
  227. self.prop_required = 0
  228. self.prop_required_ec_ph = 0
  229. self.required_volume = 0
  230. self.required_flow = 0
  231. self.total_inyected = 0
  232.  
  233. class TerminalStats:
  234. def __init__(self):
  235. self.irrigation_program = 0
  236. self.fertilization_program = 0
  237. self.next_irrigation_program = 0
  238. self.irrigation_1 = 0
  239. self.irrigation_2 = 0
  240. self.preirrigation_1 = 0
  241. self.preirrigation_2 = 0
  242. self.postirrigation_1 = 0
  243. self.postirrigation_2 = 0
  244. self.units = 0
  245. self.irrigation_time_1 = 0
  246. self.irrigation_time_2 = 0
  247. self.irrigation_time_3 = 0
  248. self.cubic_meters = 0.0
  249. self.irrigation_before_suspend_1 = 0
  250. self.irrigation_before_suspend_2 = 0
  251. self.flow_measured_m3h = 0.0
  252. self.p1 = 0.0
  253. self.p2 = 0.0
  254. self.c_prog = 0
  255. self.ec_asked = 0.0
  256. self.ph_asked = 0.0
  257. self.ec_average = 0.0
  258. self.ph_average = 0.0
  259. self.ec_measured = 0.0
  260. self.ph_measured = 0.0
  261. self.valves = [0] * 80
  262. self.inyectors = []
  263.  
  264. class ControllerState:
  265. def __init__(self):
  266. self.last_update = 0
  267. self.allIrrigation = dict()
  268. self.allFertilization = dict()
  269. self.allInyection = dict()
  270. self.username = None
  271. self.password = None
  272. self.alarm_config = AlarmConfig()
  273. self.alarm = Alarm()
  274. self.config_io = IOConfig()
  275. self.solape = ValveSolape()
  276. self.back_flushing = BackFlushing()
  277. self.other = OtherConfigs()
  278.  
  279. @staticmethod
  280. def load_from_file(path):
  281. if os.path.isfile(path):
  282. with open(path, 'rb') as in_file:
  283. return (pickle.load(in_file))
  284. return ControllerState()
  285.  
  286. def load_from_json(self, json):
  287. self.last_update = int(json['update'])
  288. for fert in json["fertilizer_prog"]:
  289. fertProg = FertilizationProgram()
  290. fertProg.program = fert["program"]
  291. fertProg.ec = fert["ec"]
  292. fertProg.ph = fert["ph"]
  293. fertProg.values_1 = fert["value_1"]
  294. fertProg.values_2 = fert["value_2"]
  295. fertProg.values_3 = fert["value_3"]
  296. fertProg.values_4 = fert["value_4"]
  297. fertProg.values_5 = fert["value_5"]
  298. fertProg.values_6 = fert["value_6"]
  299. fertProg.values_7 = fert["value_7"]
  300. fertProg.values_8 = fert["value_8"]
  301. self.allFertilization[fertProg.program] = fertProg
  302. for irr in json["irrigation_prog"]:
  303. irrProg = IrrigationProgram()
  304. irrProg.program = irr["program"]
  305. irrProg.units = irr["units"]
  306. irrProg.water_before_1 = irr["water_before_1"]
  307. irrProg.water_before_2 = irr["water_before_2"]
  308. irrProg.water_after_1 = irr["water_after_1"]
  309. irrProg.water_after_2 = irr["water_after_2"]
  310. irrProg.water_total_1 = irr["water_total_1"]
  311. irrProg.water_total_2 = irr["water_total_2"]
  312. irrProg.kicks = irr["kicks"]
  313. irrProg.fertilization_program = irr["fertilization_program"]
  314. irrProg.condition_program = irr["condition_program"]
  315. irrProg.time_start_1 = irr["time_start_1"]
  316. irrProg.time_start_2 = irr["time_start_2"]
  317. irrProg.time_between_1 = irr["time_between_1"]
  318. irrProg.time_between_2 = irr["time_between_2"]
  319. irrProg.valves = irr["valves"]
  320. irrProg.days = irr["days"]
  321. self.allIrrigation[irrProg.program] = irrProg
  322. # TODO valves
  323. for iny in json["inyection_prog"]:
  324. inyProg = InyectionProgram()
  325. inyProg.program = iny["program"]
  326. inyProg.flow = iny["flow"]
  327. inyProg.function = iny["function"]
  328. inyProg.time_on = iny["time_on"]
  329. inyProg.litres_pulse = iny["litres_pulse"]
  330. inyProg.max_deviation = iny["max_deviation"]
  331. inyProg.simulator = iny["simulator"]
  332. self.allInyection[inyProg.program] = inyProg
  333. if "io_config" in json:
  334. json_config_io = json["io_config"]
  335. config_io = IOConfig()
  336. config_io.inyection = json_config_io["inyection"]
  337. config_io.filters = json_config_io["filters"]
  338. config_io.actuators = json_config_io["actuators"]
  339. config_io.valves1to16 = json_config_io["valves1to16"]
  340. config_io.valves17to32 = json_config_io["valves17to32"]
  341. config_io.valves33to48 = json_config_io["valves33to48"]
  342. config_io.valves49to64 = json_config_io["valves49to64"]
  343. config_io.valves65to80 = json_config_io["valves65to80"]
  344. config_io.analog_input_1 = json_config_io["analog_input_1"]
  345. config_io.analog_input_2 = json_config_io["analog_input_2"]
  346. config_io.analog_input_3 = json_config_io["analog_input_3"]
  347. config_io.analog_input_4 = json_config_io["analog_input_4"]
  348. config_io.analog_input_5 = json_config_io["analog_input_5"]
  349. self.config_io = config_io
  350. if "solape_config" in json:
  351. solape_json = json["solape_config"]
  352. solape = ValveSolape()
  353. solape.time = solape_json["time"]
  354. solape.flow = solape_json["flow"]
  355. solape.solape = solape_json["solape"]
  356. self.solape = solape
  357. if "other_config" in json:
  358. other_json = json["other_config"]
  359. new_other = OtherConfigs()
  360. new_other.booster_pump = other_json["booster_pump"]
  361. new_other.flow_meter_1 = other_json["flow_meter_1"]
  362. new_other.flow_meter_2 = other_json["flow_meter_2"]
  363. new_other.time_restart_program_1 = other_json["time_restart_program_1"]
  364. new_other.time_restart_program_2 = other_json["time_restart_program_2"]
  365. new_other.blower_1 = other_json["blower_1"]
  366. new_other.blower_2 = other_json["blower_2"]
  367. new_other.toff_inyector = other_json["toff_inyector"]
  368. new_other.manual_irrigation_units = other_json["manual_irrigation_units"]
  369. new_other.manual_irrigation_water_1 = other_json["manual_irrigation_water_1"]
  370. new_other.manual_irrigation_water_2 = other_json["manual_irrigation_water_2"]
  371. new_other.manual_irrigation_program = other_json["manual_irrigation_program"]
  372. self.other = new_other
  373. if "backflushing_config" in json:
  374. json_bk = json["backflushing_config"]
  375. backflush = BackFlushing()
  376. backflush.difference_backflush_kg = json_bk["difference_backflush_kg"]
  377. backflush.time_between_flushing_hours = json_bk["time_between_flushing_hours"]
  378. backflush.time_between_flushing_minutes = json_bk["time_between_flushing_minutes"]
  379. backflush.time_between_station_min = json_bk["time_between_station_min"]
  380. backflush.time_between_station_sec = json_bk["time_between_station_sec"]
  381. backflush.pause_between_filtering_secs = json_bk["pause_between_filtering_secs"]
  382. backflush.amount_of_filters = json_bk["amount_of_filters"]
  383. backflush.delay_differential_pressure = json_bk["delay_differential_pressure"]
  384. backflush.wait_after_sustain = json_bk["wait_after_sustain"]
  385. backflush.times_wash_before_pd_alarm = json_bk["times_wash_before_pd_alarm"]
  386. self.back_flushing = backflush
  387. # if "alarm" in json:
  388. # json_alarm = json["alarm"]
  389. # alarm = Alarm()
  390. # alarm.fertilization_deficit = json_alarm["fertilization_deficit"]
  391. # alarm.fertilization_desc = json_alarm["fertilization_desc"]
  392. # alarm.ec_error = json_alarm["ec_error"]
  393. # alarm.ph_error = json_alarm["ph_error"]
  394. # alarm.ec_danger = json_alarm["ec_danger"]
  395. # alarm.ph_danger = json_alarm["ph_danger"]
  396. # alarm.low_pressure = json_alarm["low_pressure"]
  397. # alarm.high_pressure = json_alarm["high_pressure"]
  398. # alarm.flow_error = json_alarm["flow_error"]
  399. # alarm.no_water = json_alarm["no_water"]
  400. # alarm.dangerous_flow = json_alarm["dangerous_flow"]
  401. # alarm.irrigation_out_of_controls = json_alarm["irrigation_out_of_controls"]
  402. # self.alarm = alarm
  403. if "config_alarms" in json:
  404. json_alarm = json["config_alarms"]
  405. self.alarm_config.deviation_warning_max_error_flow = json_alarm["deviation_warning_max_error_flow"]
  406. self.alarm_config.delay_alarms_ec_ph_secs = json_alarm["delay_alarms_ec_ph_secs"]
  407. self.alarm_config.delay_alarm_ph_dangerous_secs = json_alarm["delay_alarm_ph_dangerous_secs"]
  408. self.alarm_config.delay_alarm_ec_dangerous_secs = json_alarm["delay_alarm_ec_dangerous_secs"]
  409. self.alarm_config.delay_alarm_high_pressure_kg = json_alarm["delay_alarm_high_pressure_kg"]
  410. self.alarm_config.delay_alarm_low_pressure_secs = json_alarm["delay_alarm_low_pressure_secs"]
  411. self.alarm_config.delay_alarm_flow_secs = json_alarm["delay_alarm_flow_secs"]
  412. self.alarm_config.max_diff_warning_error_ec = json_alarm["max_diff_warning_error_ec"]
  413. self.alarm_config.max_diff_warning_error_ph = json_alarm["max_diff_warning_error_ph"]
  414. self.alarm_config.max_deviation_under_ph = json_alarm["max_deviation_under_ph"]
  415. self.alarm_config.max_deviation_over_ec = json_alarm["max_deviation_over_ec"]
  416. self.alarm_config.level_alarm_high_pressure_kg = json_alarm["level_alarm_high_pressure_kg"]
  417. self.alarm_config.level_alarm_low_pressure_kg = json_alarm["level_alarm_low_pressure_kg"]
  418. # Dejar un byte en blanco
  419. self.alarm_config.function_alarm_fertilizer_discontinued = json_alarm["function_alarm_fertilizer_discontinued"]
  420. self.alarm_config.function_alarm_ec_ph_dangerous = json_alarm["function_alarm_ec_ph_dangerous"]
  421. self.alarm_config.function_alarm_high_pressure = json_alarm["function_alarm_high_pressure"]
  422. self.alarm_config.function_alarm_dangerous_flow = json_alarm["function_alarm_dangerous_flow"]
  423. self.alarm_config.function_alarm_no_fertilization = json_alarm["function_alarm_no_fertilization"]
  424. self.alarm_config.function_alarm_no_water = json_alarm["function_alarm_no_water"]
  425. self.alarm_config.pulses_fertilizer_no_control = json_alarm["pulses_fertilizer_no_control"]
  426. self.alarm_config.pulses_needs_fertilizer = json_alarm["pulses_needs_fertilizer"]
  427. self.alarm_config.max_seconds_between_water_pulses = json_alarm["max_seconds_between_water_pulses"]
  428. self.alarm_config.over_dangerous_flow_percentage = json_alarm["over_dangerous_flow_percentage"]
  429. self.alarm_config.delay_secs_if_diff_ec_more_1 = json_alarm["delay_secs_if_diff_ec_more_1"]
  430. self.alarm_config.delay_secs_if_diff_ec_more_05 = json_alarm["delay_secs_if_diff_ec_more_05"]
  431. self.alarm_config.delay_secs_if_diff_ec_more_03 = json_alarm["delay_secs_if_diff_ec_more_03"]
  432. self.alarm_config.coefficient_correction_ec_more_1 = json_alarm["coefficient_correction_ec_more_1"]
  433. self.alarm_config.coefficient_correction_ec_more_05 = json_alarm["coefficient_correction_ec_more_05"]
  434. self.alarm_config.coefficient_correction_ec_more_03 = json_alarm["coefficient_correction_ec_more_03"]
  435. self.alarm_config.delay_secs_if_diff_ph_more_1 = json_alarm["delay_secs_if_diff_ph_more_1"]
  436. self.alarm_config.delay_secs_if_diff_ph_more_05 = json_alarm["delay_secs_if_diff_ph_more_05"]
  437. self.alarm_config.delay_secs_if_diff_ph_more_03 = json_alarm["delay_secs_if_diff_ph_more_03"]
  438. self.alarm_config.coefficient_correction_ph_more_1 = json_alarm["coefficient_correction_ph_more_1"]
  439. self.alarm_config.coefficient_correction_ph_more_05 = json_alarm["coefficient_correction_ph_more_05"]
  440. self.alarm_config.coefficient_correction_ph_more_03 = json_alarm["coefficient_correction_ph_more_03"]
  441. self.alarm_config.secs_first_ec_correction = json_alarm["secs_first_ec_correction"]
  442. self.alarm_config.secs_first_ph_correction = json_alarm["secs_first_ph_correction"]
  443.  
  444. def save_to_file(self, filename):
  445. pickle.dumps(self)
  446. with open(filename, 'wb') as out_file:
  447. pickle.dump(self, out_file)
  448.  
  449. def what_to_upload(self, other):
  450. toReturn = dict()
  451. irrigation = list()
  452. for x in other.allIrrigation:
  453. if not x in self.allIrrigation:
  454. irrigation.append(x)
  455. else:
  456. if self.allIrrigation[x] != other.allIrrigation[x]:
  457. irrigation.append(x)
  458. toReturn["irrigation"] = irrigation
  459.  
  460. fertilization = list()
  461. for x in other.allFertilization:
  462. if not x in self.allFertilization:
  463. fertilization.append(x)
  464. else:
  465. if self.allFertilization[x] != other.allFertilization[x]:
  466. fertilization.append(x)
  467. toReturn["fertilization"] = fertilization
  468.  
  469. inyection = list()
  470. for x in other.allInyection:
  471. if not x in self.allInyection:
  472. inyection.append(x)
  473. else:
  474. if self.allInyection[x] != other.allInyection[x]:
  475. inyection.append(x)
  476. toReturn["inyection"] = inyection
  477.  
  478. toReturn["backflush"] = self.back_flushing != other.back_flushing
  479. toReturn["alarm_config"] = self.alarm_config != other.alarm_config
  480. toReturn["config_io"] = self.config_io != other.config_io
  481. toReturn["solape"] = self.solape != other.solape
  482. toReturn["other"] = self.other != other.other
  483. return toReturn
  484.  
  485. def __eq__(self, other):
  486. if self.last_update == other.last_update and \
  487. self.username == other.username and \
  488. self.password == other.password and \
  489. self.alarm_config == other.alarm_config and \
  490. self.config_io == other.config_io and \
  491. self.back_flushing == other.back_flushing and \
  492. self.solape == other.solape and \
  493. compare_dicts(self.allInyection, other.allInyection) and \
  494. compare_dicts(self.allFertilization, other.allFertilization) and \
  495. compare_dicts(self.allIrrigation, other.allIrrigation):
  496. return True
  497. else:
  498. return False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement