-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathecu.py
executable file
·375 lines (304 loc) · 10.2 KB
/
ecu.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#!/usr/bin/env python3
import os
import time
import binascii
import grpc
import sys, getopt
import argparse
sys.path.append("../common/generated")
import network_api_pb2
import network_api_pb2_grpc
import system_api_pb2
import system_api_pb2_grpc
import common_pb2
import collections
sys.path.append("../common")
sys.path.append("../signaltools")
import helper
from helper import *
from threading import Thread, Timer
import queue
from urllib.parse import urlparse
from signalcreator import SignalCreator
signal_creator = None
q = queue.Queue()
def read_signals(stub, signal):
"""Read signals
Parameters
----------
stub : NetworkServiceStub
Object instance of class
signal : SignalId
Object instance of class
Returns
-------
Signals
Object instance of class
"""
try:
read_info = network_api_pb2.SignalIds(signalId=[signal])
return stub.ReadSignals(read_info)
except grpc._channel._Rendezvous as err:
print(err)
def publish_signals(client_id, stub, signals_with_payload, frequency=0):
"""Publish signals
Parameters
----------
client_id : ClientId
Object instance of class
stub : NetworkServiceStub
Object instance of class
signals_with_payload : Signal
Object instance of class
"""
publisher_info = network_api_pb2.PublisherConfig(
clientId=client_id,
signals=network_api_pb2.Signals(signal=signals_with_payload),
frequency=frequency,
)
try:
stub.PublishSignals(publisher_info)
except grpc._channel._Rendezvous as err:
print(err)
def printer(signals):
for signal in signals:
print(f"{signal.id.name} {signal.id.namespace.name} {get_value(signal)}")
def ecu_A(stub, pause):
"""Publishes a value, read other value (published by ecu_B)
Parameters
----------
stub : NetworkServiceStub
Object instance of class
pause : int
Amount of time to pause, in seconds
"""
increasing_counter = 0
namespace = "ecu_A"
clientId = common_pb2.ClientId(id="id_ecu_A")
while True:
print("\necu_A, seed is ", increasing_counter)
# Publishes value 'counter'
publish_signals(
clientId,
stub,
[
signal_creator.signal_with_payload(
"counter", namespace, ("integer", increasing_counter)
),
# add any number of signals here, make sure that all signals/frames are unique.
# signal_creator.signal_with_payload(
# "TestFr04", namespace, ("raw", binascii.unhexlify("0a0b0c0d")), False
# ),
],
)
time.sleep(pause)
# Read the other value 'counter_times_2' and output result
read_signal_response = read_signals(
stub, signal_creator.signal("counter_times_2", namespace)
)
for signal in read_signal_response.signal:
print(f"ecu_A, (result) {signal.id.name} is {get_value(signal)}")
increasing_counter = (increasing_counter + 1) % 4
def read_on_timer(stub, signals, pause):
"""Simple reading with timer
Parameters
----------
stub : NetworkServiceStub
Object instance of class
signals : SignalId
Object instance of class
pause : int
Amount of time to pause, in seconds
"""
while True:
read_info = network_api_pb2.SignalIds(signalId=signals)
try:
response = stub.ReadSignals(read_info)
for signal in response.signal:
print(f"ecu_B, (read) {signal.id.name} is {get_value(signal)}")
except grpc._channel._Rendezvous as err:
print(err)
time.sleep(pause)
def get_value(signal):
if signal.raw != b"":
return "0x" + binascii.hexlify(signal.raw).decode("ascii")
elif signal.HasField("integer"):
return signal.integer
elif signal.HasField("double"):
return signal.double
elif signal.HasField("arbitration"):
return signal.arbitration
else:
return "empty"
def act_on_signal(client_id, stub, sub_signals, on_change, fun, on_subcribed=None):
sub_info = network_api_pb2.SubscriberConfig(
clientId=client_id,
signals=network_api_pb2.SignalIds(signalId=sub_signals),
onChange=on_change,
)
try:
subscripton = stub.SubscribeToSignals(sub_info, timeout=None)
if on_subcribed:
on_subcribed(subscripton)
print("waiting for signal...")
for subs_counter in subscripton:
fun(subs_counter.signal)
except grpc.RpcError as e:
try:
subscripton.cancel()
except grpc.RpcError as e2:
pass
except grpc._channel._Rendezvous as err:
print(err)
# reload, alternatively non-existing signal
print("subscription terminated")
def main(argv):
parser = argparse.ArgumentParser(description="Provide address to Beambroker")
parser.add_argument(
"-url",
"--url",
type=str,
help="URL of the Beamy Broker",
required=False,
default="http://127.0.0.1:50051",
)
parser.add_argument(
"-reload_config",
"--reload_config",
action="store_true",
help="Reload configuration",
required=False,
default=False,
)
parser.add_argument(
"-x_api_key",
"--x_api_key",
type=str,
help="required api key for https sessions",
required=False,
default="offline",
)
args = parser.parse_args()
run(args.url, args.reload_config, args.x_api_key)
def double_and_publish(network_stub, client_id, trigger, signals):
for signal in signals:
print(f"ecu_B, (subscribe) {signal.id.name} {get_value(signal)}")
if signal.id == trigger:
publish_signals(
client_id,
network_stub,
[
signal_creator.signal_with_payload(
"counter_times_2", "ecu_B", ("integer", get_value(signal) * 2)
),
# add any number of signals/frames here
# signal_creator.signal_with_payload(
# "TestFr04", "ecu_B", ("raw", binascii.unhexlify("0a0b0c0d")), False
# )
],
)
import grpc
from grpc_interceptor import ClientCallDetails, ClientInterceptor
from typing import Any, Callable
class HeaderInterceptor(ClientInterceptor):
def __init__(self, header_dict):
self.header_dict = header_dict
def intercept(
self,
method: Callable,
request_or_iterator: Any,
call_details: grpc.ClientCallDetails,
):
new_details = ClientCallDetails(
call_details.method,
call_details.timeout,
self.header_dict.items(),
call_details.credentials,
call_details.wait_for_ready,
call_details.compression,
)
return method(request_or_iterator, new_details)
def create_channel(url, x_api_key):
url = urlparse(url)
if url.scheme == "https":
creds = grpc.ssl_channel_credentials(
root_certificates=None, private_key=None, certificate_chain=None
)
channel = grpc.secure_channel(
url.hostname + ":" + str(url.port or "443"), creds
)
else:
channel = grpc.insecure_channel(url.hostname + ":" + str(url.port or "50051"))
intercept_channel = grpc.intercept_channel(
channel, HeaderInterceptor({"x-api-key": x_api_key})
)
return intercept_channel
def run(url, restart_broker, x_api_key):
"""Main function, checking arguments passed to script, setting up stubs, configuration and starting Threads."""
# Setting up stubs and configuration
intercept_channel = create_channel(url, x_api_key)
network_stub = network_api_pb2_grpc.NetworkServiceStub(intercept_channel)
system_stub = system_api_pb2_grpc.SystemServiceStub(intercept_channel)
check_license(system_stub)
upload_folder(system_stub, "configuration_udp")
# upload_folder(system_stub, "configuration_lin")
# upload_folder(system_stub, "configuration_can")
# upload_folder(system_stub, "configuration_canfd")
reload_configuration(system_stub)
global signal_creator
signal_creator = SignalCreator(system_stub)
# Lists available signals
configuration = system_stub.GetConfiguration(common_pb2.Empty())
for networkInfo in configuration.networkInfo:
print(
"signals in namespace ",
networkInfo.namespace.name,
system_stub.ListSignals(networkInfo.namespace),
)
# Starting Threads
# ecu b, we do this with lambda refering to double_and_publish.
ecu_b_client_id = common_pb2.ClientId(id="id_ecu_B")
ecu_B_sub_thread = Thread(
target=act_on_signal,
args=(
ecu_b_client_id,
network_stub,
[
signal_creator.signal("counter", "ecu_B"),
# here you can add any signal from any namespace
# signal_creator.signal("TestFr04", "ecu_B"),
],
True, # True: only report when signal changes
lambda signals: double_and_publish(
network_stub,
ecu_b_client_id,
signal_creator.signal("counter", "ecu_B"),
signals,
),
lambda subscripton: (q.put(("id_ecu_B", subscripton))),
),
)
ecu_B_sub_thread.start()
# wait for subscription to settle
ecu, subscription = q.get()
# ecu a, this is where we publish, and
ecu_A_thread = Thread(
target=ecu_A,
args=(
network_stub,
1,
),
)
ecu_A_thread.start()
# ecu b, bonus, periodically, read using timer.
signals = [
signal_creator.signal("counter", "ecu_B"),
# add any number of signals from any namespace
# signal_creator.signal("TestFr04", "ecu_B"),
]
ecu_read_on_timer = Thread(target=read_on_timer, args=(network_stub, signals, 1))
ecu_read_on_timer.start()
# once we are done we could cancel subscription
# subscription.cancel()
if __name__ == "__main__":
main(sys.argv[1:])