conson-xp 1.33.0__py3-none-any.whl → 1.35.0__py3-none-any.whl

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 (32) hide show
  1. {conson_xp-1.33.0.dist-info → conson_xp-1.35.0.dist-info}/METADATA +1 -1
  2. {conson_xp-1.33.0.dist-info → conson_xp-1.35.0.dist-info}/RECORD +32 -32
  3. xp/__init__.py +1 -1
  4. xp/cli/commands/conbus/conbus_actiontable_commands.py +34 -35
  5. xp/cli/commands/conbus/conbus_autoreport_commands.py +11 -10
  6. xp/cli/commands/conbus/conbus_blink_commands.py +20 -6
  7. xp/cli/commands/conbus/conbus_custom_commands.py +6 -4
  8. xp/cli/commands/conbus/conbus_datapoint_commands.py +8 -6
  9. xp/cli/commands/conbus/conbus_lightlevel_commands.py +19 -16
  10. xp/cli/commands/conbus/conbus_linknumber_commands.py +7 -6
  11. xp/cli/commands/conbus/conbus_modulenumber_commands.py +7 -6
  12. xp/cli/commands/conbus/conbus_msactiontable_commands.py +7 -9
  13. xp/cli/commands/conbus/conbus_output_commands.py +9 -7
  14. xp/cli/commands/conbus/conbus_raw_commands.py +7 -2
  15. xp/cli/commands/conbus/conbus_scan_commands.py +4 -2
  16. xp/services/conbus/actiontable/actiontable_download_service.py +79 -37
  17. xp/services/conbus/actiontable/actiontable_list_service.py +17 -17
  18. xp/services/conbus/actiontable/actiontable_upload_service.py +78 -36
  19. xp/services/conbus/actiontable/msactiontable_service.py +88 -48
  20. xp/services/conbus/conbus_blink_all_service.py +89 -35
  21. xp/services/conbus/conbus_blink_service.py +82 -24
  22. xp/services/conbus/conbus_custom_service.py +81 -26
  23. xp/services/conbus/conbus_datapoint_queryall_service.py +90 -43
  24. xp/services/conbus/conbus_datapoint_service.py +76 -28
  25. xp/services/conbus/conbus_output_service.py +82 -22
  26. xp/services/conbus/conbus_raw_service.py +78 -37
  27. xp/services/conbus/conbus_scan_service.py +86 -42
  28. xp/services/conbus/write_config_service.py +76 -26
  29. xp/utils/dependencies.py +12 -24
  30. {conson_xp-1.33.0.dist-info → conson_xp-1.35.0.dist-info}/WHEEL +0 -0
  31. {conson_xp-1.33.0.dist-info → conson_xp-1.35.0.dist-info}/entry_points.txt +0 -0
  32. {conson_xp-1.33.0.dist-info → conson_xp-1.35.0.dist-info}/licenses/LICENSE +0 -0
@@ -5,47 +5,49 @@ This service handles custom telegram operations for modules through Conbus teleg
5
5
 
6
6
  import logging
7
7
  from datetime import datetime
8
- from typing import Callable, Optional
8
+ from typing import Any, Optional
9
9
 
10
- from twisted.internet.posixbase import PosixReactorBase
10
+ from psygnal import Signal
11
11
 
12
- from xp.models import ConbusClientConfig
13
12
  from xp.models.conbus.conbus_custom import ConbusCustomResponse
14
13
  from xp.models.protocol.conbus_protocol import TelegramReceivedEvent
15
14
  from xp.models.telegram.reply_telegram import ReplyTelegram
16
15
  from xp.models.telegram.system_function import SystemFunction
17
16
  from xp.models.telegram.telegram_type import TelegramType
18
- from xp.services.protocol import ConbusProtocol
17
+ from xp.services.protocol.conbus_event_protocol import ConbusEventProtocol
19
18
  from xp.services.telegram.telegram_service import TelegramService
20
19
 
21
20
 
22
- class ConbusCustomService(ConbusProtocol):
23
- """
24
- Service for sending custom telegrams to Conbus modules.
21
+ class ConbusCustomService:
22
+ """Service for sending custom telegrams to Conbus modules.
25
23
 
