Advertisement
Guest User

Untitled

a guest
Oct 15th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.48 KB | None | 0 0
  1. import time
  2. import serial
  3. import json
  4. import requests
  5. import serial
  6. from ControllerState import ControllerState
  7.  
  8. Test = serial.Serial("COM3",9600,timeout=0.2)
  9. BASE_PROGFERT = 1600 + 8192
  10. BASE_PROGRIEGO = 0 + 8192
  11. BASE_CONFIGINYECTORES = 1960 + 8192
  12. DIRTY_ADD = 4234
  13. URL_SERVER = 'http://emiliozelione2018.pythonanywhere.com/'
  14. USERNAME = "Prueba1"
  15. PASSWORD = "goldfinger"
  16. TIME_UPDATE = 2
  17. FILEPATH_SAVE = "controller.bin"
  18.  
  19.  
  20. def fetchJson():
  21. response = requests.get(
  22. URL_SERVER + 'requests?all&username=' + USERNAME +
  23. '&password=' +
  24. PASSWORD)
  25. dataJson = response.json()
  26. return(dataJson)
  27.  
  28. def fetchLastUpdate():
  29. response = requests.get(
  30. URL_SERVER +
  31. 'requests?updated_when&username=' +
  32. USERNAME +
  33. '&password=' +
  34. PASSWORD)
  35. dataJson = response.json()
  36. return(dataJson['update'])
  37.  
  38. def checkLogin():
  39. response = requests.get(
  40. URL_SERVER +
  41. 'login?username=' +
  42. USERNAME +
  43. '&password=' +
  44. PASSWORD)
  45. dataJson = response.json()
  46. return(dataJson['ok'])
  47.  
  48. def readDirty():
  49. print(readRegisters(DIRTY_ADD,1))
  50.  
  51.  
  52. def readRegisters(Add,nRegs):
  53. byteList=[]
  54. AddH = int(Add/256)
  55. AddL = Add % 256
  56. Encabezado = [1, 3, AddH, AddL, 0, nRegs] # Son 1Registros
  57. # Tengo la lista en bytes, Aplicar los CRC
  58. byteList = Encabezado + byteList
  59. listaCRC = Calcular_CRC(byteList)
  60. byteList = byteList+listaCRC # Le agrega los bytes de CRC
  61. incoming = []
  62. Total_in = nRegs*2+5
  63. while(len(incoming)<(Total_in)):
  64. Test.write(byteList)
  65. Test.flush()
  66. incoming = Test.read(Total_in)
  67. BytesIn = BytesToInteger(incoming)
  68. print("Bytes Leidos "+str(BytesIn))
  69. CRC_in = BytesIn[(Total_in-2):Total_in]
  70. print("CRC_in " +str(CRC_in))
  71. del BytesIn[-2:] # borra los 2 ultimos elementos
  72. listaCRC = Calcular_CRC(BytesIn)
  73. print("Lista CRC " +str(listaCRC))
  74. if(listaCRC==CRC_in):
  75. del BytesIn[0:3] # borra del 0 al 3 no inclusive
  76. return BytesIn
  77. else:
  78. return None
  79.  
  80. def readFromcontrollerConfigIny(ci):
  81. FertProg =cs.allFertilization[pf]
  82. print("Prog Fertilizacion " +str(pf))
  83. Add = BASE_PROGFERT + (pf-1)*18
  84. byteList=[]
  85. byteList=readRegisters(Add,9)
  86. print(byteList)
  87.  
  88.  
  89. def readFromcontrollerPFert(pf):
  90. FertProg =cs.allFertilization[pf]
  91. print("Prog Fertilizacion " +str(pf))
  92. Add = BASE_PROGFERT + (pf-1)*18
  93. byteList=[]
  94. byteList=readRegisters(Add,9)
  95. print(byteList)
  96. i = 0
  97. Val = [0]*10
  98. while(i < 8):
  99. Val[i] = (byteList[2*i]*256+byteList[2*i+1])
  100. i = i+1
  101. Val[8] = byteList[16]/10
  102. Val[9] = byteList[17]/10
  103. print("La lista Val " + str(Val))
  104.  
  105. def readFromcontrollerPRiego(pr):
  106. ProgRiego = cs.allIrrigation[pr]
  107. print("Prog Riego " +str(pr))
  108. Add = BASE_PROGRIEGO + (pr-1)*32
  109. listaA = readRegisters(Add,8)
  110. print(listaA)
  111. ProgRiego.water_total_1=listaA[0]
  112. ProgRiego.water_total_2=listaA[1]
  113. ProgRiego.water_before_1=listaA[2]
  114. ProgRiego.water_before_2=listaA[3]
  115. ProgRiego.water_after_1=listaA[4]
  116. ProgRiego.water_after_2=listaA[5]
  117. ProgRiego.time_between_1=listaA[6]
  118. ProgRiego.time_between_2=listaA[7]
  119. ProgRiego.time_start_1=listaA[8]
  120. ProgRiego.time_start_2=listaA[9]
  121. ProgRiego.units=listaA[10]
  122. ProgRiego.fertilization_program=listaA[11]
  123. ProgRiego.kicks=listaA[12]
  124. ProgRiego.condition_program=listaA[13]
  125. Add = BASE_PROGRIEGO + (pr-1)*32 +16
  126. listaB = readRegisters(Add,8)
  127. print(listaB)
  128. del listaB[0:5] # borra del 0 al 5 no inclusive
  129. ProgRiego.valves= DecoValves(listaB)
  130. print(ProgRiego.valves)
  131.  
  132.  
  133.  
  134.  
  135.  
  136. def DecoValves(ListaValves):
  137. CantValv = 0
  138. CadValv = ""
  139. i = 0
  140. while(i < 9): # Registros
  141. j = 0
  142. while(j < 8): # bits
  143. peso = ListaValves[i] & (2**j)
  144. valv = (i*8)+j+1
  145. if(peso != 0):
  146. if(CantValv == 0):
  147. CadValv = str(valv)
  148. else:
  149. CadValv = CadValv + ',' + str(valv)
  150. CantValv = CantValv+1
  151. j = j+1
  152. i = i+1
  153. return(CadValv)
  154.  
  155. def writeRegisters(Add,nRegs,byteList):
  156. AddH = int(Add/256)
  157. AddL = Add % 256
  158. Encabezado = [1, 16, AddH, AddL, 0,nRegs,nRegs*2]
  159. byteList = Encabezado + byteList
  160. listaCRC = Calcular_CRC(byteList)
  161. byteList = byteList+listaCRC
  162. # Le agrega los bytes de CRC
  163. incoming = []
  164. Total_in = 8
  165. # Cuando Escribis Recibis 8 Bytes Fijos
  166. while(len(incoming)<(Total_in)):
  167. Test.write(byteList)
  168. Test.flush()
  169. incoming = Test.read(Total_in)
  170. BytesIn = BytesToInteger(incoming)
  171. print(BytesIn)
  172. CRC_in = BytesIn[(Total_in-2):Total_in]
  173. print(CRC_in)
  174. del BytesIn[-2:] # borra los 2 ultimos elementos
  175. listaCRC = Calcular_CRC(BytesIn)
  176. print(listaCRC)
  177. print(" incoming : "+str(len(incoming)))
  178. if(listaCRC==CRC_in):
  179. return True
  180. else:
  181. return False
  182.  
  183.  
  184.  
  185. def writeTocontrollerConfigIny(iny):
  186. byteList = []
  187. Inyector = cs.allInyection[iny]
  188. CaudMaxVenturi = Inyector.flow
  189. TON = Inyector.time_on
  190. mlPulse = Inyector.litres_pulse
  191. MaxDesvio = Inyector.max_deviation
  192. Simular = Inyector.simulator
  193. print(Inyector)
  194. byteList.insert(0,int(iny))
  195. byteList.insert(1,int(CaudMaxVenturi/256))
  196. byteList.insert(2,int(CaudMaxVenturi % 256))
  197. byteList.insert(3,int(TON))
  198. byteList.insert(4,int(mlPulse/256))
  199. byteList.insert(5,int(mlPulse % 256))
  200. byteList.insert(6,int(Simular))
  201. byteList.insert(7,int(MaxDesvio))
  202. Add = BASE_CONFIGINYECTORES+(iny-1)*8
  203. print("Config Iny :"+str(iny))
  204. print(byteList)
  205. print(writeRegisters(Add,4,byteList))
  206.  
  207.  
  208.  
  209. def writeTocontrollerPRiego(pr):
  210. byteList = []
  211. ProgRiego = cs.allIrrigation[pr]
  212.  
  213. lista_Valv = ProgRiego.valves.split(',')
  214. print(type(lista_Valv[0]))
  215. print("Lista Valvulas " + str(lista_Valv))
  216. RegistrosValvulas = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
  217. i = 0
  218. for elem in lista_Valv:
  219. bit = (int(elem)-1) % 8
  220. indice = int((int(elem)-1)/8)
  221. RegistrosValvulas[indice] = RegistrosValvulas[indice] | (2**bit)
  222. i = i+1
  223. i = 0
  224. byteList.insert(0,ProgRiego.water_total_1)
  225. byteList.insert(1,ProgRiego.water_total_2)
  226. byteList.insert(2,ProgRiego.water_before_1)
  227. byteList.insert(3,ProgRiego.water_before_2,)
  228. byteList.insert(4,ProgRiego.water_after_1)
  229. byteList.insert(5,ProgRiego.water_after_2)
  230. byteList.insert(6,ProgRiego.time_between_1)
  231. byteList.insert(7,ProgRiego.time_between_2)
  232. byteList.insert(8,ProgRiego.time_start_1)
  233. byteList.insert(9,ProgRiego.time_start_2)
  234. byteList.insert(10,ProgRiego.units)
  235. byteList.insert(11,ProgRiego.fertilization_program)
  236. byteList.insert(12,ProgRiego.kicks)
  237. byteList.insert(13,ProgRiego.condition_program)
  238. byteList.insert(14, 0) # Domingo
  239. byteList.insert(15, 0) # Lunes
  240. Add = BASE_PROGRIEGO + (pr-1)*32
  241. print(writeRegisters(Add,8,byteList))
  242. byteList = []
  243. byteList.insert(0, 0) # Martes
  244. byteList.insert(1, 0) # Miercoles
  245. byteList.insert(2, 0) # Jueves
  246. byteList.insert(3, 0) # Viernes
  247. byteList.insert(4, 0) # Sabado
  248. i = 0
  249. while(i < 10):
  250. byteList.append(RegistrosValvulas[i])
  251. i = i+1
  252. byteList.append(0) # Campo Reservado
  253. Add = BASE_PROGRIEGO+(pr-1)*32+16
  254. print(writeRegisters(Add,8,byteList))
  255.  
  256.  
  257. def sendServer():
  258. return None
  259.  
  260.  
  261.  
  262. def writeTocontrollerPFert(pf):
  263. FertProg = cs.allFertilization[pf]
  264. print("Programa de Ferti "+str(pf))
  265. newList = []
  266. byteList = []
  267. newList.append(int(FertProg.values_1))
  268. newList.append(int(FertProg.values_2))
  269. newList.append(int(FertProg.values_3))
  270. newList.append(int(FertProg.values_4))
  271. newList.append(int(FertProg.values_5))
  272. newList.append(int(FertProg.values_6))
  273. newList.append(int(FertProg.values_7))
  274. newList.append(int(FertProg.values_8))
  275. EC = FertProg.ec *10
  276. pH = FertProg.ph *10
  277. newList.append(int(EC)*256+int(pH))
  278. print("newlist "+str(newList))
  279. i = 0
  280. for elem in newList:
  281. byteList.append(int(newList[i]/256))
  282. byteList.append(newList[i] % 256)
  283. i = i+1
  284. Add = BASE_PROGFERT+(pf-1)*18
  285. print("Direccion " +str(Add))
  286. print("byteList :"+str(byteList))
  287. print(writeRegisters(Add,9,byteList))
  288.  
  289.  
  290.  
  291.  
  292. def Calcular_CRC(listCRC):
  293. i = 0
  294. rot = 0
  295. result = 0xFFFF
  296.  
  297. while(i < len(listCRC)):
  298. result = result ^ listCRC[i]
  299. while(rot < 8):
  300. if(result & 0x0001) == 1:
  301. result = result >> 1
  302. result = result ^ 0xA001
  303. else:
  304. result = result >> 1
  305. rot = rot + 1
  306. rot = 0
  307. i = i+1
  308.  
  309. CRCH = int(result/256)
  310. CRCL = result % 256
  311. listCRC = [CRCL, CRCH]
  312. return(listCRC)
  313.  
  314.  
  315. def BytesToInteger(stream):
  316. bytes_in = []
  317. a = 0
  318. for b in stream:
  319. bytes_in.append(int(hex(stream[a]), 16))
  320. a = a+1
  321. return(bytes_in)
  322.  
  323. correctLogin = False
  324. cs = ControllerState.load_from_file(FILEPATH_SAVE)
  325. if cs == None:
  326. cs = ControllerState()
  327.  
  328.  
  329. # c.save_to_file(FILEPATH_SAVE)
  330. # cs = ControllerState.load_from_file(FILEPATH_SAVE)
  331. # physicalController = serial.Serial("COM3", 9600)
  332.  
  333. while(True):
  334. if correctLogin == False:
  335. correctLogin = checkLogin()
  336. print("login ok")
  337. else:
  338. # TODO check el flag del controlador, y si hay que actualizar algo, actualizar.
  339. #readDirty()
  340.  
  341.  
  342. #if dirtyFlag():
  343.  
  344. #getModifiedDataToController(cs)
  345. #sendServer()
  346.  
  347. lastUpdate = fetchLastUpdate()
  348. if cs.last_update != int(lastUpdate):
  349. data = fetchJson()
  350. cs.load_from_json(data)
  351. # sincronizar con controlasdor
  352. for key in cs.allIrrigation:
  353. #writeTocontrollerPRiego(key)
  354. readFromcontrollerPRiego(key)
  355. #for key in cs.allInyection:
  356. # writeTocontrollerConfigIny(key)
  357. #for key in cs.allFertilization:
  358. #readFromcontrollerPFert(key)
  359. # writeTocontrollerPFert(key)
  360. print("updated")
  361. time.sleep(TIME_UPDATE)
  362.  
  363. if correctLogin:
  364. cs.save_to_file(FILEPATH_SAVE)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement