BLESession: Simplify lock and unlock statements using 'with" statement

This commit is contained in:
MDE
2020-02-02 13:52:03 +01:00
committed by Shin'ichiro Kawasaki
parent 9038ef0ce3
commit 199b54f094

View File

@@ -346,9 +346,8 @@ class BLESession(Session):
if not delegate.restart_notification_event.is_set():
delegate.restart_notification_event.wait()
try:
self.session.lock.acquire()
with self.session.lock:
self.session.perip.waitForNotifications(1.0)
self.session.lock.release()
except Exception as e:
logger.error(e)
self.session.close()
@@ -507,9 +506,8 @@ class BLESession(Session):
logger.error("Failed to get characteristic {chara_id}")
self.status = self.DONE
else:
self.lock.acquire()
with self.lock:
b = c.read()
self.lock.release()
message = base64.standard_b64encode(b).decode('ascii')
res['result'] = { 'message': message, 'encode': 'base64' }
if params.get('startNotifications') == True:
@@ -544,9 +542,8 @@ class BLESession(Session):
"yet supported: ", params['encoding'])
msg_bstr = params['message'].encode('ascii')
data = base64.standard_b64decode(msg_bstr)
self.lock.acquire()
with self.lock:
c.write(data)
self.lock.release()
res['result'] = len(data)
logger.debug(res)
@@ -559,9 +556,8 @@ class BLESession(Session):
# prepare notification handler
self.delegate.add_handle(service_id, chara_id, handle)
# request notification to the BLE device
self.lock.acquire()
with self.lock:
self.perip.writeCharacteristic(chas[0].getHandle() + 1, value, True)
self.lock.release()
def startNotifications(self, service_id, chara_id):
logger.debug(f"start notification for {chara_id}")