26
- Uses ConbusProtocol to provide custom telegram functionality
24
+ Uses ConbusEventProtocol to provide custom telegram functionality
27
25
  for sending arbitrary function codes and data to modules.
26
+
27
+ Attributes:
28
+ conbus_protocol: Protocol instance for Conbus communication.
29
+ telegram_service: Service for parsing telegrams.
30
+ on_finish: Signal emitted when custom operation completes (with response).
28
31
  """
29
32
 
33
+ on_finish: Signal = Signal(ConbusCustomResponse)
34
+
30
35
  def __init__(
31
36
  self,
37
+ conbus_protocol: ConbusEventProtocol,
32
38
  telegram_service: TelegramService,
33
- cli_config: ConbusClientConfig,
34
- reactor: PosixReactorBase,
35
39
  ) -> None:
36
40
  """Initialize the Conbus custom service.
37
41
 
38
42
  Args:
43
+ conbus_protocol: Protocol instance for Conbus communication.
39
44
  telegram_service: Service for parsing telegrams.
40
- cli_config: Configuration for Conbus client connection.
41
- reactor: Twisted reactor for event loop.
42
45
  """
43
- super().__init__(cli_config, reactor)
46
+ self.conbus_protocol = conbus_protocol
44
47
  self.telegram_service = telegram_service
45
48
  self.serial_number: str = ""
46
49
  self.function_code: str = ""
47
50
  self.data: str = ""
48
- self.finish_callback: Optional[Callable[[ConbusCustomResponse], None]] = None
49
51
  self.service_response: ConbusCustomResponse = ConbusCustomResponse(
50
52
  success=False,
51
53
  serial_number=self.serial_number,
@@ -54,7 +56,14 @@ class ConbusCustomService(ConbusProtocol):
54
56
  # Set up logging
55
57
  self.logger = logging.getLogger(__name__)
56
58
 
57
- def connection_established(self) -> None:
59
+ # Connect protocol signals
60
+ self.conbus_protocol.on_connection_made.connect(self.connection_made)
61
+ self.conbus_protocol.on_telegram_sent.connect(self.telegram_sent)
62
+ self.conbus_protocol.on_telegram_received.connect(self.telegram_received)
63
+ self.conbus_protocol.on_timeout.connect(self.timeout)
64
+ self.conbus_protocol.on_failed.connect(self.failed)
65
+
66
+ def connection_made(self) -> None:
58
67
  """Handle connection established event."""
59
68
  self.logger.debug(
60
69
  f"Connection established, sending custom telegram F{self.function_code}D{self.data}."
@@ -65,7 +74,7 @@ class ConbusCustomService(ConbusProtocol):
65
74
  self.failed(f"Invalid function code {self.function_code}")
66
75
  return
67
76
 
68
- self.send_telegram(
77
+ self.conbus_protocol.send_telegram(
69
78
  serial_number=self.serial_number,
70
79
  telegram_type=TelegramType.SYSTEM,
71
80
  system_function=system_function,
@@ -113,8 +122,13 @@ class ConbusCustomService(ConbusProtocol):
113
122
  self.service_response.data = self.data
114
123
  self.service_response.reply_telegram = reply_telegram
115
124
 
116
- if self.finish_callback:
117
- self.finish_callback(self.service_response)
125
+ # Emit finish signal
126
+ self.on_finish.emit(self.service_response)
127
+
128
+ def timeout(self) -> None:
129
+ """Handle timeout event."""
130
+ self.logger.debug("Timeout occurred")
131
+ self.failed("Timeout")
118
132
 
119
133
  def failed(self, message: str) -> None:
120
134
  """Handle failed connection event.
@@ -126,15 +140,15 @@ class ConbusCustomService(ConbusProtocol):
126
140
  self.service_response.success = False
127
141
  self.service_response.timestamp = datetime.now()
128
142
  self.service_response.error = message
129
- if self.finish_callback:
130
- self.finish_callback(self.service_response)
143
+
144
+ # Emit finish signal
145
+ self.on_finish.emit(self.service_response)
131
146
 
132
147
  def send_custom_telegram(
133
148
  self,
134
149
  serial_number: str,
135
150
  function_code: str,
136
151
  data: str,
137
- finish_callback: Callable[[ConbusCustomResponse], None],
138
152
  timeout_seconds: Optional[float] = None,
139
153
  ) -> None:
