Skip to content

Write a plugin for VSmart

Xuepeng Xu edited this page Jun 22, 2015 · 17 revisions

#Plugin

In VSmart, the user can add some customized functions. Generally, a plugin has three different modules: customized event, customized receiver, customized executor.

Customized event

A customized event is an event type designed and created by the users. A user can add new fields to the customized event. A customized event can be sent by customized executor and received by customized receiver. In general, the development of customized event comes up with the corresponding customized executor and receiver. A customized event class should extend the class Event from Event_Module.DSRC_Event. The subclass should implement function __init__() and self_parse(). The following code is an example of a customized event:

# DSRC_Plugins/DSRC_Plugin_Cross_Event.py
class CustomizedEvent(Event):
    def __init__(self):
        Event.__init__(self)
        self.subtype = None
        self.auto_set_up = False
        self.x = None
        self.y = None
        self.r = None
        self.seq = None
        self.do_it = False

    def self_parse(self):
        self.subtype = self.msg_obj.get(DSRC_Event.KEY_SUBTYPE)
        if self.subtype == 'auto_setup':
            self.auto_set_up = self.msg_obj.get("auto_setup")
            self.x = self.msg_obj['x']
            self.y = self.msg_obj['y']
            self.r = self.msg_obj['r']
        elif self.subtype == "automove":
            do_it = self.msg_obj.get("do")
            if do_it is not None:
                self.do_it = self.msg_obj["do"]

The message in the air is in JSON format. Usually, customized executor generates the corresponding JSON message and the receiver uses self_parse function to parse the message.

Customized Executor

The implementation of customized executor contains two important functions: execute(dsrc_unit) and customized_cmd(dsrc_unit, user_input). If the plugin is used, the execute(dsrc_unit) will be executed periodically. customized_cmd(dsrc_unit, user_input) implements the customized commands which will be used for user interaction.

Customized Receiver

When plugin is enabled, the received message will be handled by customized receiver. The implementation of customized receiver is to parse and handle the received message.

In the customized receiver, the function customized_event_handler(dsrc_unit, event) must be implemented.

Configuration:

A plugin is composed of three components. The configuration file of the plugin should indicate the name of the components.

Here is an example of the configuration file:

# There are three section for this file: CustomizedEvent, CustomizedSender, CustomizedReceiver
# Each section has three options, DirectoryPath, FileName and ModuleName
[CustomizedEvent]
#DirectoryPath = /home/xuepeng/plugins/
DirectoryPath = ./
FileName = DSRC_Plugin_Lane_Event.py
ModuleName = DSRC_Plugin_Lane_Event

[CustomizedExecutor]
#DirectoryPath = /home/xuepeng/plugins/
DirectoryPath = ./
FileName = DSRC_Plugin_Lane_Executor.py
ModuleName = DSRC_Plugin_Lane_Executor

[CustomizedReceiver]
#DirectoryPath = /home/xuepeng/plugins/
DirectoryPath = ./
FileName = DSRC_Plugin_Lane_Receiver_New.py
ModuleName = DSRC_Plugin_Lane_Receiver_New

Examples:

There are a few examples of plugin implemented in the DSRC_Plugins module.