Skip to content
Bob Barcklay edited this page Apr 8, 2016 · 6 revisions

Note: This only applies to VOLTTRON 3.x. RPC is not implemented in earlier versions

VOLTTRON RPC calls by example

What easier way to demonstrate RPC but by an example which can be copied and pasted into a module or adapted for use directly in an interpreter.

'''Example of how to do RPC without subclassing an agent.

Basic example:

    import gevent
    from volttron.platform.vip.agent import Agent
    agent = Agent(address=vip_address)
    gevent.spawn(agent.core.run).join(0)
    agent.vip.rpc.call(peer, method, args, kwargs).get(timeout=10)

This module contains a working example that queries the control service.
'''

from __future__ import absolute_import, print_function

import os
import sys

import gevent

from volttron.platform.jsonrpc import RemoteError
from volttron.platform.vip.agent import Agent


# Connect to platform VIP socket
home = os.environ.get('VOLTTRON_HOME', '~/.volttron/run/vip.socket')
abstract = sys.platform.startswith('linux')
path = os.path.expanduser(os.path.expandvars(home))
vip_address = 'ipc://%s%s' % ('@' if abstract else '', path)
agent = Agent(address=vip_address)

# Start agent loop in new Greenlet and yield to it to allow it to connect
greenlet = gevent.spawn(agent.core.run)
greenlet.join(0)   # or gevent.sleep(0)

# The function 'list_agents' is being called on the remote agent
# with the identity 'control' with no parameters.
asyncresult = agent.vip.rpc.call('control', 'list_agents')
print('Agent list:', asyncresult.get(timeout=10))

# The function 'list_agents' is being called on the remote agent
# with the identity 'control' with sequential arguments.
asyncresult = agent.vip.rpc.call(
    'control', 'agent_status', *['01234567-89ab-cdef-0123-456789abcdef'])
print('Agent status:', asyncresult.get(timeout=10))

try:
    # The function 'tag_agent' is being called on the remote agent
    # with the identity 'control' and passing both sequential and key
    # word arguments for that function.
    asyncresult = agent.vip.rpc.call(
        'control', 'tag_agent',
        *['01234567-89ab-cdef-0123-456789abcdef'], **{'tag': 'one'})
    asyncresult.get(timeout=10)
except RemoteError as exc:
    # Print remote traceback
    print(exc.exc_info['exc_tb'])

Wiki Home

Quick Start Guide

Getting VOLTTRON

VOLTTRON Community

VOLTTRON Core Services

Historians

Drivers

Instance Management

Applications
  • ...
Examples
Developers
HOWTOS

VOLTTRON Versions and Features

Transactional Network Platform Overview

Platform Services

Volttron Restricted

Information Exchange Standards

FAQ

Project Home

Clone this wiki locally