mobilus-client 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. mobilus_client-0.1.0/.gitignore +4 -0
  2. mobilus_client-0.1.0/LICENSE +21 -0
  3. mobilus_client-0.1.0/PKG-INFO +118 -0
  4. mobilus_client-0.1.0/README.md +75 -0
  5. mobilus_client-0.1.0/mobilus_client/__init__.py +0 -0
  6. mobilus_client-0.1.0/mobilus_client/__main__.py +60 -0
  7. mobilus_client-0.1.0/mobilus_client/app.py +63 -0
  8. mobilus_client-0.1.0/mobilus_client/config.py +20 -0
  9. mobilus_client-0.1.0/mobilus_client/messages/__init__.py +0 -0
  10. mobilus_client-0.1.0/mobilus_client/messages/encryptor.py +88 -0
  11. mobilus_client-0.1.0/mobilus_client/messages/factory.py +66 -0
  12. mobilus_client-0.1.0/mobilus_client/messages/serializer.py +20 -0
  13. mobilus_client-0.1.0/mobilus_client/messages/status.py +7 -0
  14. mobilus_client-0.1.0/mobilus_client/messages/validator.py +16 -0
  15. mobilus_client-0.1.0/mobilus_client/mqtt_client.py +87 -0
  16. mobilus_client-0.1.0/mobilus_client/proto/__init__.py +20 -0
  17. mobilus_client-0.1.0/mobilus_client/proto/call_events_request.proto +15 -0
  18. mobilus_client-0.1.0/mobilus_client/proto/call_events_request_pb2.py +38 -0
  19. mobilus_client-0.1.0/mobilus_client/proto/call_events_request_pb2.pyi +63 -0
  20. mobilus_client-0.1.0/mobilus_client/proto/current_state_request.proto +4 -0
  21. mobilus_client-0.1.0/mobilus_client/proto/current_state_request_pb2.py +36 -0
  22. mobilus_client-0.1.0/mobilus_client/proto/current_state_request_pb2.pyi +20 -0
  23. mobilus_client-0.1.0/mobilus_client/proto/current_state_response.proto +15 -0
  24. mobilus_client-0.1.0/mobilus_client/proto/current_state_response_pb2.py +38 -0
  25. mobilus_client-0.1.0/mobilus_client/proto/current_state_response_pb2.pyi +63 -0
  26. mobilus_client-0.1.0/mobilus_client/proto/devices_list_request.proto +4 -0
  27. mobilus_client-0.1.0/mobilus_client/proto/devices_list_request_pb2.py +36 -0
  28. mobilus_client-0.1.0/mobilus_client/proto/devices_list_request_pb2.pyi +20 -0
  29. mobilus_client-0.1.0/mobilus_client/proto/devices_list_response.proto +16 -0
  30. mobilus_client-0.1.0/mobilus_client/proto/devices_list_response_pb2.py +38 -0
  31. mobilus_client-0.1.0/mobilus_client/proto/devices_list_response_pb2.pyi +68 -0
  32. mobilus_client-0.1.0/mobilus_client/proto/login_request.proto +6 -0
  33. mobilus_client-0.1.0/mobilus_client/proto/login_request_pb2.py +36 -0
  34. mobilus_client-0.1.0/mobilus_client/proto/login_request_pb2.pyi +30 -0
  35. mobilus_client-0.1.0/mobilus_client/proto/login_response.proto +10 -0
  36. mobilus_client-0.1.0/mobilus_client/proto/login_response_pb2.py +36 -0
  37. mobilus_client-0.1.0/mobilus_client/proto/login_response_pb2.pyi +42 -0
  38. mobilus_client-0.1.0/mobilus_client/registries/key.py +33 -0
  39. mobilus_client-0.1.0/mobilus_client/registries/message.py +43 -0
  40. mobilus_client-0.1.0/mobilus_client/utils/encryption.py +21 -0
  41. mobilus_client-0.1.0/mobilus_client/utils/types.py +23 -0
  42. mobilus_client-0.1.0/pyproject.toml +59 -0
