Add a new page to Volttron Central

孤街醉人 提交于 2019-12-12 01:50:09

问题


I have a standalone HTML page with jQuery. The jQuery is used to do AJAX call to the Python backend. I need to integrate it with Volttron Central. I have looked at the documentation but there is no section talking about this. I think it would be nice to have this kind of info in the doc.

My current approach is to convert the backend Python to be a Volttron agent but I don't know how to integrate the front end HTML page with VC.

Any suggestion where to start? Thanks.


回答1:


When you have an agent that is going to register its own endpoint you should do that during the onstart signal. The following was extracted from the volttron central agent. It shows how to register an endpoint that is dynamic(uses volttron rpc as the endpoint) as well as static(where the html is simply served). I have removed the un-necessary bits for this example.

onstart volttron central code

For clarity MASTER_WEB and VOLTTRON_CENTRAL are unique identifiers for those specific agents running on the volttron instance.

@Core.receiver('onstart')
def _starting(self, sender, **kwargs):
    """ Starting of the platform
    :param sender:
    :param kwargs:
    :return:
    """

    ...

    # Registers dynamic route.
    self.vip.rpc.call(MASTER_WEB, 'register_agent_route',
                      r'^/jsonrpc.*',
                      self.core.identity,
                      'jsonrpc').get(timeout=30)

    # Registers static route.
    self.vip.rpc.call(MASTER_WEB, 'register_path_route', VOLTTRON_CENTRAL,
                      r'^/.*', self._webroot).get(timeout=30)

Since you added the route onstart you should also remove it when the agent is stopped. onstop referenced code

@Core.receiver("onstop")
def stopping(self, sender, **kwargs):
    '''
    Release subscription to the message bus because we are no longer able
    to respond to messages now.
    '''
    try:
        # unsubscribes to all topics that we are subscribed to.
        self.vip.pubsub.unsubscribe(peer='pubsub', prefix=None, callback=None)
    except KeyError:
        # means that the agent didn't start up properly so the pubsub
        # subscriptions never got finished.
        pass


来源:https://stackoverflow.com/questions/38469146/add-a-new-page-to-volttron-central

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!