示例#1
0
 def update(self):
     if self.__version["latest_version"] is not None:
         result = TBUtility.install_package(
             "thingsboard-gateway", self.__version["latest_version"])
     else:
         result = "Congratulations! You have the latest version."
     return result
import uuid
from threading import Thread
from time import sleep, time
from math import sin

from thingsboard_gateway.tb_utility.tb_utility import TBUtility
from tests.connectors.connector_tests_base import ConnectorTestBase, log

try:
    from opcua.ua import NodeId, NodeIdType
    from opcua import ua, uamethod, Server
except ImportError:
    log.error("OpcUa library - not found. Installing...")
    TBUtility.install_package("opcua")
    from opcua.ua import NodeId, NodeIdType
    from opcua import ua, uamethod, Server

class OpcUaConnectorGeneralTest(ConnectorTestBase):
    def test_number_one(self):
        self._create_connector("connection_test.json")
        self.assertTrue(self.connector is not None)
        self.check_or_create_server()
        self.connector.open()



    def check_or_create_server(self):
        if not hasattr(self, "test_server"):
            self.test_server = Server()
            self.__server_thread = Thread(target=self.__server_run, name="Test OPC UA server", args=(self.test_server,))
        self.assertTrue(self.test_server is not None)
#     See the License for the specific language governing permissions and
#     limitations under the License.
from hashlib import sha1
from pathlib import Path
from time import sleep
from random import choice
from string import ascii_lowercase
from threading import Thread
from simplejson import dumps, load

from thingsboard_gateway.tb_utility.tb_utility import TBUtility
try:
    import pyodbc
except ImportError:
    print("ODBC library not found - installing...")
    TBUtility.install_package("pyodbc")
    import pyodbc

from thingsboard_gateway.connectors.odbc.odbc_uplink_converter import OdbcUplinkConverter

from thingsboard_gateway.connectors.connector import Connector, log


class OdbcConnector(Connector, Thread):
    DEFAULT_SEND_IF_CHANGED = True
    DEFAULT_RECONNECT_STATE = True
    DEFAULT_SAVE_ITERATOR = False
    DEFAULT_RECONNECT_PERIOD = 60
    DEFAULT_POLL_PERIOD = 60
    DEFAULT_ENABLE_UNKNOWN_RPC = False
    DEFAULT_OVERRIDE_RPC_PARAMS = False
#     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.

import time
import threading
from random import choice
from string import ascii_lowercase

from thingsboard_gateway.tb_utility.tb_utility import TBUtility
# Try import Pymodbus library or install it and import
try:
    from pymodbus.constants import Defaults
except ImportError:
    print("Modbus library not found - installing...")
    TBUtility.install_package("pymodbus", ">=2.3.0")
    TBUtility.install_package('pyserial')
    from pymodbus.constants import Defaults

from pymodbus.client.sync import ModbusTcpClient, ModbusUdpClient, ModbusSerialClient, ModbusRtuFramer, ModbusSocketFramer
from pymodbus.bit_write_message import WriteSingleCoilResponse, WriteMultipleCoilsResponse
from pymodbus.register_write_message import WriteMultipleRegistersResponse, \
                                            WriteSingleRegisterResponse
from pymodbus.register_read_message import ReadRegistersResponseBase
from pymodbus.bit_read_message import ReadBitsResponseBase
from pymodbus.exceptions import ConnectionException

from thingsboard_gateway.connectors.connector import Connector, log
from thingsboard_gateway.connectors.modbus.bytes_modbus_uplink_converter import BytesModbusUplinkConverter
from thingsboard_gateway.connectors.modbus.bytes_modbus_downlink_converter import BytesModbusDownlinkConverter
#      limitations under the License.

from threading import Thread
from queue import Queue
from random import choice
from string import ascii_lowercase
from time import sleep, time
from re import fullmatch
from json import JSONDecodeError

from thingsboard_gateway.tb_utility.tb_utility import TBUtility
try:
    from requests import Timeout, request
except ImportError:
    print("Requests library not found - installing...")
    TBUtility.install_package("requests")
    from requests import Timeout, request
import requests
from requests.auth import HTTPBasicAuth
from requests.exceptions import RequestException

from thingsboard_gateway.connectors.connector import Connector, log
from thingsboard_gateway.connectors.request.json_request_uplink_converter import JsonRequestUplinkConverter
from thingsboard_gateway.connectors.request.json_request_downlink_converter import JsonRequestDownlinkConverter

# pylint: disable=E1101
requests.packages.urllib3.util.ssl_.DEFAULT_CIPHERS += ':ADH-AES128-SHA256'


class RequestConnector(Connector, Thread):
    def __init__(self, gateway, config, connector_type):
示例#6
0
from threading import Thread
from string import ascii_lowercase
from random import choice
from time import time
from re import fullmatch
from queue import Queue
from simplejson import loads, JSONDecodeError

from thingsboard_gateway.tb_utility.tb_loader import TBModuleLoader
from thingsboard_gateway.tb_utility.tb_utility import TBUtility

try:
    from requests import Timeout, request as regular_request
except ImportError:
    print("Requests library not found - installing...")
    TBUtility.install_package("requests")
    from requests import Timeout, request as regular_request
import requests
from requests.auth import HTTPBasicAuth as HTTPBasicAuthRequest
from requests.exceptions import RequestException
requests.packages.urllib3.util.ssl_.DEFAULT_CIPHERS += ':ADH-AES128-SHA256'

try:
    from flask import Flask, jsonify, request
except ImportError:
    print("Flask library not found - installing...")
    TBUtility.install_package("flask")
    from flask import Flask, jsonify, request
try:
    from flask_restful import reqparse, abort, Api, Resource
except ImportError:
示例#7
0
from re import fullmatch
from string import ascii_lowercase
from threading import Thread
from time import time

import aiohttp
from simplejson import JSONDecodeError, loads

from thingsboard_gateway.tb_utility.tb_loader import TBModuleLoader
from thingsboard_gateway.tb_utility.tb_utility import TBUtility

try:
    from requests import Timeout, request as regular_request
except ImportError:
    print("Requests library not found - installing...")
    TBUtility.install_package("requests")
    from requests import Timeout, request as regular_request
import requests
from requests.auth import HTTPBasicAuth as HTTPBasicAuthRequest
from requests.exceptions import RequestException

requests.packages.urllib3.util.ssl_.DEFAULT_CIPHERS += ':ADH-AES128-SHA256'

try:
    from aiohttp import web
except ImportError:
    print('AIOHTTP library not found - installing...')
    TBUtility.install_package('aiohttp')
    from aiohttp import web

from thingsboard_gateway.connectors.connector import Connector, log
#     limitations under the License.

from queue import Queue
from random import choice
from string import ascii_lowercase
from threading import Thread
from time import sleep, time

from thingsboard_gateway.tb_utility.tb_loader import TBModuleLoader
from thingsboard_gateway.tb_utility.tb_utility import TBUtility

try:
    from bacpypes.core import run, stop
except ImportError:
    print("BACnet library not found - installing...")
    TBUtility.install_package("bacpypes", ">=0.18.0")
    from bacpypes.core import run, stop

from bacpypes.pdu import Address, GlobalBroadcast, LocalBroadcast, LocalStation, RemoteStation

from thingsboard_gateway.connectors.connector import Connector, log
from thingsboard_gateway.connectors.bacnet.bacnet_utilities.tb_gateway_bacnet_application import TBBACnetApplication


class BACnetConnector(Thread, Connector):
    def __init__(self, gateway, config, connector_type):
        self._connector_type = connector_type
        self.statistics = {'MessagesReceived': 0,
                           'MessagesSent': 0}
        super().__init__()
        self.__config = config
示例#9
0
import re
import sched
import time
from threading import Thread
from copy import copy
from random import choice
from string import ascii_lowercase

from thingsboard_gateway.tb_utility.tb_utility import TBUtility

try:
    from can import Notifier, BufferedReader, Message, CanError, ThreadSafeBus
except ImportError:
    print("CAN library not found - installing...")
    TBUtility.install_package("python-can")
    from can import Notifier, BufferedReader, Message, CanError, ThreadSafeBus

from thingsboard_gateway.connectors.can.bytes_can_downlink_converter import BytesCanDownlinkConverter
from thingsboard_gateway.connectors.can.bytes_can_uplink_converter import BytesCanUplinkConverter
from thingsboard_gateway.connectors.connector import Connector, log


class CanConnector(Connector, Thread):
    CMD_REGEX = r"^(\d{1,2}):(\d{1,2}):?(big|little)?:(\d+)$"
    VALUE_REGEX = r"^(\d{1,2}):(\d{1,2}):?(big|little)?:(bool|boolean|int|long|float|double|string):?([0-9A-Za-z-_]+)?$"

    NO_CMD_ID = "no_cmd"
    UNKNOWN_ARBITRATION_ID = -1

    DEFAULT_RECONNECT_PERIOD = 30.0
import logging.config
import logging.handlers
from queue import Queue
from random import choice
from string import ascii_lowercase
from threading import Thread, RLock

from yaml import safe_load
from simplejson import load, dumps, loads

from thingsboard_gateway.tb_utility.tb_utility import TBUtility

try:
    import pyrsistent
except ImportError:
    TBUtility.install_package("pyrsistent")

from thingsboard_gateway.gateway.tb_client import TBClient
from thingsboard_gateway.gateway.tb_updater import TBUpdater
from thingsboard_gateway.gateway.tb_logger import TBLoggerHandler
from thingsboard_gateway.storage.memory_event_storage import MemoryEventStorage
from thingsboard_gateway.storage.file_event_storage import FileEventStorage
from thingsboard_gateway.gateway.tb_gateway_remote_configurator import RemoteConfigurator

log = logging.getLogger('service')
main_handler = logging.handlers.MemoryHandler(-1)

