canadapter script

canadapter

canadapter.init_canadapter()[source]

Initialize the canadapter.

Returns a ‘resource’ in the sgframework terminology.

canadapter.loop_canadapter(resource)[source]
canadapter.main()[source]
canadapter.on_send_can_data(resource, messagetype, servicename, command_name_mqtt, command_payload_mqtt)[source]

Callback for use when receiving a MQTT message. Sends a CAN message.

For callback interface, see sgframework.BaseFramework() documentation.

canadapter.signal_handler(signum, frame)[source]

canadapterlib

class canadapterlib.Converter(can_config, mqttfile_path=None, sort_json_keys=False)[source]

Converter between CAN and MQTT.

Does not store any CAN or MQTT messages.

Parameters:
  • can_config (can4python.Configuration) –
  • mqttfile_path (str or None) – Full path to the configuration (JSON) file
  • sort_json_keys (bool) – Sort keys in resulting JSON strings.

If the mqttfile_path argument not is given, it listens to all CAN signals (without name or value conversion).

canframe_to_mqtt(frame)[source]
Parameters:frame (can4python.CanFrame) – Incoming CAN frame with data.

Returns a list of MQTT messages, each represented as the tuple (MQTT signalname, MQTT payload). The payload is later converted to a string before sending.

mqtt_to_cansignals(command_name, command_payload)[source]
Parameters:
  • command_name (str) – Incoming MQTT command name (from the splitted MQTT topic)
  • command_payload (str) – Incoming MQTT payload

Returns a dictionary with the signal values to send. The keys are the CAN signalnames (str), and the items are the CAN values (numerical or None). If the CAN value is None the default value is used.

get_definitions_incoming_mqtt_command()[source]

Find the incoming MQTT command signalnames etc.

Returns a list of dictionaries, each having the arguments for the resource.register_incoming_command() call.

Note that the ‘callback’ keyword argument not is set in the output from this function, but needs to be set separately.

get_definitions_outgoing_mqtt_data()[source]

Find the outgoing MQTT data signalnames etc.

Returns a list of dictionaries, each having the arguments for the resource.register_outgoing_data() call.

get_descriptive_ascii_art()[source]

Return a string describing the conversion between CAN and MQTT.

_get_incoming_cansignal_names()[source]

Return a list (str) of incoming cansignal names (according to the CAN configuration).

_get_node_incoming_can_frameids()[source]

Return a list (int) of all incoming CAN frame ids for this node, according to the CAN configutation.

_get_node_outgoing_can_frameids()[source]

Return a list (int) of all outgoing CAN frame ids for this node, according to the CAN configutation.

_get_canframe_id(can_signal_name)[source]

Find the CAN frame id for a CAN signal name.

Parameters:can_signal_name (str) –

Returns the frame_id (int).

Raises:KeyError if the can_signal_name not is found.
_precalculate_frame_id(translationinfo)[source]

Set the frame_id field of the translationinfo.

Does not return anything.

class canadapterlib.IndividualInfo(can_name, mqtt_name=None, send_can=False, echo_mqtt=False, receive_can=True, multiplier=1.0, frame_id=None, mqtt_type=<class 'float'>)[source]

A class for describing the translation between a CAN signal and an individual MQTT signal.

can_name

str

Signal name in the KCD file for CAN

mqtt_name

str or None

Signal name on MQTT (part of the topic). Defaults to None (use same name on MQTT as on CAN.

mqtt_type

type

Conversion function used to convert the value to correct type inside the MQTT message (which itself is a string). Defaults to float.

send_can

bool

Whether the signal should be sent to CAN. Defaults to False.

echo_mqtt

bool

Whether the incoming MQTT message should be echoed back. Defaults to False.

receive_can

bool

Whether the signal should be sent to MQTT. Defaults to True.

multiplier

float

Multiplier when converting a CAN signal to an MQTT signal. In the other direction is 1/multiplier used. Defaults to 1.0.

frame_id

int or None

The id of the CAN frame the signal is using

class canadapterlib.AggregateInfo(mqtt_name, send_can=False, receive_can=True, echo_mqtt=False, frame_id=None)[source]

A class for describing the translation between a CAN signal aggregate and a MQTT signal.

All CAN signals must be located in the same CAN frame. The send_can and receive_can fields of the subsignals are not used.

mqtt_name

str

Signal name on MQTT (part of the topic).

send_can

bool

Whether the subsignals should be sent to CAN. Defaults to False.

receive_can

bool

Whether the aggregate should be sent to MQTT. Defaults to True.

echo_mqtt

bool

Whether the incoming MQTT message should be echoed back. Defaults to False.

frame_id

int or None

The id of the CAN frame the aggregate data is using

subsignals

list of IndividualInfo

Definitions for the signals within the aggregate

canadapterlib.translationfile_read(filename)[source]

Read a translation file, having the CAN signal names and the MQTT signal names etc.

Parameters:filename (str) – Full path to the input file.

The input file should be a valid JSON file, having a top object with the name ‘entities’ and the objects ‘aggregates’ and ‘signals’. Item inside ‘signals’ is a list of signaldefinition objects. Item in aggregates are several ‘signals’.

See the sgframework documentation for a more detailed discussion on the file format.

Returns a single list containing AggregateInfo and IndividualInfo. The order in the list is not relevant.

canadapterlib.parse_signal(json_signal, filename)[source]

Parse signal information from a JSON dict, and convert to an instance of IndividualInfo.

Parameters:
  • json_signal – a dict from a (part of a) parsed JSON file
  • filename (str) – Filename (for use in error messages)
Returns:

An IndividualInfo instance

canadapterlib.parse_aggregate(json_aggregate, filename)[source]

Parse signal information from a JSON dict, and convert to an instance of AggregateInfo.

Parameters:
  • json_aggregate – a dict from a (part of a) parsed JSON file
  • filename (str) – Filename (for use in error messages)
Returns:

An AggregateInfo instance