140
154
  """Send a custom telegram to a module.
@@ -143,14 +157,55 @@ class ConbusCustomService(ConbusProtocol):
143
157
  serial_number: 10-digit module serial number.
144
158
  function_code: Function code (e.g., "02", "17").
145
159
  data: Data code (e.g., "E2", "AA").
146
- finish_callback: Callback function to call when the reply is received.
147
160
  timeout_seconds: Timeout in seconds.
148
161
  """
149
162
  self.logger.info("Starting send_custom_telegram")
150
- if timeout_seconds:
151
- self.timeout_seconds = timeout_seconds
152
- self.finish_callback = finish_callback
153
163
  self.serial_number = serial_number
154
164
  self.function_code = function_code
155
165
  self.data = data
156
- self.start_reactor()
166
+ if timeout_seconds:
167
+ self.set_timeout(timeout_seconds)
168
+
169
+ def set_timeout(self, timeout_seconds: float) -> None:
170
+ """Set operation timeout.
171
+
172
+ Args:
173
+ timeout_seconds: Timeout in seconds.
174
+ """
175
+ self.conbus_protocol.timeout_seconds = timeout_seconds
176
+
177
+ def start_reactor(self) -> None:
178
+ """Start the reactor."""
179
+ self.conbus_protocol.start_reactor()
180
+
181
+ def stop_reactor(self) -> None:
182
+ """Stop the reactor."""
183
+ self.conbus_protocol.stop_reactor()
184
+
185
+ def __enter__(self) -> "ConbusCustomService":
186
+ """Enter context manager - reset state for singleton reuse.
187
+
188
+ Returns:
189
+ Self for context manager protocol.
190
+ """
191
+ # Reset state for singleton reuse
192
+ self.service_response = ConbusCustomResponse(success=False)
193
+ self.serial_number = ""
194
+ self.function_code = ""
195
+ self.data = ""
196
+ return self
197
+
198
+ def __exit__(
199
+ self, _exc_type: Optional[type], _exc_val: Optional[Exception], _exc_tb: Any
200
+ ) -> None:
201
+ """Exit context manager and disconnect signals."""
202
+ # Disconnect protocol signals
203
+ self.conbus_protocol.on_connection_made.disconnect(self.connection_made)
204
+ self.conbus_protocol.on_telegram_sent.disconnect(self.telegram_sent)
205
+ self.conbus_protocol.on_telegram_received.disconnect(self.telegram_received)
206
+ self.conbus_protocol.on_timeout.disconnect(self.timeout)
207
+ self.conbus_protocol.on_failed.disconnect(self.failed)
208
+ # Disconnect service signals
209
+ self.on_finish.disconnect()
210
+ # Stop reactor
211
+ self.stop_reactor()
@@ -5,46 +5,58 @@ This module provides service for querying all datapoint types from a module.
5
5
 
6
6
  import logging
7
7
  from datetime import datetime
8
- from typing import Callable, Optional
8
+ from typing import Any, Optional
9
9
 
10
- from twisted.internet.posixbase import PosixReactorBase
10
+ from psygnal import Signal
11
11
 
12
- from xp.models import ConbusClientConfig, ConbusDatapointResponse
12
+ from xp.models import ConbusDatapointResponse
13
13
  from xp.models.protocol.conbus_protocol import TelegramReceivedEvent
14
14
  from xp.models.telegram.datapoint_type import DataPointType
15
15
  from xp.models.telegram.reply_telegram import ReplyTelegram
16
16
  from xp.models.telegram.system_function import SystemFunction
17
17
  from xp.models.telegram.telegram_type import TelegramType
18
18
  from xp.services import TelegramService
19
- from xp.services.protocol import ConbusProtocol
19
+ from xp.services.protocol.conbus_event_protocol import ConbusEventProtocol
20
20
 
21
21
 
22
- class ConbusDatapointQueryAllService(ConbusProtocol):
23
- """
24
- Utility service for querying all datapoints from a module.
22
+ class ConbusDatapointQueryAllService:
23
+ """Utility service for querying all datapoints from a module.
25
24
 
26
25
  This service orchestrates multiple ConbusDatapointService calls to query
