class NetSuccessBean(object): __slots__ = ['_usage', '_ip_list'] usage = typed_property("usage", str) ip_list = typed_property("ip_list", str) typecode = '<16s1024s' def __init__(self, *, ip_list): self.usage = "net_success" self.ip_list = ip_list def __iter__(self): return (i for i in (self.usage, self.ip_list)) def __bytes__(self,typecode=typecode): bytes_data = [encode_(m) for m in self] return struct.pack(typecode,*bytes_data) @classmethod def frombytes(cls,bytes_data): memv = memoryview(bytes_data) bytes_data = [decode_(x) for x in struct.unpack(cls.typecode,memv[:].tobytes())] return cls(ip_list=bytes_data[1]) def send(self, _send, addr): ''' 使用send 向 addr发送数据 :param send: protocal :param addr: 地址 :return: ''' _send.send_apply(bytes(self), addr)
class MeasureDataBean: __slots__ = ['_usage', '_device_category', '_device_id', "_temperature"] usage = typed_property("usage", str) device_category = typed_property("device_category", str) device_id = typed_property("device_id", int) temperature = typed_property("temperature", float) typecode = '<16s16sid' def __init__(self, *, usage="measure_data", device_category, device_id, temperature): self.usage = usage self.device_category = device_category self.device_id = device_id self.temperature = temperature def __iter__(self): return (i for i in (self.usage, self.device_category, self.device_id, self.temperature)) def device_kind(self): return "模拟电台" if self.device_category.split("_")[-1] == "r" else "虚拟电台" @property def device_name(self): return self.device_category.split(".")[-1] + "_" + str(self.device_id) @property def ziwang_name(self): return self.device_category.split('.')[-2] def __bytes__(self, typecode=typecode): bytes_data = [encode_(m) for m in self] return struct.pack(typecode, *bytes_data) @classmethod def frombytes(cls, bytes_data): memv = memoryview(bytes_data) bytes_data = [ decode_(x) for x in struct.unpack(cls.typecode, memv[:].tobytes()) ] return cls(device_category=bytes_data[1], device_id=bytes_data[2], temperature=bytes_data[3]) def send(self, _send, addr): ''' 使用send 向 addr发送数据 :param send: protocal :param addr: 地址 :return: ''' _send.send_apply(bytes(self), addr) def __str__(self): return str(self.temperature)
class TextDataBean(object): __slots__ = ['_usage', '_device_category', '_device_id', '_data'] usage = typed_property("usage", str) device_category = typed_property("device_category", str) device_id = typed_property("device_id", int) data = typed_property("data", str) typecode = '<16s16si64s' def __init__(self, *, device_category, device_id, data): self.usage = "text_data" self.device_category = device_category self.device_id = device_id self.data = data def __iter__(self): return (i for i in (self.usage, self.device_category, self.device_id, self.data)) @property def device_name(self): return self.device_category.split(".")[-1] + "_" + str(self.device_id) @property def ziwang_name(self): return self.device_category.split('.')[-2] def __bytes__(self, typecode=typecode): bytes_data = [encode_(m) for m in self] return struct.pack(typecode, *bytes_data) @classmethod def frombytes(cls, bytes_data): memv = memoryview(bytes_data) data_para = [ decode_(x) for x in struct.unpack(cls.typecode, memv[:].tobytes()) ] return cls(device_category=data_para[1], device_id=data_para[2], data=data_para[3]) def send(self, _send, addr): ''' 使用send 向 addr发送数据 :param send: protocal :param addr: 地址 :return: ''' try: _send.send_apply(bytes(self), addr) except Exception as e: print(e)
class ApplyPosition(object): __slots__ = ['_usage'] usage = typed_property("usage", str) ENCODE_TYPE = "utf-8" def __init__(self): self.usage = "apply_position" @staticmethod def format_(): return "!16s" @property def all_data(self): return (self.usage, ) @property def pack_data(self): pack_data_ = tuple( map( lambda m: m.encode(ApplyPosition.ENCODE_TYPE) if type(m) == str else m, self.all_data)) return struct.pack(self.format_(), *pack_data_) @staticmethod def unpack_data(): bean = ApplyPosition() return bean def send(self, __send, addr): __send.send_apply(self.pack_data, addr) def __str__(self): return str(self.all_data)
class AcceptVoiceReplyBean(object): __slots__ = ['_usage'] usage = typed_property('usage', str) typecode = "<16s" def __init__(self): self.usage = 'accept_voice_r' def __iter__(self): return (i for i in (self.usage, )) def __bytes__(self, typecode=typecode): bytes_data = [encode_(m) for m in self] return struct.pack(typecode, *bytes_data) def __repr__(self): class_name = type(self).__name__ return '{}'.format(class_name) def __str__(self): return str(tuple(self)) def __eq__(self, other): return tuple(self) == tuple(other) @classmethod def frombytes(cls): return cls() def send(self, __send, addr): __send.send_apply(bytes(self), addr)
class RejectVoiceReplyBean(object): __slots__ = ['_usage'] usage = typed_property('usage', str) ENCODE_TYPE = 'utf-8' def __init__(self): self.usage = 'reject_voice_r' @staticmethod def format_(): return "!16s" @property def all_data(self): return (self.usage, ) @property def pack_data(self): pack_data_ = tuple( map( lambda m: m.encode(RejectVoiceReplyBean.ENCODE_TYPE) if type(m) == str else m, self.all_data)) return struct.pack(self.format_(), *pack_data_) @staticmethod def unpack_data(): bean = RejectVoiceReplyBean() return bean def send(self, __send, addr): __send.send_apply(self.pack_data, addr)
class NetFailedBean(object): __slots__ = ['_usage'] usage = typed_property("usage", str) typecode = "<16s" def __init__(self, usage='net_failed'): self.usage = usage def __iter__(self): return (i for i in (self.usage, )) def __bytes__(self, typecode=typecode): bytes_data = [encode_(m) for m in self] return struct.pack(typecode, *bytes_data) @classmethod def frombytes(cls): return cls() def send(self, _send, addr): ''' 使用send 向 addr发送数据 :param send: protocal :param addr: 地址 :return: ''' _send.send_apply(bytes(self), addr)
class NetFailedBean(object): __slots__ = ['_usage'] usage = typed_property("usage", str) ENCODE_TYPE = "utf-8" def __init__(self): self.usage = "net_failed" @staticmethod def format_(): return "!16s" @property def all_data(self): return ( self.usage, ) @property def pack_data(self): __pack_data = tuple( map(lambda m: m.encode(NetFailedBean.ENCODE_TYPE) if type(m) == str else m, self.all_data) ) print(__pack_data) return struct.pack(self.format_(), *__pack_data) @staticmethod def unpack_data(pack_data): # unpack_data_ = tuple( # map(lambda m: m.decode(ReplyForNetFailureBean.ENCODE_TYPE).strip("\x00"), # struct.unpack(ReplyForNetFailureBean.format_(), pack_data)) # ) bean = NetFailedBean() return bean
class VoiceDataBean(object): __slots__ = ['_usage', '_device_category', '_device_id', '_voice_data'] usage = typed_property('usage', str) device_category = typed_property('device_category', str) device_id = typed_property('device_id', int) voice_data = typed_property('voice_data', bytes) typecode = '<16s16si360s' typecode_without_voice = "<16s16si" def __init__(self, *, device_category, device_id, voice_data): self.usage = 'voice_data' self.device_category = device_category self.device_id = device_id # 待解决问题。如果获得voice_data, # 不在bean中解决,bean中数据不具有特殊型 self.voice_data = voice_data def __iter__(self): return (i for i in (self.usage, self.device_category, self.device_id, self.voice_data)) def __bytes__(self, typecode=typecode): bytes_data = [encode_(m) for m in self] return struct.pack(typecode, *bytes_data) @property def device_name(self): return self.device_category.split(".")[-1] + "_" + str(self.device_id) @property def ziwang_name(self): return self.device_category.split('.')[-2] @classmethod def frombytes(cls, bytes_data): memv = memoryview(bytes_data) data_para = [ decode_(x) for x in struct.unpack(cls.typecode_without_voice, memv[:36].tobytes()) ] return cls(device_category=data_para[1], device_id=data_para[2], voice_data=memv[36:].tobytes()) def send(self, _send, addr): _send.send_apply(bytes(self), addr)
class ApplyForVoiceBean(object): __slots__ = ['_usage', '_device_category', '_device_id'] usage = typed_property('usage', str) device_category = typed_property('device_category', str) device_id = typed_property('device_id', int) typecode = '<16s16si' def __init__(self, *, usage='apply_voice', device_category, device_id): self.usage = usage self.device_category = device_category self.device_id = device_id def __iter__(self): return (i for i in (self.usage, self.device_category, self.device_id)) def __str__(self): return str(tuple(self)) def __bytes__(self, typecode=typecode): bytes_data = [encode_(m) for m in self] return struct.pack(typecode, *bytes_data) @property def device_name(self): return self.device_category.split(".")[-1] + "_" + str(self.device_id) @property def ziwang_name(self): return self.device_category.split('.')[-2] @classmethod def frombytes(cls, bytes_data): memv = memoryview(bytes_data) data_para = [ decode_(x) for x in struct.unpack(cls.typecode, memv[:].tobytes()) ] print(data_para) return cls(device_category=data_para[1], device_id=data_para[2]) def send(self, __send, addr): try: __send.send_apply(bytes(self), addr) except Exception as e: print(e)
class NetSuccessBean(object): __slots__ = ['_usage', '_ip_list'] usage = typed_property("usage", str) ip_list = typed_property("ip_list", str) ENCODE_TYPE = "utf-8" def __init__(self, *, ip_list): self.usage = "net_success" self.ip_list = ip_list @staticmethod def format_(): return "!16s2048s" @property def all_data(self): return (self.usage, self.ip_list) @property def pack_data(self): __pack_data = tuple( map( lambda m: m.encode(NetSuccessBean.ENCODE_TYPE) if type(m) == str else m, self.all_data)) print(__pack_data) # dprint(struct.pack(ReplyForNetSuccessBean.format_(),pack_data_)) return struct.pack(NetSuccessBean.format_(), *__pack_data) @staticmethod def unpack_data(pack_data): unpack_data_ = tuple( map( lambda m: m.decode(NetSuccessBean.ENCODE_TYPE).strip("\x00") if type(m) == bytes else m, struct.unpack(NetSuccessBean.format_(), pack_data))) bean = NetSuccessBean(ip_list=unpack_data_[1]) return bean
class PositionDataBean: __slots__ = ['_usage', '_device_category', '_device_id', '_position_x', '_position_y', '_position_z'] usage = typed_property("usage", str) device_category = typed_property("device_category", str) device_id = typed_property("device_id", int) position_x = typed_property("position_x", float) position_y = typed_property("position_y", float) position_z = typed_property("position_z", float) typecode = '<16s16siddd' def __init__(self, *, usage='position_data', device_category, device_id, position_x, position_y, position_z): self.usage = usage self.device_category = device_category self.device_id = device_id self.position_x = float(position_x) self.position_y = float(position_y) self.position_z = float(position_z) def __iter__(self): return (i for i in (self.usage, self.device_category, self.device_id, self.position_x, self.position_y, self.position_z)) def device_kind(self): return "模拟电台" if self.device_category.split("_")[-1] == "r" else "虚拟电台" @property def device_name(self): return self.device_category.split(".")[-1] + "_" + str(self.device_id) @property def ziwang_name(self): return self.device_category.split('.')[-2] def __bytes__(self, typecode=typecode): bytes_data = [encode_(m) for m in self] return struct.pack(typecode, *bytes_data) @classmethod def frombytes(cls, bytes_data): memv = memoryview(bytes_data) data_para = [decode_(x) for x in struct.unpack(cls.typecode, memv[:].tobytes())] return cls(device_category=data_para[1], device_id=data_para[2], position_x=data_para[3], position_y=data_para[4], position_z=data_para[5]) def send(self, _send, addr): ''' 使用send 向 addr发送数据 :param send: protocal :param addr: 地址 :return: ''' _send.send_apply(bytes(self), addr) def __str__(self): return "经度:{:.3f}°,纬度:{:.3f}°,高度:{:.3f}km".format(self.position_x, self.position_y, self.position_z)
class RejectVoiceReplyBean(object): __slots__ = ['_usage'] usage = typed_property('usage', str) typecode = '<16s' def __init__(self): self.usage = 'reject_voice_r' def __iter__(self): return (i for i in (self.usage, )) def __bytes__(self, typecode=typecode): bytes_data = [encode_(m) for m in self] return struct.pack(typecode, *bytes_data) @classmethod def frombytes(cls): return cls() def send(self, _send, addr): _send.send_apply(bytes(self), addr)
class TextSuccessReceive(object): __slots__ = ['_usage'] usage = typed_property("usage", str) typecode = '<16s' def __init__(self): self.usage = "text_received" def __iter__(self): return (i for i in (self.usage, )) def __bytes__(self): bytes_data = [encode_(m) for m in self] return struct.pack(self.typecode, *bytes_data) @classmethod def frombytes(cls): return cls() def send(self, _send, addr): _send.send_apply(bytes(self), addr)
class ApplyPosition: __slots__ = ['_usage'] usage = typed_property("usage", str) typecode = '<16s' def __init__(self): self.usage = "apply_position" def __iter__(self): return (i for i in (self.usage, )) def __bytes__(self, typecode=typecode): bytes_data = [encode_(m) for m in self] return struct.pack(typecode, *bytes_data) @classmethod def frombytes(cls): return cls() def send(self, __send, addr): __send.send_apply(bytes(self), addr) def __str__(self): return str(self)
class MeasureSuccessReceive(object): __slots__ = ['_usage'] usage = typed_property("usage", str) typecode = '<16s' def __init__(self, *, usage='measure_recv'): self.usage = usage def __iter__(self): return (i for i in (self.usage,)) def __bytes__(self, typecode=typecode): bytes_data = [encode_(m) for m in self] return struct.pack(typecode, *bytes_data) @classmethod def frombytes(cls): return cls() def send(self, _send, addr): _send.send_apply(bytes(self), addr) def __str__(self): return str(tuple(self))
class MeasureDataBean: __slots__ = ['_usage', '_device_category', '_device_id', "_temperature"] usage = typed_property("usage", str) device_category = typed_property("device_category", str) device_id = typed_property("device_id", int) temperature = typed_property("temperature", float) ENCODE_TYPE = "utf-8" def __init__(self, *, device_category, device_id, temperature): self.usage = "position_data" self.device_category = device_category self.device_id = device_id self.temperature = temperature @staticmethod def format_(): return "!16s16sid" @property def all_data(self): return (self.usage, self.device_category, self.device_id, self.temperature) @property def device_name(self): return self.device_category + "_" + str(self.device_id) @property def pack_data(self): __pack_data_ = tuple( map( lambda m: m.encode(MeasureDataBean.ENCODE_TYPE) if type(m) == str else m, self.all_data)) return struct.pack(self.format_(), *__pack_data_) @staticmethod def unpack_data(pack_data): unpack_data_ = tuple( map( lambda m: m.decode(MeasureDataBean.ENCODE_TYPE).strip("\x00") if type(m) == bytes else m, struct.unpack(MeasureDataBean.format_(), pack_data))) bean = MeasureDataBean(device_category=unpack_data_[1], device_id=unpack_data_[2], temperature=unpack_data_[3]) return bean def send(self, __send, addr): ''' 使用send 向 addr发送数据 :param send: protocal :param addr: 地址 :return: ''' try: __send.send_apply(self.pack_data, addr) except Exception as e: logger.error(e) def __str__(self): return str(self.all_data)
class PositionDataBean: __slots__ = [ '_usage', '_device_category', '_device_id', '_position_x', '_position_y' ] usage = typed_property("usage", str) device_category = typed_property("device_category", str) device_id = typed_property("device_id", int) position_x = typed_property("position_x", int) position_y = typed_property("position_y", int) ENCODE_TYPE = "utf-8" def __init__(self, *, device_category, device_id, position_x, position_y): self.usage = "position_data" self.device_category = device_category self.device_id = device_id self.position_x = position_x self.position_y = position_y @staticmethod def format_(): return "!16s16siii" @property def all_data(self): return (self.usage, self.device_category, self.device_id, self.position_x, self.position_y) @property def device_name(self): return self.device_category + "_" + str(self.device_id) @property def pack_data(self): __pack_data_ = tuple( map( lambda m: m.encode(PositionDataBean.ENCODE_TYPE) if type(m) == str else m, self.all_data)) return struct.pack(self.format_(), *__pack_data_) @staticmethod def unpack_data(pack_data): unpack_data_ = tuple( map( lambda m: m.decode(PositionDataBean.ENCODE_TYPE).strip("\x00") if type(m) == bytes else m, struct.unpack(PositionDataBean.format_(), pack_data))) bean = PositionDataBean(device_category=unpack_data_[1], device_id=unpack_data_[2], position_x=unpack_data_[3], position_y=unpack_data_[4]) return bean def send(self, __send, addr): ''' 使用send 向 addr发送数据 :param send: protocal :param addr: 地址 :return: ''' try: __send.send_apply(self.pack_data, addr) except Exception as e: logger.error(e) def __str__(self): return "device_category:" + self.device_category + "_" + str( self.device_id) + " positoin:" + "(" + str( self.position_x) + "," + str(self.position_y) + ")"
class ApplyForNetBean: __slots__ = [ '_usage', '_device_category', '_width_band', '_interval', '_routing_parameter', "_device_id" ] usage = typed_property("usage", str) device_category = typed_property("device_category", str) width_band = typed_property("width_band", int) interval = typed_property("interval", int) routing_parameter = typed_property("routing_parameter", str) device_id = typed_property("device_id", int) typecode = "<16s16sii16si" def __init__(self, *, usage='apply_net', device_category, width_band, interval, routing_parameter, device_id): self.usage = usage self.device_category = device_category self.device_id = device_id self.width_band = width_band self.interval = interval self.routing_parameter = routing_parameter def __iter__(self): return (i for i in (self.usage, self.device_category, self.width_band, self.interval, self.routing_parameter, self.device_id)) def __repr__(self): classs_name = type(self).__name__ return "{}(usage={!r},device_category={!r},width_band={!r},interval={!r},routing_parameter={!r},device_id={!r})".format( classs_name, *self) def __eq__(self, other): return tuple(self) == tuple(other) def __str__(self): return str(tuple(self)) def __bytes__(self, typecode=typecode): bytes_data = [encode_(m) for m in self] return struct.pack(typecode, *bytes_data) @property def device_name(self): return self.device_category.split(".")[-1] + "_" + str(self.device_id) @property def ziwang_name(self): return self.device_category.split('.')[-2] @classmethod def frombytes(cls, bytes_data): memv = memoryview(bytes_data) data_para = [ decode_(x) for x in struct.unpack(cls.typecode, memv[:].tobytes()) ] print(data_para) return cls(device_category=data_para[1], width_band=data_para[2], interval=data_para[3], routing_parameter=data_para[4], device_id=data_para[5]) def send(self, __send, addr): __send.send_apply(bytes(self), addr) def __bool__(self): if self.width_band in [ 437, ] and self.interval in [25, 625] and self.routing_parameter in [ "OSPF协议", ]: return True else: return False
class ApplyForNetBean(object): __slots__ = [ '_usage', '_device_category', '_width_band', '_interval', '_routing_parameter', "_device_id" ] usage = typed_property("usage", str) device_category_ = typed_property("device_category", str) width_band = typed_property("width_band", int) interval = typed_property("interval", int) routing_parameter = typed_property("routing_parameter", str) device_id = typed_property("device_id", int) ENCODE_TYPE = "utf-8" def __init__(self, *, device_category, width_band, interval, routing_parameter, device_id): self.usage = "apply_net" self.device_category_ = device_category self.device_id = device_id self.width_band = width_band self.interval = interval self.routing_parameter = routing_parameter @staticmethod def format_(): return "!16s16sii16si" @property def all_data(self): return ( self.usage, self.device_category_, self.width_band, self.interval, self.routing_parameter, self.device_id, ) @property def pack_data(self): pack_data_ = tuple( map( lambda m: m.encode(ApplyForNetBean.ENCODE_TYPE) if type(m) == str else m, self.all_data)) return struct.pack(self.format_(), *pack_data_) @staticmethod def unpack_data(pack_data): unpack_data_ = tuple( map( lambda m: m.decode(ApplyForNetBean.ENCODE_TYPE).strip("\x00") if type(m) == bytes else m, struct.unpack(ApplyForNetBean.format_(), pack_data))) bean = ApplyForNetBean(width_band=unpack_data_[2], interval=unpack_data_[3], routing_parameter=unpack_data_[4], device_id=unpack_data_[5], device_category=unpack_data_[1]) return bean def send(self, send, addr): ''' :param send: protocal :param addr: 地址 :return: ''' send.send_apply(self.pack_data, addr) def is_allow_in(self): ''' :return: 看一下self.width_band == 1 2 4 8 self.interval 625 25 ''' if self.width_band in [1, 2, 4, 8] and self.interval in [ 25, 625 ] and self.routing_parameter in [ "OSPF协议", ]: return True else: print(12) return False