本文整理汇总了Python中ouimeaux.environment.Environment.start方法的典型用法代码示例。如果您正苦于以下问题:Python Environment.start方法的具体用法?Python Environment.start怎么用?Python Environment.start使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ouimeaux.environment.Environment
的用法示例。
在下文中一共展示了Environment.start方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: toggle_switch
# 需要导入模块: from ouimeaux.environment import Environment [as 别名]
# 或者: from ouimeaux.environment.Environment import start [as 别名]
def toggle_switch(switch_name):
env = Environment(on_switch, on_motion)
env.start()
env.discover(seconds=1)
#time.sleep(2)
switch = env.get_switch(switch_name)
switch.blink()
示例2: wemo_env
# 需要导入模块: from ouimeaux.environment import Environment [as 别名]
# 或者: from ouimeaux.environment.Environment import start [as 别名]
class wemo_env(object):
def __init__(self):
self._watcheux = {}
self._env = Environment()
try:
self._env.start()
self._env.discover(3)
except:
print("WeMo environment cannot start ;( !!!!")
def start_watching(self):
statechange.connect(self.update_state,
unique=False,
dispatch_uid=id(self))
def get_device(self, name):
return self._env.get(name)
def register(self, name, fct):
if name not in self._watcheux:
self._watcheux[name] = []
self._watcheux[name].append(fct)
if len(self._watcheux) > 0:
self.start_watching()
def update_state(self, sender, **kwargs):
state = kwargs.get('state')
if state != 0:
state = 1
if sender.name in self._watcheux:
for watcheu in self._watcheux[sender.name]:
watcheu(state)
示例3: get_switch_state
# 需要导入模块: from ouimeaux.environment import Environment [as 别名]
# 或者: from ouimeaux.environment.Environment import start [as 别名]
def get_switch_state(switch_name):
env = Environment(on_switch, on_motion)
env.start()
env.discover(seconds=1)
#time.sleep(2)
switch = env.get_switch(switch_name)
return switch.basicevent.GetBinaryState()['BinaryState']
示例4: main
# 需要导入模块: from ouimeaux.environment import Environment [as 别名]
# 或者: from ouimeaux.environment.Environment import start [as 别名]
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])
示例5: devices
# 需要导入模块: from ouimeaux.environment import Environment [as 别名]
# 或者: from ouimeaux.environment.Environment import start [as 别名]
def devices():
try:
env = Environment()
env.start()
env.discover(seconds=3)
result = env.list_switches()
except:
raise
return result
示例6: __init__
# 需要导入模块: from ouimeaux.environment import Environment [as 别名]
# 或者: from ouimeaux.environment.Environment import start [as 别名]
class WemoControl:
def __init__(self, wemoConfig):
self.wemoConfig = wemoConfig
def process(self):
try:
self.env = Environment(bridge_callback=self.on_bridge, with_subscribers=False)
self.env.start()
self.env.discover(10)
except Exception, e:
log.exception("Failed to start environment.")
示例7: Wemo
# 需要导入模块: from ouimeaux.environment import Environment [as 别名]
# 或者: from ouimeaux.environment.Environment import start [as 别名]
class Wemo(Command):
def __init__(self):
self.env = Environment(with_discovery=False, with_subscribers=False)
self.env.start()
def on(self, device):
switch = self.env.get_switch(closest_match(device, self.env.list_switches()))
switch.basicevent.SetBinaryState(BinaryState=1)
def off(self, device):
switch = self.env.get_switch(closest_match(device, self.env.list_switches()))
switch.basicevent.SetBinaryState(BinaryState=0)
示例8: init
# 需要导入模块: from ouimeaux.environment import Environment [as 别名]
# 或者: from ouimeaux.environment.Environment import start [as 别名]
def init(devname):
try:
env = Environment()
env.start()
env.discover(seconds=3)
switch = env.get_switch(devname)
#except UnknownDevice:
# return None, None
except:
raise
return env, switch
示例9: mainloop
# 需要导入模块: from ouimeaux.environment import Environment [as 别名]
# 或者: from ouimeaux.environment.Environment import start [as 别名]
def mainloop(names, pubsub_client, times=0, delay=10):
env = Environment(with_cache=False)
env.start()
env.discover(5)
if times < 1:
while True:
do(env,names,pubsub_client)
time.sleep(delay)
else:
for i in range(0, times):
do(env,names,pubsub_client)
time.sleep(delay)
示例10: WemoClient
# 需要导入模块: from ouimeaux.environment import Environment [as 别名]
# 或者: from ouimeaux.environment.Environment import start [as 别名]
class WemoClient():
def __init__(self):
self.env = Environment(_on_switch, _on_motion)
self.env.start()
self.env.discover(4)
def toggle(self):
_switch_map.values()[0].toggle()
def switch_on(self):
_switch_map.values()[0].on()
def switch_off(self):
_switch_map.values()[0].off()
示例11: toggleLight
# 需要导入模块: from ouimeaux.environment import Environment [as 别名]
# 或者: from ouimeaux.environment.Environment import start [as 别名]
def toggleLight():
env = Environment()
try:
env.start()
except:
print "server may have been started already"
for i in range(1, 5):
try:
env.discover(i)
switch = env.get_switch("WeMo Switch")
switch.toggle()
except:
continue
break
示例12: Wemo
# 需要导入模块: from ouimeaux.environment import Environment [as 别名]
# 或者: from ouimeaux.environment.Environment import start [as 别名]
class Wemo(object):
state_map = {'on': 1, 'off': 0}
state_map_wemo = {'1': 'on', '0': 'off'}
def __init__(self):
self.env = Environment(with_cache=False)
self.env.start()
self.env.discover()
self.list_switches = self.env.list_switches()
print self.list_switches
def get(self, name, use_cache=False):
'''
get - returns the state of the given device
'''
status = 0
state = None
s = self.env.get_switch(name)
try:
state = s.basicevent.GetBinaryState()
if 'BinaryState' in state:
print 'get info: {}'.format(state)
state = Wemo.state_map_wemo[state['BinaryState']]
return status, state
except Exception as e:
print 'exception {}'.format(e)
status = 2
return status, state
def set(self, name, val_on_off, use_cache=False):
'''
set - turns the wemo switch either on or off. Will
execute for all the switches in the list
'''
status = 0
s = self.env.get_switch(name)
print 'state: {}'.format(val_on_off)
state = Wemo.state_map[val_on_off.lower()]
try:
s.basicevent.SetBinaryState(BinaryState=state)
except:
status = 2
return status
示例13: get_switch
# 需要导入模块: from ouimeaux.environment import Environment [as 别名]
# 或者: from ouimeaux.environment.Environment import start [as 别名]
def get_switch():
env = Environment()
try:
env.start()
except:
pass
env.discover(5)
found = None
for switch in env.list_switches():
if matches(switch):
found = env.get_switch(switch)
break
else:
raise Exception('Switch not found!')
return found
示例14: main
# 需要导入模块: from ouimeaux.environment import Environment [as 别名]
# 或者: from ouimeaux.environment.Environment import start [as 别名]
def main():
# define input parameters
parser = OptionParser()
parser.add_option("-a", help="all of the wemo devices", default=False, action="store_true")
parser.add_option("-g", help="get switch state", default=False, action="store_true")
parser.add_option("-l", help="list all the switches", default=False, action="store_true")
parser.add_option("--off", help="off flag - default is on", default=False, action="store_true")
parser.add_option("-s", help="switch name", default=None)
# read input values
(options, args) = parser.parse_args()
flag_on_off = ON
if options.off:
flag_on_off = OFF
flag_list = options.l
flag_get = options.g
switch_name = options.s
#
# environment - wemo
env = Environment()
env.start()
env.discover()
switches = env.list_switches()
if switch_name is not None:
if switch_name not in switches:
print "%s is not available" % switch_name
return {"status": 1}
else:
switches = [switch_name]
if flag_list:
return {"status": 0, "msg": switches}
if flag_get:
return {"status": 0, "msg": switch_get(env, switches)}
#
# switch - on/off
switch_set(env, switches, flag_on_off)
return {"status": 0, "msg": "switch(es):%s val:%s" % (switches, flag_on_off)}
示例15: _on_switch
# 需要导入模块: from ouimeaux.environment import Environment [as 别名]
# 或者: from ouimeaux.environment.Environment import start [as 别名]
class WemoController:
_env = None
def _on_switch(self, switch):
print "Light Switch found: ", switch.name
def _on_motion(self, motion):
print "Motion Sensor found: ", motion.name
def connect(self):
self._env = Environment(self._on_switch, self._on_motion)
try:
self._env.start()
except TypeError:
print "Start error"
try:
self._env.discover(seconds=3)
except TypeError:
print "Discovery error"
def find_switches(self):
result = []
switches = self._env.list_switches()
print "Found " + str(len(switches))
print switches
for s in switches:
print "Found " + s
result.append({ "name" : s, "state" : self._env.get_switch(s).get_state() })
return result
def switch_on(self, switch_name):
switch = self._env.get_switch(switch_name)
switch.on()
def switch_off(self, switch_name):
switch = self._env.get_switch(switch_name)
switch.off()
def switch_state(self, switch_name):
switch = self._env.get_switch(switch_name)
return switch.get_state()