Skip to content

Commit f32b017

Browse files
Adapt tests for seat adjuster to work after importing to template (#88)
* Adapt tests for seat adjuster to work after importing to template * Update pre commit hook versions * Align linter with template * Add adaptions for correct unit testing --------- Signed-off-by: Dennis Meister <[email protected]>
1 parent 466299b commit f32b017

File tree

15 files changed

+403
-222
lines changed

15 files changed

+403
-222
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
name: pre-commit
16+
description: run pre-commit
17+
inputs:
18+
extra_args:
19+
description: Options to pass to pre-commit run
20+
required: false
21+
default: "--all-files"
22+
runs:
23+
using: composite
24+
steps:
25+
- run: python -m pip install pre-commit
26+
shell: bash
27+
- run: python -m pip freeze --local
28+
shell: bash
29+
- uses: actions/cache@v3
30+
with:
31+
path: ~/.cache/pre-commit
32+
key: pre-commit-3|${{ env.pythonLocation }}|${{ hashFiles('.pre-commit-config.yaml') }}
33+
- run: pre-commit run --show-diff-on-failure --color=always ${{ inputs.extra_args }}
34+
shell: bash

.github/workflows/ci.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ jobs:
5757
python3 -m pip install tox-gh-actions
5858
5959
- name: Run Linters
60-
uses: pre-commit/action@v3.0.0
60+
uses: ./.github/actions/pre-commit-action
6161

6262
- name: Run the daparized databroker binary
6363
run: |

.pre-commit-config.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ repos:
5757
)$
5858
5959
- repo: https://github.com/psf/black
60-
rev: 22.10.0
60+
rev: 23.1.0
6161
hooks:
6262
- id: black
6363
exclude: >
@@ -103,7 +103,7 @@ repos:
103103
pass_filenames: false
104104

105105
- repo: https://github.com/pycqa/pydocstyle
106-
rev: 6.1.1
106+
rev: 6.3.0
107107
hooks:
108108
- id: pydocstyle
109109
exclude: >

NOTICE-3RD-PARTY-CONTENT.md

-1
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,4 @@
100100
|actions/setup-python|v4|MIT License|
101101
|actions/upload-artifact|v3|MIT License|
102102
|github/codeql-action|v2|MIT License|
103-
|pre-commit/action|v3.0.0|MIT License|
104103
|softprops/action-gh-release|v1|MIT License|

examples/dog-mode/src/main.py

-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ async def on_change(self, data: DataPointReply):
108108
)
109109

110110
async def display_values(self):
111-
112111
logger.info("Publish Current Temperature and StateOfCharge")
113112
try:
114113
logger.info(

examples/seat-adjuster/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ RUN pip3 install --no-cache-dir pyinstaller==5.9.0 \
4141

4242
WORKDIR /app
4343

44-
RUN pyinstaller --clean -F -s src/main.py
44+
RUN pyinstaller --clean -F -s --paths=src src/main.py
4545

4646
WORKDIR /app/dist
4747

Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
git+https://github.com/eclipse-velocitas/[email protected].1
1+
git+https://github.com/eclipse-velocitas/[email protected].2

examples/seat-adjuster/src/main.py

+3-73
Original file line numberDiff line numberDiff line change
@@ -12,97 +12,27 @@
1212
#
1313
# SPDX-License-Identifier: Apache-2.0
1414

15-
"""A sample Velocitas vehicle app for adjusting seat position."""
15+
"""The launcher for starting a Vehicle App."""
1616

1717
import asyncio
18-
import json
1918
import logging
2019
import signal
2120

22-
from vehicle import Vehicle, vehicle # type: ignore
21+
from vapp import SeatAdjusterApp # type: ignore # noqa: E402
22+
from vehicle import vehicle # type: ignore
2323

2424
from sdv.util.log import ( # type: ignore
2525
get_opentelemetry_log_factory,
2626
get_opentelemetry_log_format,
2727
)
28-
from sdv.vdb.subscriptions import DataPointReply
29-
from sdv.vehicle_app import VehicleApp, subscribe_topic
3028

3129
logging.setLogRecordFactory(get_opentelemetry_log_factory())
3230
logging.basicConfig(format=get_opentelemetry_log_format())
3331
logging.getLogger().setLevel("DEBUG")
3432
logger = logging.getLogger(__name__)
3533

3634

37-
class SeatAdjusterApp(VehicleApp):
38-
"""
39-
Sample Velocitas Vehicle App.
40-
41-
The SeatAdjusterApp subscribes to a MQTT topic to listen for incoming
42-
requests to change the seat position and calls the SeatService to move the seat
43-
upon such a request, but only if Vehicle.Speed equals 0.
44-
45-
It also subcribes to the VehicleDataBroker for updates of the
46-
Vehicle.Cabin.Seat.Row1.Pos1.Position signal and publishes this
47-
information via another specific MQTT topic
48-
"""
49-
50-
def __init__(self, vehicle_client: Vehicle):
51-
super().__init__()
52-
self.Vehicle = vehicle_client
53-
54-
async def on_start(self):
55-
"""Run when the vehicle app starts"""
56-
await self.Vehicle.Cabin.Seat.Row1.Pos1.Position.subscribe(
57-
self.on_seat_position_changed
58-
)
59-
60-
async def on_seat_position_changed(self, data: DataPointReply):
61-
response_topic = "seatadjuster/currentPosition"
62-
await self.publish_event(
63-
response_topic,
64-
json.dumps(
65-
{"position": data.get(self.Vehicle.Cabin.Seat.Row1.Pos1.Position).value}
66-
),
67-
)
68-
69-
@subscribe_topic("seatadjuster/setPosition/request")
70-
async def on_set_position_request_received(self, data_str: str) -> None:
71-
data = json.loads(data_str)
72-
response_topic = "seatadjuster/setPosition/response"
73-
response_data = {"requestId": data["requestId"], "result": {}}
74-
75-
vehicle_speed = (await self.Vehicle.Speed.get()).value
76-
77-
position = data["position"]
78-
if vehicle_speed == 0:
79-
try:
80-
await self.Vehicle.Cabin.Seat.Row1.Pos1.Position.set(position)
81-
response_data["result"] = {
82-
"status": 0,
83-
"message": f"Set Seat position to: {position}",
84-
}
85-
except ValueError as error:
86-
response_data["result"] = {
87-
"status": 1,
88-
"message": f"Failed to set the position {position}, error: {error}",
89-
}
90-
except Exception:
91-
response_data["result"] = {
92-
"status": 1,
93-
"message": "Exception on set Seat position",
94-
}
95-
96-
else:
97-
error_msg = f"""Not allowed to move seat because vehicle speed
98-
is {vehicle_speed} and not 0"""
99-
response_data["result"] = {"status": 1, "message": error_msg}
100-
101-
await self.publish_event(response_topic, json.dumps(response_data))
102-
103-
10435
async def main():
105-
10636
"""Main function"""
10737
logger.info("Starting seat adjuster app...")
10838
seat_adjuster_app = SeatAdjusterApp(vehicle)

examples/seat-adjuster/src/vapp.py

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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

Comments
 (0)