27
26
  all available datapoint types sequentially.
27
+
28
+ Attributes:
29
+ conbus_protocol: ConbusEventProtocol for protocol communication.
30
+ telegram_service: TelegramService for dependency injection.
31
+ on_progress: Signal emitted for each datapoint response received.
32
+ on_finish: Signal emitted when all datapoints queried (with response).
28
33
  """
29
34
 
35
+ on_progress: Signal = Signal(ReplyTelegram)
36
+ on_finish: Signal = Signal(ConbusDatapointResponse)
37
+
30
38
  def __init__(
31
39
  self,
40
+ conbus_protocol: ConbusEventProtocol,
32
41
  telegram_service: TelegramService,
33
- cli_config: ConbusClientConfig,
34
- reactor: PosixReactorBase,
35
42
  ) -> None:
36
43
  """Initialize the query all service.
37
44
 
38
45
  Args:
46
+ conbus_protocol: ConbusEventProtocol for protocol communication.
39
47
  telegram_service: TelegramService for dependency injection.
40
- cli_config: ConbusClientConfig for connection settings.
41
- reactor: PosixReactorBase for async operations.
42
48
  """
43
- super().__init__(cli_config, reactor)
49
+ self.conbus_protocol = conbus_protocol
44
50
  self.telegram_service = telegram_service
51
+
52
+ # Connect protocol signals
53
+ self.conbus_protocol.on_connection_made.connect(self.connection_made)
54
+ self.conbus_protocol.on_telegram_sent.connect(self.telegram_sent)
55
+ self.conbus_protocol.on_telegram_received.connect(self.telegram_received)
56
+ self.conbus_protocol.on_timeout.connect(self.timeout)
57
+ self.conbus_protocol.on_failed.connect(self.failed)
58
+
45
59
  self.serial_number: str = ""
46
- self.finish_callback: Optional[Callable[[ConbusDatapointResponse], None]] = None
47
- self.progress_callback: Optional[Callable[[ReplyTelegram], None]] = None
48
60
  self.service_response: ConbusDatapointResponse = ConbusDatapointResponse(
49
61
  success=False,
50
62
  serial_number=self.serial_number,
@@ -55,7 +67,7 @@ class ConbusDatapointQueryAllService(ConbusProtocol):
55
67
  # Set up logging
56
68
  self.logger = logging.getLogger(__name__)
57
69
 
58
- def connection_established(self) -> None:
70
+ def connection_made(self) -> None:
59
71
  """Handle connection established event."""
60
72
  self.logger.debug("Connection established, querying datapoints.")
61
73
  self.next_datapoint()
@@ -75,7 +87,7 @@ class ConbusDatapointQueryAllService(ConbusProtocol):
75
87
  datapoint_type = DataPointType(datapoint_type_code)
76
88
 
77
89
  self.logger.debug(f"Datapoint: {datapoint_type}")
78
- self.send_telegram(
90
+ self.conbus_protocol.send_telegram(
79
91
  telegram_type=TelegramType.SYSTEM,
80
92
  serial_number=self.serial_number,
81
93
  system_function=SystemFunction.READ_DATAPOINT,
@@ -84,24 +96,19 @@ class ConbusDatapointQueryAllService(ConbusProtocol):
84
96
  self.current_index += 1
85
97
  return True
86
98
 
87
- def timeout(self) -> bool:
88
- """Handle timeout event by querying next datapoint.
89
-
90
- Returns:
91
- True to continue, False to stop the reactor.
92
- """
99
+ def timeout(self) -> None:
100
+ """Handle timeout event by querying next datapoint."""
93
101
  self.logger.debug("Timeout, querying next datapoint")
94
102
  query_next_datapoint = self.next_datapoint()
95
103
  if not query_next_datapoint:
96
- if self.finish_callback:
97
- self.logger.debug("Received all datapoints telegram")
98
- self.service_response.success = True
99
- self.service_response.timestamp = datetime.now()
100
- self.service_response.serial_number = self.serial_number
101
- self.service_response.system_function = SystemFunction.READ_DATAPOINT
102
- self.finish_callback(self.service_response)
103
- return False
104
- return True
104
+ self.logger.debug("Received all datapoints telegram")
105
+ self.service_response.success = True
106
+ self.service_response.timestamp = datetime.now()
107
+ self.service_response.serial_number = self.serial_number
108
+ self.service_response.system_function = SystemFunction.READ_DATAPOINT
109
+
110
+ # Emit finish signal
111
+ self.on_finish.emit(self.service_response)
105
112
 
106
113
  def telegram_sent(self, telegram_sent: str) -> None:
107
114
  """Handle telegram sent event.