@@ -0,0 +1,4 @@
1
+ __pycache__
2
+ tmp
3
+ .coverage
4
+ .tool-versions
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Zbigniew Pieślak
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,118 @@
1
+ Metadata-Version: 2.3
2
+ Name: mobilus-client
3
+ Version: 0.1.0
4
+ Summary: Python client for Mobilus Cosmo GTW
5
+ Project-URL: homepage, https://github.com/zpieslak/mobilus-client
6
+ Project-URL: issues, https://github.com/zpieslak/mobilus-client/issues
7
+ Author-email: Zbigniew Pieślak <zpieslak@gmail.com>
8
+ License: MIT License
9
+
10
+ Copyright (c) 2024 Zbigniew Pieślak
11
+
12
+ Permission is hereby granted, free of charge, to any person obtaining a copy
13
+ of this software and associated documentation files (the "Software"), to deal
14
+ in the Software without restriction, including without limitation the rights
15
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
+ copies of the Software, and to permit persons to whom the Software is
17
+ furnished to do so, subject to the following conditions:
18
+
19
+ The above copyright notice and this permission notice shall be included in all
20
+ copies or substantial portions of the Software.
21
+
22
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
+ SOFTWARE.
29
+ License-File: LICENSE
30
+ Requires-Python: >=3.9
31
+ Requires-Dist: cryptography~=43.0
32
+ Requires-Dist: paho-mqtt~=1.6.0
33
+ Requires-Dist: protobuf==5.28.1
34
+ Provides-Extra: test
35
+ Requires-Dist: coverage~=7.0; extra == 'test'
36
+ Requires-Dist: factory-boy~=2.0; extra == 'test'
37
+ Requires-Dist: flake8~=7.0; extra == 'test'
38
+ Requires-Dist: mypy-protobuf~=3.0; extra == 'test'
39
+ Requires-Dist: mypy~=1.0; extra == 'test'
40
+ Requires-Dist: types-paho-mqtt~=1.0; extra == 'test'
41
+ Requires-Dist: types-protobuf~=5.28.0; extra == 'test'
42
+ Description-Content-Type: text/markdown
43
+
44
+ ## Mobilus Cosmo GTW Client
45
+
46
+ ![test workflow](https://github.com/zpieslak/mobilus-client/actions/workflows/test.yml/badge.svg)
47
+
48
+ This code provides a native Python client for the Mobilus Cosmo GTW. It connects directly to the gateway's MQTT broker and sends message commands to control the associated devices. The connection is established locally, so the client must be run on the same network as the gateway. Currently, the only tested and supported devices are radio shutters (Mobilus COSMO 2WAY).
49
+
50
+ In order to use the client, configuration and setup need to be done on the Mobilus Cosmo GTW side. This includes creating a user and pairing the devices with the gateway.
51
+
52
+ More information can be found on [blog post](https://codegyver.com/2024/09/22/mobilus-cosmo-gtw-reverse-engineering-a-radio-shutter-device/).
53
+
54
+ ## Installation
55
+
56
+ Install the module with pip:
57
+
58
+ pip install mobilus-client
59
+
60
+ ## Usage
61
+
62
+ For convenience, the client can be run as a module:
63
+
64
+ python -m mobilus_client --host=GATEWAY_HOST --login=USER_LOGIN --password=USER_PASSWORD COMMANDS
65
+
66
+ Where:
67
+
68
+ * `GATEWAY_HOST` is the IP address of the gateway.
69
+ * `USER_LOGIN` and `USER_PASSWORD` are the user credentials to access the gateway.
70
+ * `COMMANDS` is a list of commands to be sent to the gateway.
71
+
72
+ Currently, the following commands are supported:
73
+
74
+ * `current_state` - Get the current state of the devices. Please note that in the case of shutters, this doesn't return any meaningful data.
75
+
76
+ * `devices_list` - List all devices that are paired with the gateway.
77
+
78
+ * `call_events:device_id=DEVICE_ID,value=VALUE` - Send a command to the device. This can be used for actual control of the device (shutter).
79
+
80
+ * `DEVICE_ID` - The ID of the device as returned from devices_list.
81
+ * `VALUE` - The value to be sent to the device, currently one of "UP", "DOWN", "STOP", or a percentage value like "85%".
82
+
83
+ Multiple commands can be sent at once, separated by spaces:
84
+
85
+ python -m mobilus_client --host=192.168.2.1 --login=admin --password=my_password devices_list call_events:device_id=0,value=100% call_events:device_id=1,value=80%
86
+
87
+ Output is printed to the console in JSON format, and therefore the client command can be easily chained with `jq` for prettier output. For example:
88
+
89
+ python -m mobilus_client --host=192.168.2.1 --login=admin --password=my_password current_state | jq
90
+
91
+ Verbose mode can be enabled with the `--verbose` flag:
92
+
93
+ python -m mobilus_client --host=192.168.2.1 --login=admin --password=my_password --verbose current_state
94
+
95
+ ## Caveats
96
+
97
+ The client is tested with Mobilus Cosmo GTW with shutter devices only (COSMO 2WAY). It is possible that it will work with other devices, but it is not guaranteed. The purpose of the client is to provide a simple way to control the devices without the need to use the official Mobilus Cosmo GTW web interface or app, and to integrate it with open-source systems like Home Assistant. I am not affiliated with Mobilus, and the client is provided as is.
98
+
99
+ ## Development
100
+
101
+ Clone the repository and navigate to the project directory.
102
+ Install the package in editable mode with the following command:
103
+
104
+ pip install -e .[test]
105
+
106
+ Check mypy and flake8 with the following commands:
107
+
108
+ mypy .
109
+ flake8 .
110
+
111
+ The tests can be run with the following command:
112
+
113
+ python -m unittest -v
114
+
115
+ To generate a coverage report:
116
+
117
+ coverage run -m unittest discover
118
+ coverage report
@@ -0,0 +1,75 @@
1
+ ## Mobilus Cosmo GTW Client
2
+
3
+ ![test workflow](https://github.com/zpieslak/mobilus-client/actions/workflows/test.yml/badge.svg)
4
+
5
+ This code provides a native Python client for the Mobilus Cosmo GTW. It connects directly to the gateway's MQTT broker and sends message commands to control the associated devices. The connection is established locally, so the client must be run on the same network as the gateway. Currently, the only tested and supported devices are radio shutters (Mobilus COSMO 2WAY).
6
+
7
+ In order to use the client, configuration and setup need to be done on the Mobilus Cosmo GTW side. This includes creating a user and pairing the devices with the gateway.
8
+
9
+ More information can be found on [blog post](https://codegyver.com/2024/09/22/mobilus-cosmo-gtw-reverse-engineering-a-radio-shutter-device/).
10
+
11
+ ## Installation
12
+
13
+ Install the module with pip:
14
+
15
+ pip install mobilus-client
16
+
17
+ ## Usage
18
+
19
+ For convenience, the client can be run as a module:
20
+
21
+ python -m mobilus_client --host=GATEWAY_HOST --login=USER_LOGIN --password=USER_PASSWORD COMMANDS
22
+
23
+ Where:
24
+
25
+ * `GATEWAY_HOST` is the IP address of the gateway.
26
+ * `USER_LOGIN` and `USER_PASSWORD` are the user credentials to access the gateway.
27
+ * `COMMANDS` is a list of commands to be sent to the gateway.
28
+
29
+ Currently, the following commands are supported:
30
+
31
+ * `current_state` - Get the current state of the devices. Please note that in the case of shutters, this doesn't return any meaningful data.
32
+
33
+ * `devices_list` - List all devices that are paired with the gateway.
34
+
35
+ * `call_events:device_id=DEVICE_ID,value=VALUE` - Send a command to the device. This can be used for actual control of the device (shutter).
36
+
37
+ * `DEVICE_ID` - The ID of the device as returned from devices_list.
38
+ * `VALUE` - The value to be sent to the device, currently one of "UP", "DOWN", "STOP", or a percentage value like "85%".
39
+
40
+ Multiple commands can be sent at once, separated by spaces:
41
+
42
+ python -m mobilus_client --host=192.168.2.1 --login=admin --password=my_password devices_list call_events:device_id=0,value=100% call_events:device_id=1,value=80%
43
+
44
+ Output is printed to the console in JSON format, and therefore the client command can be easily chained with `jq` for prettier output. For example:
45
+
46
+ python -m mobilus_client --host=192.168.2.1 --login=admin --password=my_password current_state | jq
47
+
48
+ Verbose mode can be enabled with the `--verbose` flag:
49
+
50
+ python -m mobilus_client --host=192.168.2.1 --login=admin --password=my_password --verbose current_state
51
+
52
+ ## Caveats
53
+
54
+ The client is tested with Mobilus Cosmo GTW with shutter devices only (COSMO 2WAY). It is possible that it will work with other devices, but it is not guaranteed. The purpose of the client is to provide a simple way to control the devices without the need to use the official Mobilus Cosmo GTW web interface or app, and to integrate it with open-source systems like Home Assistant. I am not affiliated with Mobilus, and the client is provided as is.
55
+
56
+ ## Development
57
+
58
+ Clone the repository and navigate to the project directory.
59
+ Install the package in editable mode with the following command:
60
+
61
+ pip install -e .[test]
62
+
63
+ Check mypy and flake8 with the following commands:
64
+
65
+ mypy .
66
+ flake8 .
67
+
68
+ The tests can be run with the following command:
69
+
70
+ python -m unittest -v
71
+
72
+ To generate a coverage report:
73
+
74
+ coverage run -m unittest discover
75
+ coverage report
File without changes
@@ -0,0 +1,60 @@
1
+ import argparse
2
+ import logging
3
+ import sys
4
+ from typing import Tuple, Dict
5
+ from mobilus_client.config import Config
6
+ from mobilus_client.app import App
7
+
8
+ logger = logging.getLogger(__name__)
9
+
10
+
11
+ def main() -> None:
12
+ parser = argparse.ArgumentParser(
13
+ description="Run Mobilus Client with specific commands and parameters."
14
+ )
15
+ parser.add_argument('--host', required=True, help="Cosmo GTW address")
16
+ parser.add_argument('--login', required=True, help="User login")
17
+ parser.add_argument('--password', required=True, help="User password")
18
+ parser.add_argument('--verbose', action='store_true', help="Enable verbose logging")
19
+ parser.add_argument('commands', nargs='+', help="Commands to run")
20
+
21
+ # Parse arguments
22
+ args = parser.parse_args()
23
+
24
+ # Create a config object
25
+ config = Config(
26
+ gateway_host=args.host,
27
+ user_login=args.login,
28
+ user_password=args.password,
29
+ )
30
+
31
+ # Set up logging
32
+ logging.basicConfig(
33
+ stream=sys.stdout,
34
+ level=logging.DEBUG if args.verbose else logging.WARNING,
35
+ format='{"name": "[%(name)s]", "levelname": "%(levelname)s", "message": "%(message)s"}'
36
+ )
37
+
38
+ commands = []
39
+ for command_str in args.commands:
40
+ command, params = _parse_command(command_str)
41
+ commands.append((command, params))
42
+
43
+ app = App(config)
44
+ responses = app.call(commands)
45
+ print(responses)
46
+
47
+
48
+ def _parse_command(command_str: str) -> Tuple[str, Dict[str, str]]:
49
+ """
50
+ " Parse a command string into a command and a dictionary of parameters. "
51
+ " Example: 'command:param1=value1,param2=value2' "
52
+ " Returns: ('command', {'param1': 'value1', 'param2': 'value2'}) "
53
+ """
54
+ command, *params = command_str.split(':', 1)
55
+ params_dict = dict(param.split('=') for param in params[0].split(',')) if params else {}
56
+ return command, params_dict
57
+
58
+
59
+ if __name__ == '__main__':
60
+ main()
@@ -0,0 +1,63 @@
1
+ import logging
2
+ import socket
3
+ from typing import Dict, List, Tuple
4
+ from mobilus_client.config import Config
5
+ from mobilus_client.messages.serializer import MessageSerializer
6
+ from mobilus_client.mqtt_client import MqttClient
7
+ from mobilus_client.registries.message import MessageRegistry
8
+ from mobilus_client.registries.key import KeyRegistry
9
+
10
+ logger = logging.getLogger(__name__)
11
+
12
+
13
+ class App:
14
+ def __init__(self, config: Config) -> None:
15
+ self.config = config
16
+ self.message_registry = MessageRegistry()
17
+ self.key_registry = KeyRegistry(config.user_key)
18
+ self.client = MqttClient(
19
+ client_id=config.client_id,
20
+ transport=config.gateway_protocol,
21
+ userdata={
22
+ "config": config,
23
+ "key_registry": self.key_registry,
24
+ "message_registry": self.message_registry,
25
+ }
26
+ )
27
+
28
+ def call(self, commands: List[Tuple[str, Dict[str, str]]]) -> str:
29
+ if not commands:
30
+ return self._empty_response()
31
+
32
+ try:
33
+ # Connect to the MQTT broker and start the loop
34
+ self.client.connect(self.config.gateway_host, self.config.gateway_port, 60)
35
+ self.client.loop_start()
36
+
37
+ # Wait for the client to authenticate
38
+ self.client.authenticated_event.wait(timeout=self.config.auth_timeout_period)
39
+
40
+ if not self.client.authenticated_event.is_set():
41
+ logger.error("Failed to authenticate with the gateway host")
42
+ return self._empty_response()
43
+
44
+ # Execute the provided commands
45
+ for command, params in commands:
46
+ self.client.send_request(command, **params)
47
+
48
+ # Wait for the completion event to be triggered
49
+ self.client.completed_event.wait(timeout=self.config.timeout_period)
50
+ except socket.gaierror:
51
+ logger.error("Failed to connect to the gateway host")
52
+ except TimeoutError:
53
+ logger.error("Timeout occurred")
54
+ finally:
55
+ self.client.disconnect()
56
+
57
+ # Return serialized responses from the message registry
58
+ return MessageSerializer.serialize_list_to_json(
59
+ self.message_registry.get_responses()
60
+ )
61
+
62
+ def _empty_response(self) -> str:
63
+ return MessageSerializer.serialize_list_to_json([])
@@ -0,0 +1,20 @@
1
+ import random
2
+ from dataclasses import dataclass
3
+ from mobilus_client.utils.encryption import create_key
4
+
5
+
6
+ @dataclass
7
+ class Config:
8
+ gateway_host: str
9
+ user_login: str
10
+ user_password: str
11
+
12
+ auth_timeout_period: float = 30
13
+ client_id: str = ''.join(random.choice('0123456789ABCDEF') for _ in range(12))
14
+ gateway_port: int = 8884
15
+ gateway_protocol: str = "websockets"
16
+ timeout_period: float = 30
17
+ user_key: bytes = b''
18
+
19
+ def __post_init__(self) -> None:
20
+ self.user_key = create_key(self.user_password)
@@ -0,0 +1,88 @@
1
+ import struct
2
+ import time
3
+ from google.protobuf.message import DecodeError
4
+ from typing import cast, Optional
5
+ from mobilus_client.registries.key import KeyRegistry
6
+ from mobilus_client.proto import (
7
+ CallEventsRequest,
8
+ CurrentStateRequest,
9
+ CurrentStateResponse,
10
+ DevicesListRequest,
11
+ DevicesListResponse,
12
+ LoginRequest,
13
+ LoginResponse
14
+ )
15
+ from mobilus_client.utils.encryption import create_iv, decrypt_body, encrypt_body
16
+ from mobilus_client.utils.types import MessageRequest, MessageResponse
17
+
18
+
19
+ class MessageEncryptor:
20
+ CATEGORY_MAP = {
21
+ 1: LoginRequest,
22
+ 2: LoginResponse,
23
+ 3: DevicesListRequest,
24
+ 4: DevicesListResponse,
25
+ 13: CallEventsRequest,
26
+ 26: CurrentStateRequest,
27
+ 27: CurrentStateResponse,
28
+ }
29
+ CLASS_TO_CATEGORY_MAP = {v: k for k, v in CATEGORY_MAP.items()}
30
+
31
+ @staticmethod
32
+ def encrypt(message: MessageRequest, client_id: str, key_registry: KeyRegistry) -> bytes:
33
+ category = MessageEncryptor.CLASS_TO_CATEGORY_MAP.get(type(message))
34
+ body = message.SerializeToString()
35
+ timestamp = int(time.time())
36
+ length = 13
37
+
38
+ private_key = key_registry.get_encryption_key(type(message))
39
+
40
+ if private_key is None:
41
+ encrypted_body = body
42
+ else:
43
+ iv = create_iv(timestamp)
44
+ encrypted_body = encrypt_body(private_key, iv, body)
45
+
46
+ return (
47
+ struct.pack('>IBI', length, category, timestamp) +
48
+ bytes.fromhex(client_id) +
49
+ struct.pack('>2B', 4, 0) +
50
+ encrypted_body
51
+ )
52
+
53
+ @staticmethod
54
+ def decrypt(encrypted_message: bytes, key_registry: KeyRegistry) -> Optional[MessageResponse]:
55
+ # Define header format and calculate size
56
+ header_format = '>IBI6sBB'
57
+ header_size = struct.calcsize(header_format)
58
+
59
+ # Do early return if message is too short
60
+ if len(encrypted_message) < header_size:
61
+ return None
62
+
63
+ # Unpack header
64
+ length, category, timestamp, user_id, platform, response_code = struct.unpack(
65
+ header_format, encrypted_message[:header_size])
66
+
67
+ # Extract encrypted body
68
+ encrypted_body = encrypted_message[header_size:]
69
+
70
+ # Choose proper klass
71
+ message_klass = MessageEncryptor.CATEGORY_MAP.get(category)
72
+ if message_klass is None:
73
+ return None
74
+
75
+ # Choose proper decryption key
76
+ key = key_registry.get_decryption_key(message_klass)
77
+
78
+ # Decrypt body
79
+ iv = create_iv(timestamp)
80
+ body = decrypt_body(key, iv, encrypted_body)
81
+
82
+ try:
83
+ message = cast(MessageResponse, message_klass())
84
+ message.ParseFromString(body)
85
+ except DecodeError:
86
+ return None
87
+
88
+ return message
@@ -0,0 +1,66 @@
1
+ from abc import ABC, abstractmethod
2
+ from typing import Any, Optional
3
+ from mobilus_client.proto import (
4
+ CallEvent,
5
+ CallEventsRequest,
6
+ CurrentStateRequest,
7
+ DevicesListRequest,
8
+ LoginRequest,
9
+ )
10
+ from mobilus_client.utils.types import MessageRequest
11
+
12
+
13
+ class MessageBuilder(ABC):
14
+ @abstractmethod
15
+ def build(self, **kwargs: Any) -> Optional[MessageRequest]:
16
+ pass
17
+
18
+
19
+ class LoginMessageBuilder(MessageBuilder):
20
+ def build(self, **kwargs: Any) -> Optional[LoginRequest]:
21
+ if 'login' not in kwargs or 'password' not in kwargs:
22
+ return None
23
+
24
+ request = LoginRequest()
25
+ request.login = str(kwargs['login'])
26
+ request.password = bytes(kwargs['password'])
27
+ return request
28
+
29
+
30
+ class DevicesListMessageBuilder(MessageBuilder):
31
+ def build(self, **kwargs: Any) -> DevicesListRequest:
32
+ return DevicesListRequest()
33
+
34
+
35
+ class CurrentStateMessageBuilder(MessageBuilder):
36
+ def build(self, **kwargs: Any) -> CurrentStateRequest:
37
+ return CurrentStateRequest()
38
+
39
+
40
+ class CallEventsMessageBuilder(MessageBuilder):
41
+ def build(self, **kwargs: Any) -> Optional[CallEventsRequest]:
42
+ if 'device_id' not in kwargs or 'value' not in kwargs:
43
+ return None
44
+
45
+ event = CallEvent()
46
+ event.event_number = int(kwargs.get('event_number', 6))
47
+ event.device_id = int(kwargs['device_id'])
48
+ event.value = str(kwargs['value'])
49
+
50
+ request = CallEventsRequest()
51
+ request.events.append(event)
52
+ return request
53
+
54
+
55
+ class MessageFactory:
56
+ _builders = {
57
+ "call_events": CallEventsMessageBuilder(),
58
+ "current_state": CurrentStateMessageBuilder(),
59
+ "devices_list": DevicesListMessageBuilder(),
60
+ "login": LoginMessageBuilder(),
61
+ }
62
+
63
+ @staticmethod
64
+ def create_message(message_name: str, **kwargs: Any) -> Optional[MessageRequest]:
65
+ builder = MessageFactory._builders.get(message_name)
66
+ return builder.build(**kwargs) if builder else None
@@ -0,0 +1,20 @@
1
+ import json
2
+ from google.protobuf.json_format import MessageToJson
3
+ from mobilus_client.utils.types import MessageResponse
4
+
5
+
6
+ class MessageSerializer:
7
+ @staticmethod
8
+ def serialize_to_json(message: MessageResponse) -> str:
9
+ return MessageToJson(message)
10
+
11
+ @staticmethod
12
+ def serialize_list_to_json(messages: list[MessageResponse]) -> str:
13
+ return json.dumps(
14
+ [
15
+ json.loads(
16
+ MessageSerializer.serialize_to_json(message)
17
+ )
18
+ for message in messages
19
+ ]
20
+ )
@@ -0,0 +1,7 @@
1
+ from enum import Enum
2
+
3
+
4
+ class MessageStatus(Enum):
5
+ SUCCESS = 0
6
+ AUTHENTICATION_ERROR = 1
7
+ UNKNOWN_MESSAGE = 2
@@ -0,0 +1,16 @@
1
+ from typing import Optional, Union
2
+ from mobilus_client.messages.status import MessageStatus
3
+ from mobilus_client.proto import LoginResponse
4
+ from mobilus_client.utils.types import MessageRequest, MessageResponse
5
+
6
+
7
+ class MessageValidator():
8
+ @staticmethod
9
+ def validate(message: Optional[Union[MessageRequest, MessageResponse]]) -> MessageStatus:
10
+ if message is None:
11
+ return MessageStatus.UNKNOWN_MESSAGE
12
+
13
+ if isinstance(message, LoginResponse) and message.login_status == 1:
14
+ return MessageStatus.AUTHENTICATION_ERROR
15
+
16
+ return MessageStatus.SUCCESS
@@ -0,0 +1,87 @@
1
+ import logging
2
+ import paho.mqtt.client as mqtt
3
+ import threading
4
+ from typing import Any, Dict, cast
5
+ from mobilus_client.messages.encryptor import MessageEncryptor
6
+ from mobilus_client.messages.factory import MessageFactory
7
+ from mobilus_client.messages.status import MessageStatus
8
+ from mobilus_client.messages.validator import MessageValidator
9
+ from mobilus_client.proto import (LoginRequest, LoginResponse)
10
+ from mobilus_client.utils.types import MessageRequest
11
+
12
+
13
+ logger = logging.getLogger(__name__)
14
+
15
+
16
+ class MqttClient(mqtt.Client):
17
+ _userdata: Dict[str, Any]
18
+
19
+ def __init__(self, *args: Any, **kwargs: Any) -> None:
20
+ super().__init__(*args, **kwargs)
21
+ self.authenticated_event = threading.Event()
22
+ self.completed_event = threading.Event()
23
+ self.enable_logger(logger)
24
+
25
+ def send_request(self, command: str, **params: Any) -> None:
26
+ if not self.is_connected():
27
+ logger.error(f"Sending request - {command} failed. Client is not connected.")
28
+ return
29
+
30
+ message = MessageFactory.create_message(command, **params)
31
+ status = MessageValidator.validate(message)
32
+
33
+ if status != MessageStatus.SUCCESS:
34
+ logger.error(f"Command - {command} returned an error - {status.name}")
35
+ self.disconnect()
36
+ return
37
+
38
+ if not isinstance(message, LoginRequest):
39
+ self._userdata['message_registry'].register_request(message)
40
+
41
+ encrypted_message = MessageEncryptor.encrypt(
42
+ cast(MessageRequest, message),
43
+ self._userdata["config"].client_id,
44
+ self._userdata['key_registry']
45
+ )
46
+
47
+ self.publish("module", encrypted_message)
48
+
49
+ def on_disconnect(self, client: mqtt.Client, userdata: Any, reason_code: Any) -> None: # type: ignore
50
+ logger.info(f"Disconnected with result code - {reason_code}")
51
+
52
+ def on_connect(self, client: mqtt.Client, userdata: Any, flags: Any, reason_code: Any) -> None: # type: ignore
53
+ client.subscribe([
54
+ (userdata["config"].client_id, 0),
55
+ ("clients", 0)
56
+ ])
57
+
58
+ def on_subscribe(self, client: mqtt.Client, userdata: Any, mid: Any, granted_qos: Any) -> None: # type: ignore
59
+ self.send_request(
60
+ "login",
61
+ login=userdata["config"].user_login,
62
+ password=userdata["config"].user_key
63
+ )
64
+
65
+ def on_message(self, client: mqtt.Client, userdata: Any, message: Any) -> None: # type: ignore
66
+ logger.info(f"Received message on topic - {message.topic}")
67
+
68
+ message = MessageEncryptor.decrypt(message.payload, userdata["key_registry"])
69
+ logger.info(f"Decrypted message - {type(message).__name__}")
70
+
71
+ status = MessageValidator.validate(message)
72
+
73
+ if status != MessageStatus.SUCCESS:
74
+ logger.error(f"Message - {type(message).__name__} returned an error - {status.name}")
75
+ self.disconnect()
76
+ return
77
+
78
+ logger.info(f"Message - {type(message).__name__} validated successfully")
79
+
80
+ if isinstance(message, LoginResponse):
81
+ userdata["key_registry"].register_keys(message)
82
+ self.authenticated_event.set()
83
+ else:
84
+ userdata["message_registry"].register_response(message)
85
+
86
+ if userdata["message_registry"].all_responses_received():
87
+ self.completed_event.set()