def get_version(self, mac, addr): target = (bytes.fromhex(mac) + bytes(8))[:8] self.sequence = (self.sequence + 1) & 0xff out_data = protocol.Frame( frame_header=protocol.FrameHeader(source=self.source), frame_address=protocol.FrameAddress(target=target, res_required=True, sequence=self.sequence), protocol_header=protocol.ProtocolHeader( _type=protocol.PacketType.DEVICE_GET_VERSION)).serialize() now = time.monotonic() timeout = now for i in range(GET_VERSION_TRIES): self.socket.sendto(out_data, addr) timeout += GET_VERSION_TIMEOUT while now < timeout: r, _, _ = select.select([self.socket], [], [], timeout - now) if len(r): in_data, in_addr = self.socket.recvfrom(0x1000) frame = protocol.Frame() frame.deserialize(in_data) if (in_addr == addr and frame.frame_header.protocol == 1024 and frame.frame_header.addressable and frame.frame_header.source == self.source and frame.frame_address.target == target and frame.frame_address.sequence == self.sequence and frame.protocol_header.type == protocol.PacketType.DEVICE_STATE_VERSION): return frame.payload now = time.monotonic() raise UDPException()
def set_color(self, hsbk, now): target = (bytes.fromhex(self.mac) + bytes(8))[:8] self.sequence = (self.sequence + 1) & 0xff self.out_data = protocol.Frame( frame_header=protocol.FrameHeader(source=udp.source), frame_address=protocol.FrameAddress(target=target, ack_required=True, sequence=self.sequence), protocol_header=protocol.ProtocolHeader( _type=protocol.PacketType.LIGHT_SET_COLOR), payload=protocol.LightSetColor(color=protocol.LightHsbk( hue=int(round((hsbk[HSBK_HUE] % 360.) * (0xffff / 360.))), saturation=int(round(hsbk[HSBK_SAT] * 0xffff)), brightness=int(round(hsbk[HSBK_BR] * 0xffff)), kelvin=int(round(hsbk[HSBK_KELV]))))).serialize() if self.timeout is None: self.timeout = now
def get_service(self, mac=None): target = bytes(8) if mac is None else (bytes.fromhex(mac) + bytes(8))[:8] self.sequence = (self.sequence + 1) & 0xff out_data = protocol.Frame( frame_header=protocol.FrameHeader(source=self.source), frame_address=protocol.FrameAddress(target=target, res_required=True, sequence=self.sequence), protocol_header=protocol.ProtocolHeader( _type=protocol.PacketType.DEVICE_GET_SERVICE)).serialize() now = time.monotonic() timeout = now result = {} for i in range(GET_SERVICE_TRIES): self.socket.sendto(out_data, ('255.255.255.255', 56700)) timeout += GET_SERVICE_TIMEOUT while now < timeout: r, _, _ = select.select([self.socket], [], [], timeout - now) if len(r): in_data, in_addr = self.socket.recvfrom(0x1000) frame = protocol.Frame() frame.deserialize(in_data) if (frame.frame_header.protocol == 1024 and frame.frame_header.addressable and frame.frame_header.source == self.source and frame.frame_address.sequence == self.sequence and frame.protocol_header.type == protocol.PacketType.DEVICE_STATE_SERVICE): mac = frame.frame_address.target[:6].hex() if mac not in result: result[mac] = (in_addr, {}) result[mac][1][ frame.payload.service] = frame.payload.port now = time.monotonic() return result
def set_color(self, mac, addr, hsbk): target = (bytes.fromhex(mac) + bytes(8))[:8] self.sequence = (self.sequence + 1) & 0xff out_data = protocol.Frame( frame_header=protocol.FrameHeader(source=self.source), frame_address=protocol.FrameAddress(target=target, ack_required=True, sequence=self.sequence), protocol_header=protocol.ProtocolHeader( _type=protocol.PacketType.LIGHT_SET_COLOR), payload=protocol.LightSetColor(color=protocol.LightHsbk( hue=int(round((hsbk[HSBK_HUE] % 360.) * (0xffff / 360.))), saturation=int(round(hsbk[HSBK_SAT] * 0xffff)), brightness=int(round(hsbk[HSBK_BR] * 0xffff)), kelvin=int(round(hsbk[HSBK_KELV]))))).serialize() now = time.monotonic() timeout = now for i in range(SET_COLOR_TRIES): self.socket.sendto(out_data, addr) timeout += SET_COLOR_TIMEOUT while now < timeout: r, _, _ = select.select([self.socket], [], [], timeout - now) if len(r): in_data, in_addr = self.socket.recvfrom(0x1000) frame = protocol.Frame() frame.deserialize(in_data) if (in_addr == addr and frame.frame_header.protocol == 1024 and frame.frame_header.addressable and frame.frame_header.source == self.source and frame.frame_address.target == target and frame.frame_address.sequence == self.sequence and frame.protocol_header.type == protocol.PacketType.DEVICE_ACKNOWLEDGEMENT): return now = time.monotonic() raise UDPException()
def get_color(self, mac, addr): target = (bytes.fromhex(mac) + bytes(8))[:8] self.sequence = (self.sequence + 1) & 0xff out_data = protocol.Frame( frame_header=protocol.FrameHeader(source=self.source), frame_address=protocol.FrameAddress(target=target, res_required=True, sequence=self.sequence), protocol_header=protocol.ProtocolHeader( _type=protocol.PacketType.LIGHT_GET)).serialize() now = time.monotonic() timeout = now for i in range(GET_COLOR_TRIES): self.socket.sendto(out_data, addr) timeout += GET_COLOR_TIMEOUT while now < timeout: r, _, _ = select.select([self.socket], [], [], timeout - now) if len(r): in_data, in_addr = self.socket.recvfrom(0x1000) frame = protocol.Frame() frame.deserialize(in_data) if (in_addr == addr and frame.frame_header.protocol == 1024 and frame.frame_header.addressable and frame.frame_header.source == self.source and frame.frame_address.target == target and frame.frame_address.sequence == self.sequence and frame.protocol_header.type == protocol.PacketType.LIGHT_STATE): return numpy.array([ frame.payload.color.hue * (360. / 0xffff), frame.payload.color.saturation * (1. / 0xffff), frame.payload.color.brightness * (1. / 0xffff), frame.payload.color.kelvin ], numpy.double) now = time.monotonic() raise UDPException()