DEFAULT_CONNECTORS = {
    "mqtt": "MqttConnector",
    "modbus": "ModbusConnector",
    "opcua": "OpcUaConnector",
ODBC_DRIVER_WITHOUT_STORED_PROCEDURE = "sqlite"
IS_ODBC_DRIVER_WITH_STORED_PROCEDURE_INSTALLED = False
IS_ODBC_DRIVER_INSTALLED = False

for driver_name in pyodbc.drivers():
    IS_ODBC_DRIVER_WITH_STORED_PROCEDURE_INSTALLED = IS_ODBC_DRIVER_WITH_STORED_PROCEDURE_INSTALLED or \
                                                     ODBC_DRIVER_WITH_STORED_PROCEDURE in driver_name.lower()
    IS_ODBC_DRIVER_INSTALLED = IS_ODBC_DRIVER_WITH_STORED_PROCEDURE_INSTALLED or \
                               ODBC_DRIVER_WITHOUT_STORED_PROCEDURE in driver_name.lower()

if IS_ODBC_DRIVER_WITH_STORED_PROCEDURE_INSTALLED:
    try:
        import testing.postgresql
    except ImportError:
        print("ODBC library not found - installing...")
        TBUtility.install_package("testing.postgresql")
        import testing.postgresql


@unittest.skipIf(not IS_ODBC_DRIVER_INSTALLED, "To run ODBC tests install " +
                 ODBC_DRIVER_WITH_STORED_PROCEDURE + "or" +
                 ODBC_DRIVER_WITHOUT_STORED_PROCEDURE + " ODBC driver")
class OdbcConnectorTests(unittest.TestCase):
    CONFIG_PATH = path.join(path.dirname(path.dirname(path.abspath(__file__))),
                            "data" + path.sep + "odbc" + path.sep)
    POSTGRES_PORT = 12345

    def setUp(self):
        self.gateway = Mock(spec=thingsboard_gateway.TBGatewayService)
        self.gateway.get_config_path.return_value = self.CONFIG_PATH
#     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.

import time
import threading
from random import choice
from string import ascii_lowercase

from thingsboard_gateway.tb_utility.tb_utility import TBUtility
# Try import Pymodbus library or install it and import
try:
    from pymodbus.constants import Defaults
except ImportError:
    print("Modbus library not found - installing...")
    TBUtility.install_package("pymodbus", ">=2.3.0")
    from pymodbus.constants import Defaults

from pymodbus.client.sync import ModbusTcpClient, ModbusUdpClient, ModbusSerialClient, ModbusRtuFramer, ModbusSocketFramer
from pymodbus.bit_write_message import WriteSingleCoilResponse, WriteMultipleCoilsResponse
from pymodbus.register_write_message import WriteMultipleRegistersResponse, \
                                            WriteSingleRegisterResponse
from pymodbus.register_read_message import ReadRegistersResponseBase
from pymodbus.bit_read_message import ReadBitsResponseBase
from pymodbus.exceptions import ConnectionException

from thingsboard_gateway.connectors.connector import Connector, log
from thingsboard_gateway.connectors.modbus.bytes_modbus_uplink_converter import BytesModbusUplinkConverter
from thingsboard_gateway.connectors.modbus.bytes_modbus_downlink_converter import BytesModbusDownlinkConverter

import datetime
from thingsboard_gateway.tb_utility.tb_utility import TBUtility

try:
    from cryptography.hazmat.backends import default_backend
    from cryptography.hazmat.primitives import serialization
    from cryptography.hazmat.primitives.asymmetric import rsa
    from cryptography import x509
    from cryptography.x509.oid import NameOID
    from cryptography.hazmat.primitives import hashes
except ImportError:
    print("Requests library not found - installing...")
    TBUtility.install_package("cryptography")
    from cryptography.hazmat.backends import default_backend
    from cryptography.hazmat.primitives import serialization
    from cryptography.hazmat.primitives.asymmetric import rsa
    from cryptography import x509
    from cryptography.x509.oid import NameOID
    from cryptography.hazmat.primitives import hashes


class SSLGenerator:
    def __init__(self, hostname):
        self.hostname: str = hostname

    def generate_certificate(self):
        key = rsa.generate_private_key(
            public_exponent=65537,
            key_size=2048,
            backend=default_backend(),
        )
示例#14
0
#      limitations under the License.

from threading import Thread
from time import sleep, time
from random import choice
from string import ascii_lowercase
from socket import gethostbyname
from re import search

from thingsboard_gateway.connectors.connector import Connector, log
from thingsboard_gateway.tb_utility.tb_utility import TBUtility

try:
    import puresnmp
except ImportError:
    TBUtility.install_package("puresnmp")
    import puresnmp

from puresnmp.exc import Timeout as SNMPTimeoutException


class SNMPConnector(Connector, Thread):
    def __init__(self, gateway, config, connector_type):
        super().__init__()
        self.daemon = True
        self.__gateway = gateway
        self._connected = False
        self.__stopped = False
        self._connector_type = connector_type
        self.__config = config
        self.__devices = self.__config["devices"]
示例#15
0
#from build.lib.thingsboard_gateway.connectors import connector
import logging
import time
import threading
from random import choice
from string import ascii_lowercase

import snap7

from thingsboard_gateway.tb_utility.tb_utility import TBUtility
# Try import Pymodbus library or install it and import
try:
    from snap7 import client
except ImportError:
    print("snap7 library not found - installing...")
    TBUtility.install_package("python-snap7")
    #TBUtility.install_package('pyserial')
    from snap7 import client

from snap7 import snap7types
from snap7.snap7types import S7AreaPA, S7AreaTM, S7CpuInfo, S7WLDWord
import struct
'''
#from pymodbus.client.sync import ModbusTcpClient, ModbusUdpClient, ModbusSerialClient, ModbusRtuFramer, ModbusSocketFramer
#from pymodbus.bit_write_message import WriteSingleCoilResponse, WriteMultipleCoilsResponse
#from pymodbus.register_write_message import WriteMultipleRegistersResponse, \
                                            WriteSingleRegisterResponse
from pymodbus.register_read_message import ReadRegistersResponseBase
from pymodbus.bit_read_message import ReadBitsResponseBase
from pymodbus.exceptions import ConnectionException
'''
#     limitations under the License.

import asyncio
from time import sleep
from random import choice
from string import ascii_lowercase
from threading import Thread
from queue import Queue

from thingsboard_gateway.tb_utility.tb_utility import TBUtility

try:
    from bleak import BleakScanner
except ImportError:
    print("BLE library not found - installing...")
    TBUtility.install_package("bleak")

from thingsboard_gateway.connectors.connector import Connector, log
from thingsboard_gateway.connectors.ble.device import Device


class BLEConnector(Connector, Thread):
    process_data = Queue(-1)

    def __init__(self, gateway, config, connector_type):
        self.statistics = {'MessagesReceived': 0, 'MessagesSent': 0}
        super().__init__()
        self._connector_type = connector_type
        self.__gateway = gateway
        self.__config = config