This repository was archived by the owner on Feb 10, 2026. It is now read-only.
forked from eclipse-velocitas/vehicle-app-python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
125 lines (101 loc) · 4.43 KB
/
Copy pathmain.py
File metadata and controls
125 lines (101 loc) · 4.43 KB
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
119
120
121
122
123
124
125
# Copyright (c) 2022 Robert Bosch GmbH and Microsoft Corporation
#
# 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
"""A sample Velocitas vehicle app for adjusting seat position."""
# pylint: disable=C0103, C0413, E1101
import asyncio
import json
import logging
import signal
import grpc
from sdv_model import Vehicle, vehicle # type: ignore
from sdv_model.proto.seats_pb2 import BASE, SeatLocation # type: ignore
from sdv.util.log import ( # type: ignore
get_opentelemetry_log_factory,
get_opentelemetry_log_format,
)
from sdv.vdb.subscriptions import DataPointReply
from sdv.vehicle_app import VehicleApp, subscribe_topic
logging.setLogRecordFactory(get_opentelemetry_log_factory())
logging.basicConfig(format=get_opentelemetry_log_format())
logging.getLogger().setLevel("INFO")
logger = logging.getLogger(__name__)
class SeatAdjusterApp(VehicleApp):
"""
Sample Velocitas Vehicle App.
The SeatAdjusterApp subscribes to a MQTT topic to listen for incoming
requests to change the seat position and calls the SeatService to move the seat
upon such a request, but only if Vehicle.Speed equals 0.
It also subcribes to the VehicleDataBroker for updates of the
Vehicle.Cabin.Seat.Row1.Pos1.Position signal and publishes this
information via another specific MQTT topic
"""
def __init__(self, vehicle_client: Vehicle):
super().__init__()
self.Vehicle = vehicle_client
async def on_start(self):
"""Run when the vehicle app starts"""
await self.Vehicle.Cabin.Seat.Row(1).Pos(1).Position.subscribe(
self.on_seat_position_changed
)
async def on_seat_position_changed(self, data: DataPointReply):
response_topic = "seatadjuster/currentPosition"
await self.publish_mqtt_event(
response_topic,
json.dumps(
{"position": data.get(self.Vehicle.Cabin.Seat.Row(1).Pos(1).Position)}
),
)
@subscribe_topic("seatadjuster/setPosition/request")
async def on_set_position_request_received(self, data_str: str) -> None:
data = json.loads(data_str)
response_topic = "seatadjuster/setPosition/response"
response_data = {"requestId": data["requestId"], "result": {}}
vehicle_speed = await self.Vehicle.Speed.get()
if vehicle_speed == 0:
try:
location = SeatLocation(row=1, index=1)
await self.Vehicle.Cabin.SeatService.MoveComponent(
location, BASE, data["position"]
)
response_data["result"] = {
"status": 0,
"message": f"""Called MoveComponent {data["position"]}""",
}
except grpc.RpcError as rpcerror:
if rpcerror.code() == grpc.StatusCode.INVALID_ARGUMENT:
error_msg = f"""Provided position '{data["position"]}' \
should be in between (0-1000)"""
else:
error_msg = f"Received unknown RPC error: code={rpcerror.code()}\
message={rpcerror.details()}" # pylint: disable=E1101
response_data["result"] = {"status": 1, "message": error_msg}
except Exception:
response_data["result"] = {
"status": 1,
"message": "Exception on MoveComponent",
}
else:
error_msg = f"""Not allowed to move seat because vehicle speed
is {vehicle_speed} and not 0"""
response_data["result"] = {"status": 1, "message": error_msg}
await self.publish_mqtt_event(response_topic, json.dumps(response_data))
async def main():
"""Main function"""
logger.info("Starting seat adjuster app...")
seat_adjuster_app = SeatAdjusterApp(vehicle)
await seat_adjuster_app.run()
LOOP = asyncio.get_event_loop()
LOOP.add_signal_handler(signal.SIGTERM, LOOP.stop)
LOOP.run_until_complete(main())
LOOP.close()