mirror of
https://github.com/thingsboard/thingsboard-gateway
synced 2025-10-26 22:31:42 +08:00
Merge branch 'thingsboard:master' into fix-remote-configurator
This commit is contained in:
@@ -29,10 +29,11 @@ from platform import system
|
||||
from time import time, sleep
|
||||
import asyncio
|
||||
|
||||
from bleak import BleakClient, BleakError
|
||||
from bleak import BleakClient
|
||||
|
||||
from thingsboard_gateway.connectors.connector import log
|
||||
from thingsboard_gateway.tb_utility.tb_loader import TBModuleLoader
|
||||
from thingsboard_gateway.connectors.ble.error_handler import ErrorHandler
|
||||
|
||||
MAC_ADDRESS_FORMAT = {
|
||||
'Darwin': '-',
|
||||
@@ -74,7 +75,7 @@ class Device(Thread):
|
||||
'serverSideRpc': config.get('serverSideRpc', [])
|
||||
}
|
||||
self.callback = config['callback']
|
||||
self.last_polled_time = 0
|
||||
self.last_polled_time = self.poll_period + 1
|
||||
|
||||
self.notifying_chars = []
|
||||
|
||||
@@ -104,9 +105,6 @@ class Device(Thread):
|
||||
self.stopped = True
|
||||
|
||||
async def timer(self):
|
||||
await self.__process_self()
|
||||
self.last_polled_time = time()
|
||||
|
||||
while True:
|
||||
try:
|
||||
if time() - self.last_polled_time >= self.poll_period:
|
||||
@@ -115,7 +113,8 @@ class Device(Thread):
|
||||
else:
|
||||
await asyncio.sleep(.2)
|
||||
except Exception as e:
|
||||
log.exception(e)
|
||||
log.error('Problem with connection')
|
||||
log.debug(e)
|
||||
|
||||
try:
|
||||
await self.client.disconnect()
|
||||
@@ -166,15 +165,25 @@ class Device(Thread):
|
||||
try:
|
||||
data = await self.client.read_gatt_char(char_id)
|
||||
not_converted_data[section].append({'data': data, **item})
|
||||
except BleakError as e:
|
||||
log.exception(e)
|
||||
except Exception as e:
|
||||
error = ErrorHandler(e)
|
||||
if error.is_char_not_found() or error.is_operation_not_supported():
|
||||
log.error(e)
|
||||
pass
|
||||
else:
|
||||
raise e
|
||||
elif item['method'] == 'notify' and char_id not in self.notifying_chars:
|
||||
try:
|
||||
self.__set_char_handle(item, char_id)
|
||||
self.notifying_chars.append(char_id)
|
||||
await self.notify(char_id)
|
||||
except BleakError as e:
|
||||
log.error(e)
|
||||
except Exception as e:
|
||||
error = ErrorHandler(e)
|
||||
if error.is_char_not_found() or error.is_operation_not_supported():
|
||||
log.error(e)
|
||||
pass
|
||||
else:
|
||||
raise e
|
||||
|
||||
if len(not_converted_data['telemetry']) > 0 or len(not_converted_data['attributes']) > 0:
|
||||
data_for_converter = {
|
||||
|
||||
21
thingsboard_gateway/connectors/ble/error_handler.py
Normal file
21
thingsboard_gateway/connectors/ble/error_handler.py
Normal file
@@ -0,0 +1,21 @@
|
||||
class ErrorHandler:
|
||||
def __init__(self, e):
|
||||
self.e = e
|
||||
|
||||
def is_char_not_found(self):
|
||||
try:
|
||||
if "could not be found!" in self.e.args[0]:
|
||||
return True
|
||||
except IndexError:
|
||||
return False
|
||||
|
||||
return False
|
||||
|
||||
def is_operation_not_supported(self):
|
||||
try:
|
||||
if 'not permitted' in self.e.args[1] or 'not supported' in self.e.args[1]:
|
||||
return True
|
||||
except IndexError:
|
||||
return False
|
||||
|
||||
return False
|
||||
@@ -11,8 +11,6 @@
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
from os import path
|
||||
from os.path import exists
|
||||
|
||||
from simplejson import dumps
|
||||
|
||||
@@ -22,13 +20,11 @@ from thingsboard_gateway.connectors.connector import log
|
||||
class BackwardCompatibilityAdapter:
|
||||
config_files_count = 1
|
||||
CONFIG_PATH = None
|
||||
if exists('config/'):
|
||||
CONFIG_PATH = 'config/'
|
||||
elif exists("/etc/thingsboard-gateway/config/".replace('/', path.sep)):
|
||||
CONFIG_PATH = "/etc/thingsboard-gateway/config/".replace('/', path.sep)
|
||||
|
||||
def __init__(self, config):
|
||||
def __init__(self, config, config_dir):
|
||||
self.__config = config
|
||||
self.__config_dir = config_dir
|
||||
BackwardCompatibilityAdapter.CONFIG_PATH = self.__config_dir
|
||||
self.__keys = ['host', 'port', 'type', 'method', 'timeout', 'byteOrder', 'wordOrder', 'retries', 'retryOnEmpty',
|
||||
'retryOnInvalid', 'baudrate']
|
||||
|
||||
@@ -46,7 +42,8 @@ class BackwardCompatibilityAdapter:
|
||||
|
||||
log.warning(
|
||||
'You are using old configuration structure for Modbus connector. It will be DEPRECATED in the future '
|
||||
'version! New config file "modbus_new.json" was generated in config/ folder. Please, use it.')
|
||||
'version! New config file "modbus_new.json" was generated in %s folder. Please, use it.', self.CONFIG_PATH)
|
||||
log.warning('You have to manually connect the new generated config file to tb_gateway.yaml!')
|
||||
|
||||
slaves = []
|
||||
for device in self.__config['server'].get('devices', []):
|
||||
|
||||
@@ -94,7 +94,7 @@ class ModbusConnector(Connector, Thread):
|
||||
self.__gateway = gateway
|
||||
self._connector_type = connector_type
|
||||
|
||||
self.__backward_compatibility_adapter = BackwardCompatibilityAdapter(config)
|
||||
self.__backward_compatibility_adapter = BackwardCompatibilityAdapter(config, gateway.config_dir)
|
||||
self.__config = self.__backward_compatibility_adapter.convert()
|
||||
|
||||
self.setName(self.__config.get("name", 'Modbus Default ' + ''.join(choice(ascii_lowercase) for _ in range(5))))
|
||||
|
||||
@@ -263,6 +263,10 @@ class TBGatewayService:
|
||||
log.info("The gateway has been stopped.")
|
||||
self.tb_client.stop()
|
||||
|
||||
@property
|
||||
def config_dir(self):
|
||||
return self._config_dir
|
||||
|
||||
def __close_connectors(self):
|
||||
for current_connector in self.available_connectors:
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user