def __init__(self, switch_callback=_NOOP, motion_callback=_NOOP, with_discovery=True, with_subscribers=True, with_cache=None, subscriber_port=8989, bind=None, config_filename=None): """ Create a WeMo environment. @param switch_callback: A function to be called when a new switch is discovered. @type switch_callback: function @param motion_callback: A function to be called when a new motion is discovered. @type motion_callback: function @param with_subscribers: Whether to register for events with discovered devices. @type with_subscribers: bool @param bind: ip:port to which to bind the response server. @type bind: str """ self._config = WemoConfiguration(filename=config_filename) self.upnp = UPnP(bind=bind or self._config.bind) discovered.connect(self._found_device, self.upnp) self.registry = SubscriptionRegistry(subscriber_port) if with_cache is None: with_cache = (self._config.cache if self._config.cache is not None else True) self._with_cache = with_cache self._with_discovery = with_discovery self._with_subscribers = with_subscribers self._switch_callback = switch_callback self._motion_callback = motion_callback self._switches = {} self._motions = {} self.devices = {}
def __init__(self, switch_callback=_NOOP, motion_callback=_NOOP, bridge_callback=_NOOP, maker_callback=_NOOP, with_discovery=True, with_subscribers=True, with_cache=_MARKER, bind=None, config_filename=None): """ Create a WeMo environment. @param switch_callback: A function to be called when a new switch is discovered. @type switch_callback: function @param motion_callback: A function to be called when a new motion is discovered. @type motion_callback: function @param with_subscribers: Whether to register for events with discovered devices. @type with_subscribers: bool @param bind: ip:port to which to bind the response server. @type bind: str """ if with_cache is not _MARKER: log.warn("with_cache argument is deprecated (and nonfunctional)") self._config = WemoConfiguration(filename=config_filename) self.upnp = UPnP(bind=bind or self._config.bind) discovered.connect(self._found_device, self.upnp) self.registry = SubscriptionRegistry() self._with_discovery = with_discovery self._with_subscribers = with_subscribers self._switch_callback = switch_callback self._motion_callback = motion_callback self._bridge_callback = bridge_callback self._maker_callback = maker_callback self._switches = {} self._motions = {} self._bridges = {} self._makers = {} self.devices = {}
def __init__(self, switch_callback=_NOOP, motion_callback=_NOOP, with_discovery=True, with_subscribers=True, with_cache=None, bind=None, config_filename=None): """ Create a WeMo environment. @param switch_callback: A function to be called when a new switch is discovered. @type switch_callback: function @param motion_callback: A function to be called when a new motion is discovered. @type motion_callback: function @param with_subscribers: Whether to register for events with discovered devices. @type with_subscribers: bool @param bind: ip:port to which to bind the response server. @type bind: str """ self._config = WemoConfiguration(filename=config_filename) self.upnp = UPnP(bind=bind or self._config.bind) discovered.connect(self._found_device, self.upnp) self.registry = SubscriptionRegistry() if with_cache is None: with_cache = (self._config.cache if self._config.cache is not None else True) self._with_cache = with_cache self._with_discovery = with_discovery self._with_subscribers = with_subscribers self._switch_callback = switch_callback self._motion_callback = motion_callback self._switches = {} self._motions = {} self.devices = {}
def main(): ''' Server routine ''' port = "9801" context = zmq.Context.instance() # Receive input from the outside world socket = context.socket(zmq.DEALER) # Specify unique identity socket.setsockopt(zmq.IDENTITY, b"WeMo") socket.connect("tcp://127.0.0.1:%s" % port) print "Ready to receive" # Where we will store references to the worker threads worker_sockets = {} # Start the ouimeaux environment for discovery env = Environment(with_subscribers=False, with_discovery=True, with_cache=False) env.start() discovered.connect(discovered_wemo) # Run the polling mechanism in the background BackgroundDiscovery(env).start() while True: # Get the outside message in several parts # Store the client_addr client_addr, _, msg = socket.recv_multipart() print "Received request {} from '{}'".format(msg, client_addr) msg = msg.split(' ') command = msg[0] # General commands if command == 'list': # Send the current set of devices (only switches supported) socket.send_multipart( [client_addr, b'', ",".join(env.list_switches())]) continue # Commands on objects switch_name = msg[1] print switch_name s = env.get_switch(switch_name) if command == 'on': s.on() socket.send_multipart([client_addr, b'', 'OK']) elif command == 'off': s.off() socket.send_multipart([client_addr, b'', 'OK']) elif command == 'state': st = s.get_state() st = 'on' if st else 'off' socket.send_multipart([client_addr, b'', st])
def main(): ''' Server routine ''' port = "9801" context = zmq.Context.instance() # Receive input from the outside world socket = context.socket(zmq.DEALER) # Specify unique identity socket.setsockopt(zmq.IDENTITY, b"WeMo") socket.connect("tcp://127.0.0.1:%s" % port) print "Ready to receive" # Where we will store references to the worker threads worker_sockets = {} # Start the ouimeaux environment for discovery env = Environment(with_subscribers = False, with_discovery=True, with_cache=False) env.start() discovered.connect(discovered_wemo) # Run the polling mechanism in the background BackgroundDiscovery(env).start() while True: # Get the outside message in several parts # Store the client_addr client_addr, _, msg = socket.recv_multipart() print "Received request {} from '{}'".format(msg, client_addr) msg = msg.split(' ') command = msg[0] # General commands if command == 'list': # Send the current set of devices (only switches supported) socket.send_multipart([client_addr, b'', ",".join(env.list_switches())]) continue # Commands on objects switch_name = msg[1] print switch_name s = env.get_switch(switch_name) if command == 'on': s.on() socket.send_multipart([client_addr, b'', 'OK']) elif command == 'off': s.off() socket.send_multipart([client_addr, b'', 'OK']) elif command == 'state': st = s.get_state() st = 'on' if st else 'off' socket.send_multipart([client_addr, b'', st])