|
| 1 | +# Copyright (c) 2022-2023 Robert Bosch GmbH and Microsoft Corporation |
| 2 | +# |
| 3 | +# This program and the accompanying materials are made available under the |
| 4 | +# terms of the Apache License, Version 2.0 which is available at |
| 5 | +# https://www.apache.org/licenses/LICENSE-2.0. |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | +# License for the specific language governing permissions and limitations |
| 11 | +# under the License. |
| 12 | +# |
| 13 | +# SPDX-License-Identifier: Apache-2.0 |
| 14 | + |
| 15 | +"""A sample Velocitas vehicle app for adjusting seat position.""" |
| 16 | + |
| 17 | +import json |
| 18 | +import logging |
| 19 | + |
| 20 | +from vehicle import Vehicle # type: ignore |
| 21 | + |
| 22 | +from sdv.util.log import ( # type: ignore |
| 23 | + get_opentelemetry_log_factory, |
| 24 | + get_opentelemetry_log_format, |
| 25 | +) |
| 26 | +from sdv.vdb.reply import DataPointReply |
| 27 | +from sdv.vehicle_app import VehicleApp, subscribe_topic |
| 28 | + |
| 29 | +logging.setLogRecordFactory(get_opentelemetry_log_factory()) |
| 30 | +logging.basicConfig(format=get_opentelemetry_log_format()) |
| 31 | +logging.getLogger().setLevel("DEBUG") |
| 32 | +logger = logging.getLogger(__name__) |
| 33 | + |
| 34 | + |
| 35 | +class SeatAdjusterApp(VehicleApp): |
| 36 | + """ |
| 37 | + Sample Velocitas Vehicle App. |
| 38 | +
|
| 39 | + The SeatAdjusterApp subscribes to a MQTT topic to listen for incoming |
| 40 | + requests to change the seat position and calls the SeatService to move the seat |
| 41 | + upon such a request, but only if Vehicle.Speed equals 0. |
| 42 | +
|
| 43 | + It also subcribes to the VehicleDataBroker for updates of the |
| 44 | + Vehicle.Cabin.Seat.Row1.Pos1.Position signal and publishes this |
| 45 | + information via another specific MQTT topic |
| 46 | + """ |
| 47 | + |
| 48 | + def __init__(self, vehicle_client: Vehicle): |
| 49 | + super().__init__() |
| 50 | + self.Vehicle = vehicle_client |
| 51 | + |
| 52 | + async def on_start(self): |
| 53 | + """Run when the vehicle app starts""" |
| 54 | + await self.Vehicle.Cabin.Seat.Row1.Pos1.Position.subscribe( |
| 55 | + self.on_seat_position_changed |
| 56 | + ) |
| 57 | + |
| 58 | + async def on_seat_position_changed(self, data: DataPointReply): |
| 59 | + response_topic = "seatadjuster/currentPosition" |
| 60 | + await self.publish_event( |
| 61 | + response_topic, |
| 62 | + json.dumps( |
| 63 | + {"position": data.get(self.Vehicle.Cabin.Seat.Row1.Pos1.Position).value} |
| 64 | + ), |
| 65 | + ) |
| 66 | + |
| 67 | + @subscribe_topic("seatadjuster/setPosition/request") |
| 68 | + async def on_set_position_request_received(self, data_str: str) -> None: |
| 69 | + data = json.loads(data_str) |
| 70 | + response_topic = "seatadjuster/setPosition/response" |
| 71 | + response_data = {"requestId": data["requestId"], "result": {}} |
| 72 | + |
| 73 | + vehicle_speed = (await self.Vehicle.Speed.get()).value |
| 74 | + |
| 75 | + position = data["position"] |
| 76 | + if vehicle_speed == 0: |
| 77 | + try: |
| 78 | + await self.Vehicle.Cabin.Seat.Row1.Pos1.Position.set(position) |
| 79 | + response_data["result"] = { |
| 80 | + "status": 0, |
| 81 | + "message": f"Set Seat position to: {position}", |
| 82 | + } |
| 83 | + except ValueError as error: |
| 84 | + response_data["result"] = { |
| 85 | + "status": 1, |
| 86 | + "message": f"Failed to set the position {position}, error: {error}", |
| 87 | + } |
| 88 | + except Exception: |
| 89 | + response_data["result"] = { |
| 90 | + "status": 1, |
| 91 | + "message": "Exception on set Seat position", |
| 92 | + } |
| 93 | + |
| 94 | + else: |
| 95 | + error_msg = f"""Not allowed to move seat because vehicle speed |
| 96 | + is {vehicle_speed} and not 0""" |
| 97 | + response_data["result"] = {"status": 1, "message": error_msg} |
| 98 | + |
| 99 | + await self.publish_event(response_topic, json.dumps(response_data)) |
0 commit comments