11from __future__ import annotations
22
33import sys
4- import time
54from abc import ABC , abstractmethod
65from types import TracebackType
76from typing import TYPE_CHECKING , Optional , Type
87
9- from grpc import RpcError
10- from ni .pythonpanel .v1 .python_panel_service_pb2 import ConnectRequest , DisconnectRequest
11- from ni .pythonpanel .v1 .python_panel_service_pb2_grpc import PythonPanelServiceStub
12- from ni_measurement_plugin_sdk_service .grpc .channelpool import GrpcChannelPool
8+ from ni_measurement_plugin_sdk_service .discovery import DiscoveryClient
9+
10+ from nipanel ._panel_client import PanelClient
1311
1412if TYPE_CHECKING :
1513 if sys .version_info >= (3 , 11 ):
2119class Panel (ABC ):
2220 """This class allows you to connect to a panel and specify values for its controls."""
2321
24- RETRY_WAIT_TIME = 1 # time in seconds to wait before retrying connection
25-
26- _channel_pool : GrpcChannelPool
27- _stub : PythonPanelServiceStub | None
22+ _panel_client : PanelClient
2823 _panel_id : str
2924 _panel_uri : str
3025
31- __slots__ = ["_channel_pool" , "_stub " , "_panel_id" , "_panel_uri" , "__weakref__" ]
26+ __slots__ = ["_panel_client " , "_panel_id" , "_panel_uri" , "__weakref__" ]
3227
3328 def __init__ (self , panel_id : str , panel_uri : str ) -> None :
3429 """Initialize the panel."""
35- self ._channel_pool = GrpcChannelPool ()
36- self ._stub = None
30+ self ._panel_client = PanelClient (self ._resolve_service_address )
3731 self ._panel_id = panel_id
3832 self ._panel_uri = panel_uri
3933
@@ -64,27 +58,11 @@ def __exit__(
6458
6559 def connect (self ) -> None :
6660 """Connect to the panel and open it."""
67- address = self ._resolve_service_address ()
68- channel = self ._channel_pool .get_channel (address )
69- self ._stub = PythonPanelServiceStub (channel )
70-
71- connect_request = ConnectRequest (panel_id = self ._panel_id , panel_uri = self ._panel_uri )
72- try :
73- self ._stub .Connect (connect_request )
74- except RpcError :
75- # retry the connection if it fails, but only once
76- time .sleep (self .RETRY_WAIT_TIME )
77- self ._stub .Connect (connect_request )
61+ self ._panel_client .connect (self ._panel_id , self ._panel_uri )
7862
7963 def disconnect (self ) -> None :
8064 """Disconnect from the panel (does not close the panel)."""
81- if self ._stub is None :
82- raise RuntimeError ("connect() must be called before disconnect()" )
83-
84- disconnect_request = DisconnectRequest (panel_id = self ._panel_id )
85- self ._stub .Disconnect (disconnect_request )
86- self ._stub = None
87- self ._channel_pool .close ()
65+ self ._panel_client .disconnect (self ._panel_id )
8866
8967 def get_value (self , value_id : str ) -> object :
9068 """Get the value for a control on the panel.
@@ -95,7 +73,7 @@ def get_value(self, value_id: str) -> object:
9573 Returns:
9674 The value
9775 """
98- # TODO: AB#3095681 - get the Any from _stub.GetValue and convert it to the correct type
76+ # TODO: AB#3095681 - get the Any from _client.get_value and convert it to the correct type
9977 return "placeholder value"
10078
10179 def set_value (self , value_id : str , value : object ) -> None :
@@ -105,10 +83,10 @@ def set_value(self, value_id: str, value: object) -> None:
10583 value_id: The id of the value
10684 value: The value
10785 """
108- # TODO: AB#3095681 - Convert the value to an Any and pass it to _stub.SetValue
86+ # TODO: AB#3095681 - Convert the value to an Any and pass it to _client.set_value
10987 pass
11088
11189 @abstractmethod
112- def _resolve_service_address (self ) -> str :
113- """Resolve the service location for the panel."""
90+ def _resolve_service_address (self , discovery_client : DiscoveryClient ) -> str :
91+ """Resolve the service address for the panel."""
11492 raise NotImplementedError
0 commit comments