Skip to content

Commit 5f860e6

Browse files
committed
Add private getter method
1 parent a5d6678 commit 5f860e6

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

adafruit_ble_beacon.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -92,23 +92,29 @@ def beacon_tx_power(self) -> int:
9292
raise NotImplementedError("Must be defined in beacon subclass")
9393

9494

95-
9695
class iBeaconAdvertisement(_BeaconAdvertisement):
9796

9897
match_prefixes = (struct.pack("<BHBB", _MANUFACTURING_DATA_ADT, _APPLE_COMPANY_ID, _IBEACON_TYPE, _IBEACON_LENGTH),)
9998

10099
_data_format = ">HBBQQHHb"
101100
_beacon_data = MultiStruct(_data_format, advertising_data_type=0xFF)
102101

102+
_uuid_msb_index = 3
103+
_uuid_lsb_index = 4
104+
_major_index = 5
105+
_minor_index = 6
106+
_beacon_tx_power_index = 7
107+
108+
103109
def __init__(self, *, entry: Optional[_bleio.ScanEntry] = None, ) -> None:
104110
super().__init__(entry=entry)
105-
106111
if not entry:
107112
self._init_struct()
108113

109114
@property
110115
def uuid(self) -> bytes:
111-
_, _, _, uuid_msb, uuid_lsb, _, _, _ = self._beacon_data
116+
uuid_msb = self._get_struct_index(self._uuid_msb_index)
117+
uuid_lsb = self._get_struct_index(self._uuid_lsb_index)
112118
return struct.pack(">QQ", uuid_msb, uuid_lsb)
113119

114120
@uuid.setter
@@ -120,42 +126,36 @@ def uuid(self, id: bytes) -> None:
120126

121127
@property
122128
def major(self) -> int:
123-
_, _, _, _, _, major, _, _ = self._beacon_data
124-
return major
129+
return self._get_struct_index(self._major_index)
125130

126131
@major.setter
127132
def major(self, number: int) -> None:
128-
#flipped = self.flip_endian(number)
129133
self._set_struct_index(5, number)
130134

131135
@property
132136
def minor(self) -> int:
133-
_, _, _, _, _, _, minor, _ = self._beacon_data
134-
return minor
137+
return self._get_struct_index(self._minor_index)
135138

136139
@minor.setter
137140
def minor(self, number: int) -> None:
138141
self._set_struct_index(6, number)
139142

140143
@property
141144
def beacon_tx_power(self) -> int:
142-
_, _, _, _, _, _, _, tx_power = self._beacon_data
143-
return tx_power
145+
return self._get_struct_index(self._beacon_tx_power_index)
144146

145147
@beacon_tx_power.setter
146148
def beacon_tx_power(self, power: int) -> None:
147149
self._set_struct_index(7, power)
148150

149151
def _set_struct_index(self, index: int, value: int) -> int:
150152
current_beacon_data = list(self._beacon_data)
151-
flipped = self.flip_endian(value, index)
152-
current_beacon_data[index] = flipped
153+
current_beacon_data[index] = value
153154
self._beacon_data = current_beacon_data
154155

156+
def _get_struct_index(self, index: int) -> int:
157+
temp_tuple = self._beacon_data
158+
return temp_tuple[index]
159+
155160
def _init_struct(self) -> None:
156161
self._beacon_data = (_APPLE_COMPANY_ID_FLIPPED, _IBEACON_TYPE, _IBEACON_LENGTH, 0, 0, 0, 0, 0)
157-
158-
def flip_endian(self, number: int, index: int):
159-
index_format = self._data_format[index+1]
160-
temp_bytes = struct.pack("<" + index_format, number)
161-
return struct.unpack("<" + index_format, temp_bytes)[0]

0 commit comments

Comments
 (0)