Advertisement
Guest User

Untitled

a guest
Dec 18th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 48.44 KB | None | 0 0
  1. '''
  2. (keep-lines "^def .*:")
  3. '''
  4.  
  5. import time
  6. import serial
  7. import json
  8. import requests
  9. import os.path
  10. import serial
  11. from controllerstate import *
  12.  
  13. # irrigation, fertilization and inyection
  14. BASE_PROGFERT = 1600 + 8192
  15. BASE_PROGRIEGO = 0 + 8192
  16. BASE_CONFIGINYECTORES = 1960 + 8192
  17. # IO
  18. BASE_ADDINYECTORS = 2501 + 8192
  19. BASE_ADDFILTROS = 2509 + 8192
  20. BASE_ADDACTUADORES = 2517 + 8192
  21. BASE_PRIVALEC = 2530 + 8192
  22. BASE_PRIVALBYTES = 2538 + 8192
  23. BASE_V1AV16 = 2421 + 8192
  24. BASE_V17AV32 = 2437 + 8192
  25. BASE_V33AV48 = 2453 + 8192
  26. BASE_V49AV64 = 2469 + 8192
  27. BASE_V65AV80 = 2485 + 8192
  28. # alarm config
  29. BASE_CONFIGALARMAS = 2237 + 8192
  30. BASE_CONFIGECPHPARAMS = 2224 + 8192
  31. # alarms
  32. # other configs
  33. BASE_FLOWMETER = 2204 + 8192
  34. BASE_BLOWER = 0x8A0 + 8192
  35. BASE_BOOSTER = 0x9FE + 8192
  36. BASE_MANUAL_IRRIGATION = 2414 + 8192
  37. BASE_MANUAL_IRRIGATION_PROG = 0x1DB
  38. BASE_TIME_RESTART = 2556 + 8192
  39. BASE_TOFF_INYECTOR = 2420 + 8192
  40. # solape
  41. BASE_SOLAPE = 2024 + 8192
  42. BASE_SOLAPE_MMSS = 2184 + 8192
  43. # backflushing
  44. BASE_ADDRETROLAVADO = 2213 + 8192
  45.  
  46. # terminal stats
  47. # irrigation - total a regar h - total a regar m - agua antes h - agua antes m - agua dp h - agua dp m - unidad
  48. BASE_IRRIGATION = 0x1F5
  49. BASE_FERT = 0x168
  50. BASE_NEXT_PROG = 0x1DA
  51. # 3 bytes
  52. BASE_M3 = 0xB0D
  53. BASE_IRRIGATION_TIME = 0x203
  54. BASE_TOTAL_IRRIGATION = 0xB0D
  55. BASE_MEASURED_FLOW = 0xA24
  56. # 2 bytes
  57. BASE_C_PROG = 0x83C
  58. BASE_P1_P2 = 0x394 # float
  59. BASE_EC_PH_MEASURED = 0x392 # float
  60. BASE_EC_PH_ASKED = 0x0224 # float
  61. BASE_EC_PH_AVERAGE = 0x28F # float
  62. # 96 byes / 48 registers
  63. # 32 bytes first 2 vars
  64. BASE_INYECTOR_STATS_1_2 = 0xA04
  65. BASE_INYECTOR_STATS_3 = 0xA5C
  66. BASE_INYECTOR_STATS_4 = 0xA78
  67. BASE_INYECTOR_STATS_5 = 0xAF2
  68. BASE_VALVES_STATS = 0xBA5
  69.  
  70. TOTAL_FERT = 20
  71. TOTAL_INY = 8
  72. TOTAL_IRR = 50
  73. DIRTY_ADD = 4234
  74. URL_SERVER = 'http://emiliozelione2018.pythonanywhere.com/'
  75. #USERNAME = "Prueba1"
  76. #PASSWORD = "goldfinger"
  77. USERNAME = "Prueba_Taller"
  78. PASSWORD = "prueba2018"
  79. TIME_UPDATE = 2
  80. FILEPATH_SAVE = "controller.bin"
  81.  
  82. write_irrProg = [False] * 50
  83. write_fertProg = [False] * 20
  84. write_ConfIny = [False] * 8
  85. write_ConfigAl = False
  86. write_configIO = False
  87. write_backflush = False
  88. write_solape = False
  89. write_alarms = False
  90. write_other = False
  91.  
  92. terminalSerial = serial.Serial("COM3", 9600, timeout=0.2)
  93.  
  94. def fetch_json():
  95. response = requests.get(
  96. URL_SERVER + 'requests?all&username=' +
  97. USERNAME + '&password=' + PASSWORD)
  98. dataJson = response.json()
  99. return (dataJson)
  100.  
  101. def fetch_last_update():
  102. response = requests.get(
  103. URL_SERVER +
  104. 'requests?updated_when&username=' +
  105. USERNAME +
  106. '&password=' +
  107. PASSWORD)
  108. dataJson = response.json()
  109. return (dataJson['update'], dataJson['updated_by'])
  110.  
  111. def is_set(x, n):
  112. val = x & 2 ** n != 0
  113. if val:
  114. return 1
  115. else:
  116. return 0
  117.  
  118. def check_login():
  119. response = requests.get(
  120. URL_SERVER +
  121. 'login?username=' +
  122. USERNAME +
  123. '&password=' +
  124. PASSWORD)
  125. dataJson = response.json()
  126. return (dataJson['ok'])
  127.  
  128. def read_dirty():
  129. regs = read_registers(DIRTY_ADD, 1)
  130. if regs[0] == 186:
  131. return True
  132. else:
  133. return False
  134.  
  135. def write_dirty():
  136. val = [0, 0]
  137. write_registers(DIRTY_ADD, 1, val)
  138.  
  139. def read_registers(Add, nRegs):
  140. byteList = []
  141. AddH = int(Add / 256)
  142. AddL = Add % 256
  143. Encabezado = [1, 3, AddH, AddL, 0, nRegs] # Son 1Registros
  144. # Tengo la lista en bytes, Aplicar los CRC
  145. byteList = Encabezado + byteList
  146. listaCRC = calculate_crc(byteList)
  147. byteList = byteList + listaCRC # Le agrega los bytes de CRC
  148. incoming = []
  149. Total_in = nRegs * 2 + 5
  150. while (len(incoming) < (Total_in)):
  151. terminalSerial.write(byteList)
  152. terminalSerial.flush()
  153. incoming = terminalSerial.read(Total_in)
  154. BytesIn = bytes2integer(incoming)
  155. CRC_in = BytesIn[(Total_in - 2):Total_in]
  156. del BytesIn[-2:] # borra los 2 ultimos elementos
  157. listaCRC = calculate_crc(BytesIn)
  158. if (listaCRC == CRC_in):
  159. del BytesIn[0:3] # borra del 0 al 3 no inclusive
  160. return BytesIn
  161. else:
  162. return None
  163.  
  164. def write_registers(Add, nRegs, byteList):
  165. AddH = int(Add / 256)
  166. AddL = Add % 256
  167. Encabezado = [1, 16, AddH, AddL, 0, nRegs, nRegs * 2]
  168. byteList = Encabezado + byteList
  169. listaCRC = calculate_crc(byteList)
  170. byteList = byteList + listaCRC
  171. # Le agrega los bytes de CRC
  172. incoming = []
  173. Total_in = 8
  174. # Cuando Escribis Recibis 8 Bytes Fijos
  175. while (len(incoming) < (Total_in)):
  176. terminalSerial.write(byteList)
  177. terminalSerial.flush()
  178. incoming = terminalSerial.read(Total_in)
  179. BytesIn = bytes2integer(incoming)
  180. CRC_in = BytesIn[(Total_in - 2):Total_in]
  181. del BytesIn[-2:] # borra los 2 ultimos elementos
  182. listaCRC = calculate_crc(BytesIn)
  183. if (listaCRC == CRC_in):
  184. return True
  185. else:
  186. return False
  187.  
  188. def read_from_controller_inyectors(ci):
  189. if ci in cs.allInyection:
  190. ConfigInyL = cs.allInyection[ci]
  191. else:
  192. ConfigInyL = InyectionProgram()
  193. ConfigInyL.program = ci
  194. ConfigIny = InyectionProgram()
  195. write_ConfIny[ci - 1] = False
  196. Add = BASE_CONFIGINYECTORES + (ci - 1) * 8
  197. byteList = []
  198. byteList = read_registers(Add, 4)
  199. function = byteList[0]
  200. flow = byteList[1] * 256 + byteList[2]
  201. timeOn = byteList[3]
  202. mlPulso = byteList[4] * 256 + byteList[5]
  203. simulator = byteList[6]
  204. maxDeviation = byteList[7]
  205. ConfigIny.program = ci
  206. ConfigIny.flow = flow
  207. ConfigIny.time_on = timeOn
  208. ConfigIny.function = function
  209. ConfigIny.litres_pulse = mlPulso
  210. if simulator == 0:
  211. ConfigIny.simulator = False
  212. else:
  213. ConfigIny.simulator = True
  214. ConfigIny.max_deviation = maxDeviation
  215. if ConfigInyL != ConfigIny:
  216. write_ConfIny[ci - 1] = True
  217. cs.allInyection[ci] = ConfigIny
  218.  
  219. def read_from_controller_fertilization(pf):
  220. if pf in cs.allFertilization:
  221. FertProgL = cs.allFertilization[pf]
  222. else:
  223. FertProgL = FertilizationProgram()
  224. FertProg = FertilizationProgram()
  225. write_fertProg[pf - 1] = False
  226. Add = BASE_PROGFERT + (pf - 1) * 18
  227. byteList = []
  228. byteList = read_registers(Add, 9)
  229. i = 0
  230. Val = [0] * 10
  231. while (i < 8):
  232. Val[i] = float(byteList[2 * i] * 256 + byteList[2 * i + 1])
  233. Val[i] = Val[i]/10
  234. i = i + 1
  235. Val[8] = byteList[16] / 10
  236. Val[9] = byteList[17] / 10
  237. FertProg.program = pf
  238. FertProg.values_1 = Val[0]
  239. FertProg.values_2 = Val[1]
  240. FertProg.values_3 = Val[2]
  241. FertProg.values_4 = Val[3]
  242. FertProg.values_5 = Val[4]
  243. FertProg.values_6 = Val[5]
  244. FertProg.values_7 = Val[6]
  245. FertProg.values_8 = Val[7]
  246. FertProg.ec = Val[8]
  247. FertProg.ph = Val[9]
  248. if FertProgL != FertProg:
  249. write_fertProg[pf - 1] = True
  250. cs.allFertilization[pf] = FertProg
  251.  
  252. def read_from_solape():
  253. global write_solape
  254. solapeNew = ValveSolape()
  255. byteList = []
  256. offset = 0
  257. while offset < (80+80)/2:
  258. byteList += read_registers(BASE_SOLAPE+offset, 10)
  259. offset += 10
  260. flow, solape, time = [], [], []
  261. i = 0
  262. while i < 80:
  263. flow.append(byteList[i])
  264. solape.append(byteList[i+80])
  265. i += 1
  266. i = 0
  267. byteList = read_registers(BASE_SOLAPE_MMSS, 10)
  268. while i < 20:
  269. # negative
  270. if byteList[i] >= 128:
  271. t = byteList[i]*256 + byteList[i+1]
  272. t = 65536 - t
  273. t /= 100.0
  274. time.append(-t)
  275. else:
  276. t = byteList[i]*256 + byteList[i+1]
  277. t /= 100
  278. time.append(t)
  279. i += 2
  280. solapeNew.flow = ','.join(map(str, flow))
  281. solapeNew.solape = ','.join(map(str, solape))
  282. solapeNew.time = ','.join(map(str, time))
  283. if cs.solape != solapeNew:
  284. write_solape = True
  285. cs.solape = solapeNew
  286.  
  287. def read_from_alarms():
  288. raise NotImplementedError("Hay que implementar esto")
  289.  
  290. def read_from_other_configs():
  291. global write_other
  292. otherNew = OtherConfigs()
  293. byteList = read_registers(BASE_MANUAL_IRRIGATION, 2)
  294. byteListProg = read_registers(BASE_MANUAL_IRRIGATION_PROG, 1)
  295. otherNew.manual_irrigation_program = byteListProg[0]
  296. otherNew.manual_irrigation_units = byteList[0]
  297. otherNew.manual_irrigation_water_1 = byteList[2]
  298. otherNew.manual_irrigation_water_2 = byteList[3]
  299. byteList = read_registers(BASE_TOFF_INYECTOR, 1)
  300. otherNew.toff_inyector = byteList[0]
  301. byteList = read_registers(BASE_FLOWMETER, 2)
  302. otherNew.flow_meter_1 = byteList[0]*256 + byteList[1]
  303. otherNew.flow_meter_2 = byteList[2]
  304. byteList = read_registers(BASE_BLOWER, 1)
  305. otherNew.blower_1 = byteList[0]
  306. otherNew.blower_2 = byteList[1]
  307. byteList = read_registers(BASE_TIME_RESTART, 1)
  308. otherNew.time_restart_program_1 = byteList[0]
  309. otherNew.time_restart_program_2 = byteList[1]
  310. byteList = read_registers(BASE_BOOSTER, 1)
  311. otherNew.booster_pump = byteList[0]
  312. if cs.other != otherNew:
  313. write_other = True
  314. cs.other = otherNew
  315.  
  316. def read_from_backflushing():
  317. backFlushing = cs.back_flushing
  318. backFlushingNew = BackFlushing()
  319. byteList = []
  320. byteList = read_registers(BASE_ADDRETROLAVADO, 5)
  321. backFlushingNew.difference_backflush_kg = float(byteList[0])/10
  322. backFlushingNew.time_between_flushing_hours = byteList[1]
  323. backFlushingNew.time_between_flushing_minutes = byteList[2]
  324. backFlushingNew.time_between_station_min = byteList[3]
  325. backFlushingNew.time_between_station_sec = byteList[4]
  326. backFlushingNew.pause_between_filtering_secs = byteList[5]
  327. backFlushingNew.amount_of_filters = byteList[6]
  328. backFlushingNew.times_wash_before_pd_alarm = byteList[7]
  329. backFlushingNew.delay_differential_pressure = byteList[8]
  330. backFlushingNew.wait_after_sustain = byteList[9]
  331. if backFlushing != backFlushingNew:
  332. write_backflush = True
  333. cs.back_flushing = backFlushingNew
  334.  
  335. def read_from_controller_irrigation(pr):
  336. if pr in cs.allIrrigation:
  337. ProgRiegoL = cs.allIrrigation[pr]
  338. else:
  339. ProgRiegoL = IrrigationProgram()
  340. write_irrProg[pr - 1] = False
  341. ProgRiego = IrrigationProgram()
  342. Add = BASE_PROGRIEGO + (pr - 1) * 32
  343. listaA = read_registers(Add, 8)
  344. ProgRiego.program = pr
  345.  
  346. ProgRiego.water_total_1 = listaA[0]
  347. ProgRiego.water_total_2 = listaA[1]
  348. ProgRiego.water_before_1 = listaA[2]
  349. ProgRiego.water_before_2 = listaA[3]
  350. ProgRiego.water_after_1 = listaA[4]
  351. ProgRiego.water_after_2 = listaA[5]
  352. ProgRiego.time_between_1 = listaA[6]
  353. ProgRiego.time_between_2 = listaA[7]
  354. ProgRiego.time_start_1 = listaA[8]
  355. ProgRiego.time_start_2 = listaA[9]
  356. ProgRiego.units = listaA[10]
  357. ProgRiego.fertilization_program = listaA[11]
  358. ProgRiego.kicks = listaA[12]
  359. ProgRiego.condition_program = listaA[13]
  360. Add = BASE_PROGRIEGO + (pr - 1) * 32 + 16
  361. listaB = read_registers(Add, 8)
  362. listaDias = listaA[14:16] + listaB[0:5]
  363. i = 0
  364. for elem in listaDias:
  365. if i == 0:
  366. cadDias = str(elem)
  367. else:
  368. cadDias = cadDias + ',' + str(elem)
  369. i = i + 1
  370. del listaB[0:5] # borra del 0 al 5 no inclusive
  371. if cadDias != ProgRiegoL.days:
  372. write_irrProg[pr - 1] = True
  373.  
  374. ProgRiego.days = cadDias
  375. Cadvalves = decode_valves(listaB)
  376. if Cadvalves != ProgRiegoL.valves:
  377. write_irrProg[pr - 1] = True
  378. ProgRiego.valves = Cadvalves
  379.  
  380. if ProgRiego != ProgRiegoL:
  381. write_irrProg[pr - 1] = True
  382. cs.allIrrigation[pr] = ProgRiego
  383.  
  384. def read_from_controller_config_alarms():
  385. ConfigAl = cs.alarm_config
  386. ConfigAlW = AlarmConfig()
  387. global write_ConfigAl
  388. write_ConfigAl = False
  389. listaA = read_registers(BASE_CONFIGECPHPARAMS, 6)
  390. listaB = read_registers(BASE_CONFIGALARMAS - 1, 7)
  391. listaC = read_registers(BASE_CONFIGALARMAS + 13, 6)
  392. ConfigAlW.delay_secs_if_diff_ec_more_1 = listaA[0]
  393. ConfigAlW.delay_secs_if_diff_ec_more_05 = listaA[1]
  394. ConfigAlW.delay_secs_if_diff_ec_more_03 = listaA[2]
  395. ConfigAlW.coefficient_correction_ec_more_1 = listaA[3]
  396. ConfigAlW.coefficient_correction_ec_more_05 = listaA[4]
  397. ConfigAlW.coefficient_correction_ec_more_03 = listaA[5]
  398. ConfigAlW.delay_secs_if_diff_ph_more_1 = listaA[6]
  399. ConfigAlW.delay_secs_if_diff_ph_more_05 = listaA[7]
  400. ConfigAlW.delay_secs_if_diff_ph_more_03 = listaA[8]
  401. ConfigAlW.coefficient_correction_ph_more_1 = listaA[9]
  402. ConfigAlW.coefficient_correction_ph_more_05 = listaA[10]
  403. ConfigAlW.coefficient_correction_ph_more_03 = listaA[11]
  404. ConfigAlW.secs_first_ec_correction = listaB[0]
  405. ConfigAlW.secs_first_ph_correction = listaB[0]
  406. ConfigAlW.deviation_warning_max_error_flow = listaB[1]
  407. ConfigAlW.delay_alarms_ec_ph_secs = listaB[2]
  408. ConfigAlW.delay_alarm_ph_dangerous_secs = listaB[3]
  409. ConfigAlW.delay_alarm_ec_dangerous_secs = listaB[4]
  410. ConfigAlW.delay_alarm_high_pressure_kg = listaB[5]
  411. ConfigAlW.delay_alarm_low_pressure_secs = listaB[6]
  412. ConfigAlW.delay_alarm_flow_secs = listaB[7]
  413. ConfigAlW.max_diff_warning_error_ec = float(listaB[8])/10
  414. ConfigAlW.max_diff_warning_error_ph = float(listaB[9])/10
  415. ConfigAlW.max_deviation_under_ph = float(listaB[10])/10
  416. ConfigAlW.max_deviation_over_ec = float(listaB[11])/10
  417. ConfigAlW.level_alarm_high_pressure_kg = float(listaB[12])/10
  418. ConfigAlW.level_alarm_low_pressure_kg = float(listaB[13])/10
  419.  
  420. ConfigAlW.function_alarm_fertilizer_discontinued = listaC[1]
  421. ConfigAlW.function_alarm_ec_ph_dangerou = listaC[2]
  422. ConfigAlW.function_alarm_high_pressure = listaC[3]
  423. ConfigAlW.function_alarm_dangerous_flow = listaC[4]
  424. ConfigAlW.function_alarm_no_fertilization = listaC[5]
  425. ConfigAlW.function_alarm_no_water = listaC[6]
  426. ConfigAlW.pulses_fertilizer_no_control = listaC[7]
  427. ConfigAlW.pulses_needs_fertilizer = listaC[8]
  428. ConfigAlW.max_seconds_between_water_pulses = listaC[9]
  429. ConfigAlW.over_dangerous_flow_percentage = listaC[10]
  430.  
  431. if ConfigAlW != ConfigAl:
  432. write_ConfigAl = True
  433. cs.alarm_config = ConfigAlW
  434.  
  435. def read_from_controller_input_output():
  436. ConfigIO_w = IOConfig()
  437. ConfigIO = cs.config_io
  438. global write_configIO
  439. wrte_configIO = False
  440. inyfilt = ConfigIO.inyection + ',' + ConfigIO.filters
  441. listaA = read_registers(BASE_ADDINYECTORS, 4)
  442. ConfigIO_w.inyection = ','.join(str(e) for e in listaA)
  443. listaA = read_registers(BASE_ADDFILTROS, 4)
  444. ConfigIO_w.filters = ','.join(str(e) for e in listaA)
  445. listaA = read_registers(BASE_ADDACTUADORES, 4)
  446. ConfigIO_w.actuators = ','.join(str(e) for e in listaA)
  447. listaA = read_registers(BASE_V1AV16, 8)
  448. ConfigIO_w.valves1to16 = ','.join(str(e) for e in listaA)
  449. listaA = read_registers(BASE_V17AV32, 8)
  450. ConfigIO_w.valves17to32 = ','.join(str(e) for e in listaA)
  451. listaA = read_registers(BASE_V33AV48, 8)
  452. ConfigIO_w.valves33to48 = ','.join(str(e) for e in listaA)
  453. listaA = read_registers(BASE_V49AV64, 8)
  454. ConfigIO_w.valves49to64 = ','.join(str(e) for e in listaA)
  455. listaA = read_registers(BASE_V65AV80, 8)
  456. ConfigIO_w.valves65to80 = ','.join(str(e) for e in listaA)
  457. listaA = read_registers(BASE_PRIVALEC, 4)
  458. listaB = read_registers(BASE_PRIVALBYTES, 8)
  459. analog_input_1 = listaA[0:4]
  460. analog_input_2 = listaA[4:8]
  461. listaRead = []
  462. i = 0
  463. while (i < 8):
  464. listaRead.append(listaB[2 * i] * 256 + listaB[2 * i + 1])
  465. i = i + 1
  466. analog_input_3 = listaRead[0:4]
  467. analog_input_4 = listaRead[4:8]
  468. ConfigIO_w.analog_input_1 = ','.join(str(e) for e in analog_input_1)
  469. ConfigIO_w.analog_input_2 = ','.join(str(e) for e in analog_input_2)
  470. ConfigIO_w.analog_input_3 = ','.join(str(e) for e in analog_input_3)
  471. ConfigIO_w.analog_input_4 = ','.join(str(e) for e in analog_input_4)
  472. if (ConfigIO != ConfigIO_w):
  473. write_configIO = True
  474. cs.config_io = ConfigIO_w
  475.  
  476. def decode_valves(ListaValves):
  477. CantValv = 0
  478. CadValv = ""
  479. i = 0
  480. while (i < 9): # Registros
  481. j = 0
  482. while (j < 8): # bits
  483. peso = ListaValves[i] & (2 ** j)
  484. valv = (i * 8) + j + 1
  485. if (peso != 0):
  486. if (CantValv == 0):
  487. CadValv = str(valv)
  488. else:
  489. CadValv = CadValv + ',' + str(valv)
  490. CantValv = CantValv + 1
  491. j = j + 1
  492. i = i + 1
  493. return (CadValv)
  494.  
  495. def write_controller_fertilization(pf):
  496. FertProg = cs.allFertilization[pf]
  497. newList = []
  498. byteList = []
  499. newList.append(int(FertProg.values_1))
  500. newList.append(int(FertProg.values_2))
  501. newList.append(int(FertProg.values_3))
  502. newList.append(int(FertProg.values_4))
  503. newList.append(int(FertProg.values_5))
  504. newList.append(int(FertProg.values_6))
  505. newList.append(int(FertProg.values_7))
  506. newList.append(int(FertProg.values_8))
  507. EC = FertProg.ec * 10
  508. pH = FertProg.ph * 10
  509. newList.append(int(EC) * 256 + int(pH))
  510. i = 0
  511. for elem in newList:
  512. byteList.append(int(elem / 256))
  513. byteList.append(elem % 256)
  514. Add = BASE_PROGFERT + (pf - 1) * 18
  515. write_registers(Add, 9, byteList)
  516.  
  517. def write_controller_input_output():
  518. listaSal = []
  519. listaSal = cs.config_io.inyection.split(',')
  520. listaSal = [int(i) for i in listaSal]
  521. write_registers(BASE_ADDINYECTORS, 4, listaSal)
  522. listaSal = []
  523. listaSal = cs.config_io.filters.split(',')
  524. listaSal = [int(i) for i in listaSal]
  525. write_registers(BASE_ADDFILTROS, 4, listaSal)
  526. listaSal = []
  527. listaSal = cs.config_io.actuators.split(',')
  528. listaSal = [int(i) for i in listaSal]
  529. write_registers(BASE_ADDACTUADORES, 4, listaSal)
  530. listaSal = []
  531. listaSal = cs.config_io.valves1to16.split(',')
  532. listaSal = [int(i) for i in listaSal]
  533. write_registers(BASE_V1AV16, 8, listaSal)
  534. listaSal = []
  535. listaSal = cs.config_io.valves17to32.split(',')
  536. listaSal = [int(i) for i in listaSal]
  537. write_registers(BASE_V17AV32, 8, listaSal)
  538. listaSal = []
  539. listaSal = cs.config_io.valves33to48.split(',')
  540. listaSal = [int(i) for i in listaSal]
  541. write_registers(BASE_V33AV48, 8, listaSal)
  542. listaSal = []
  543. listaSal = cs.config_io.valves49to64.split(',')
  544. listaSal = [int(i) for i in listaSal]
  545. write_registers(BASE_V49AV64, 8, listaSal)
  546. listaSal = []
  547. listaSal = cs.config_io.valves65to80.split(',')
  548. listaSal = [int(i) for i in listaSal]
  549. write_registers(BASE_V65AV80, 8, listaSal)
  550. listaSal = []
  551. listaSal = cs.config_io.valves65to80.split(',')
  552. listaSal = [int(i) for i in listaSal]
  553. write_registers(BASE_V65AV80, 8, listaSal)
  554. # Leer los analog_inputs Pedirle a Fernando que cambie los labels
  555. listaSalA = []
  556. listaSalA = cs.config_io.analog_input_1.split(',')
  557. listaSalA = [int(i) for i in listaSalA]
  558. listaSalB = []
  559. listaSalB = cs.config_io.analog_input_2.split(',')
  560. listaSalB = [int(i) for i in listaSalB]
  561. listaSalC = []
  562. listaSalC = cs.config_io.analog_input_3.split(',')
  563. listaSalC = [int(i) for i in listaSalC]
  564. listaSalD = []
  565. listaSalD = cs.config_io.analog_input_4.split(',')
  566. listaSalD = [int(i) for i in listaSalD]
  567. listaSal2 = listaSalC + listaSalD
  568. listaSal1 = listaSalA + listaSalB
  569. write_registers(BASE_PRIVALEC, 4, listaSal1)
  570. i = 0
  571. listaBytes = [0] * 16
  572. while (i < 8):
  573. listaBytes[i * 2] = int(listaSal2[i] / 256)
  574. listaBytes[i * 2 + 1] = listaSal2[i] % 256
  575. i = i + 1
  576. write_registers(BASE_PRIVALBYTES, 8, listaBytes)
  577.  
  578. def write_other_configs():
  579. otherConfs = cs.other
  580. byteList = []
  581. import math
  582. buffBytes = read_registers(BASE_FLOWMETER+2, 1)
  583. byteList.append(int(otherConfs.flow_meter_1/256))
  584. byteList.append(otherConfs.flow_meter_1 % 256)
  585. byteList.append(otherConfs.flow_meter_2)
  586. byteList.append(buffBytes[1])
  587. write_registers(BASE_FLOWMETER, 2, byteList)
  588. buffBytes = read_registers(BASE_TOFF_INYECTOR, 1)
  589. byteList = []
  590. byteList.append(cs.other.toff_inyector*10)
  591. byteList.append(buffBytes[1])
  592. write_registers(BASE_TOFF_INYECTOR, 1, byteList)
  593. buffBytes = read_registers(BASE_MANUAL_IRRIGATION, 1)
  594. byteList.append(cs.other.manual_irrigation_units)
  595. byteList.append(buffBytes[1])
  596. byteList.append(cs.other.manual_irrigation_water_1)
  597. byteList.append(cs.other.manual_irrigation_water_2)
  598. write_registers(BASE_MANUAL_IRRIGATION, 2, byteList)
  599. byteListProg = read_registers(BASE_MANUAL_IRRIGATION_PROG, 1)
  600. byteListProg[0] = cs.other.manual_irrigation_program
  601. write_registers(BASE_MANUAL_IRRIGATION_PROG, 1, byteListProg)
  602. byteList = []
  603. byteList.append(cs.other.blower_1)
  604. byteList.append(cs.other.blower_2)
  605. write_registers(BASE_BLOWER, 1, byteList)
  606. byteList = []
  607. byteList.append(cs.other.time_restart_program_1)
  608. byteList.append(cs.other.time_restart_program_2)
  609. write_registers(BASE_TIME_RESTART, 1, byteList)
  610. secondByte = read_registers(BASE_BOOSTER, 1)
  611. byteList = []
  612. byteList.append(cs.other.booster_pump)
  613. byteList.append(secondByte[1])
  614. write_registers(BASE_BOOSTER, 1, byteList)
  615.  
  616.  
  617. def write_controller_inyection(iny):
  618. byteList = []
  619. Inyector = cs.allInyection[iny]
  620. CaudMaxVenturi = Inyector.flow
  621. TON = Inyector.time_on
  622. mlPulse = Inyector.litres_pulse
  623. MaxDesvio = Inyector.max_deviation
  624. Simular = Inyector.simulator
  625. byteList.insert(0, Inyector.function)
  626. byteList.insert(1, int(CaudMaxVenturi / 256))
  627. byteList.insert(2, int(CaudMaxVenturi % 256))
  628. byteList.insert(3, int(TON*10))
  629. byteList.insert(4, int(mlPulse / 256))
  630. byteList.insert(5, int(mlPulse % 256))
  631. byteList.insert(6, int(Simular))
  632. byteList.insert(7, int(MaxDesvio))
  633. Add = BASE_CONFIGINYECTORES + (iny - 1) * 8
  634. write_registers(Add, 4, byteList)
  635.  
  636. def write_controller_irrigation(pr):
  637. byteList = []
  638. lista_Valv = []
  639. ProgRiego = cs.allIrrigation[pr]
  640. RegistrosValvulas = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
  641. if (ProgRiego.valves != ''):
  642. lista_Valv = ProgRiego.valves.split(',')
  643. i = 0
  644. for elem in lista_Valv:
  645. bit = (int(elem) - 1) % 8
  646. indice = int((int(elem) - 1) / 8)
  647. RegistrosValvulas[indice] = RegistrosValvulas[indice] | (2 ** bit)
  648. i = i + 1
  649. RegistrosDays = [0, 0, 0, 0, 0, 0, 0]
  650. if (ProgRiego.days != ''):
  651. lista_days = ProgRiego.days.split(',')
  652. for d in lista_days:
  653. RegistrosDays.append(d)
  654. i = 0
  655. byteList.insert(0, ProgRiego.water_total_1)
  656. byteList.insert(1, ProgRiego.water_total_2)
  657. byteList.insert(2, ProgRiego.water_before_1)
  658. byteList.insert(3, ProgRiego.water_before_2, )
  659. byteList.insert(4, ProgRiego.water_after_1)
  660. byteList.insert(5, ProgRiego.water_after_2)
  661. byteList.insert(6, ProgRiego.time_between_1)
  662. byteList.insert(7, ProgRiego.time_between_2)
  663. byteList.insert(8, ProgRiego.time_start_1)
  664. byteList.insert(9, ProgRiego.time_start_2)
  665. byteList.insert(10, ProgRiego.units)
  666. byteList.insert(11, ProgRiego.fertilization_program)
  667. byteList.insert(12, ProgRiego.kicks)
  668. byteList.insert(13, ProgRiego.condition_program)
  669. byteList.insert(14, int(RegistrosDays[0])) # Domingo
  670. byteList.insert(15, int(RegistrosDays[1])) # Lunes
  671. Add = BASE_PROGRIEGO + (pr - 1) * 32
  672. write_registers(Add, 8, byteList)
  673. byteList = []
  674. byteList.insert(0, int(RegistrosDays[2])) # Martes
  675. byteList.insert(1, int(RegistrosDays[3])) # Miercoles
  676. byteList.insert(2, int(RegistrosDays[4])) # Jueves
  677. byteList.insert(3, int(RegistrosDays[5])) # Viernes
  678. byteList.insert(4, int(RegistrosDays[6])) # Sabado
  679. byteList += RegistrosValvulas
  680. byteList.append(0) # Campo Reservado
  681. Add = BASE_PROGRIEGO + (pr - 1) * 32 + 16
  682. write_registers(Add, 8, byteList)
  683.  
  684. def write_controller_config_alarms():
  685. ConfigAl = cs.alarm_config
  686. byteList = []
  687. byteList.append(ConfigAl.delay_secs_if_diff_ec_more_1)
  688. byteList.append(ConfigAl.delay_secs_if_diff_ec_more_05)
  689. byteList.insert(2, ConfigAl.delay_secs_if_diff_ec_more_03)
  690. byteList.insert(3, ConfigAl.coefficient_correction_ec_more_1)
  691. byteList.insert(4, ConfigAl.coefficient_correction_ec_more_05)
  692. byteList.insert(5, ConfigAl.coefficient_correction_ec_more_03)
  693. byteList.insert(6, ConfigAl.delay_secs_if_diff_ph_more_1)
  694. byteList.insert(7, ConfigAl.delay_secs_if_diff_ph_more_05)
  695. byteList.insert(8, ConfigAl.delay_secs_if_diff_ph_more_03)
  696. byteList.insert(9, ConfigAl.coefficient_correction_ph_more_1)
  697. byteList.insert(10, ConfigAl.coefficient_correction_ph_more_05)
  698. byteList.insert(11, ConfigAl.coefficient_correction_ph_more_03)
  699. write_registers(BASE_CONFIGECPHPARAMS, 6, byteList)
  700. byteList = []
  701. byteList.insert(0, ConfigAl.secs_first_ph_correction)
  702. byteList.insert(1, ConfigAl.deviation_warning_max_error_flow)
  703. byteList.insert(2, ConfigAl.delay_alarms_ec_ph_secs)
  704. byteList.insert(3, ConfigAl.delay_alarm_ph_dangerous_secs)
  705. byteList.insert(4, ConfigAl.delay_alarm_ec_dangerous_secs)
  706. byteList.insert(5, ConfigAl.delay_alarm_high_pressure_kg)
  707. byteList.insert(6, ConfigAl.delay_alarm_low_pressure_secs)
  708. byteList.insert(7, ConfigAl.delay_alarm_flow_secs)
  709. byteList.insert(8, int(ConfigAl.max_diff_warning_error_ec * 10))
  710. byteList.insert(9, int(ConfigAl.max_diff_warning_error_ph * 10))
  711. byteList.insert(10, int(ConfigAl.max_deviation_under_ph * 10))
  712. byteList.insert(11, int(ConfigAl.max_deviation_over_ec * 10))
  713. byteList.insert(12, int(ConfigAl.level_alarm_high_pressure_kg * 10))
  714. byteList.insert(13, int(ConfigAl.level_alarm_low_pressure_kg * 10))
  715. write_registers(BASE_CONFIGALARMAS - 1, 7, byteList)
  716. byteList = []
  717. byteList.insert(0, 0)
  718. byteList.insert(1, ConfigAl.function_alarm_fertilizer_discontinued)
  719. byteList.insert(3, ConfigAl.function_alarm_ec_ph_dangerous)
  720. byteList.insert(3, ConfigAl.function_alarm_high_pressure)
  721. byteList.insert(4, ConfigAl.function_alarm_dangerous_flow)
  722. byteList.insert(5, ConfigAl.function_alarm_no_fertilization)
  723. byteList.insert(6, ConfigAl.function_alarm_no_water)
  724. byteList.insert(7, ConfigAl.pulses_fertilizer_no_control)
  725. byteList.insert(8, ConfigAl.pulses_needs_fertilizer)
  726. byteList.insert(9, ConfigAl.max_seconds_between_water_pulses)
  727. byteList.insert(10, ConfigAl.over_dangerous_flow_percentage)
  728. # Tiempo Anti Picos
  729. byteList.insert(11, 10)
  730. write_registers(BASE_CONFIGALARMAS + 13, 6, byteList)
  731.  
  732. def write_controller_solape():
  733. solape = cs.solape
  734. byteList = []
  735. import math
  736. flow = solape.flow.split(',')
  737. flow = [math.floor(float(i)) for i in flow]
  738. solapeNew = solape.solape.split(',')
  739. solapeNew = [int(i) for i in solapeNew]
  740. time = solape.time.split(',')
  741. time = [float(i) for i in time]
  742. i = 0
  743. while i < len(flow):
  744. byteList.append(flow[i])
  745. i += 1
  746. i = 0
  747. while i < len(solapeNew):
  748. byteList.append(solapeNew[i])
  749. i += 1
  750. i = 0
  751. counter = 0
  752. while counter < 20:
  753. write_registers(BASE_SOLAPE + counter*8, 4, byteList[counter*8:counter*8+8])
  754. counter += 1
  755. byteListFix = []
  756. i = 0
  757. while i < 10:
  758. import math
  759. t = time[i]
  760. if t >= 0.0:
  761. t = t * 100
  762. byteListFix.append(math.floor(t / 256))
  763. byteListFix.append(math.floor(t % 256))
  764. else:
  765. t = 65536 - abs(t * 100)
  766. byteListFix.append(math.floor(t / 256))
  767. byteListFix.append(math.floor(t % 256))
  768. i += 1
  769. write_registers(BASE_SOLAPE_MMSS, 5, byteListFix[0:10])
  770. write_registers(BASE_SOLAPE_MMSS+10, 5, byteListFix[10:20])
  771.  
  772. def write_controller_backflush():
  773. backflush = cs.back_flushing
  774. byteList = []
  775. byteList.append(int(backflush.difference_backflush_kg*10))
  776. byteList.append(backflush.time_between_flushing_hours)
  777. byteList.append(backflush.time_between_flushing_minutes)
  778. byteList.append(backflush.time_between_station_min)
  779. byteList.append(backflush.time_between_station_sec)
  780. byteList.append(backflush.pause_between_filtering_secs)
  781. byteList.append(backflush.amount_of_filters)
  782. byteList.append(backflush.delay_differential_pressure)
  783. byteList.append(backflush.wait_after_sustain)
  784. byteList.append(backflush.times_wash_before_pd_alarm)
  785. write_registers(BASE_ADDRETROLAVADO, 5, byteList)
  786.  
  787. def send_server():
  788. if write_backflush and not cs.back_flushing.iszero():
  789. send_backflush()
  790. if write_ConfigAl and not cs.alarm_config.iszero():
  791. send_set_config_alarms()
  792. if write_solape and not cs.solape.iszero():
  793. send_solape()
  794. if write_configIO and not cs.config_io.iszero():
  795. send_config_input_output()
  796. if write_other and not cs.other.iszero():
  797. send_other()
  798. for key in cs.allIrrigation:
  799. if write_irrProg[key - 1] == True and not cs.allIrrigation[key].iszero():
  800. send_set_irrigation(key)
  801. for key in cs.allInyection:
  802. if write_ConfIny[key - 1] == True and not cs.allInyection[key].iszero():
  803. send_set_inyection(key)
  804. for key in cs.allFertilization:
  805. if write_fertProg[key - 1] == True and not cs.allFertilization[key].iszero():
  806. send_set_fertilization(key)
  807.  
  808. def send_set_fertilization(pf):
  809. if pf in cs.allFertilization:
  810. fert = cs.allFertilization[pf]
  811. response = requests.get(
  812. URL_SERVER + 'requests?set_fertilization&username=' + USERNAME + 'password=' + PASSWORD +
  813. "&username=Prueba1&password=goldfinger&program=" + str(fert.program) + "&who=1" + "&value_1=" + str(fert.values_1) +
  814. "&value_2=" + str(fert.values_2) + "&value_3=" + str(fert.values_3) + "&value_4=" + str(fert.values_4) +
  815. "&value_5=" + str(fert.values_5) + "&value_6=" + str(fert.values_6) + "&value_7=" + str(fert.values_7) +
  816. "&value_8=" + str(fert.values_8) + "&ec=" + str(fert.ec) + "&ph=" + str(fert.ph))
  817. dataJson = response.json()
  818. return (dataJson)
  819.  
  820. def send_set_irrigation(irrId):
  821. if irrId in cs.allIrrigation:
  822. irr = cs.allIrrigation[irrId]
  823. response = requests.get(URL_SERVER + 'requests?set_irrigation&username=' + USERNAME + '&password=' + PASSWORD +
  824. "&username=Prueba1&password=goldfinger&program=" + str(irr.program) + "&who=1"
  825. "&units=" + str(irr.units) + "&water_before_1=" + str(irr.water_before_1) +
  826. "&water_before_2=" + str(irr.water_before_2) + "&water_after_1=" +
  827. str(irr.water_after_1) + "&water_after_2=" + str(irr.water_after_2) +
  828. "&water_total_1=" + str(irr.water_total_1) + "&water_total_2=" + str(irr.water_total_2) +
  829. "&kicks=" + str(irr.kicks) + "&fertilization_program=" + str(irr.fertilization_program) +
  830. "&condition_program=" + str(irr.condition_program) + "&time_start_1=" + str(irr.time_start_1) +
  831. "&time_start_2=" + str(irr.time_start_2) + "&time_between_1=" + str(irr.time_between_1) +
  832. "&time_between_2=" + str(irr.time_between_2) + "&valves=" + str(irr.valves) + "&days=" + str(irr.days))
  833. dataJson = response.json()
  834. return (dataJson)
  835.  
  836. def send_set_inyection(inyId):
  837. if inyId in cs.allInyection:
  838. iny = cs.allInyection[inyId]
  839. response = requests.get(URL_SERVER + 'requests?set_inyector&username=' + USERNAME + '&password=' + PASSWORD +
  840. "&username=Prueba1&password=goldfinger&program=" + str(iny.program) + "&who=1&flow=" + str(iny.flow) +
  841. "&time_on=" + str(iny.time_on) + "&litres_pulse=" + str(iny.litres_pulse) + "&max_deviation=" + str(iny.max_deviation) +
  842. "&simulator=" + str(iny.simulator) + "&function=" + str(iny.function))
  843. dataJson = response.json()
  844. return (dataJson)
  845.  
  846. def send_set_config_alarms():
  847. cA = cs.alarm_config
  848. response = requests.get(URL_SERVER + 'requests?set_config_alarms&username=' + USERNAME + '&password=' + PASSWORD +
  849. "&who=1&deviation_warning_max_error_flow=" + str(cA.deviation_warning_max_error_flow) +
  850. "&function_alarm_ec_ph_dangerous=" + str(cA.function_alarm_ec_ph_dangerous) +
  851. "&delay_alarms_ec_ph_secs=" + str(cA.delay_alarms_ec_ph_secs) +
  852. "&delay_alarm_ph_dangerous_secs=" + str(cA.delay_alarm_ph_dangerous_secs) +
  853. "&delay_alarm_ec_dangerous_secs=" + str(cA.delay_alarm_ec_dangerous_secs) +
  854. "&delay_alarm_high_pressure_kg=" + str(cA.delay_alarm_high_pressure_kg) +
  855. "&delay_alarm_low_pressure_secs=" + str(cA.delay_alarm_low_pressure_secs) +
  856. "&delay_alarm_flow_secs=" + str(cA.delay_alarm_flow_secs) +
  857. "&max_diff_warning_error_ec=" + str(cA.max_diff_warning_error_ec) +
  858. "&max_diff_warning_error_ph=" + str(cA.max_diff_warning_error_ph) +
  859. "&max_deviation_under_ph=" + str(cA.max_deviation_under_ph) +
  860. "&max_deviation_over_ec=" + str(cA.max_deviation_over_ec) +
  861. "&level_alarm_high_pressure_kg=" + str(cA.level_alarm_high_pressure_kg) +
  862. "&level_alarm_low_pressure_kg=" + str(cA.level_alarm_low_pressure_kg) +
  863. "&function_alarm_fertilizer_discontinued=" + str(cA.function_alarm_fertilizer_discontinued) +
  864. "&function_alarm_high_pressure=" + str(cA.function_alarm_high_pressure) +
  865. "&function_alarm_dangerous_flow=" + str(cA.function_alarm_dangerous_flow) +
  866. "&function_alarm_no_fertilization=" + str(cA.function_alarm_no_fertilization) +
  867. "&function_alarm_no_water=" + str(cA.function_alarm_no_water) +
  868. "&pulses_fertilizer_no_control=" + str(cA.pulses_fertilizer_no_control) +
  869. "&pulses_needs_fertilizer=" + str(cA.pulses_needs_fertilizer) +
  870. "&max_seconds_between_water_pulses=" + str(cA.max_seconds_between_water_pulses) +
  871. "&over_dangerous_flow_percentage=" + str(cA.over_dangerous_flow_percentage) +
  872. "&delay_secs_if_diff_ec_more_1=" + str(cA.delay_secs_if_diff_ec_more_1) +
  873. "&delay_secs_if_diff_ec_more_05=" + str(cA.delay_secs_if_diff_ec_more_05) +
  874. "&delay_secs_if_diff_ec_more_03=" + str(cA.delay_secs_if_diff_ec_more_03) +
  875. "&coefficient_correction_ec_more_1=" + str(cA.coefficient_correction_ec_more_1) +
  876. "&coefficient_correction_ec_more_05=" + str(cA.coefficient_correction_ec_more_05) +
  877. "&coefficient_correction_ec_more_03=" + str(cA.coefficient_correction_ec_more_03) +
  878. "&delay_secs_if_diff_ph_more_1=" + str(cA.delay_secs_if_diff_ph_more_1) +
  879. "&delay_secs_if_diff_ph_more_05=" + str(cA.delay_secs_if_diff_ph_more_05) +
  880. "&delay_secs_if_diff_ph_more_03=" + str(cA.delay_secs_if_diff_ph_more_03) +
  881. "&coefficient_correction_ph_more_1=" + str(cA.coefficient_correction_ph_more_1) +
  882. "&coefficient_correction_ph_more_05=" + str(cA.coefficient_correction_ph_more_05) +
  883. "&coefficient_correction_ph_more_03=" + str(cA.coefficient_correction_ph_more_03) +
  884. "&secs_first_ec_correction=" + str(cA.secs_first_ec_correction) +
  885. "&secs_first_ph_correction=" + str(cA.secs_first_ph_correction))
  886. dataJson = response.json()
  887. return (dataJson)
  888.  
  889. def send_config_input_output():
  890. cIO = cs.config_io
  891. cIO.analog_input_5 = "0,0,0,0"
  892. response = requests.get(URL_SERVER + 'requests?set_io_config&username=' + USERNAME + '&password=' + PASSWORD +
  893. "&who=1&inyection=" + str(cIO.inyection) + "&filters=" + str(cIO.filters) +
  894. "&actuators=" + str(cIO.actuators) + "&valves1to16=" + str(cIO.valves1to16) +
  895. "&valves17to32=" + str(cIO.valves17to32) + "&valves33to48=" + str(cIO.valves33to48) +
  896. "&valves49to64=" + str(cIO.valves49to64) + "&valves65to80=" + str(cIO.valves65to80) +
  897. "&analog_input_1=" + str(cIO.analog_input_1) + "&analog_input_2=" + str(cIO.analog_input_2) +
  898. "&analog_input_3=" + str(cIO.analog_input_3) + "&analog_input_4=" + str(cIO.analog_input_4) +
  899. "&analog_input_5=" + str(cIO.analog_input_5))
  900. dataJson = response.json()
  901. return dataJson
  902.  
  903. def send_backflush():
  904. bf = cs.back_flushing
  905. response = requests.get(URL_SERVER + 'requests?set_backflushing_config&username=' + USERNAME + '&password=' + PASSWORD +
  906. '&who=1&time_between_station_sec=' + str(bf.time_between_station_sec) +
  907. '&pause_between_filtering_secs=' + str(bf.pause_between_filtering_secs) +
  908. '&amount_of_filters=' + str(bf.amount_of_filters) +
  909. '&difference_backflush_kg=' + str(bf.difference_backflush_kg) +
  910. '&delay_differential_pressure=' + str(bf.delay_differential_pressure) +
  911. '&wait_after_sustain=' + str(bf.wait_after_sustain) +
  912. '&times_wash_before_pd_alarm=' + str(bf.times_wash_before_pd_alarm) +
  913. '&time_between_flushing_hours=' + str(bf.time_between_flushing_hours) +
  914. '&time_between_flushing_minutes=' + str(bf.time_between_flushing_minutes))
  915. dataJson = response.json()
  916. return dataJson
  917.  
  918. def send_solape():
  919. solape = cs.solape
  920. response = requests.get(URL_SERVER + 'requests?set_solape_config&username=' + USERNAME + '&password=' + PASSWORD + '&who=1&solape=' + solape.solape + '&time=' + solape.time + '&flow=' + solape.flow)
  921. dataJson = response.json()
  922. return dataJson
  923.  
  924. def send_other():
  925. other = cs.other
  926. response = requests.get(URL_SERVER + 'requests?set_other_config&username=' + USERNAME + '&password=' + PASSWORD +
  927. '&who=1&booster_pump=' + str(other.booster_pump) +
  928. '&flow_meter_1=' + str(other.flow_meter_1) +
  929. '&flow_meter_2=' + str(other.flow_meter_2) +
  930. '&time_restart_program_1=' + str(other.time_restart_program_1) +
  931. '&time_restart_program_2=' + str(other.time_restart_program_2) +
  932. '&blower_1=' + str(other.blower_1) +
  933. '&blower_2=' + str(other.blower_2) +
  934. '&toff_inyector=' + str(other.toff_inyector) +
  935. '&manual_irrigation_units=' + str(other.manual_irrigation_units) +
  936. '&manual_irrigation_water_1=' + str(other.manual_irrigation_water_1) +
  937. '&manual_irrigation_water_2=' + str(other.manual_irrigation_water_2) +
  938. '&manual_irrigation_program=' + str(other.manual_irrigation_program))
  939. dataJson = response.json()
  940. return dataJson
  941.  
  942. def send_alarm():
  943. alarm = cs.alarm
  944. response = requests.get(URL_SERVER + 'requests?set_alarms&username=' + USERNAME + '&password=' + PASSWORD +
  945. '&who=1&fertilization_deficit=' + alarm.fertilization_deficit +
  946. '&fertilization_desc=' + alarm.fertilization_desc +
  947. '&ec_error=' + str(alarm.ec_error) +
  948. '&ph_error=' + str(alarm.ph_error) +
  949. '&ec_danger=' + str(alarm.ec_danger) +
  950. '&ph_danger=' + str(alarm.ph_danger) +
  951. '&low_pressure=' + str(alarm.low_pressure) +
  952. '&high_pressure=' + str(alarm.high_pressure) +
  953. '&flow_error=' + str(alarm.flow_error) +
  954. '&no_water=' + str(alarm.no_water) +
  955. '&dangerous_flow=' + str(alarm.dangerous_flow) +
  956. '&irrigation_out_of_controls=' + str(alarm.irrigation_out_of_controls))
  957. dataJson = response.json()
  958. return dataJson
  959.  
  960. def read_from_terminal_stats():
  961. termStats = TerminalStats()
  962. byteList = read_registers(BASE_FERT, 1)
  963. termStats.fertilization_program = byteList[0]
  964. byteList = read_registers(BASE_NEXT_PROG, 1)
  965. termStats.next_irrigation_program = byteList[0]
  966. byteList = read_registers(BASE_IRRIGATION, 5)
  967. termStats.irrigation_program = byteList[0]
  968. termStats.irrigation_1 = byteList[1]
  969. termStats.irrigation_2 = byteList[2]
  970. termStats.preirrigation_1 = byteList[3]
  971. termStats.preirrigation_2 = byteList[4]
  972. termStats.postirrigation_1 = byteList[6]
  973. termStats.postirrigation_2 = byteList[7]
  974. termStats.units = byteList[8]
  975. byteList = read_registers(BASE_M3, 2)
  976. termStats.cubic_meters = float(byteList[0])/10
  977. byteList = read_registers(BASE_IRRIGATION_TIME, 2)
  978. termStats.irrigation_time_1 = byteList[0]
  979. termStats.irrigation_time_2 = byteList[1]
  980. termStats.irrigation_time_3 = byteList[2]
  981. byteList = read_registers(BASE_TOTAL_IRRIGATION, 2)
  982. test = byteList[0] * 256 + byteList[1]
  983. test += float(byteList[2])/100
  984. termStats.cubic_meters = test
  985. byteList = read_registers(BASE_MEASURED_FLOW, 2)
  986. test = byteList[0] * 256 + byteList[1]
  987. test += float(byteList[2])/100
  988. termStats.flow_measured_m3_h = test
  989. byteList = read_registers(BASE_C_PROG, 1)
  990. termStats.c_prog = byteList[1]
  991. byteList = read_registers(BASE_P1_P2, 1)
  992. termStats.p1 = float(byteList[0])/10
  993. termStats.p2 = float(byteList[1])/10
  994. byteList = read_registers(BASE_EC_PH_MEASURED, 1)
  995. termStats.ec_measured = float(byteList[0])/10
  996. termStats.ph_measured = float(byteList[1])/10
  997. byteList = read_registers(BASE_EC_PH_ASKED, 1)
  998. termStats.ec_asked = float(byteList[1])/10
  999. termStats.ph_asked = float(byteList[0])/10
  1000. byteList = read_registers(BASE_EC_PH_AVERAGE, 1)
  1001. termStats.ec_average = float(byteList[0])/10
  1002. termStats.ph_average = float(byteList[1])/10
  1003. byteList = read_registers(BASE_VALVES_STATS, 5)
  1004. termStats.valves = []
  1005. for i in byteList:
  1006. y = 0
  1007. while y < 8:
  1008. termStats.valves.append(is_set(i, y))
  1009. y += 1
  1010. termStats.valves = ','.join(str(e) for e in termStats.valves)
  1011. byteList = []
  1012. byteList += read_registers(BASE_INYECTOR_STATS_1_2, 8)
  1013. byteList += read_registers(BASE_INYECTOR_STATS_1_2+16, 8)
  1014. byteList += read_registers(BASE_INYECTOR_STATS_3, 6)
  1015. byteList += read_registers(BASE_INYECTOR_STATS_3+12, 6)
  1016. byteList += read_registers(BASE_INYECTOR_STATS_4, 8)
  1017. byteList += read_registers(BASE_INYECTOR_STATS_5, 6)
  1018. byteList += read_registers(BASE_INYECTOR_STATS_5+12, 6)
  1019. termStats.inyectors = []
  1020. i, f = 0, 0
  1021. while f < 8:
  1022. iny = InyectorStats()
  1023. iny.inyector = f + 1
  1024. helper = byteList[0 +f*2 ] * 256 + byteList[1 + f*2]
  1025. iny.prop_required = helper
  1026. helper = byteList[16 + f*2] * 256 + byteList[17 + f*2]
  1027. iny.prop_required_ec_ph = helper
  1028. helper = byteList[32 + f*3] * 256 + byteList[33 + f*3]
  1029. helper += float(byteList[34 + f*3])/100
  1030. iny.required_flow = helper
  1031. helper = byteList[56 + f*2] * 256 + byteList[57 + f*2]
  1032. iny.required_volume = float(helper)/10
  1033. helper = byteList[72 + f*3] * 256 + byteList[73 + f*3]
  1034. helper += float(byteList[74 + f*3])/100
  1035. iny.total_inyected = helper
  1036. termStats.inyectors.append(iny)
  1037. i = i + 12
  1038. f = f + 1
  1039. return termStats
  1040.  
  1041. def send_terminal_stats():
  1042. stats = read_from_terminal_stats()
  1043. if stats != None:
  1044. response = requests.get(URL_SERVER + 'requests?set_stats&username=' + USERNAME + '&password=' + PASSWORD +
  1045. '&irrigation_program=' + str(stats.irrigation_program) +
  1046. '&fertilization_program=' + str(stats.fertilization_program) +
  1047. '&next_irrigation_program=' + str(stats.next_irrigation_program) +
  1048. '&irrigation_1=' + str(stats.irrigation_1) +
  1049. '&irrigation_2=' + str(stats.irrigation_2) +
  1050. '&preirrigation_1=' + str(stats.preirrigation_1) +
  1051. '&preirrigation_2=' + str(stats.preirrigation_2) +
  1052. '&postirrigation_1=' + str(stats.postirrigation_1) +
  1053. '&postirrigation_2=' + str(stats.postirrigation_2) +
  1054. '&units=' + str(stats.units) +
  1055. '&irrigation_time_1=' + str(stats.irrigation_time_1) +
  1056. '&irrigation_time_2=' + str(stats.irrigation_time_2) +
  1057. '&irrigation_time_3=' + str(stats.irrigation_time_3) +
  1058. '&cubic_meters=' + str(stats.cubic_meters) +
  1059. '&irrigation_before_suspend_1=' + str(stats.irrigation_before_suspend_1) +
  1060. '&irrigation_before_suspend_2=' + str(stats.irrigation_before_suspend_2) +
  1061. '&flow_measured_m3_h=' + str(stats.flow_measured_m3_h) +
  1062. '&p1_kg=' + str(stats.p1) +
  1063. '&p2_kg=' + str(stats.p2) +
  1064. '&condition_program=' + str(stats.c_prog) +
  1065. '&ec_asked=' + str(stats.ec_asked) +
  1066. '&ph_asked=' + str(stats.ph_asked) +
  1067. '&ec_medium=' + str(stats.ec_measured) +
  1068. '&ph_medium=' + str(stats.ph_measured) +
  1069. '&ec_average=' + str(stats.ec_average) +
  1070. '&ph_average=' + str(stats.ph_average) +
  1071. '&valves=' + str(stats.valves))
  1072. dataJson = response.json()
  1073. for i in stats.inyectors:
  1074. response = requests.get(URL_SERVER + 'requests?set_inyector_stats&username=' + USERNAME + '&password=' + PASSWORD +
  1075. '&program=' + str(i.inyector) +
  1076. '&prop_required=' + str(i.prop_required) +
  1077. '&prop_required_ec_ph=' + str(i.prop_required_ec_ph) +
  1078. '&required_flow=' + str(i.required_flow) +
  1079. '&required_volume=' + str(i.required_volume) +
  1080. '&total_inyected=' + str(i.total_inyected))
  1081. return dataJson
  1082.  
  1083. def on_controller_modifier():
  1084. data = fetch_json()
  1085. cs.load_from_json(data)
  1086. read_from_backflushing()
  1087. read_from_solape()
  1088. read_from_controller_config_alarms()
  1089. read_from_controller_input_output()
  1090. read_from_other_configs()
  1091. for i in range(0, TOTAL_FERT):
  1092. read_from_controller_fertilization(i + 1)
  1093. for i in range(0, TOTAL_IRR):
  1094. read_from_controller_irrigation(i + 1)
  1095. for i in range(0, TOTAL_INY):
  1096. read_from_controller_inyectors(i + 1)
  1097.  
  1098. def calculate_crc(listCRC):
  1099. i = 0
  1100. rot = 0
  1101. result = 0xFFFF
  1102. while (i < len(listCRC)):
  1103. result = result ^ listCRC[i]
  1104. while (rot < 8):
  1105. if (result & 0x0001) == 1:
  1106. result = result >> 1
  1107. result = result ^ 0xA001
  1108. else:
  1109. result = result >> 1
  1110. rot = rot + 1
  1111. rot = 0
  1112. i = i + 1
  1113. CRCH = int(result / 256)
  1114. CRCL = result % 256
  1115. listCRC = [CRCL, CRCH]
  1116. return listCRC
  1117.  
  1118. def bytes2integer(stream):
  1119. bytes_in = []
  1120. a = 0
  1121. for b in stream:
  1122. bytes_in.append(int(hex(b), 16))
  1123. return bytes_in
  1124.  
  1125. correctLogin = False
  1126. cs = ControllerState()
  1127. if os.path.isfile(FILEPATH_SAVE):
  1128. cs = ControllerState.load_from_file(FILEPATH_SAVE)
  1129. tickCounter, tickCounterErr = 0, 0
  1130. statsCounter = 0
  1131.  
  1132. def main_loop():
  1133. global correctLogin, tickCounter, tickCounterErr, cs, statsCounter
  1134. if not correctLogin:
  1135. correctLogin = check_login()
  1136. if correctLogin:
  1137. print("login ok")
  1138. else:
  1139. print("login error")
  1140. else:
  1141. statsCounter += 1
  1142. # mandar cada 2 updates, aprox 4s
  1143. if statsCounter % 2 == 0:
  1144. send_terminal_stats()
  1145. if read_dirty():
  1146. print("modified on controller")
  1147. on_controller_modifier()
  1148. send_server()
  1149. write_dirty()
  1150. else:
  1151. updatedbywhen = fetch_last_update()
  1152. lastUpdate = updatedbywhen[0]
  1153. who = updatedbywhen[1]
  1154. print(str(tickCounter) + ": " + str(updatedbywhen))
  1155. tickCounter += 1
  1156. if cs.last_update != int(lastUpdate) and who == 0:
  1157. print("modified on android")
  1158. data = fetch_json()
  1159. new_cs = ControllerState()
  1160. new_cs.load_from_json(data)
  1161. what = cs.what_to_upload(new_cs)
  1162. cs = new_cs
  1163. cs.save_to_file(FILEPATH_SAVE)
  1164. for x in what["irrigation"]:
  1165. write_controller_irrigation(x)
  1166. for x in what["fertilization"]:
  1167. write_controller_fertilization(x)
  1168. for x in what["inyection"]:
  1169. write_controller_inyection(x)
  1170. if what["backflush"]:
  1171. write_controller_backflush()
  1172. if what["other"]:
  1173. write_other_configs()
  1174. if what["config_io"]:
  1175. write_controller_input_output()
  1176. if what["alarm_config"]:
  1177. write_controller_config_alarms()
  1178. if what["solape"]:
  1179. write_controller_solape()
  1180. while True:
  1181. try:
  1182. main_loop()
  1183. tickCounterErr = 0
  1184. except Exception as ex:
  1185. print("exception, restarting")
  1186. print(ex)
  1187. tickCounterErr += 1
  1188. if tickCounterErr >= 5:
  1189. print("exception count too high, exiting")
  1190. exit()
  1191. time.sleep(TIME_UPDATE)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement