def tcplink(sock, addr): print('Accept new connection from %s:%s...' % addr) while True: data = sock.recv(1024) time.sleep(0.1) echo = data.decode('utf-8') netcmd = echo.split(' ') if echo == '': break if not echo == 'keepAlive': print('<---' + addr.__str__() + '>' + echo) # 发送别忘了\n if netcmd[0] == 'keepAlive': sock.send('keepAlive\n'.encode('utf-8')) # print('--->' + addr.__str__() + '>' + 'keepAlive') elif netcmd[0] == 'verify': if Security.verify(netcmd[1]): sock.send('verify keycorrect\n'.encode('utf-8')) print('--->' + addr.__str__() + '>' + 'verify keycorrect') else: sock.send('verify keywrong\n'.encode('utf-8')) print('--->' + addr.__str__() + '>' + 'verify keywrong') elif netcmd[0] == 'isSmartLock': sock.send('SmartLock\n'.encode('utf-8')) print('--->' + addr.__str__() + '>' + 'SmartLock') elif netcmd[0] == 'setkey': if Security.isverify(): sock.send(Security.setkey(echo.split(' ')[1]).encode('utf-8')) elif netcmd[0] == 'state': if Security.isverify(): pass elif netcmd[0] == 'door_switch': if Security.isverify(): Door.door_switch(SmartHome.pinList['door']) sock.send(('door_switch ' + Door.lockstate + '\n').encode('utf-8')) print('--->' + addr.__str__() + '>' + 'door_switch ' + Door.lockstate) elif netcmd[0] == 'whiteLight_switch': if Security.isverify(): Furniture.whiteLight_switch(SmartHome.pinList['whiteLight']) sock.send(('whiteLight_switch ' + Furniture.whiteLightState + '\n').encode('utf-8')) print('--->' + addr.__str__() + '>' + 'whiteLight_switch ' + Furniture.whiteLightState) elif netcmd[0] == 'test': if Security.isverify(): pass elif echo == 'exit': break sock.close() print('Connection from %s:%s closed.' % addr)
print("命令行参数:%s" % sys.argv) #TCPSocket.startServer() HttpServer.start() about() print("SmartHome已启动\n控制台帮助请输入help") # ---------------------------- # loop while True: cmd = input("wxx>") if cmd == "help": print("SmartHome控制台帮助") print("about或version-----显示版本信息") print("help-----显示此帮助") print("stop或exit-----退出程序") print("") elif cmd == "stop" or cmd == "exit": stop() elif cmd == "about" or cmd == "version": about() elif cmd == "doortest1": doortest1() elif cmd == 'doorswitch': Door.door_switch(pinList['door']) elif cmd == '': pass else: print("未知命令 输入help查看帮助") cmd = "" # --------------------------------------