UNPKG

@yinyinfurong_zmr/dbc-can

Version:

A general purpose CAN (Controller Area Network) toolbox with support for .dbc file parsing, CAN message decoding, and more

180 lines (179 loc) 6.86 kB
import { AdditionalAttributeObjects, AdditionalMessageOptions, AdditionalSignalOptions, Attribute, AttributeDataType, Attributes, DbcData, Message, Node, RequiredAttributeProps, Signal } from './DbcTypes'; /** * Creates a DBC instance that allows for parsing/loading of an existing DBC file * or write data to a new DBC file. * * If loading data from an existing file, simply call: * const dbc = new Dbc(); * dbc.load(fileContent) * * By default, when a new Dbc() instance is created, the encapsulated data will be empty. * If you are wanting to create fresh data you can call createMessage or createSignal to * create messages and signals, respectively. * Calls to createMessage and createSignal do not by default add the messages to the data, * you will need to make subsequent calls to addMessage or addSignal to add the data * to the class. * */ declare class Dbc { data: DbcData; errors: Map<number, SyntaxError[]>; constructor(); /** * Adds a version number to dbc data */ set version(version: string); /** * Adds a short description for the DBC data */ set description(description: string); createNode(name: string, options?: { description?: string; attributes?: Attributes; }): Node; /** * * Creates a Message instance that can later be added using addMessage() * or using the attached method .add() * * Ex. * let msg = dbc.createMessage('MessageName',100,8); * msg.add(); or dbc.addMessage(msg); * * @param name Name of CAN message * @param id ID of CAN message * @param dlc Data Length Code (data length) of CAN message * @param options Optional attributes that can be used when creating a message: signals, attributes, signalGroups, * sendingNode, and description * @returns Message */ createMessage(name: string, id: number, dlc: number, extended: boolean, options?: AdditionalMessageOptions): Message; /** * * Adds/appends message to existing message list * * @param message Message object to be added */ addMessage(message: Message | Message[]): void; /** * Removes an existing message from the DBC data * @param messageName Name of the message to remove */ removeMessage(messageName: string): void; /** * Creates a signal based on characteristics such as start bit and length * @param name Name of the signal * @param startBit Bit position of where the signal is to start in the Message bitfield * @param length Length of the signal * @param options Additional options, such as signed, endian, etc. */ createSignal(name: string, startBit: number, length: number, options?: AdditionalSignalOptions): Signal; /** * Removes a signal from an existing message * @param signalName Name of the signal * @param messageName Name of the message containing the signal */ removeSignal(signalName: string, messageName: string): void; /** * * Adds a Signal object to a specified Message * * @param messageName Name of the message the signal will be added to * @param signal Signal object to be added to the specified message */ addSignal(messageName: string, signal: Signal | Signal[]): void; /** * * Returns a message with the corresponding CAN ID. If message does not exist * a MessageDoesNotExist error will be thrown. * * @param id The CAN ID of the message wanting to be found * @returns Message * @throws MessageDoesNotExist */ getMessageById(id: number): Message; /** * * Finds a specific message within the DBC file data by name * * @param name string * @returns Message * @error MessageDoesNotExist */ getMessageByName(name: string): Message | undefined; /** * * Returns a signal object located in a specific CAN message by name * * @param name string * @param messageName string * @returns Signal * @error SignalDoesNotExist */ getSignalByName(name: string, messageName: string): Signal; /** * Convenience method that will create an Attribute object that can be appended to DBC data * @param name Name to be assigned to the attribute * @param type Attribute type: FLOAT, HEX, ENUM, INT, STRING * @param props Required properties of the attribute based on the type provided * @param options Additional attribute options that can be added */ createAttribute(name: string, type: AttributeDataType, props?: RequiredAttributeProps, options?: AdditionalAttributeObjects): Attribute; /** * Adds an existing attribute to the DBC data based on the supplied type * @param attribute Attribute * @param options node, id, signalName, or evName */ addAttribute(attribute: Attribute, options?: { node?: string; id?: number; signalName?: string; evName?: string; }): void; /** * Returns an environmental variable by name * @param name Name of environmental variable * @throws Error if environmental variable does not exist in database */ getEnvironmentalVariable(name: string): import("./DbcTypes").EnvironmentVariable; /** * Returns a node if it exists in the database * @param name Name of the node (string) * @throws Error if node does not exist */ getNode(name: string): Node; /** * Returns the mapped name in the database based on the supplied CAN ID * @param id Message ID (number) * @throws Error if no message with the corresponding ID exists */ messageIdToName(id: number): string; /** * * Loads a DBC file, as opposed to the default method 'load', which is * a non-blocking/async call whose promise must be caught for the return data to be used. * * @param fileContent Full file path to the dbc file, including extension * @param throwOnError * @returns DbcData Data contained in the dbc file */ load(fileContent: string, throwOnError?: boolean): DbcData; /** * * Writes the encapsulated data of a Dbc class instance to a dbc file */ write(): string; /** * * Transforms the internal DBC data from class instance into a JSON object/string * * @param options Additional formatting options, such as pretty print. * @returns JSON representation of loaded DBC data */ toJson(options?: { pretty?: boolean; preserveFormat?: boolean; }): string; initDbcDataObj(): DbcData; } export default Dbc;