def start_car(): ## 启动小车 a = Switch('switch002', 'host_remote') a.execute('on') ## 启动小车 print("\033[1;31;47mStart the car\033[0m") time.sleep(3) a.execute('off') ## 关闭小车
import redis from switch import Switch rc = redis.Redis(host='182.254.134.84', port=36379) ps = rc.pubsub() ps.subscribe(['000-000-001', 'bar']) a = Switch('switchv2.0','host') for item in ps.listen(): if item['type'] == 'message': data = str(item['data']) if data ==r"b'switch on'": print('success') a.execute('on') elif data == r"b'switch off'": a.execute("off") else: print(item['data'])
from switch import Switch from dht11 import DHT11 import time while True: b = Switch('switch002', "host", "on") a = DHT11('dht11v2.0', 'host') temp = a.getHumidity() print(temp) if float(temp) > 80: print('c') b.execute('on') time.sleep(2) b.execute('off') time.sleep(1)
"The Command Pattern Use Case Example. A smart light Switch" from light import Light from switch import Switch from switch_on_command import SwitchOnCommand from switch_off_command import SwitchOffCommand # Create a receiver LIGHT = Light() # Create Commands SWITCH_ON = SwitchOnCommand(LIGHT) SWITCH_OFF = SwitchOffCommand(LIGHT) # Register the commands with the invoker SWITCH = Switch() SWITCH.register("ON", SWITCH_ON) SWITCH.register("OFF", SWITCH_OFF) # Execute the commands that are registered on the Invoker SWITCH.execute("ON") SWITCH.execute("OFF") SWITCH.execute("ON") SWITCH.execute("OFF") # show history SWITCH.show_history() # replay last two executed commands SWITCH.replay_last(2)