def StartTCP(): global count count += 1 print('count', count) host, port = Method.getServerIPAndPort() global sockLocal sockLocal = socket.socket(socket.AF_INET, socket.SOCK_STREAM) Method.MyPrint('try connect to ', host, port) try: sockLocal.settimeout(10) sockLocal.connect((host, port)) Method.MyPrint('connect %s %d sucess' % (host, port)) # TCP 连接成功 TCPConnectSuccess() except: print('failed') global reTryConnect # 如果存在,停止计时器 try: reTryConnect.cancel() except: pass reTryConnect = threading.Timer(5, StartTCP) reTryConnect.start() pass
def send(data): s = socket(AF_INET, SOCK_DGRAM) s.setsockopt(SOL_SOCKET, SO_BROADCAST, 1) if len(data): host = Method.get255BroadAddList() port = Method.port() print('send to', host, port) s.sendto(data, (host, port)) else: print('数据为空')
def reviceData(s): while True: # 接收数据: data, addr = s.recvfrom(1024) myIP = Method.getSelfIP() # 判断是否为自己IP if addr[0] == myIP: Method.MyPrint('udp from self') pass else: Manage.handleUDP(s, data, addr) EsptouchConfig.logmsg(s, data, addr)
def StartUDP(): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) port = Method.port() try: # 绑定端口: s.bind(('', port)) # 不添加host表示接受广播 Method.MyPrint(Method.getSelfIP()) Method.MyPrint('wating for UDP message') h = threading.Thread(target=reviceData, args=(s, )) h.start() except ValueError as e: Method.MyPrint('Bind to port %s failed du to %s' % (port, e)) pass
def sendData(data): try: print('TCP发送') sockLocal.send(data) except: Method.MyPrint('TCP 发送失败')
def getIP(): myaddr = Method.getSelfIP() tumple = myaddr.split('.') a = int(tumple[0]) b = int(tumple[1]) c = int(tumple[2]) d = int(tumple[3]) return bytes([a, b, c, d])
def judgeUDPCmd(cmd, bodyData, mac): macSelf = DeviceMsg.getMac() # 设备发现 if cmd == 0x00: Method.MyPrint('0x00') sendData = DicToData.dataWithHeaderBody(DicToData.discover()) Brocaster.send(sendData) elif mac == macSelf: # 设备心跳 if cmd == 0x01: Method.MyPrint('0x01') sendData = DicToData.dataWithHeaderBody(DicToData.devHeartBeat()) Brocaster.send(sendData) pass # 设备控制 elif cmd == 0x02: Method.MyPrint('0x02') if bodyData[0] == 0x00: GPIOMethod.OPenClose22(False) sendData = DicToData.dataWithHeaderBody( DicToData.GPIOSControlRevice()) Brocaster.send(sendData) if bodyData[0] == 0xff: GPIOMethod.OPenClose22(True) sendData = DicToData.dataWithHeaderBody( DicToData.GPIOSControlRevice()) Brocaster.send(sendData) pass # 设备上报 elif cmd == 0x03: pass # 设备查询 elif cmd == 0x04: Method.MyPrint('0x04') sendData = DicToData.dataWithHeaderBody(DicToData.GPIOStaues()) Brocaster.send(sendData) pass else: pass
def reviceData(): while True: try: # 接收数据: data, addr = sockLocal.recvfrom(1024) if len(data) > 0: Method.MyPrint('Received from %s:%s.' % (data, addr)) Manage.handleTCP(sockLocal, data, addr) except: pass
def handleUDP(socket, data, addr): try: result = data.decode('utf-8') if result == '01': Method.MyPrint('let on') GPIOMethod.OPenClose22(True) Brocaster.send(b'let on') else: Method.MyPrint('let off') GPIOMethod.OPenClose22(False) Brocaster.send(b'let off') pass except: if len(data) >= 24: #GroupId if data[6:10] == DeviceMsg.getGroupId(): cmd = data[12] mac = data[13:21] Method.MyPrint('recvie cmd:%s mac:%s' % (cmd, mac)) judgeUDPCmd(cmd, data[24:len(data)], mac) pass
def sendData(data): try: print('tcp发送', data) sockLocal.send(data) except: Method.MyPrint('TCP 发送失败') if heartBeat.isAlive: heartBeat.cancel() if TCPRevice.isAlive: TCPRevice._stop try: StartTCP() except: pass
def handleTCP(socket, data, addr): try: result = data.decode('utf-8') if not data or data.decode('utf-8') == 'exit': socket.close() except: if len(data) >= 24: #GroupId if data[6:10] == DeviceMsg.getGroupId(): cmd = data[12] Method.MyPrint('recvie cmd:%s' % cmd) else: pass pass
def haedrDataWithArr(mac, body): bodyData = body[1] length = len(bodyData) cmd = body[0] # data = bytearray() msgId = getMessgeId() for b in (0x40, 0x02): data.insert(len(data), b) # for b in Method.intTO2BYtes(msgId): data.insert(len(data), b) # for b in (0xff, 0x00): data.insert(len(data), b) #GroupId for b in DeviceMsg.getGroupId(): data.insert(len(data), b) # for b in Method.intTO2BYtes(length): data.insert(len(data), b) # data.insert(len(data), cmd) # for b in mac: data.insert(len(data), b) # for b in (0x00, 0x00): data.insert(len(data), b) # data.insert(len(data), checksum(bodyData)) return data