本文整理汇总了Python中bluepy.btle.BTLEDisconnectError方法的典型用法代码示例。如果您正苦于以下问题:Python btle.BTLEDisconnectError方法的具体用法?Python btle.BTLEDisconnectError怎么用?Python btle.BTLEDisconnectError使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bluepy.btle
的用法示例。
在下文中一共展示了btle.BTLEDisconnectError方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: on_attributes_update
# 需要导入模块: from bluepy import btle [as 别名]
# 或者: from bluepy.btle import BTLEDisconnectError [as 别名]
def on_attributes_update(self, content):
log.debug(content)
for device in self.__devices_around:
if self.__devices_around[device]['device_config'].get('name') == content['device']:
for requests in self.__devices_around[device]['device_config']["attributeUpdates"]:
for service in self.__devices_around[device]['services']:
if requests['characteristicUUID'] in self.__devices_around[device]['services'][service]:
characteristic = self.__devices_around[device]['services'][service][requests['characteristicUUID']]['characteristic']
if 'WRITE' in characteristic.propertiesToString():
if content['data'].get(requests['attributeOnThingsBoard']) is not None:
try:
self.__check_and_reconnect(device)
content_to_write = content['data'][requests['attributeOnThingsBoard']].encode('UTF-8')
characteristic.write(content_to_write, True)
except BTLEDisconnectError:
self.__check_and_reconnect(device)
content_to_write = content['data'][requests['attributeOnThingsBoard']].encode('UTF-8')
characteristic.write(content_to_write, True)
except Exception as e:
log.exception(e)
else:
log.error(
'Cannot process attribute update request for device: %s with data: %s and config: %s',
device,
content,
self.__devices_around[device]['device_config']["attributeUpdates"])
示例2: main
# 需要导入模块: from bluepy import btle [as 别名]
# 或者: from bluepy.btle import BTLEDisconnectError [as 别名]
def main():
if MQTT_DISCOVERY:
discovery()
BluetoothFailCounter = 0
while True:
try:
scanner = btle.Scanner(HCI_DEV).withDelegate(ScanProcessor())
scanner.scan(5) # Adding passive=True to try and fix issues on RPi devices
except BTLEDisconnectError as error:
sys.stderr.write(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} - btle disconnected: {error}\n")
pass
except BTLEManagementError as error:
sys.stderr.write(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} - Bluetooth connection error: {error}\n")
if BluetoothFailCounter >= 4:
sys.stderr.write(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} - 5+ Bluetooth connection errors. Resetting Bluetooth...\n")
cmd = 'hciconfig hci0 reset'
ps = subprocess.Popen(cmd, shell=True)
time.sleep(30)
BluetoothFailCounter = 0
else:
BluetoothFailCounter+=1
pass
except Exception as error:
sys.stderr.write(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} - Error while running the script: {error}\n")
pass
else:
BluetoothFailCounter = 0
time.sleep(TIME_INTERVAL)
示例3: status_update
# 需要导入模块: from bluepy import btle [as 别名]
# 或者: from bluepy.btle import BTLEDisconnectError [as 别名]
def status_update(self):
from bluepy import btle
for name, lywsd02 in self.devices.items():
try:
ret = lywsd02.readAll()
except btle.BTLEDisconnectError as e:
self.log_connect_exception(_LOGGER, name, e)
except btle.BTLEException as e:
self.log_unspecified_exception(_LOGGER, name, e)
else:
yield [MqttMessage(topic=self.format_topic(name), payload=json.dumps(ret))]
示例4: connect
# 需要导入模块: from bluepy import btle [as 别名]
# 或者: from bluepy.btle import BTLEDisconnectError [as 别名]
def connect(self, timeout=5):
from bluepy import btle
try:
device = btle.Peripheral(self.mac)
_LOGGER.debug("%s connected ", self.mac)
return device
except btle.BTLEDisconnectError as er:
_LOGGER.debug("failed connect %s", er)
示例5: update
# 需要导入模块: from bluepy import btle [as 别名]
# 或者: from bluepy.btle import BTLEDisconnectError [as 别名]
def update(self):
from bluepy import btle
if not self.connected:
return list()
self.values = list()
self.cnt += 1
try:
if self.cnt > 5:
self.cnt = 0
self.getBattery()
while self.device.waitForNotifications(1):
pass
if self.values:
self.offline = 0
else:
_LOGGER.debug("%s is silent", self.mac)
if self.offline > 3:
try:
self.device.disconnect()
except btle.BTLEInternalError as e:
_LOGGER.debug("%s", e)
self.device = None
_LOGGER.debug("%s reconnect", self.mac)
else:
self.offline += 1
except btle.BTLEDisconnectError as e:
_LOGGER.debug("%s", e)
self.device = None
finally:
return (self.batteryPct, self.values)
示例6: status_update
# 需要导入模块: from bluepy import btle [as 别名]
# 或者: from bluepy.btle import BTLEDisconnectError [as 别名]
def status_update(self):
from bluepy import btle
for name, lywsd03mmc in self.devices.items():
try:
ret = lywsd03mmc.readAll()
except btle.BTLEDisconnectError as e:
self.log_connect_exception(_LOGGER, name, e)
except btle.BTLEException as e:
self.log_unspecified_exception(_LOGGER, name, e)
else:
yield [MqttMessage(topic=self.format_topic(name), payload=json.dumps(ret))]
示例7: subscribe
# 需要导入模块: from bluepy import btle [as 别名]
# 或者: from bluepy.btle import BTLEDisconnectError [as 别名]
def subscribe(self):
while True:
try:
self.device.waitForNotifications(1.0)
except KeyboardInterrupt:
print("Module Interrupted")
return True
except BTLEDisconnectError:
print_error("Device disconnected...")
except:
self.disconnect()
示例8: server_side_rpc_handler
# 需要导入模块: from bluepy import btle [as 别名]
# 或者: from bluepy.btle import BTLEDisconnectError [as 别名]
def server_side_rpc_handler(self, content):
log.debug(content)
try:
for device in self.__devices_around:
if self.__devices_around[device]['device_config'].get('name') == content['device']:
for requests in self.__devices_around[device]['device_config']["serverSideRpc"]:
for service in self.__devices_around[device]['services']:
if requests['characteristicUUID'] in self.__devices_around[device]['services'][service]:
characteristic = self.__devices_around[device]['services'][service][requests['characteristicUUID']]['characteristic']
if requests.get('methodProcessing') and requests['methodProcessing'].upper() in characteristic.propertiesToString():
if content['data']['method'] == requests['methodRPC']:
response = None
if requests['methodProcessing'].upper() == 'WRITE':
try:
self.__check_and_reconnect(device)
response = characteristic.write(content['data'].get('params', '').encode('UTF-8'),
requests.get('withResponse', False))
except BTLEDisconnectError:
self.__check_and_reconnect(device)
response = characteristic.write(content['data'].get('params', '').encode('UTF-8'),
requests.get('withResponse', False))
elif requests['methodProcessing'].upper() == 'READ':
try:
self.__check_and_reconnect(device)
response = characteristic.read()
except BTLEDisconnectError:
self.__check_and_reconnect(device)
response = characteristic.read()
elif requests['methodProcessing'].upper() == 'NOTIFY':
try:
self.__check_and_reconnect(device)
delegate = self.__notify_handler(self.__devices_around[device],
characteristic.handle)
response = delegate.data
except BTLEDisconnectError:
self.__check_and_reconnect(device)
delegate = self.__notify_handler(self.__devices_around[device],
characteristic.handle)
response = delegate.data
if response is not None:
log.debug('Response from device: %s', response)
if requests['withResponse']:
response = 'success'
self.__gateway.send_rpc_reply(content['device'], content['data']['id'],
str(response))
else:
log.error(
'Method for rpc request - not supported by characteristic or not found in the config.\nDevice: %s with data: %s and config: %s',
device,
content,
self.__devices_around[device]['device_config']["serverSideRpc"])
except Exception as e:
log.exception(e)