-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathclient.py
118 lines (100 loc) · 3.95 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# Copyright (c) 2022-2024 Contributors to the Eclipse Foundation
#
# This program and the accompanying materials are made available under the
# terms of the Apache License, Version 2.0 which is available at
# https://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.
#
# SPDX-License-Identifier: Apache-2.0
import asyncio
import logging
from typing import List, Optional
from urllib.parse import urlparse
import grpc
from velocitas_sdk import config
from velocitas_sdk.proto.broker_pb2 import (
GetDatapointsRequest,
GetMetadataRequest,
SetDatapointsRequest,
SubscribeRequest,
)
from velocitas_sdk.proto.broker_pb2_grpc import BrokerStub
logger = logging.getLogger(__name__)
class VehicleDataBrokerClient:
"""VehicleDataBrokerClient provides the Graph API to access vehicle services
and vehicle signals."""
_instance = None
def __new__(cls, port: Optional[int] = None):
if cls._instance is None:
cls._instance = super(VehicleDataBrokerClient, cls).__new__(cls)
service_locator = config.middleware.service_locator
_location = service_locator.get_service_location("vehicledatabroker")
_hostname = urlparse(_location).hostname
if port is None:
_port = urlparse(_location).port
else:
_port = port
_address = f"{_hostname}:{_port}"
cls._channel = grpc.aio.insecure_channel(_address) # type: ignore
metadata = service_locator.get_metadata("vehicledatabroker")
cls._metadata = metadata
cls._stub = BrokerStub(cls._channel)
return cls._instance
async def close(self):
"""Closes runtime gRPC channel."""
if self._channel:
await self._channel.close() # type: ignore
def __enter__(self) -> "VehicleDataBrokerClient":
return self
def __exit__(self, exc_type, exc_value, traceback) -> None:
asyncio.run_coroutine_threadsafe(self.close(), asyncio.get_event_loop())
async def GetDatapoints(self, datapoints: List[str]):
try:
response = await self._stub.GetDatapoints(
GetDatapointsRequest(datapoints=datapoints), metadata=self._metadata
)
return response
except grpc.aio.AioRpcError: # type: ignore
logger.exception(
"Error occured in VehicleDataBrokerClient.GetDatapoints",
)
raise
async def SetDatapoints(self, datapoints):
try:
response = await self._stub.SetDatapoints(
SetDatapointsRequest(datapoints=datapoints), metadata=self._metadata
)
return response
except grpc.aio.AioRpcError: # type: ignore
logger.exception(
"Error occured in VehicleDataBrokerClient.SetDatapoints",
)
raise
def Subscribe(self, query: str):
try:
response = self._stub.Subscribe(
SubscribeRequest(query=query),
metadata=self._metadata,
)
return response
except grpc.aio.AioRpcError: # type: ignore
logger.exception(
"Error occured in VehicleDataBrokerClient.Subscribe",
)
raise
async def GetMetadata(self, names: list):
try:
response = await self._stub.GetMetadata(
GetMetadataRequest(names=names), metadata=self._metadata
)
return response
except grpc.aio.AioRpcError: # type: ignore
logger.exception(
"Error occured in VehicleDataBrokerClient.GetMetadata",
)
raise