Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 23.89 KB | None | 0 0
  1. # !/usr/bin/python
  2.  
  3. import time
  4. import serial
  5. from xml.dom import minidom
  6. import os
  7. import xml.etree.ElementTree as ET
  8.  
  9. # Global variables
  10.  
  11. # bcast, loopvalue and zmode will hold the previous values
  12. bcast = ""
  13. loopvalue = ""
  14. zmode = []
  15. ton = []
  16. toff = []
  17. tdim = []
  18.  
  19. # newbcast, newloopvalue and newzmode will hold the current values and compare it with bcast and loopvalue for change
  20. newbcast = ""
  21. newloopvalue = ""
  22. newzmode = []
  23. newton = []
  24. newtoff = []
  25. newtdim = []
  26.  
  27. # Try variables are used for counting number of tries made
  28. autotry = 0
  29. autotry1 = 0
  30. overtry = 0
  31. overtry1 = 0
  32. timeontry = 0
  33. timeonetry1 = 0
  34. timeofftry = 0
  35. timeofftry1 = 0
  36. dimtry = 0
  37. dimtry1 = 0
  38. bcasttry = 0
  39. bcasttry1 = 0
  40. discoverytry = 0
  41. discoverytry1 = 0
  42. looptry = 0
  43.  
  44. # Class to run only once at the start - to get and assign the initial values for bcast and loopvalue.
  45. # Source: https://stackoverflow.com/questions/29764090/how-to-call-a-function-only-once-in-python/29764355
  46. class CallOnce(object):
  47.     called = False
  48.     def service(cls):
  49.         if cls.called:
  50.             print "Already Called"
  51.             return
  52.         else:
  53.             # Get Broadcast Value
  54.             mydoc = minidom.parse("/var/www/html/bcast.xml")
  55.             values=mydoc.getElementsByTagName('mode')
  56.             for elem in values:
  57.                 x = elem.firstChild.data
  58.                 global bcast
  59.                 bcast=int(x)
  60.             # Get Loop Value
  61.             tree = ET.parse('/var/www/html/ZoneUp.xml')
  62.             root = tree.getroot()
  63.             for child in root:
  64.                 # print(child.tag, child.text)
  65.                 if child.tag == 'LoopFlag':
  66.                     global loopvalue
  67.                     loopvalue = int(child.text)
  68.             tree = ET.parse('/var/www/html/LoopInfo.xml')
  69.             root = tree.getroot()
  70.             global zmode
  71.             global ton
  72.             global toff
  73.             global tdim
  74.             for child in root:
  75.                 # print(child.tag, child.text)
  76.                 for i in range(1,13):
  77.                     zmodestr = 'Z'+str(i)+'Mode'
  78.                     zton = 'Z'+str(i)+'TimeOn'
  79.                     ztof = 'Z'+str(i)+'TimeOff'
  80.                     zdim = 'Z'+str(i)+'Dim'
  81.                     # print zmodestr
  82.                     if child.tag == zmodestr:
  83.                         zmode.append(int(child.text))
  84.                         # print zmode[i-1]
  85.                     elif child.tag == zton:
  86.                         ton.append(int(child.text))
  87.                         # print ton[i-1]
  88.                     elif child.tag == ztof:
  89.                         toff.append(int(child.text))
  90.                         # print toff[i-1]
  91.                     elif child.tag == zdim:
  92.                         tdim.append(int(child.text))
  93.                         # print tdim[i-1]
  94.             cls.called = True
  95.             # For troubleshooting enable this display
  96.             # display()
  97.             return
  98.  
  99. # Function to get new values of Broadcast and Loop
  100. def newval():
  101.     mydoc = minidom.parse("/var/www/html/bcast.xml")
  102.     values=mydoc.getElementsByTagName('mode')
  103.     for elem in values:
  104.         x = elem.firstChild.data
  105.         global newbcast
  106.         newbcast=int(x)
  107.     tree = ET.parse('/var/www/html/ZoneUp.xml')
  108.     root = tree.getroot()
  109.     for child in root:
  110.         # print(child.tag, child.text)
  111.         if child.tag == 'LoopFlag':
  112.             global newloopvalue
  113.             newloopvalue = int(child.text)
  114.     tree = ET.parse('/var/www/html/LoopInfo.xml')
  115.     root = tree.getroot()
  116.     global newzmode
  117.     global newton
  118.     global newtoff
  119.     global newtdim
  120.     newzmode = []
  121.     newton = []
  122.     newtoff = []
  123.     newtdim = []
  124.     for child in root:
  125.         # print(child.tag, child.text)
  126.         for i in range(1,13):
  127.             newzmodestr = 'Z'+str(i)+'Mode'
  128.             newzton = 'Z'+str(i)+'TimeOn'
  129.             newztof = 'Z'+str(i)+'TimeOff'
  130.             newzdim = 'Z'+str(i)+'Dim'
  131.             # print newzmodestr
  132.             if child.tag == newzmodestr:
  133.                 newzmode.append(int(child.text))
  134.                 # print newzmode
  135.             elif child.tag == newzton:
  136.                 newton.append(int(child.text))
  137.                 # print ton[i-1]
  138.             elif child.tag == newztof:
  139.                 newtoff.append(int(child.text))
  140.                 # print toff[i-1]
  141.             elif child.tag == newzdim:
  142.                 newtdim.append(int(child.text))
  143.                 # print tdim[i-1]
  144.     return
  145.  
  146. # Function to display old values
  147. def display():
  148.     print("Old")
  149.     print(bcast)
  150.     print(loopvalue)
  151.     print(zmode)
  152.     print(ton)
  153.     print(toff)
  154.     print(tdim)
  155.  
  156. # Function to display new values
  157. def newdisplay():
  158.     print("New")
  159.     print(newbcast)
  160.     print(newloopvalue)
  161.     print(newzmode)
  162.     print(newton)
  163.     print(newtoff)
  164.     print(newtdim)
  165.  
  166. # Function to do Broadcast On/Off
  167. def broad():
  168.     # print "Starting Program"
  169.     global bcast
  170.     global bcasttry
  171.     global bcasttry1
  172.     bcast = 0
  173.     bcasttry = 0
  174.     bcasttry1 = 0
  175.     ser = serial.Serial('/dev/ttyAMA0', baudrate=9600, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS)
  176.     tree = ET.parse('/var/www/html/bcast.xml')
  177.     root = tree.getroot()
  178.     for child in root:
  179.         for mode in child:
  180.             x = mode.text
  181.             y=int(x)
  182.             # print(y)
  183.             if y == 0:
  184.                 os.system("echo \"\x02\x02\x09\x09\x0D\" > /dev/ttyAMA0")
  185.                 # print 'Broadcast Off Sent'
  186.                 time.sleep(.5)
  187.                 data_left1=ser.inWaiting()
  188.                 # print data_left1
  189.                 # print bcasttry
  190.                 # Check whether there is response or not - 0 indicates No Response and 3 Retries are made
  191.                 if data_left1 == 0:
  192.                     while (bcasttry < 3):
  193.                         if data_left1 == 0:
  194.                             # print bcasttry
  195.                             bcasttry = bcasttry + 1
  196.                             os.system("echo \"\x02\x02\x09\x09\x0D\" > /dev/ttyAMA0")
  197.                             time.sleep(.5)
  198.                             data_left=ser.inWaiting()
  199.                 # On 3 successful retries to get any response, Mode is updated with 3
  200.                 # print bcasttry
  201.                 if bcasttry == 3:
  202.                     tree = ET.parse('/var/www/html/bcast.xml')
  203.                     root = tree.getroot()
  204.                     for child in root:
  205.                         for mode in child:
  206.                             mode.text = '3'
  207.                             tree.write("/var/www/html/bcast.xml")
  208.                     bcastupdate()
  209.                     display()
  210.                 else:
  211.                     data = ser.readline()
  212.                     bcastfind = data.encode('hex')
  213.                     # If data is received, check whether the data is same as sent
  214.                     if bcastfind != '020209090d0a':
  215.                         while (bcasttry1 < 3):
  216.                             # If same break else retry 3 times
  217.                             if bcastfind == '020209090d0a':
  218.                                 break
  219.                             else:
  220.                                 bcasttry1 = bcasttry1 + 1
  221.                                 os.system("echo \"\x02\x02\x09\x09\x0D\" > /dev/ttyAMA0")
  222.                                 # print 'Bcast Sent'
  223.                                 data = ser.readline()
  224.                                 bcastfind = data.encode('hex')
  225.                     # On 3 successful retries to get proper response, Mode is updated with 4
  226.                     if bcasttry1 == 3:
  227.                         tree = ET.parse('/var/www/html/bcast.xml')
  228.                         root = tree.getroot()
  229.                         for child in root:
  230.                             for mode in child:
  231.                                 mode.text = '4'
  232.                                 tree.write("/var/www/html/bcast.xml")
  233.                         bcastupdate()
  234.                         display()
  235.             else:
  236.                 os.system("echo \"\x02\x02\x08\x08\x0D\" > /dev/ttyAMA0")
  237.                 # print 'Broadcast Off Sent'
  238.                 time.sleep(.5)
  239.                 data_left1=ser.inWaiting()
  240.                 # print data_left1
  241.                 # print bcasttry
  242.                 # Check whether there is response or not - 0 indicates No Response and 3 Retries are made
  243.                 if data_left1 == 0:
  244.                     while (bcasttry < 3):
  245.                         if data_left1 == 0:
  246.                             # print bcasttry
  247.                             bcasttry = bcasttry + 1
  248.                             os.system("echo \"\x02\x02\x08\x08\x0D\" > /dev/ttyAMA0")
  249.                             time.sleep(.5)
  250.                             data_left=ser.inWaiting()
  251.                 # On 3 successful retries to get any response, Mode is updated with 3
  252.                 # print bcasttry
  253.                 if bcasttry == 3:
  254.                     tree = ET.parse('/var/www/html/bcast.xml')
  255.                     root = tree.getroot()
  256.                     for child in root:
  257.                         for mode in child:
  258.                             mode.text = '3'
  259.                             tree.write("/var/www/html/bcast.xml")
  260.                     bcastupdate()
  261.                     display()
  262.                 else:
  263.                     data = ser.readline()
  264.                     bcastfind = data.encode('hex')
  265.                     # If data is received, check whether the data is same as sent
  266.                     if bcastfind != '020208080d0a':
  267.                         while (bcasttry1 < 3):
  268.                             if bcastfind == '020208080d0a':
  269.                                 break
  270.                             else:
  271.                                 bcasttry1 = bcasttry1 + 1
  272.                                 os.system("echo \"\x02\x02\x08\x08\x0D\" > /dev/ttyAMA0")
  273.                                 # print 'Bcast Sent'
  274.                                 data = ser.readline()
  275.                                 bcastfind = data.encode('hex')
  276.                     # On 3 successful retries to get proper response, Mode is updated with 4
  277.                     if bcasttry1 == 3:
  278.                         tree = ET.parse('/var/www/html/bcast.xml')
  279.                         root = tree.getroot()
  280.                         for child in root:
  281.                             for mode in child:
  282.                                 mode.text = '4'
  283.                                 tree.write("/var/www/html/bcast.xml")
  284.                         bcastupdate()
  285.                         display()
  286.  
  287. # Function to do Discovery and Update XML
  288. def discov():
  289.     # print "Starting Program"
  290.     ser = serial.Serial('/dev/ttyAMA0', baudrate=9600, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS)
  291.     # time.sleep(1)
  292.     try:
  293.         global discoverytry
  294.         global discoverytry1
  295.         discoverytry = 0
  296.         discoverytry1 = 0
  297.         os.system("echo \"\x02\x02\x03\x03\x0D\" > /dev/ttyAMA0")
  298.         # print 'Discovery Sent'
  299.         time.sleep(.5)
  300.         data_left=ser.inWaiting()
  301.         # print data_left
  302.         # print discoverytry
  303.         # Check whether there is response or not - 0 indicates No Response and 3 Retries are made
  304.         if data_left == 0:
  305.             while (discoverytry < 3):
  306.                 if data_left == 0:
  307.                     # print discoverytry
  308.                     discoverytry = discoverytry + 1
  309.                     os.system("echo \"\x02\x02\x03\x03\x0D\" > /dev/ttyAMA0")
  310.                     time.sleep(.5)
  311.                     data_left=ser.inWaiting()
  312.         # On 3 successful retries to get any response, Loopvalue is updated with 512
  313.         # print discoverytry
  314.         if discoverytry == 3:
  315.             # print data_left
  316.             tree = ET.parse('/var/www/html/ZoneUp.xml')
  317.             root = tree.getroot()
  318.             for child in root:
  319.                 # print(child.tag, child.text)
  320.                 if child.tag == 'LoopFlag':
  321.                     child.text = '512'
  322.                     tree.write("/var/www/html/ZoneUp.xml")
  323.                 # print(child.tag, child.text)
  324.             # print 'No Error'
  325.             disupdate()
  326.             display()
  327.         else:
  328.             data1 = ser.read(data_left)
  329.             discover = data1.encode('hex')
  330.             # print discover
  331.             # If data is received, check whether the data is same as sent
  332.             if discover != '020203030d0a':
  333.                 while (discoverytry1 < 3):
  334.                     if discover == '020203030d0a':
  335.                         break
  336.                     else:
  337.                         discoverytry1 = discoverytry1 + 1
  338.                         os.system("echo \"\x02\x02\x03\x03\x0D\" > /dev/ttyAMA0")
  339.                         # print 'Discovery Sent'
  340.                         data1 = ser.readline()
  341.                         discover = data1.encode('hex')
  342.             # On 3 successful retries to get proper response, Loopvalue is updated with 1024
  343.             if discoverytry1 == 3:
  344.                 tree = ET.parse('/var/www/html/ZoneUp.xml')
  345.                 root = tree.getroot()
  346.                 for child in root:
  347.                     # print(child.tag, child.text)
  348.                     if child.tag == 'LoopFlag':
  349.                         child.text = '1024'
  350.                         tree.write("/var/www/html/ZoneUp.xml")
  351.                     # print(child.tag, child.text)
  352.                 # print 'No Error'
  353.                 disupdate()
  354.                 display()
  355.             else:
  356.                 time.sleep(5)
  357.                 os.system("echo \"\x02\x02\x88\x88\x0D\" > /dev/ttyAMA0")
  358.                 # print 'Get Info Sent'
  359.                 while True:
  360.                     time.sleep(1)
  361.                     if ser.inWaiting() > 0:
  362.                         data = ser.readline()
  363.                         # print data
  364.                         loop = data.encode('hex')
  365.                         loopvalue = loop[6:8]
  366.                         # print loopvalue
  367.                         if loopvalue == '01':
  368.                             os.system("echo \"\x02\x02\x88\x88\x0D\" > /dev/ttyAMA0")
  369.                         else:
  370.                             tree = ET.parse('/var/www/html/ZoneUp.xml')
  371.                             root = tree.getroot()
  372.                             for child in root:
  373.                                 # print(child.tag, child.text)
  374.                                 if child.tag == 'LoopFlag':
  375.                                     x = int(loopvalue)
  376.                                     # print x
  377.                                     y = str(x)
  378.                                     # print y
  379.                                     child.text = y
  380.                                     tree.write("/var/www/html/ZoneUp.xml")
  381.                                 # print(child.tag, child.text)
  382.                             # print 'No Error'
  383.                             break
  384.                     else:
  385.                         # If the response is 0 - then 3 retires are made, failing will update Loopvalue to 2048
  386.                         global looptry
  387.                         looptry = looptry + 1
  388.                         if looptry == 3:
  389.                             tree = ET.parse('/var/www/html/ZoneUp.xml')
  390.                             root = tree.getroot()
  391.                             for child in root:
  392.                                 # print(child.tag, child.text)
  393.                                 if child.tag == 'LoopFlag':
  394.                                     child.text = '2048'
  395.                                     tree.write("/var/www/html/ZoneUp.xml")
  396.                                     break
  397.                         else:
  398.                             os.system("echo \"\x02\x02\x88\x88\x0D\" > /dev/ttyAMA0")
  399.  
  400.     except KeyboardInterrupt:
  401.         print "Exiting Program"
  402.  
  403.     except:
  404.         print "Error Occurs"
  405.         return
  406.  
  407.     finally:
  408.         ser.close()
  409.         pass
  410.  
  411. # Function to do Zmode Data and Update XML
  412. def zmodefun(val):
  413.     ser = serial.Serial('/dev/ttyAMA0', baudrate=9600, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS)
  414.     global zmode
  415.     global ton
  416.     global toff
  417.     global tdim
  418.     global newzmode
  419.     global newton
  420.     global newtoff
  421.     global newtdim
  422.     global autotry
  423.     global autotry1
  424.     global overtry
  425.     global overtry1
  426.     global timeontry
  427.     global timeonetry1
  428.     global timeofftry
  429.     global timeofftry1
  430.     global dimtry
  431.     global dimtry1
  432.     if newzmode[val] < 128:
  433.         if len(hex(int(val)+1)) == 3:
  434.             hexval = hex((int(val)+1))
  435.             hexval = hexval[1:2]+'0'+hexval[-1]
  436.             hexval = hexval.upper()
  437.             hexval = '0'+hexval
  438.             # print hexval
  439.         if len(hex(newzmode[val])) == 3:
  440.             hexmode = hex(newzmode[val])
  441.             hexmode = hexmode[1:2]+'0'+hexmode[-1]
  442.             hexmode = hexmode.upper()
  443.             hexmode = '0'+hexmode
  444.             # print hexmode
  445.         else:
  446.             hexmode = hex(newzmode[val])
  447.             hexmode = hexmode.upper()
  448.             # print hexmode
  449.         valcheck = 21+int(val)+1+newzmode[val]
  450.         hexvalcheck = hex(valcheck)
  451.         hexvalcheck = hexvalcheck[1:]
  452.         hexvalcheck = hexvalcheck.upper()
  453.         hexvalcheck = '0'+hexvalcheck
  454.         if len(hexvalcheck) == 3:
  455.             hexvalcheck = hexvalcheck[1:2]+'0'+hexvalcheck[-1]
  456.             hexvalcheck = hexvalcheck.upper()
  457.             hexvalcheck = '0'+hexvalcheck
  458.         # print hexvalcheck
  459.         cmd=['0x02','0x04','0x15',hexval,hexmode,hexvalcheck,'0x0D','0x0A']
  460.         print (cmd)
  461.         incmd = [int(h, base=16) for h in cmd]
  462.         ser.write(serial.to_bytes(incmd))
  463.         time.sleep(.5)
  464.         data_left=ser.inWaiting()
  465.         zmodestr = 'Z'+str(val+1)+'Mode'
  466.         # Check whether there is response or not - 0 indicates No Response and 3 Retries are made
  467.         x = bin(newzmode[val])
  468.         timecheck = x[-1:]
  469.         if timecheck == '1':
  470.                 # print timecheck
  471.                 if ton[val] != newton[val] or toff[val] != newtoff[val]:
  472.                     print 'Time Sent - '+str(val+1)
  473.                 if len(hex(int(val+1))) == 3:
  474.                     hexi = hex(i)
  475.                         hexi = hexi[1:2]+'0'+hexi[-1]
  476.                     hexi = hexi.upper()
  477.                         hexi = '0'+hexi
  478.                         # print hexi
  479.                 decton = newton[val]
  480.                     if len(hex(int(decton))) == 3:
  481.                         hexton = hex(int(decton))
  482.                         hexton = hexton[1:2]+'0'+hexton[-1]
  483.                     hexton = hexton.upper()
  484.                         hexton = '0'+hexton
  485.                         # print hexton
  486.                 else:
  487.                         hexton = hex(int(decton))
  488.                         hexton = hexton[1:]
  489.                     hexton = hexton.upper()
  490.                         hexton = '0'+hexton
  491.                         # print hexton
  492.                 dectoff = newtoff[val]
  493.                 if len(hex(int(dectoff))) == 3:
  494.                     hextof = hex(int(dectoff))
  495.                     hextof = hextof[1:2]+'0'+hextof[-1]
  496.                     hextof = hextof.upper()
  497.                     hextof = '0'+hextof
  498.                     # print hextof
  499.                 else:
  500.                     hextof = hex(int(dectoff))
  501.                     hextof = hextof[1:]
  502.                     hextof = hextof.upper()
  503.                     hextof = '0'+hextof
  504.                     # print hexton
  505.                 check = 17+i+int(decton)+int(dectoff)
  506.                     if len(hex(check)) == 3:
  507.                         hexcheck = hex(check)
  508.                         hexcheck = hexcheck[1:2]+'0'+hexcheck[-1]
  509.                     hexcheck = hexcheck.upper()
  510.                         hexcheck = '0'+hexcheck
  511.                         # print hexcheck
  512.                 else:
  513.                         hexcheck = hex(check)
  514.                         hexcheck = hexcheck[1:]
  515.                     hexcheck = hexcheck.upper()
  516.                         hexcheck = '0'+hexcheck
  517.                         # print hexcheck
  518.                 cmd=['0x02','0x07','0x11',hexi,hexton,'0x00',hextof,'0x00',hexcheck,'0x0D','0x0A']
  519.                 print (cmd)
  520.                     incmd = [int(h, base=16) for h in cmd]
  521.                     ser.write(serial.to_bytes(incmd))
  522.                     zmodeupdate(val)
  523.             else:
  524.                 print 'No Time Change - '+str(val+1)
  525.                 # break
  526.             else:
  527.                 print 'Time Disabled - '+str(val+1)
  528.             dimcheck = x[-7:-6]
  529.         if dimcheck == '1':
  530.                 # print dimcheck
  531.             if tdim[val] != newtdim[val]:
  532.                     print 'Dim Sent - '+str(val+1)
  533.                 if len(hex(int(val+1))) == 3:
  534.                         hexi = hex(k)
  535.                         hexi = hexi[1:2]+'0'+hexi[-1]
  536.                     hexi = hexi.upper()
  537.                         hexi = '0'+hexi
  538.                         # print hexi
  539.                 decdim = newtdim[val]
  540.                     if len(hex(int(decdim))) == 3:
  541.                         hexdim = hex(int(decdim))
  542.                         hexdim = hexdim[1:2]+'0'+hexdim[-1]
  543.                     hexdim = hexdim.upper()
  544.                         hexdim = '0'+hexdim
  545.                     # print hexdim
  546.                     else:
  547.                         hexdim = hex(int(decdim))
  548.                         hexdim = hexdim[1:]
  549.                     hexdim = hexdim.upper()
  550.                         hexdim = '0'+hexdim
  551.                         # print hexdim
  552.                 check = 23+i+int(decdim)
  553.                     if len(hex(check)) == 3:
  554.                         hexcheck = hex(check)
  555.                         hexcheck = hexcheck[1:2]+'0'+hexcheck[-1]
  556.                     hexcheck = hexcheck.upper()
  557.                         hexcheck = '0'+hexcheck
  558.                         # print hexcheck
  559.                 else:
  560.                         hexcheck = hex(check)
  561.                         hexcheck = hexcheck[1:]
  562.                     hexcheck = hexcheck.upper()
  563.                         hexcheck = '0'+hexcheck
  564.                         # print hexcheck
  565.                 cmd=['0x02','0x04','0x17',hexi,hexdim,hexcheck,'0x0D','0x0A']
  566.                 print (cmd)
  567.                     incmd = [int(h, base=16) for h in cmd]
  568.                     ser.write(serial.to_bytes(incmd))
  569.                     zmodeupdate(val)
  570.             elif tdim[val] == newtdim[val]:
  571.                     print 'No Dim Change - '+str(val+1)
  572.                     # break
  573.             else:
  574.                 print 'Dim Disabled - '+str(val+1)
  575.     else:
  576.         if zmode[val] != newzmode[val]:
  577.             print 'Override - ',newzmode[i-1]
  578.             if len(hex(int(val)+1)) == 3:
  579.                 hexval = hex((int(val)+1))
  580.                 hexval = hexval[1:2]+'0'+hexval[-1]
  581.                 hexval = hexval.upper()
  582.                 hexval = '0'+hexval
  583.                 # print hexval
  584.             if len(hex(newzmode[val])) == 3:
  585.                 hexmode = hex(newzmode[val])
  586.                 hexmode = hexmode[1:2]+'0'+hexmode[-1]
  587.                 hexmode = hexmode.upper()
  588.                 hexmode = '0'+hexmode
  589.                 # print hexmode
  590.             else:
  591.                 hexmode = hex(newzmode[val])
  592.                 hexmode = hexmode.upper()
  593.                 # print hexmode
  594.             valcheck = 21+int(val)+1+newzmode[val]
  595.             hexvalcheck = hex(valcheck)
  596.             hexvalcheck = hexvalcheck[1:]
  597.             hexvalcheck = hexvalcheck.upper()
  598.             hexvalcheck = '0'+hexvalcheck
  599.             if len(hexvalcheck) == 3:
  600.                 hexvalcheck = hexvalcheck[1:2]+'0'+hexvalcheck[-1]
  601.                 hexvalcheck = hexvalcheck.upper()
  602.                 hexvalcheck = '0'+hexvalcheck
  603.             # print hexvalcheck
  604.             cmd=['0x02','0x04','0x15',hexval,hexmode,hexvalcheck,'0x0D','0x0A']
  605.             print (cmd)
  606.             incmd = [int(h, base=16) for h in cmd]
  607.             ser.write(serial.to_bytes(incmd))
  608.             # break
  609.  
  610. # Function to do Zmode Data and Update XML
  611. def zmodefuncheck(val):
  612.     ser = serial.Serial('/dev/ttyAMA0', baudrate=9600, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS)
  613.     tree = ET.parse('/var/www/html/LoopInfo.xml')
  614.     root = tree.getroot()
  615.     zmodestr = 'Z'+str(val+1)+'Mode'
  616.     zton = 'Z'+str(val+1)+'TimeOn'
  617.     ztof = 'Z'+str(val+1)+'TimeOff'
  618.     zdim = 'Z'+str(val+1)+'Dim'
  619.     global ton
  620.     global toff
  621.     global tdim
  622.     global newzmode
  623.     global newton
  624.     global newtoff
  625.     global newtdim
  626.     x = bin(newzmode[val])
  627.     timecheck = x[-1:]
  628.     if timecheck == '1':
  629.         # print timecheck
  630.             if ton[val] != newton[val] or toff[val] != newtoff[val]:
  631.                 print 'Auto Time Sent - '+str(val+1)
  632.             if len(hex(int(val+1))) == 3:
  633.                 hexi = hex(int(val+1))
  634.                 hexi = hexi[1:2]+'0'+hexi[-1]
  635.                 hexi = hexi.upper()
  636.                     hexi = '0'+hexi
  637.                     # print hexi
  638.             decton = newton[val]
  639.                 if len(hex(int(decton))) == 3:
  640.                     hexton = hex(int(decton))
  641.                     hexton = hexton[1:2]+'0'+hexton[-1]
  642.                 hexton = hexton.upper()
  643.                 hexton = '0'+hexton
  644.                     # print hexton
  645.                 else:
  646.                     hexton = hex(int(decton))
  647.                     hexton = hexton[1:]
  648.                 hexton = hexton.upper()
  649.                 hexton = '0'+hexton
  650.                     # print hexton
  651.                 dectoff = newtoff[val]
  652.             if len(hex(int(dectoff))) == 3:
  653.                 hextof = hex(int(dectoff))
  654.                 hextof = hextof[1:2]+'0'+hextof[-1]
  655.                 hextof = hextof.upper()
  656.                 hextof = '0'+hextof
  657.                 # print hextof
  658.             else:
  659.                 hextof = hex(int(dectoff))
  660.                 hextof = hextof.upper()
  661.                 hextof = hextof[1:]
  662.                 hextof = '0'+hextof
  663.                 # print hexton
  664.                 check = 17+i+int(decton)+int(dectoff)
  665.                 if len(hex(check)) == 3:
  666.                     hexcheck = hex(check)
  667.                     hexcheck = hexcheck[1:2]+'0'+hexcheck[-1]
  668.                 hexcheck = hexcheck.upper()
  669.                     hexcheck = '0'+hexcheck
  670.                     # print hexcheck
  671.             else:
  672.                 hexcheck = hex(check)
  673.                     hexcheck = hexcheck[1:]
  674.                 hexcheck = hexcheck.upper()
  675.                     hexcheck = '0'+hexcheck
  676.                     # print hexcheck
  677.             cmd=['0x02','0x07','0x11',hexi,hexton,'0x00',hextof,'0x00',hexcheck,'0x0D','0x0A']
  678.             print (cmd)
  679.             incmd = [int(h, base=16) for h in cmd]
  680.                 ser.write(serial.to_bytes(incmd))
  681.                 zmodeupdate(val)
  682.         # else:
  683.             # print 'No Auto Time Change - '+str(val+1)
  684.             # break
  685.     else:
  686.         print 'Auto Time Disabled - '+str(val+1)
  687.         # break
  688.     x = bin(newzmode[val])
  689.     dimcheck = x[-7:-6]
  690.     if dimcheck == '1':
  691.         # print dimcheck
  692.         if tdim[val] != newtdim[val]:
  693.                 print 'Auto Dim Sent - '+str(val+1)
  694.             if len(hex(int(val+1))) == 3:
  695.                     hexi = hex(int(val+1))
  696.                 hexi = hexi[1:2]+'0'+hexi[-1]
  697.                 hexi = hexi.upper()
  698.                     hexi = '0'+hexi
  699.                     # print hexi
  700.             decdim = newtdim[val]
  701.             if len(hex(int(decdim))) == 3:
  702.                     hexdim = hex(int(decdim))
  703.                     hexdim = hexdim[1:2]+'0'+hexdim[-1]
  704.                 hexdim = hexdim.upper()
  705.                     hexdim = '0'+hexdim
  706.                 # print hexdim
  707.                 else:
  708.                     hexdim = hex(int(decdim))
  709.                     hexdim = hexdim[1:]
  710.                 hexdim = hexdim.upper()
  711.                     hexdim = '0'+hexdim
  712.                     # print hexdim
  713.             check = 23+i+int(decdim)
  714.                 if len(hex(check)) == 3:
  715.                     hexcheck = hex(check)
  716.                     hexcheck = hexcheck[1:2]+'0'+hexcheck[-1]
  717.                 hexcheck = hexcheck.upper()
  718.                     hexcheck = '0'+hexcheck
  719.                     # print hexcheck
  720.             else:
  721.                     hexcheck = hex(check)
  722.                     hexcheck = hexcheck[1:]
  723.                 hexcheck = hexcheck.upper()
  724.                     hexcheck = '0'+hexcheck
  725.                     # print hexcheck
  726.             cmd=['0x02','0x04','0x17',hexi,hexdim,hexcheck,'0x0D','0x0A']
  727.             print (cmd)
  728.                 incmd = [int(h, base=16) for h in cmd]
  729.                 ser.write(serial.to_bytes(incmd))
  730.                 zmodeupdate(val)
  731.         # elif tdim[val] == newtdim[val]:
  732.                 # print 'No Auto Dim Change - '+str(val+1)
  733.                 # break
  734.     else:
  735.         print 'Auto Dim Disabled - '+str(val+1)
  736.         # break
  737.  
  738. # In Broadcast, After change is found, The function carried out, Old values are updated so that they can be used as new values to check for changes
  739. def bcastupdate():
  740.     mydoc = minidom.parse("/var/www/html/bcast.xml")
  741.     values=mydoc.getElementsByTagName('mode')
  742.     for elem in values:
  743.         x = elem.firstChild.data
  744.         global bcast
  745.         bcast = int(x)
  746.  
  747. # In Discovery, After change is found, The function carried out, Old values are updated so that they can be used as new values to check for changes
  748. def disupdate():
  749.     tree = ET.parse('/var/www/html/ZoneUp.xml')
  750.     root = tree.getroot()
  751.     for child in root:
  752.         # print(child.tag, child.text)
  753.         if child.tag == 'LoopFlag':
  754.             global loopvalue
  755.             loopvalue = int(child.text)
  756.  
  757. # In Zmode, After change is found, The function carried out, Old values are updated so that they can be used as new values to check for changes
  758. def zmodeupdate(val):
  759.     tree = ET.parse('/var/www/html/LoopInfo.xml')
  760.     root = tree.getroot()
  761.     global zmode
  762.     global ton
  763.     global toff
  764.     global tdim
  765.     global newzmode
  766.     global newton
  767.     global newtoff
  768.     global newtdim
  769.     for child in root:
  770.         # print(child.tag, child.text)
  771.         zmodestr = 'Z'+str(val+1)+'Mode'
  772.         # print zmodestr
  773.         for i in range(1,13):
  774.             # print zmodestr
  775.             if child.tag == zmodestr and val == i-1:
  776.                 # print zmodestr
  777.                 # print zmode[i-1]
  778.                 zmode[i-1] = int(newzmode[i-1])
  779.                 ton[i-1] = int(newton[i-1])
  780.                 toff[i-1] = int(newtoff[i-1])
  781.                 tdim[i-1] = int(newtdim[i-1])
  782.  
  783. # Call only once in the entire Program
  784. call_once_object = CallOnce()
  785. call_once_object.service()
  786. display()
  787.  
  788. # Infinite Loop
  789. while True:
  790.     time.sleep(.1)
  791.     # print("Start")
  792.     newval()
  793.     # newdisplay()
  794.     # Check whether Broadcast Change is there are not
  795.     if newbcast != bcast:
  796.         print("Bcast Change")
  797.         bcast = newbcast
  798.         broad()
  799.         bcastupdate()
  800.     # Check whether Discover Change is there are not
  801.     elif newloopvalue != loopvalue:
  802.         tem=int(newloopvalue)
  803.         if tem == 1:
  804.             # If Discovery change is detected, is it for Discovery
  805.             print("Loop Change - Discover")
  806.             discov()
  807.             disupdate()
  808.         else:
  809.             print("Loop Change")
  810.             disupdate()
  811.     for i in range(0,12):
  812.         # print zmode[i]," - ",newzmode[i]
  813.         if zmode[i] != newzmode[i]:
  814.             print "Zmode Change - ",i+1
  815.             zmodefun(i)
  816.             zmodeupdate(i)
  817.         elif zmode[i] < 128:
  818.             print "Zmode Auto - ",i+1
  819.             zmodefuncheck(i)
  820.     else:
  821.         print("No Change")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement