Advertisement
Guest User

Untitled

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