mirror of
https://github.com/thingsboard/thingsboard-gateway
synced 2025-10-26 22:31:42 +08:00
16
thingsboard_gateway/config/list.json
Normal file
16
thingsboard_gateway/config/list.json
Normal file
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"deny": {
|
||||
"MQTT Broker Connector": [
|
||||
"Temperature Device",
|
||||
"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+$)"
|
||||
],
|
||||
"Modbus Connector": [
|
||||
"My Modbus Device"
|
||||
]
|
||||
},
|
||||
"allow": {
|
||||
"MQTT Broker Connector": [
|
||||
"My Temperature Sensor"
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,9 @@ thingsboard:
|
||||
enable: true
|
||||
statsSendPeriodInSeconds: 3600
|
||||
# configuration: statistics/statistics_linux.json
|
||||
deviceFiltering:
|
||||
enable: false
|
||||
filterFile: list.json
|
||||
maxPayloadSizeBytes: 1024
|
||||
minPackSendDelayMS: 200
|
||||
minPackSizeToSend: 500
|
||||
|
||||
@@ -36,3 +36,4 @@ class Status(Enum):
|
||||
NOT_FOUND = 2,
|
||||
SUCCESS = 3,
|
||||
NO_NEW_DATA = 4
|
||||
FORBIDDEN_DEVICE = 5
|
||||
|
||||
31
thingsboard_gateway/gateway/device_filter.py
Normal file
31
thingsboard_gateway/gateway/device_filter.py
Normal 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
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user