-
Notifications
You must be signed in to change notification settings - Fork 218
RPC by example
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
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'])
- Platform Agent
- VOLTTRON Central Agent
- Platform Commands
- Platform Configuration
- [Platform Hardening Security Recommendations] (Linux-Platform-Hardening-Recommendations-for-VOLTTRON-users)
- ...
- [Building VOLTTRON] (Building-VOLTTRON)
- Example Agents
- Agent Development
- [Shortcut Scripts] (Scripts)
- [VOLTTRON Conventions] (Conventions)
- [sMAP Test Server] (sMAP-Test-Instance)
- [Design Discussions] (Design Discussions)
- VIP
- VIP - VOLTTRON Interconnect Protocol
- RPC by example
- VIP - Known Identities
- VIP - Authentication
- VIP - Authorization
- Protecting Pub/Sub Topics
- Setup Eclipse for VOLTTRON
- Deployment Walkthrough
- Forward Historian Walkthrough
- [Create New Historian Agent] (Developing-Historian-Agents)
- [Create New Driver Agent] (Develop-Driver-Agent)
- [Developing With Eclipse] (Eclipse)
- Migrations
- [2.x to 3.x Migration](2.x-to 3.x-Migration)
- 1.2 to 2.0 Migration
- [Deployment Recommendations](Recommendations for Deployments)
VOLTTRON Versions and Features
Transactional Network Platform Overview
- Established Topics
- Working with the Actuator Agent
- Logging
- [Multi-Node Communication] (MultiBuildingMessaging)
Information Exchange Standards