Advertisement
Guest User

Untitled

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