-
Notifications
You must be signed in to change notification settings - Fork 844
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1556 from samson0v/pr/prod-4685
Implemented new socket connector config
- Loading branch information
Showing
6 changed files
with
227 additions
and
7 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
{ | ||
"socket": { | ||
"type": "TCP", | ||
"address": "127.0.0.1", | ||
"port": 50000, | ||
"bufferSize": 1024 | ||
}, | ||
"devices": [ | ||
{ | ||
"address": "*:*", | ||
"deviceName": "Device Example", | ||
"deviceType": "default", | ||
"encoding": "utf-8", | ||
"telemetry": [ | ||
{ | ||
"key": "temp", | ||
"byteFrom": 0, | ||
"byteTo": -1 | ||
}, | ||
{ | ||
"key": "hum", | ||
"byteFrom": 0, | ||
"byteTo": 2 | ||
} | ||
], | ||
"attributes": [ | ||
{ | ||
"key": "name", | ||
"byteFrom": 0, | ||
"byteTo": -1 | ||
}, | ||
{ | ||
"key": "num", | ||
"byteFrom": 2, | ||
"byteTo": 4 | ||
} | ||
], | ||
"attributeRequests": [ | ||
{ | ||
"type": "shared", | ||
"requestExpressionSource": "expression", | ||
"attributeNameExpressionSource": "expression", | ||
"requestExpression": "${[0:3]==atr}", | ||
"attributeNameExpression": "[3:]" | ||
} | ||
], | ||
"attributeUpdates": [ | ||
{ | ||
"encoding": "utf-16", | ||
"attributeOnThingsBoard": "sharedName" | ||
} | ||
], | ||
"serverSideRpc": [ | ||
{ | ||
"methodRPC": "rpcMethod1", | ||
"withResponse": true, | ||
"encoding": "utf-8" | ||
} | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
{ | ||
"type": "TCP", | ||
"address": "127.0.0.1", | ||
"port": 50000, | ||
"bufferSize": 1024, | ||
"devices": [ | ||
{ | ||
"addressFilter": "*:*", | ||
"deviceName": "Device Example", | ||
"deviceType": "default", | ||
"encoding": "utf-8", | ||
"telemetry": [ | ||
{ | ||
"key": "temp", | ||
"byteFrom": 0, | ||
"byteTo": -1 | ||
}, | ||
{ | ||
"key": "hum", | ||
"byteFrom": 0, | ||
"byteTo": 2 | ||
} | ||
], | ||
"attributes": [ | ||
{ | ||
"key": "name", | ||
"byteFrom": 0, | ||
"byteTo": -1 | ||
}, | ||
{ | ||
"key": "num", | ||
"byteFrom": 2, | ||
"byteTo": 4 | ||
} | ||
], | ||
"attributeRequests": [ | ||
{ | ||
"type": "shared", | ||
"requestExpression": "${[0:3]==atr}", | ||
"attributeNameExpression": "[3:]" | ||
} | ||
], | ||
"attributeUpdates": [ | ||
{ | ||
"encoding": "utf-16", | ||
"attributeOnThingsBoard": "sharedName" | ||
} | ||
], | ||
"serverSideRpc": [ | ||
{ | ||
"methodRPC": "rpcMethod1", | ||
"withResponse": true, | ||
"encoding": "utf-8" | ||
} | ||
] | ||
} | ||
] | ||
} |
45 changes: 45 additions & 0 deletions
45
tests/unit/connectors/socket/test_socket_backward_compatibility_adapter.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import unittest | ||
from os import path | ||
from tests.unit.BaseUnitTest import BaseUnitTest | ||
from simplejson import load | ||
from thingsboard_gateway.connectors.socket.backward_compatibility_adapter import BackwardCompatibilityAdapter | ||
|
||
|
||
class SocketBackwardCompatibilityAdapterTests(BaseUnitTest): | ||
CONFIG_PATH = path.join(path.dirname(path.dirname(path.dirname(path.abspath(__file__)))), | ||
"connectors" + path.sep + "socket" + path.sep + "data" + path.sep) | ||
|
||
@staticmethod | ||
def convert_json(config_path): | ||
with open(config_path, 'r') as file: | ||
config = load(file) | ||
|
||
return config | ||
|
||
def setUp(self): | ||
self.maxDiff = 8000 | ||
self.adapter = BackwardCompatibilityAdapter(config={}) | ||
|
||
def test_is_old_config(self): | ||
is_old_config = self.convert_json(self.CONFIG_PATH + 'old_config.json') | ||
self.assertTrue(is_old_config, True) | ||
|
||
def test_convert_with_empty_config(self): | ||
result = self.adapter.convert() | ||
expected_result = {'socket': | ||
{'address': '127.0.0.1', | ||
'bufferSize': 1024, | ||
'port': 50000, | ||
'type': 'TCP'} | ||
} | ||
self.assertEqual(expected_result, result) | ||
|
||
def test_convert_from_old_to_new_config(self): | ||
self.adapter._config = self.convert_json(self.CONFIG_PATH + 'old_config.json') | ||
result = self.adapter.convert() | ||
expected_result = self.convert_json(self.CONFIG_PATH + 'new_config.json') | ||
self.assertEqual(expected_result, result) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
34 changes: 34 additions & 0 deletions
34
thingsboard_gateway/connectors/socket/backward_compatibility_adapter.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from copy import deepcopy | ||
|
||
|
||
class BackwardCompatibilityAdapter: | ||
def __init__(self, config): | ||
self._config = deepcopy(config) | ||
|
||
@staticmethod | ||
def is_old_config(config): | ||
return config.get('socket') is None | ||
|
||
def convert(self): | ||
socket_type = self._config.pop('type', 'TCP') | ||
address = self._config.pop('address', '127.0.0.1') | ||
port = self._config.pop('port', 50000) | ||
buffer_size = self._config.pop('bufferSize', 1024) | ||
|
||
self._config['socket'] = { | ||
'type': socket_type, | ||
'address': address, | ||
'port': port, | ||
'bufferSize': buffer_size | ||
} | ||
|
||
for device in self._config.get('devices', []): | ||
if device.get('addressFilter'): | ||
address_filter = device.pop('addressFilter') | ||
device['address'] = address_filter | ||
|
||
for attribute_requests in device.get('attributeRequests', []): | ||
attribute_requests['requestExpressionSource'] = 'expression' | ||
attribute_requests['attributeNameExpressionSource'] = 'expression' | ||
|
||
return self._config |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters