Search code examples
volttron

Add a new page to Volttron Central


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.


Solution

  • 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