@@ -142,8 +149,7 @@ class ConbusDatapointQueryAllService(ConbusProtocol):
142
149
  return
143
150
 
144
151
  self.logger.debug("Received a datapoint telegram")
145
- if self.progress_callback:
146
- self.progress_callback(datapoint_telegram)
152
+ self.on_progress.emit(datapoint_telegram)
147
153
 
148
154
  def failed(self, message: str) -> None:
149
155
  """Handle failed connection event.
@@ -155,28 +161,69 @@ class ConbusDatapointQueryAllService(ConbusProtocol):
155
161
  self.service_response.success = False
156
162
  self.service_response.timestamp = datetime.now()
157
163
  self.service_response.error = message
158
- if self.finish_callback:
159
- self.finish_callback(self.service_response)
164
+
165
+ # Emit finish signal
166
+ self.on_finish.emit(self.service_response)
160
167
 
161
168
  def query_all_datapoints(
162
169
  self,
163
170
  serial_number: str,
164
- finish_callback: Callable[[ConbusDatapointResponse], None],
165
- progress_callback: Callable[[ReplyTelegram], None],
166
171
  timeout_seconds: Optional[float] = None,
167
172
  ) -> None:
168
173
  """Query all datapoints from a module.
169
174
 
170
175
  Args:
171
176
  serial_number: 10-digit module serial number.
172
- finish_callback: Callback function to call when all datapoints are received.
173
- progress_callback: Callback function to call when each datapoint is received.
174
177
  timeout_seconds: Timeout in seconds.
175
178
  """
176
179
  self.logger.info("Starting query_all_datapoints")
177
180
  if timeout_seconds:
178
- self.timeout_seconds = timeout_seconds
179
- self.finish_callback = finish_callback
180
- self.progress_callback = progress_callback
181
+ self.conbus_protocol.timeout_seconds = timeout_seconds
181
182
  self.serial_number = serial_number
182
- self.start_reactor()
183
+
184
+ def set_timeout(self, timeout_seconds: float) -> None:
185
+ """Set operation timeout.
186
+
187
+ Args:
188
+ timeout_seconds: Timeout in seconds.
189
+ """
190
+ self.conbus_protocol.timeout_seconds = timeout_seconds
191
+
192
+ def start_reactor(self) -> None:
193
+ """Start the reactor."""
194
+ self.conbus_protocol.start_reactor()
195
+
196
+ def stop_reactor(self) -> None:
197
+ """Stop the reactor."""
198
+ self.conbus_protocol.stop_reactor()
199
+
200
+ def __enter__(self) -> "ConbusDatapointQueryAllService":
201
+ """Enter context manager - reset state for singleton reuse.
202
+
203
+ Returns:
204
+ Self for context manager protocol.
205
+ """
206
+ # Reset state for singleton reuse
207
+ self.service_response = ConbusDatapointResponse(
208
+ success=False,
209
+ serial_number="",
210
+ )
211
+ self.datapoint_types = list(DataPointType)
212
+ self.current_index = 0
213
+ self.serial_number = ""
214
+ return self
215
+
216
+ def __exit__(
217
+ self, _exc_type: Optional[type], _exc_val: Optional[Exception], _exc_tb: Any
218
+ ) -> None:
219
+ """Exit context manager - cleanup signals and reactor."""
220
+ # Disconnect protocol signals
221
+ self.conbus_protocol.on_connection_made.disconnect(self.connection_made)
222
+ self.conbus_protocol.on_telegram_sent.disconnect(self.telegram_sent)
223
+ self.conbus_protocol.on_telegram_received.disconnect(self.telegram_received)
224
+ self.conbus_protocol.on_timeout.disconnect(self.timeout)
225
+ self.conbus_protocol.on_failed.disconnect(self.failed)
226
+ # Disconnect service signals
227
+ self.on_progress.disconnect()
228
+ # Stop reactor
229
+ self.stop_reactor()
@@ -5,48 +5,57 @@ This service handles datapoint query operations for modules through Conbus teleg
5
5
 
6
6
  import logging
7
7
  from datetime import datetime
8
- from typing import Callable, Optional
8
+ from typing import Any, Optional
9
9
 
10
- from twisted.internet.posixbase import PosixReactorBase
10
+ from psygnal import Signal
11
11
 
12
- from xp.models import ConbusClientConfig, ConbusDatapointResponse
12
+ from xp.models import ConbusDatapointResponse
13
13
  from xp.models.protocol.conbus_protocol import TelegramReceivedEvent
14
14
  from xp.models.telegram.datapoint_type import DataPointType
15
15
  from xp.models.telegram.reply_telegram import ReplyTelegram
16
16
  from xp.models.telegram.system_function import SystemFunction
17
17
  from xp.models.telegram.telegram_type import TelegramType
18
- from xp.services.protocol import ConbusProtocol
18
+ from xp.services.protocol.conbus_event_protocol import ConbusEventProtocol
19
19
  from xp.services.telegram.telegram_service import TelegramService
20
20
 
21
21
 
22
- class ConbusDatapointService(ConbusProtocol):
23
- """
24
- Service for querying datapoints from Conbus modules.
22
+ class ConbusDatapointService:
23
+ """Service for querying datapoints from Conbus modules.
25
24
 
26
- Uses ConbusProtocol to provide datapoint query functionality
25
+ Uses ConbusEventProtocol to provide datapoint query functionality
27
26
  for reading sensor data and module information.
27
+
28
+ Attributes:
29
+ conbus_protocol: Protocol instance for Conbus communication.
30
+ telegram_service: Service for parsing telegrams.
31
+ on_finish: Signal emitted when datapoint query completes (with response).
28
32
  """
