def main(): global bus global config global activehome bus = AutobusConnection() activehome = bus["activehome"] bus.add_interface("home", Interface()) config = Configuration(bus, "configure.home", "homed.conf") bus.start_connecting() bus.interrupt_loop()
def main(): global bus global receive_event global receive_action_event pythoncom.CoInitialize() try: Dispatch("X10.ActiveHome") # Make sure we've got it before hand except: print_exc() print "You need to install the ActiveHome Scripting SDK before you" print "can use activehomed." sys.exit() print "Connecting to server " + str(os.getenv("AUTOBUS_SERVER")) bus = AutobusConnection() bus.add_interface("activehome", RPC()) receive_action_event = bus.add_event("activehome", "receive_action", """\ Fired when the CM15A receives a powerline or RF signal. This event is quite low-level and hence the receive event should be preferred to this one. The signature of this event is identical to the ActiveHome Scripting SDK's OnRecvAction event, except that all parameters are passed as strings and the last two parameters are not present (the scripting SDK doesn't make use of those at present, so you're not losing any information). """) receive_event = bus.add_event("activehome", "receive", """\ receive(mode, house, unit, address, command, sequence, timestamp, repeat) Fired when the CM15A receives a powerline or RF signal. This event is more high-level than receive_action. mode is the mode, which will either be plc or rf. house is the house code. unit is the unit code. address is the string concatenation of the house and unit codes. For example, house, unit, and address could be "d", "2", and "d2", respectively. All three parameters are passed as strings. command is the command that occurred. The ActiveHome Scripting SDK contains a list of all valid commands, but some of the more common ones are On, Off, AllLightsOn, AllUnitsOff, Bright, Dim, PanLeft, CamGoPosition1, and DeviceStop (the latter of which occurs when the CM15A is unplugged from the computer). sequence is slightly command-specific, as follows: If this is an RF command, sequence will be the sequence number, which is 0 for the button being pressed down, 1 and upward for holding the button down (fired roughly six times per second), and -1 for letting go of the button. A word of warning: this doesn't seem to be hugely reliable. For example, pushing an on button on a ScanPad or PalmPad results in six events: three pairs of button press and button release commands, despite the fact that the button was only pressed once. Holding the button down just repeats these pairs further. If you want a reliable indicator of whether or not a button press is a repeat, use the repeat parameter passed to this event. If this is a powerline command, sequence will be null unless the command is Bright or Dim, in which case it will be the bright/dim level. When a bright/dim button is held down on an RF controller, the result is a repeated firing of Bright/Dim commands. When a bright/dim button is held down on a powerline controller, the result is that no events are fired until the button is let go, and then a single Bright/Dim command is fired with the sequence parameter being set to the percent change in brightness that would have resulted had the command been directed at an actual module. The timestamp parameter is only present for RF commands. It specifies, as a string, the date and time at which the command was received. This generally won't be used since Autobus only incurs a few milliseconds' latency, so the date could just be detected locally. repeat is a boolean. It will be set to true if this event is most likely a repeat of another event. For example, if an on button on a ScanPad controller is pressed, the result is three pairs of button press and button release commands. The first command will have its repeat parameter set to false, and the remaining five commands will have their repeat parameters set to true. Another example is a bug in the CM15A: If two units were simultaneously turned on (for example, by A1 A2 A off going across the powerline), and then an AllLightsOn received on the powerline for that house code, the CM15A will report that as two events: A1 AllLightsOff and A2 AllLightsOff. The latter will be detected as a repeat when this event is fired. NOTE: There are currently problems with detecting RF bright/dim repeats, so you should generally ignore this parameter for RF bright/dim commands. """) bus.start_connecting() try: HTTPServer(("127.0.0.1", port), HTTPHandler).serve_forever() except KeyboardInterrupt: print "Interrupted, shutting down" finally: bus.shutdown()
from time import sleep class ExampleInterface(object): """ An example interface. It doesn't do much. Basically, all it allows you to do is say hello. """ def say_hello(self, name): """ Formats a "hello world"-style message greeting the specified person by name and returns it. The argument must be a string or an exception will be raised. """ print "We're about to say hi to " + name return "Hello, " + name + ". How are you?" server = AutobusConnection() server.add_interface("example", ExampleInterface()) server.connect() # That's it for the actual autobus code. The rest of the code is a hack to get # around python not letting the application die with ctrl+c. try: while True: sleep(1) except KeyboardInterrupt: print "Shutting down" server.shutdown()
from libautobus import AutobusConnection from os import getpid class ExampleInterface(object): def say_hello(self, name): return "Hello, " + name + ". How are you?" for i in range(10): server = AutobusConnection() server.add_interface("afntest.autobus3." + str(i), ExampleInterface()) server.connect() print "Started up. pid is " + str(getpid()) + " which you can use to kill" print "this once you're done with it."