mirror of
https://github.com/kawasaki/pyscrlink.git
synced 2025-09-05 17:20:20 +02:00
BLESession: Simplify lock and unlock statements using 'with" statement
This commit is contained in:
committed by
Shin'ichiro Kawasaki
parent
9038ef0ce3
commit
199b54f094
@@ -346,9 +346,8 @@ class BLESession(Session):
|
|||||||
if not delegate.restart_notification_event.is_set():
|
if not delegate.restart_notification_event.is_set():
|
||||||
delegate.restart_notification_event.wait()
|
delegate.restart_notification_event.wait()
|
||||||
try:
|
try:
|
||||||
self.session.lock.acquire()
|
with self.session.lock:
|
||||||
self.session.perip.waitForNotifications(1.0)
|
self.session.perip.waitForNotifications(1.0)
|
||||||
self.session.lock.release()
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(e)
|
logger.error(e)
|
||||||
self.session.close()
|
self.session.close()
|
||||||
@@ -507,9 +506,8 @@ class BLESession(Session):
|
|||||||
logger.error("Failed to get characteristic {chara_id}")
|
logger.error("Failed to get characteristic {chara_id}")
|
||||||
self.status = self.DONE
|
self.status = self.DONE
|
||||||
else:
|
else:
|
||||||
self.lock.acquire()
|
with self.lock:
|
||||||
b = c.read()
|
b = c.read()
|
||||||
self.lock.release()
|
|
||||||
message = base64.standard_b64encode(b).decode('ascii')
|
message = base64.standard_b64encode(b).decode('ascii')
|
||||||
res['result'] = { 'message': message, 'encode': 'base64' }
|
res['result'] = { 'message': message, 'encode': 'base64' }
|
||||||
if params.get('startNotifications') == True:
|
if params.get('startNotifications') == True:
|
||||||
@@ -544,9 +542,8 @@ class BLESession(Session):
|
|||||||
"yet supported: ", params['encoding'])
|
"yet supported: ", params['encoding'])
|
||||||
msg_bstr = params['message'].encode('ascii')
|
msg_bstr = params['message'].encode('ascii')
|
||||||
data = base64.standard_b64decode(msg_bstr)
|
data = base64.standard_b64decode(msg_bstr)
|
||||||
self.lock.acquire()
|
with self.lock:
|
||||||
c.write(data)
|
c.write(data)
|
||||||
self.lock.release()
|
|
||||||
res['result'] = len(data)
|
res['result'] = len(data)
|
||||||
|
|
||||||
logger.debug(res)
|
logger.debug(res)
|
||||||
@@ -559,9 +556,8 @@ class BLESession(Session):
|
|||||||
# prepare notification handler
|
# prepare notification handler
|
||||||
self.delegate.add_handle(service_id, chara_id, handle)
|
self.delegate.add_handle(service_id, chara_id, handle)
|
||||||
# request notification to the BLE device
|
# request notification to the BLE device
|
||||||
self.lock.acquire()
|
with self.lock:
|
||||||
self.perip.writeCharacteristic(chas[0].getHandle() + 1, value, True)
|
self.perip.writeCharacteristic(chas[0].getHandle() + 1, value, True)
|
||||||
self.lock.release()
|
|
||||||
|
|
||||||
def startNotifications(self, service_id, chara_id):
|
def startNotifications(self, service_id, chara_id):
|
||||||
logger.debug(f"start notification for {chara_id}")
|
logger.debug(f"start notification for {chara_id}")
|
||||||
|
Reference in New Issue
Block a user