29
33
 
34
+ on_finish: Signal = Signal(ConbusDatapointResponse)
35
+
30
36
  def __init__(
31
37
  self,
38
+ conbus_protocol: ConbusEventProtocol,
32
39
  telegram_service: TelegramService,
33
- cli_config: ConbusClientConfig,
34
- reactor: PosixReactorBase,
35
40
  ) -> None:
36
41
  """Initialize the Conbus datapoint service.
37
42
 
38
43
  Args:
44
+ conbus_protocol: Protocol instance for Conbus communication.
39
45
  telegram_service: Service for parsing telegrams.
40
- cli_config: Configuration for Conbus client connection.
41
- reactor: Twisted reactor for event loop.
42
46
  """
43
- super().__init__(cli_config, reactor)
47
+ self.conbus_protocol = conbus_protocol
44
48
  self.telegram_service = telegram_service
49
+
50
+ # Connect protocol signals
51
+ self.conbus_protocol.on_connection_made.connect(self.connection_made)
52
+ self.conbus_protocol.on_telegram_sent.connect(self.telegram_sent)
53
+ self.conbus_protocol.on_telegram_received.connect(self.telegram_received)
54
+ self.conbus_protocol.on_timeout.connect(self.timeout)
55
+ self.conbus_protocol.on_failed.connect(self.failed)
56
+
45
57
  self.serial_number: str = ""
46
58
  self.datapoint_type: Optional[DataPointType] = None
