SHARE
TWEET

Untitled

a guest May 2nd, 2014 174 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #! /usr/bin/python
  2.  
  3. import agoclient
  4. import threading
  5. import time
  6. import logging
  7.  
  8. #readInterface = agoclient.get_config_option("x10","interface","CM11")
  9. #readPort = agoclient.get_config_option("x10", "device", "/dev/ttyUSB1")
  10.  
  11.  
  12. POLLFREQ = 1
  13. # Std Python modules
  14. import sys, time, re
  15.  
  16. # CM19a module
  17. import CM19aDriver
  18.  
  19. # Start logging
  20. log = CM19aDriver.startLogging()        # log is an instance of the logger class
  21.  
  22. # Configure the CM19a and start polling for inbound commands from a remote
  23. # Initialise device. Note: auto polling/receiving in a thread is turned ON
  24. cm19a = CM19aDriver.CM19aDevice(POLLFREQ, log, polling = True)            # cm19a is an instance of the CM19aDevice class
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34. #  Dictionaries to decrypt hex values sent from CM11A to house/device codes as well as function on/off
  35. #  Other functions exist but on/off are the only ones handled.  All other functions are ignored
  36. #  Functions are displayed as decimal values ON = 255 and OFF = 0
  37. #  See http://www.smarthome.com/manuals/protocol.txt for details
  38.  
  39. x10_house =  {'6': 'A', 'e': 'B', '2': 'C', 'a': 'D', '1': 'E', '9': 'F', '5': 'G', 'd': 'H', '7': 'I', 'f': 'J', '3': 'K', 'b': 'L', '0': 'M', '8': 'N', '4': 'O', 'c': 'P'}
  40. x10_device = {'6': '1', 'e': '2', '2': '3', 'a': '4', '1': '5', '9': '6', '5': '7', 'd': '8', '7': '9', 'f': '10', '3': '11', 'b': '12', '0': '13', '8': '14', '4': '15', 'c': '16'}
  41. x10_funct  = {'2': '255', '3': '0'}
  42.  
  43.  
  44. client = agoclient.AgoConnection("X10")
  45. # specify our message handler method
  46. #client.add_handler(messageHandler)
  47.  
  48. # X10 device configuration
  49. readSwitches = agoclient.get_config_option("x10", "switches", "A2,A3,A9,B3,B4,B5,B9,B12")
  50. switches = map(str, readSwitches.split(','))
  51. for switch in switches:
  52.         client.add_device(switch, "switch")
  53. readDimmers = agoclient.get_config_option("x10", "dimmers", "D1")
  54. dimmers = map(str, readDimmers.split(','))
  55. for dimmer in dimmers:
  56.         client.add_device(dimmer, "dimmer")
  57.  
  58.  
  59.  
  60. # This section handles sending X10 devices over the CM11A using Python-X10
  61.  
  62. # this class will be instantiated and spawned into background to not block the messageHandler
  63. x10lock = threading.Lock()
  64.  
  65. class x10send(threading.Thread):
  66.         def __init__(self, id, functioncommand, level):
  67.                 threading.Thread.__init__(self)
  68.                 self.id = id
  69.                 self.functioncommand = functioncommand
  70.                 self.level = level
  71.         def run(self):
  72.                 housecode=self.id[:1].upper()
  73.                 devicecode=self.id[1:]
  74.                 if self.functioncommand == "on":
  75.                         print "switching on: " + housecode + devicecode
  76.                         result = cm19a.send(housecode, devicecode, "ON")     # True if the command was sent OK
  77.                         if not result:
  78.                                 print  >> sys.stderr, "Command failed"
  79.                         client.emit_event(self.id, "event.device.statechanged", "255", "")
  80.                 if self.functioncommand == "off":
  81.                         print "switching off: " + housecode + devicecode
  82.                         result = cm19a.send(housecode, devicecode, "OFF")     # True if the command was sent OK
  83.                         if not result:
  84.                                 print  >> sys.stderr, "Command failed"
  85.                         client.emit_event(self.id, "event.device.statechanged", "0", "")
  86.  
  87.                 if self.functioncommand == "setlevel":
  88.                         x10level=int(self.level * 2.55)
  89.                         print "Dimming: " + self.id + " " + str(x10level) + "%"
  90.                         # fix later
  91.                         client.emit_event(self.id, "event.device.statechanged", self.level, "")
  92.  
  93.  
  94. def messageHandler(internalid, content):
  95.         if "command" in content:
  96.                 if "level" in content:
  97.                         background = x10send(internalid, content["command"], content["level"])
  98.                 else:
  99.                         background = x10send(internalid, content["command"], "")
  100.                 background.setDaemon(True)
  101.                 background.start()
  102.  
  103. client.add_handler(messageHandler)
  104.  
  105. # This section is used to monitor for incoming RF signals on the CM11A
  106.  
  107.  
  108. class readX10(threading.Thread):
  109.         def __init__(self,):
  110.                 threading.Thread.__init__(self)
  111.         def run(self):
  112.                 if cm19a.initialised:
  113.                
  114.                     # Now a simple macro
  115.                     print 'Waiting for commands'
  116.                     alive = True
  117.                     lasthouse = ''
  118.                     lastunit = ''
  119.                     while alive:
  120.                         # Check for any button presses from an X10 remote
  121.                         buttonpresses = cm19a.getReceiveQueue()         # -> List
  122.                         if buttonpresses:
  123.                             # One or more button presses were received so loop through the list of button presses
  124.                             for button in buttonpresses:
  125.                                 print "Actioning button press: %s" % button
  126.                                 # Use a regular expression to extract the house code, unit number, and command
  127.                                 b = re.search(r'^(.)(\d+)(.+)', button)     # start of string, 1 character (house code), 1 or more digits (unit number), 1 or more characters (command)
  128.                                 if b:
  129.                                     house = b.group(1).upper()
  130.                                     unit = b.group(2).upper()
  131.                                     command = b.group(3).upper()
  132.                                 else:
  133.                                     print  >> sys.stderr, "Could not extract house code etc from %s" % button
  134.  
  135.                             # Other button pressess
  136.                             if command.find('BRIGHTBUTTONPRESSED')>= 0:
  137.                                 print 'Bright button was pressed => Brightening the last used device..'
  138.                                 result = cm19a.send(lasthouse, lastunit, 'bright')       # True if the command was sent OK
  139.                                 if not result:
  140.                                     print  >> sys.stderr, "Command failed"
  141.                             elif command.find('DIMBUTTONPRESSED')>= 0:
  142.                                 print 'Dim button was pressed => Dimming the last used device...'
  143.                                 result = cm19a.send(lasthouse, lastunit, 'dim')       # True if the command was sent OK
  144.                                 if not result:
  145.                                     print  >> sys.stderr, "Command failed"
  146.                             else:
  147.                                 # Just retransmit any other command recevied
  148.                                 send_x10_address = house + unit
  149.                                 if command == "ON":
  150.                                         level=255
  151.                                 if command =="OFF":
  152.                                         level=0
  153.                                 client.emit_event(send_x10_address , "event.device.statechanged", level , "");
  154.                                 lasthouse = house
  155.                                 lastunit = unit
  156.                             #end for loop
  157.                         else:
  158.                             # No buttons pressed so do just wait a bit (2 seconds in this case) before checking again
  159.                             time.sleep(2)
  160.                     # end while alive loop
  161.                
  162.                     cm19a.finish()
  163.                 else:
  164.                     print "Error initialising the CM19a...exiting..."
  165.                     sys.exit(1)
  166.                
  167. background = readX10()
  168. background.setDaemon(True)
  169. background.start()
  170.  
  171. client.run()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top