From 3c857d1c333127d7b674082de6d0fc3151ca0f98 Mon Sep 17 00:00:00 2001 From: samson0v Date: Thu, 22 Sep 2022 11:07:47 +0300 Subject: [PATCH] Init OCPP Connector --- setup.py | 4 +- thingsboard_gateway/config/ocpp.json | 3 + thingsboard_gateway/config/tb_gateway.yaml | 5 ++ .../connectors/ocpp/__init__.py | 13 ++++ .../connectors/ocpp/ocpp_connector.py | 61 +++++++++++++++++++ .../connectors/ocpp/ocpp_converter.py | 21 +++++++ .../extensions/ocpp/__init__.py | 13 ++++ .../gateway/tb_gateway_service.py | 1 + 8 files changed, 119 insertions(+), 2 deletions(-) create mode 100644 thingsboard_gateway/config/ocpp.json create mode 100644 thingsboard_gateway/connectors/ocpp/__init__.py create mode 100644 thingsboard_gateway/connectors/ocpp/ocpp_connector.py create mode 100644 thingsboard_gateway/connectors/ocpp/ocpp_converter.py create mode 100644 thingsboard_gateway/extensions/ocpp/__init__.py diff --git a/setup.py b/setup.py index 4e8ecf27..50ddc30e 100644 --- a/setup.py +++ b/setup.py @@ -40,13 +40,13 @@ setup( 'thingsboard_gateway.storage.file', 'thingsboard_gateway.storage.sqlite', 'thingsboard_gateway.tb_client', 'thingsboard_gateway.connectors', 'thingsboard_gateway.connectors.ble', 'thingsboard_gateway.connectors.socket', 'thingsboard_gateway.connectors.mqtt', 'thingsboard_gateway.connectors.opcua_asyncio', 'thingsboard_gateway.connectors.xmpp', - 'thingsboard_gateway.connectors.opcua', 'thingsboard_gateway.connectors.request', + 'thingsboard_gateway.connectors.opcua', 'thingsboard_gateway.connectors.request', 'thingsboard_gateway.connectors.ocpp', 'thingsboard_gateway.connectors.modbus', 'thingsboard_gateway.connectors.can', 'thingsboard_gateway.connectors.bacnet', 'thingsboard_gateway.connectors.bacnet.bacnet_utilities', 'thingsboard_gateway.connectors.odbc', 'thingsboard_gateway.connectors.rest', 'thingsboard_gateway.connectors.snmp', 'thingsboard_gateway.connectors.ftp', 'thingsboard_gateway.tb_utility', 'thingsboard_gateway.extensions', 'thingsboard_gateway.extensions.mqtt', 'thingsboard_gateway.extensions.modbus', 'thingsboard_gateway.extensions.opcua', - 'thingsboard_gateway.extensions.opcua_asyncio', + 'thingsboard_gateway.extensions.opcua_asyncio', 'thingsboard_gateway.extensions.ocpp', 'thingsboard_gateway.extensions.ble', 'thingsboard_gateway.extensions.serial', 'thingsboard_gateway.extensions.request', 'thingsboard_gateway.extensions.can', 'thingsboard_gateway.extensions.bacnet', 'thingsboard_gateway.extensions.odbc', 'thingsboard_gateway.extensions.rest', 'thingsboard_gateway.extensions.snmp', 'thingsboard_gateway.extensions.ftp', diff --git a/thingsboard_gateway/config/ocpp.json b/thingsboard_gateway/config/ocpp.json new file mode 100644 index 00000000..0e0dcd23 --- /dev/null +++ b/thingsboard_gateway/config/ocpp.json @@ -0,0 +1,3 @@ +{ + +} \ No newline at end of file diff --git a/thingsboard_gateway/config/tb_gateway.yaml b/thingsboard_gateway/config/tb_gateway.yaml index 7af12dd0..d589eaa9 100644 --- a/thingsboard_gateway/config/tb_gateway.yaml +++ b/thingsboard_gateway/config/tb_gateway.yaml @@ -115,6 +115,11 @@ connectors: # type: xmpp # configuration: xmpp.json # +# - +# name: OCPP Connector +# type: ocpp +# configuration: ocpp.json +# # ========= Customization ========== # # diff --git a/thingsboard_gateway/connectors/ocpp/__init__.py b/thingsboard_gateway/connectors/ocpp/__init__.py new file mode 100644 index 00000000..52b93288 --- /dev/null +++ b/thingsboard_gateway/connectors/ocpp/__init__.py @@ -0,0 +1,13 @@ +# Copyright 2022. ThingsBoard +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# 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. diff --git a/thingsboard_gateway/connectors/ocpp/ocpp_connector.py b/thingsboard_gateway/connectors/ocpp/ocpp_connector.py new file mode 100644 index 00000000..945c80bd --- /dev/null +++ b/thingsboard_gateway/connectors/ocpp/ocpp_connector.py @@ -0,0 +1,61 @@ +# Copyright 2022. ThingsBoard +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# 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 threading import Thread +from random import choice +from string import ascii_lowercase + +from thingsboard_gateway.connectors.connector import Connector, log +from thingsboard_gateway.tb_utility.tb_utility import TBUtility + + +try: + import ocpp +except ImportError: + print('OCPP library not found - installing...') + TBUtility.install_package("ocpp") + import ocpp + + +class OcppConnector(Connector, Thread): + def __init__(self, gateway, config, connector_type): + super().__init__() + self._log = log + self._config = config + self._connector_type = connector_type + self.statistics = {'MessagesReceived': 0, + 'MessagesSent': 0} + self._gateway = gateway + self.setName(config.get("name", 'OCPP Connector ' + ''.join(choice(ascii_lowercase) for _ in range(5)))) + self._connected = False + self.__stopped = False + self.daemon = True + + def open(self): + pass + + def close(self): + pass + + def get_name(self): + return self.name + + def is_connected(self): + pass + + def on_attributes_update(self, content): + pass + + def server_side_rpc_handler(self, content): + pass diff --git a/thingsboard_gateway/connectors/ocpp/ocpp_converter.py b/thingsboard_gateway/connectors/ocpp/ocpp_converter.py new file mode 100644 index 00000000..e8f2bca5 --- /dev/null +++ b/thingsboard_gateway/connectors/ocpp/ocpp_converter.py @@ -0,0 +1,21 @@ +# Copyright 2022. ThingsBoard +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# 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 thingsboard_gateway.connectors.converter import Converter, abstractmethod, log + + +class OcppConverter(Converter): + @abstractmethod + def convert(self, config, val, data=None): + pass diff --git a/thingsboard_gateway/extensions/ocpp/__init__.py b/thingsboard_gateway/extensions/ocpp/__init__.py new file mode 100644 index 00000000..52b93288 --- /dev/null +++ b/thingsboard_gateway/extensions/ocpp/__init__.py @@ -0,0 +1,13 @@ +# Copyright 2022. ThingsBoard +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# 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. diff --git a/thingsboard_gateway/gateway/tb_gateway_service.py b/thingsboard_gateway/gateway/tb_gateway_service.py index e8205ec1..4feca97d 100644 --- a/thingsboard_gateway/gateway/tb_gateway_service.py +++ b/thingsboard_gateway/gateway/tb_gateway_service.py @@ -72,6 +72,7 @@ DEFAULT_CONNECTORS = { "ftp": "FTPConnector", "socket": "SocketConnector", "xmpp": "XMPPConnector", + "ocpp": "OcppConnector", } DEFAULT_STATISTIC = {