47
- self.datapoint_finished_callback: Optional[
48
- Callable[[ConbusDatapointResponse], None]
49
- ] = None
50
59
  self.service_response: ConbusDatapointResponse = ConbusDatapointResponse(
51
60
  success=False,
52
61
  serial_number=self.serial_number,
@@ -55,7 +64,7 @@ class ConbusDatapointService(ConbusProtocol):
55
64
  # Set up logging
56
65
  self.logger = logging.getLogger(__name__)
57
66
 
58
- def connection_established(self) -> None:
67
+ def connection_made(self) -> None:
59
68
  """Handle connection established event."""
60
69
  self.logger.debug(
61
70
  f"Connection established, querying datapoint {self.datapoint_type}."
@@ -64,7 +73,7 @@ class ConbusDatapointService(ConbusProtocol):
64
73
  self.failed("Datapoint type not set")
65
74
  return
66
75
 
67
- self.send_telegram(
76
+ self.conbus_protocol.send_telegram(
68
77
  telegram_type=TelegramType.SYSTEM,
69
78
  serial_number=self.serial_number,
70
79
  system_function=SystemFunction.READ_DATAPOINT,
@@ -128,9 +137,14 @@ class ConbusDatapointService(ConbusProtocol):
128
137
  self.service_response.datapoint_type = self.datapoint_type
129
138
  self.service_response.datapoint_telegram = datapoint_telegram
130
139
  self.service_response.data_value = datapoint_telegram.data_value
131
- if self.datapoint_finished_callback:
132
- self.datapoint_finished_callback(self.service_response)
133
- self._stop_reactor()
140
+
141
+ # Emit finish signal
142
+ self.on_finish.emit(self.service_response)
143
+ self.stop_reactor()
144
+
145
+ def timeout(self) -> None:
146
+ """Handle timeout event."""
147
+ self.failed("Timeout")
134
148
 
135
149
  def failed(self, message: str) -> None:
136
150
  """Handle failed connection event.
@@ -143,14 +157,14 @@ class ConbusDatapointService(ConbusProtocol):
143
157
  self.service_response.timestamp = datetime.now()
144
158
  self.service_response.serial_number = self.serial_number
145
159
  self.service_response.error = message
146
- if self.datapoint_finished_callback:
147
- self.datapoint_finished_callback(self.service_response)
160
+
161
+ # Emit finish signal
162
+ self.on_finish.emit(self.service_response)
148
163
 
149
164
  def query_datapoint(
150
165
  self,
151
166
  serial_number: str,
152
167
  datapoint_type: DataPointType,
153
- finish_callback: Callable[[ConbusDatapointResponse], None],
154
168
  timeout_seconds: Optional[float] = None,
155
169
  ) -> None:
156
170
  """Query a specific datapoint from a module.
@@ -158,13 +172,47 @@ class ConbusDatapointService(ConbusProtocol):
158
172
  Args:
159
173
  serial_number: 10-digit module serial number.
160
174
  datapoint_type: Type of datapoint to query.
161
- finish_callback: Callback function to call when the datapoint is received.
162
175
  timeout_seconds: Timeout in seconds.
163
176
  """
164
177
  self.logger.info("Starting query_datapoint")
165
178
  if timeout_seconds:
166
- self.timeout_seconds = timeout_seconds
167
- self.datapoint_finished_callback = finish_callback
179
+ self.conbus_protocol.timeout_seconds = timeout_seconds
168
180
  self.serial_number = serial_number
169
181
  self.datapoint_type = datapoint_type
170
- self.start_reactor()
182
+
183
+ def set_timeout(self, timeout_seconds: float) -> None:
184
+ """Set operation timeout.
185
+
186
+ Args:
187
+ timeout_seconds: Timeout in seconds.
188
+ """
189
+ self.conbus_protocol.timeout_seconds = timeout_seconds
190
+
191
+ def start_reactor(self) -> None:
192
+ """Start the reactor."""
193
+ self.conbus_protocol.start_reactor()
194
+
195
+ def stop_reactor(self) -> None:
196
+ """Stop the reactor."""
197
+ self.conbus_protocol.stop_reactor()
198
+
199
+ def __enter__(self) -> "ConbusDatapointService":
200
+ """Enter context manager - reset state for singleton reuse.
201
+
202
+ Returns:
203
+ Self for context manager protocol.
204
+ """
205
+ self.datapoint_response = ConbusDatapointResponse(success=False)
206
+ return self
207
+
208
+ def __exit__(
209
+ self, _exc_type: Optional[type], _exc_val: Optional[Exception], _exc_tb: Any
210
+ ) -> None:
211
+ """Exit context manager and disconnect signals."""
212
+ self.conbus_protocol.on_connection_made.disconnect(self.connection_made)
213
+ self.conbus_protocol.on_telegram_sent.disconnect(self.telegram_sent)
214
+ self.conbus_protocol.on_telegram_received.disconnect(self.telegram_received)
215
+ self.conbus_protocol.on_timeout.disconnect(self.timeout)
216
+ self.conbus_protocol.on_failed.disconnect(self.failed)
217
+ self.on_finish.disconnect()
218
+ self.stop_reactor()