1
0
mirror of https://github.com/thingsboard/thingsboard-gateway synced 2025-10-26 22:31:42 +08:00

Added device filtering

This commit is contained in:
samson0v
2023-01-03 13:18:27 +02:00
parent 4857f8fd0d
commit 5fdab962ff
5 changed files with 70 additions and 0 deletions

View File

@@ -36,3 +36,4 @@ class Status(Enum):
NOT_FOUND = 2,
SUCCESS = 3,
NO_NEW_DATA = 4
FORBIDDEN_DEVICE = 5

View File

@@ -0,0 +1,31 @@
import re
import simplejson
class DeviceFilter:
def __init__(self, config_path):
self._config_path = config_path
self._config = self._load_config()
def _load_config(self):
if self._config_path:
with open(self._config_path, 'r') as file:
return simplejson.load(file)
return {'deny': {}, 'allow': {}}
def validate_device(self, connector_name, data):
for con_name, device_list in self._config['deny'].items():
if con_name == connector_name:
for device in device_list:
if re.fullmatch(device, data['deviceName']):
return False
for con_name, device_list in self._config['allow'].items():
if con_name == connector_name:
for device in device_list:
if re.fullmatch(device, data['deviceName']):
return True
return True

View File

@@ -33,6 +33,7 @@ from yaml import safe_load
from thingsboard_gateway.gateway.constant_enums import DeviceActions, Status
from thingsboard_gateway.gateway.constants import CONNECTED_DEVICES_FILENAME, CONNECTOR_PARAMETER, \
PERSISTENT_GRPC_CONNECTORS_KEY_FILENAME
from thingsboard_gateway.gateway.device_filter import DeviceFilter
from thingsboard_gateway.gateway.duplicate_detector import DuplicateDetector
from thingsboard_gateway.gateway.statistics_service import StatisticsService
from thingsboard_gateway.gateway.tb_client import TBClient
@@ -81,6 +82,10 @@ DEFAULT_STATISTIC = {
'statsSendPeriodInSeconds': 3600
}
DEFAULT_DEVICE_FILTER = {
'enable': False
}
SECURITY_VAR = ('accessToken', 'caCert', 'privateKey', 'cert')
@@ -258,6 +263,12 @@ class TBGatewayService:
name="Send data to Thingsboard Thread")
self._send_thread.start()
self.__device_filter_config = self.__config['thingsboard'].get('deviceFiltering', DEFAULT_DEVICE_FILTER)
self.__device_filter = None
if self.__device_filter_config['enable']:
self.__device_filter = DeviceFilter(config_path=self._config_dir + self.__device_filter_config[
'filterFile'] if self.__device_filter_config.get('filterFile') else None)
self.__duplicate_detector = DuplicateDetector(self.available_connectors)
log.info("Gateway started.")
@@ -661,6 +672,14 @@ class TBGatewayService:
def send_to_storage(self, connector_name, data):
try:
device_valid = True
if self.__device_filter:
device_valid = self.__device_filter.validate_device(connector_name, data)
if not device_valid:
log.warning('Device %s forbidden', data['deviceName'])
return Status.FORBIDDEN_DEVICE
filtered_data = self.__duplicate_detector.filter_data(connector_name, data)
if filtered_data:
self.__converted_data_queue.put((connector_name, filtered_data), True, 100)