Skip to content

Commit

Permalink
Unyfiying code
Browse files Browse the repository at this point in the history
  • Loading branch information
akadlec committed Sep 14, 2021
1 parent 3341564 commit 74758c3
Show file tree
Hide file tree
Showing 28 changed files with 1,708 additions and 644 deletions.
30 changes: 30 additions & 0 deletions devices_module/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/python3

# Copyright 2021. FastyBird s.r.o.
#
# 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.

"""
Package exceptions classes
"""


class HandleExchangeDataException(Exception):
"""
Exception raised when received exchange message could not be handled
@package FastyBird:DevicesModule!
@module exceptions
@author Adam Kadlec <adam.kadlec@fastybird.com>
"""
144 changes: 130 additions & 14 deletions devices_module/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
# Library dependencies
import uuid
from abc import ABC
from typing import Dict, Set, Tuple
from typing import Dict, Set, Tuple, List
from modules_metadata.types import DataType


Expand All @@ -35,6 +35,7 @@ class PropertyItem(ABC):
@author Adam Kadlec <adam.kadlec@fastybird.com>
"""
__id: uuid.UUID
__name: str or None
__key: str
__identifier: str
__settable: bool
Expand All @@ -51,6 +52,7 @@ def __init__(
self,
property_id: uuid.UUID,
property_key: str,
property_name: str or None,
property_identifier: str,
property_settable: bool,
property_queryable: bool,
Expand All @@ -60,6 +62,7 @@ def __init__(
device_id: uuid.UUID,
) -> None:
self.__id = property_id
self.__name = property_name
self.__key = property_key
self.__identifier = property_identifier
self.__settable = property_settable
Expand All @@ -86,6 +89,13 @@ def property_id(self) -> uuid.UUID:

# -----------------------------------------------------------------------------

@property
def name(self) -> str or None:
"""Property name"""
return self.__name

# -----------------------------------------------------------------------------

@property
def key(self) -> str:
"""Property unique human readable key"""
Expand Down Expand Up @@ -173,6 +183,7 @@ def to_dict(self) -> Dict[str, str or int or bool or None]:

return {
"id": self.property_id.__str__(),
"name": self.name,
"key": self.key,
"identifier": self.identifier,
"settable": self.settable,
Expand Down Expand Up @@ -214,6 +225,7 @@ class ChannelPropertyItem(PropertyItem):
def __init__(
self,
property_id: uuid.UUID,
property_name: str or None,
property_key: str,
property_identifier: str,
property_settable: bool,
Expand All @@ -225,15 +237,16 @@ def __init__(
channel_id: uuid.UUID,
) -> None:
super().__init__(
property_id,
property_key,
property_identifier,
property_settable,
property_queryable,
property_data_type,
property_unit,
property_format,
device_id,
property_id=property_id,
property_name=property_name,
property_key=property_key,
property_identifier=property_identifier,
property_settable=property_settable,
property_queryable=property_queryable,
property_data_type=property_data_type,
property_unit=property_unit,
property_format=property_format,
device_id=device_id,
)

self.__channel_id = channel_id
Expand All @@ -253,7 +266,7 @@ def to_dict(self) -> Dict[str, str or int or bool or None]:
}, **super().to_dict()}


class ConnectorItem:
class ConnectorItem(ABC):
"""
Connector entity item
Expand All @@ -267,6 +280,7 @@ class ConnectorItem:
__name: str
__enabled: bool
__type: str
__control: List[str]
__params: dict

def __init__(
Expand All @@ -276,14 +290,16 @@ def __init__(
connector_key: str,
connector_enabled: bool,
connector_type: str,
connector_params: dict,
connector_control: List[str],
connector_params: dict or None,
) -> None:
self.__id = connector_id
self.__key = connector_key
self.__name = connector_name
self.__enabled = connector_enabled
self.__type = connector_type
self.__params = connector_params
self.__control = connector_control
self.__params = connector_params if connector_params is not None else {}

# -----------------------------------------------------------------------------

Expand Down Expand Up @@ -322,6 +338,13 @@ def type(self) -> str:

# -----------------------------------------------------------------------------

@property
def control(self) -> List[str]:
"""Connector plain control"""
return self.__control

# -----------------------------------------------------------------------------

@property
def params(self) -> dict:
"""Connector configuration params"""
Expand All @@ -337,5 +360,98 @@ def to_dict(self) -> Dict[str, str or int or bool or None]:
"name": self.name,
"enabled": self.enabled,
"type": self.type,
"params": self.params,
"control": self.control,
}


class FbBusConnectorItem(ConnectorItem):
"""
FastyBird BUS connector entity item
@package FastyBird:DevicesModule!
@module items
@author Adam Kadlec <adam.kadlec@fastybird.com>
"""
@property
def address(self) -> int or None:
"""Connector address"""
return int(self.params.get("address", None)) \
if self.params is not None and self.params.get("address") is not None else None

# -----------------------------------------------------------------------------

@property
def serial_interface(self) -> str or None:
"""Connector serial interface"""
return str(self.params.get("serial_interface", None)) \
if self.params is not None and self.params.get("serial_interface") is not None else None

# -----------------------------------------------------------------------------

@property
def baud_rate(self) -> int or None:
"""Connector communication baud rate"""
return int(self.params.get("baud_rate", None)) \
if self.params is not None and self.params.get("baud_rate") is not None else None

# -----------------------------------------------------------------------------

def to_dict(self) -> Dict[str, str or int or bool or None]:
"""Convert connector item to dictionary"""
return {**{
"address": self.address,
"serial_interface": self.serial_interface,
"baud_rate": self.baud_rate,
}, **super().to_dict()}


class FbMqttV1ConnectorItem(ConnectorItem):
"""
FastyBird MQTT v1 connector entity item
@package FastyBird:DevicesModule!
@module items
@author Adam Kadlec <adam.kadlec@fastybird.com>
"""
@property
def server(self) -> str or None:
"""Connector server address"""
return str(self.params.get("server", None)) \
if self.params is not None and self.params.get("server") is not None else None

# -----------------------------------------------------------------------------

@property
def port(self) -> int or None:
"""Connector server port"""
return int(self.params.get("port", None)) \
if self.params is not None and self.params.get("port") is not None else None

# -----------------------------------------------------------------------------

@property
def secured_port(self) -> int or None:
"""Connector server secured port"""
return int(self.params.get("secured_port", None)) \
if self.params is not None and self.params.get("secured_port") is not None else None

# -----------------------------------------------------------------------------

@property
def username(self) -> str or None:
"""Connector server username"""
return str(self.params.get("username", None)) \
if self.params is not None and self.params.get("username") is not None else None

# -----------------------------------------------------------------------------

def to_dict(self) -> Dict[str, str or int or bool or None]:
"""Convert connector item to dictionary"""
return {**{
"server": self.server,
"port": self.port,
"secured_port": self.secured_port,
"username": self.username,
}, **super().to_dict()}
Loading

0 comments on commit 74758c3

Please sign in to comment.