Advertisement
maesfreek

Pylight

Apr 7th, 2014
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.88 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import sys
  3. import socket
  4. import struct
  5. from time import sleep
  6.  
  7. UDP_IP = "192.168.0.197"
  8. UDP_PORT = 8899
  9. sock = None
  10.  
  11. def send_udp_message(p1, p2, p3):
  12.     hexstring = struct.pack('B', p1) + struct.pack('B', p2) + struct.pack('B', p3)
  13.     print ("Sent UDP command " + str(hex(p1)) + " " + str(hex(p2)) + " " + str(hex(p3)) + " to " + UDP_IP + ":" + str(UDP_PORT))
  14.     sock.sendto(hexstring, (UDP_IP, UDP_PORT))
  15.     return
  16.    
  17. def printAbout():
  18.     print "Usage: milight ZONE COMMAND [PARAMETER]"
  19.     print ""
  20.     print "The ZONE argument specifies which bulb zone the command refers to."
  21.     print "Possible values:"
  22.     print "  ALL/0 - All zones"
  23.     print "  1     - Zone 1"
  24.     print "  2     - Zone 2"
  25.     print "  3     - Zone 3"
  26.     print "  4     - Zone 4"
  27.     print ""
  28.     print "The COMMAND argument specifies the command to be sent to the given bulb zone."
  29.     print "Some commands require a parameter (see below)."
  30.     print "Accepted commands:"
  31.     print "  ON                 - Turn the bulbs in the given zone on."
  32.     print "  OFF                - Turn the bulbs in the given zone off."
  33.     print "  WHITE/W            - Set the color of the bulbs in the given zone to white."
  34.     print "  DISCO/D [+/-]      - If no parameter is specified, turn disco mode on."
  35.     print "                       The '+' optional parameter increases the disco speed."
  36.     print "                       The '-' optional parameter decreases the disco speed."
  37.     print "  BRIGHTNESS/B VALUE - Set the brightness of the bulbs in the given zone."
  38.     print "                       The VALUE mandatory parameter specifies the brightness"
  39.     print "                       and must be an integer number in the range 1-19."
  40.     print "  COLOR/C VALUE      - Set the color of the bulbs in the given zone."
  41.     print "                       The VALUE mandatory parameter specifies the color"
  42.     print "                       and must be an integer number in the range 0-255."
  43.     return
  44.  
  45. def sendOnCommand(zone):
  46.     codes = [66, 69, 71, 73, 75];
  47.     send_udp_message(codes[int(zone)], 00, 85);
  48.     return
  49.  
  50. def sendOffCommand(zone):
  51.     codes = [65, 70, 72, 74, 76];
  52.     send_udp_message(codes[int(zone)], 00, 85);
  53.     return
  54.  
  55. def sendWhiteCommand(zone):
  56.     codes = [194, 197, 199, 201, 203];    
  57.     sendOnCommand(zone)
  58.     sleep(0.1)
  59.     send_udp_message(codes[int(zone)], 00, 85);
  60.     return
  61.  
  62. def senColorCommand (zone, color):
  63.     sendOnCommand(zone)
  64.     sleep(0.1)
  65.     send_udp_message(64, color, 85);
  66.     return
  67.  
  68. def sendBrightnessCommand(zone, brightness):
  69.     codes = [2, 3, 4, 5, 8, 9, 10, 11, 13, 14, 15, 16, 18, 19, 20, 21, 23, 24, 25]
  70.     sendOnCommand(zone)
  71.     sleep(0.1)
  72.     send_udp_message(78, codes[int(brightness)-1], 85)
  73.     return
  74.  
  75. def sendDiscoCommand (zone):
  76.     sendOnCommand(zone)
  77.     sleep(0.1)
  78.     send_udp_message(77, 00, 85)
  79.     print "sendDiscoCommand"
  80.     return
  81.  
  82. def sendDiscoIncCommand (zone):
  83.     sendOnCommand(zone)
  84.     sleep(0.1)
  85.     send_udp_message(68, 00, 85)
  86.     return
  87.  
  88. def sendDiscoDecCommand (zone):
  89.     sendOnCommand(zone)
  90.     sleep(0.1)
  91.     send_udp_message(67, 00, 85)
  92.     return
  93.  
  94. def processCommand(programName, zone, command, param):
  95.     if command == "ON":
  96.         if param != "":
  97.             printAbout()
  98.             return False
  99.         return sendOnCommand(zone)
  100.  
  101.     if command == "OFF":
  102.         if param != "":
  103.             printAbout()
  104.             return False
  105.         return sendOffCommand(zone)
  106.  
  107.     if command == "WHITE" or command == "W":
  108.         if param != "":
  109.             printAbout()
  110.             return False
  111.         return sendWhiteCommand(zone)
  112.  
  113.     if command == "COLOR" or command == "C":
  114.         color = int(param)
  115.         if param == "" or color < 0 or color > 255:
  116.             printAbout()
  117.             return False
  118.         return senColorCommand (zone, color)
  119.    
  120.     if command == "BRIGHTNESS" or command == "B":
  121.         brightness = int(param)
  122.         if param == "" or int(brightness) < 1 or int(brightness) > 19:
  123.             printAbout()
  124.             return False
  125.         return sendBrightnessCommand(zone, brightness)
  126.  
  127.     if command == "DISCO" or command == "D":
  128.         if param == "":
  129.             return sendDiscoCommand(zone)
  130.         if param == "+":
  131.             return sendDiscoIncCommand (zone);        
  132.         if param == "-":
  133.             return sendDiscoDecCommand (zone)
  134.         printAbout()
  135.         return False
  136.    
  137.     printAbout();
  138.     return False        
  139.  
  140. # parse command line options
  141. if len(sys.argv) < 3 or len(sys.argv)>4 :
  142.     printAbout()
  143.     exit(1)
  144.  
  145. programName = str(sys.argv[0])
  146. zone        = str(sys.argv[1]).upper()
  147. command     = str(sys.argv[2]).upper()
  148. param       = ""
  149.  
  150. if len(sys.argv) > 3 :
  151.     param = str(sys.argv[3]).upper()
  152.  
  153. # Connect to inet UDP
  154. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  155.  
  156. result = processCommand(programName, zone, command, param)
  157.  
  158. exit(0 if result else 1);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement