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 ecu_B_subscribe_(stub): """Subscribe to a value published by ecu_A and output value Parameters ---------- stub : NetworkServiceStub Object instance of class """ namespace = "custom_can" client_id = common_pb2.ClientId(id="id_ecu_B") # Subscribe to value 'SteerAngle' steer_angle = common_pb2.SignalId( name="SteerAngle", namespace=common_pb2.NameSpace(name=namespace)) sub_info = network_api_pb2.SubscriberConfig( clientId=client_id, signals=network_api_pb2.SignalIds(signalId=[steer_angle]), onChange=True, ) # Output subscribed signal try: for subs_counter in stub.SubscribeToSignals(sub_info): # For clean exit when stopping script if exit_event.is_set(): break print("ecu_B, (subscribe) SteerAngle is ", subs_counter.signal[0]) except grpc._channel._Rendezvous as err: print(err)
def read_on_timer(stub, signals, pause): """Simple reading with timer, logs on purpose tabbed with double space 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( " read_on_timer " + signal.id.name + " value " + str(signal.integer) ) except grpc._channel._Rendezvous as err: print(err) time.sleep(pause)
def read_on_timer(stub, signals, pause): while True: read_info = network_api_pb2.SignalIds(signalId=signals) try: response = stub.ReadSignals(read_info) for signal in response.signal: print(" read_on_timer " + signal.id.name + " value " + str(signal.integer)) except grpc._channel._Rendezvous as err: print(err) time.sleep(pause)
def subscribe_to_arbitration(stub): source = common_pb2.ClientId(id="app_identifier") namespace = common_pb2.NameSpace(name = "VirtualCanInterface") signal = common_pb2.SignalId(name="BenchC_c_5", namespace=namespace) sub_info = network_api_pb2.SubscriberConfig(clientId=source, signals=network_api_pb2.SignalIds(signalId=[signal]), onChange=False) try: for response in stub.SubscribeToSignals(sub_info): print(response) except grpc._channel._Rendezvous as err: print(err)
def ecu_B_subscribe_2(stub): """Shows possibility to subscribe to same signal multiple times, also using array to subscribe to additional signals. Logs on purpose tabbed with single space. Parameters ---------- stub : NetworkServiceStub Object instance of class """ namespace = "ecu_B" client_id = common_pb2.ClientId(id="id_ecu_B") # Signal value 'counter' counter = common_pb2.SignalId( name="counter", namespace=common_pb2.NameSpace(name=namespace) ) # Signal value 'TestFr06_Child02' testFr06_Child02 = common_pb2.SignalId( name="TestFr06_Child02", namespace=common_pb2.NameSpace(name=namespace) ) # Frame value 'TestFr04' testFr04 = common_pb2.SignalId( name="TestFr04", namespace=common_pb2.NameSpace(name=namespace) ) # Subscribes to 'counter', 'TestFr06_Child02' and 'TestFr04' sub_info = network_api_pb2.SubscriberConfig( clientId=client_id, signals=network_api_pb2.SignalIds( signalId=[counter, testFr06_Child02, testFr04] ), onChange=True, ) try: for response in stub.SubscribeToSignals(sub_info): # Since we subscribed to a set of signals we need to check which arrived. for signal in response.signal: if signal.id.name == "counter": print(" ecu_B, (subscribe_2) counter is ", signal.integer) if signal.id.name == "TestFr06_Child02": print( " ecu_B signal: " + signal.id.name + " arrived: " + str(signal.integer) ) if signal.id.name == "TestFr04": print(" ecu_B, (subscribe_2) raw is ", binascii.hexlify(signal.raw)) except grpc._channel._Rendezvous as err: print(err)
def subscribe_to_diag(client_id, stub, diag_frame_req, diag_frame_resp): sub_info = network_api_pb2.SubscriberConfig( clientId=client_id, signals=network_api_pb2.SignalIds(signalId=[diag_frame_req]), onChange=False) try: for response in stub.SubscribeToSignals(sub_info): print(response) print(binascii.hexlify(response.signal[0].raw)) # here we could switch on the response.signal[0].raw which contains service id and did. publish_signals(client_id, stub, diag_frame_resp) except grpc._channel._Rendezvous as err: print(err)
def subscribe_to_signal(stub): source = common_pb2.ClientId(id="app_identifier") signals = [ common_pb2.SignalId(name="SteerWhlAgSafe", namespace=common_pb2.NameSpace(name = "ChassisCANhs")), common_pb2.SignalId(name="DrvrDesDir", namespace=common_pb2.NameSpace(name = "ChassisCANhs")), common_pb2.SignalId(name="RecOfImpctSafeCntr", namespace=common_pb2.NameSpace(name = "BodyCANhs")), ] sub_info = network_api_pb2.SubscriberConfig(clientId=source, signals=network_api_pb2.SignalIds(signalId=signals), onChange=False) try: for response in stub.SubscribeToSignals(sub_info): print(response) except grpc._channel._Rendezvous as err: print(err)
def ecu_B_subscribe(stub): namespace = "ecu_B" client_id = common_pb2.ClientId(id="id_ecu_B") counter = common_pb2.SignalId(name="counter", namespace=common_pb2.NameSpace(name = namespace)) sub_info = network_api_pb2.SubscriberConfig(clientId=client_id, signals=network_api_pb2.SignalIds(signalId=[counter]), onChange=False) try: for subs_counter in stub.SubscribeToSignals(sub_info): print("ecu_B, (subscribe) counter is ", subs_counter.signal[0].integer) counter_times_2 = common_pb2.SignalId(name="counter_times_2", namespace=common_pb2.NameSpace(name = namespace)) signal_with_payload = network_api_pb2.Signal(id = counter_times_2, integer = subs_counter.signal[0].integer * 2) publish_signals(client_id, stub, [signal_with_payload]) except grpc._channel._Rendezvous as err: print(err)
def ecu_slave_subscribe(stub, client_id, signals, onChange): global increasing_counter sub_info = network_api_pb2.SubscriberConfig( clientId=client_id, signals=network_api_pb2.SignalIds(signalId=signals), onChange=onChange) try: for subscription in stub.SubscribeToSignals(sub_info): for signal in subscription.signal: if not signal.HasField("arbitration"): print("slave, %s with value %s " % (signal.id, signal)) except grpc._channel._Rendezvous as err: print(err)
def read_signal(stub, signal): """Read signals Parameters ---------- stub : NetworkServiceStub Object instance of class signal : SignalId Object instance of class Returns ------- Signals Object instance of class """ read_info = network_api_pb2.SignalIds(signalId=[signal]) return stub.ReadSignals(read_info)
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 ecu_B_subscribe_2(stub): namespace = "ecu_B" # specify some signals client_id = common_pb2.ClientId(id="id_ecu_B") counter = common_pb2.SignalId(name="counter", namespace=common_pb2.NameSpace(name = namespace)) testFr06_Child02 = common_pb2.SignalId(name="TestFr06_Child02", namespace=common_pb2.NameSpace(name = namespace)) testFr04 = common_pb2.SignalId(name="TestFr04", namespace=common_pb2.NameSpace(name = namespace)) sub_info = network_api_pb2.SubscriberConfig(clientId=client_id, signals=network_api_pb2.SignalIds(signalId=[counter, testFr06_Child02, testFr04]), onChange=False) try: for response in stub.SubscribeToSignals(sub_info): # since we subscribe to a set of signal we need to check which arrived. for signal in response.signal: if (signal.id.name == "counter"): print(" ecu_B, (subscribe_2) counter is ", signal.integer) if (signal.id.name == "TestFr06_Child02"): print(" ecu_B signal: " + signal.id.name + " arrived: " + str(signal.integer)) if (signal.id.name == "TestFr04"): print(" ecu_B, (subscribe_2) raw is ", binascii.hexlify(signal.raw)) except grpc._channel._Rendezvous as err: print(err)
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 ecu_B_subscribe(stub): """Subscribe to a value published by ecu_A and publish doubled value back to ecu_A Parameters ---------- stub : NetworkServiceStub Object instance of class """ namespace = "ecu_B" client_id = common_pb2.ClientId(id="id_ecu_B") # Subscribe to value 'counter' counter = common_pb2.SignalId( name="counter", namespace=common_pb2.NameSpace(name=namespace) ) sub_info = network_api_pb2.SubscriberConfig( clientId=client_id, signals=network_api_pb2.SignalIds(signalId=[counter]), onChange=True, ) # Publish doubled value as 'counter_times_2' try: for subs_counter in stub.SubscribeToSignals(sub_info): print("ecu_B, (subscribe) counter is ", subs_counter.signal[0].integer) counter_times_2 = common_pb2.SignalId( name="counter_times_2", namespace=common_pb2.NameSpace(name=namespace) ) signal_with_payload = network_api_pb2.Signal( id=counter_times_2, integer=subs_counter.signal[0].integer * 2 ) publish_signals(client_id, stub, [signal_with_payload]) except grpc._channel._Rendezvous as err: print(err)
def read_signal(stub, signal): read_info = network_api_pb2.SignalIds(signalId=[signal]) return stub.ReadSignals(read_info)
channel = grpc.insecure_channel("localhost:50051") # Create the system stup, to upload relevant confiuration system_stub = system_api_pb2_grpc.SystemServiceStub(channel) check_license(system_stub) upload_folder(system_stub, "configuration") reload_configuration(system_stub) # create the identifier of *this* client client_id = common_pb2.ClientId(id="virtual_example_sub") # Create the stub network_stub = network_api_pb2_grpc.NetworkServiceStub(channel) # Create a signal namespace = common_pb2.NameSpace(name="VirtualInterface") signal = common_pb2.SignalId(name="my_madeup_virtual_signal", namespace=namespace) # Create a subscriber config signals = network_api_pb2.SignalIds(signalId=[signal]) sub_info = network_api_pb2.SubscriberConfig(clientId=client_id, signals=signals, onChange=False) # Subscribe while True: try: for response in network_stub.SubscribeToSignals(sub_info, timeout=None): for signal in response.signal: print( f"received {signal.id.name} value is {get_value(signal)}" ) except grpc._channel._Rendezvous as err: print(err)