Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/python
- import agoclient
- import threading
- import time
- import logging
- #readInterface = agoclient.get_config_option("x10","interface","CM11")
- #readPort = agoclient.get_config_option("x10", "device", "/dev/ttyUSB1")
- POLLFREQ = 1
- # Std Python modules
- import sys, time, re
- # CM19a module
- import CM19aDriver
- # Start logging
- log = CM19aDriver.startLogging() # log is an instance of the logger class
- # Configure the CM19a and start polling for inbound commands from a remote
- # Initialise device. Note: auto polling/receiving in a thread is turned ON
- cm19a = CM19aDriver.CM19aDevice(POLLFREQ, log, polling = True) # cm19a is an instance of the CM19aDevice class
- # Dictionaries to decrypt hex values sent from CM11A to house/device codes as well as function on/off
- # Other functions exist but on/off are the only ones handled. All other functions are ignored
- # Functions are displayed as decimal values ON = 255 and OFF = 0
- # See http://www.smarthome.com/manuals/protocol.txt for details
- 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'}
- 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'}
- x10_funct = {'2': '255', '3': '0'}
- client = agoclient.AgoConnection("X10")
- # specify our message handler method
- #client.add_handler(messageHandler)
- # X10 device configuration
- readSwitches = agoclient.get_config_option("x10", "switches", "A2,A3,A9,B3,B4,B5,B9,B12")
- switches = map(str, readSwitches.split(','))
- for switch in switches:
- client.add_device(switch, "switch")
- readDimmers = agoclient.get_config_option("x10", "dimmers", "D1")
- dimmers = map(str, readDimmers.split(','))
- for dimmer in dimmers:
- client.add_device(dimmer, "dimmer")
- # This section handles sending X10 devices over the CM11A using Python-X10
- # this class will be instantiated and spawned into background to not block the messageHandler
- x10lock = threading.Lock()
- class x10send(threading.Thread):
- def __init__(self, id, functioncommand, level):
- threading.Thread.__init__(self)
- self.id = id
- self.functioncommand = functioncommand
- self.level = level
- def run(self):
- housecode=self.id[:1].upper()
- devicecode=self.id[1:]
- if self.functioncommand == "on":
- print "switching on: " + housecode + devicecode
- result = cm19a.send(housecode, devicecode, "ON") # True if the command was sent OK
- if not result:
- print >> sys.stderr, "Command failed"
- client.emit_event(self.id, "event.device.statechanged", "255", "")
- if self.functioncommand == "off":
- print "switching off: " + housecode + devicecode
- result = cm19a.send(housecode, devicecode, "OFF") # True if the command was sent OK
- if not result:
- print >> sys.stderr, "Command failed"
- client.emit_event(self.id, "event.device.statechanged", "0", "")
- if self.functioncommand == "setlevel":
- x10level=int(self.level * 2.55)
- print "Dimming: " + self.id + " " + str(x10level) + "%"
- # fix later
- client.emit_event(self.id, "event.device.statechanged", self.level, "")
- def messageHandler(internalid, content):
- if "command" in content:
- if "level" in content:
- background = x10send(internalid, content["command"], content["level"])
- else:
- background = x10send(internalid, content["command"], "")
- background.setDaemon(True)
- background.start()
- client.add_handler(messageHandler)
- # This section is used to monitor for incoming RF signals on the CM11A
- class readX10(threading.Thread):
- def __init__(self,):
- threading.Thread.__init__(self)
- def run(self):
- if cm19a.initialised:
- # Now a simple macro
- print 'Waiting for commands'
- alive = True
- lasthouse = ''
- lastunit = ''
- while alive:
- # Check for any button presses from an X10 remote
- buttonpresses = cm19a.getReceiveQueue() # -> List
- if buttonpresses:
- # One or more button presses were received so loop through the list of button presses
- for button in buttonpresses:
- print "Actioning button press: %s" % button
- # Use a regular expression to extract the house code, unit number, and command
- b = re.search(r'^(.)(\d+)(.+)', button) # start of string, 1 character (house code), 1 or more digits (unit number), 1 or more characters (command)
- if b:
- house = b.group(1).upper()
- unit = b.group(2).upper()
- command = b.group(3).upper()
- else:
- print >> sys.stderr, "Could not extract house code etc from %s" % button
- # Other button pressess
- if command.find('BRIGHTBUTTONPRESSED')>= 0:
- print 'Bright button was pressed => Brightening the last used device..'
- result = cm19a.send(lasthouse, lastunit, 'bright') # True if the command was sent OK
- if not result:
- print >> sys.stderr, "Command failed"
- elif command.find('DIMBUTTONPRESSED')>= 0:
- print 'Dim button was pressed => Dimming the last used device...'
- result = cm19a.send(lasthouse, lastunit, 'dim') # True if the command was sent OK
- if not result:
- print >> sys.stderr, "Command failed"
- else:
- # Just retransmit any other command recevied
- send_x10_address = house + unit
- if command == "ON":
- level=255
- if command =="OFF":
- level=0
- client.emit_event(send_x10_address , "event.device.statechanged", level , "");
- lasthouse = house
- lastunit = unit
- #end for loop
- else:
- # No buttons pressed so do just wait a bit (2 seconds in this case) before checking again
- time.sleep(2)
- # end while alive loop
- cm19a.finish()
- else:
- print "Error initialising the CM19a...exiting..."
- sys.exit(1)
- background = readX10()
- background.setDaemon(True)
- background.start()
- client.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement