Skip to content

Commit

Permalink
Merge branch 'release/3.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
craig8 committed Oct 21, 2015
2 parents bb4c485 + 6ff1fb0 commit 4546b0a
Show file tree
Hide file tree
Showing 19 changed files with 926 additions and 14 deletions.
2 changes: 1 addition & 1 deletion examples/DataPublisher/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@

setup(
name = package,
version = "0.1",
version = "3.0",
install_requires = ['volttron'],
packages = packages,
entry_points = {
Expand Down
11 changes: 11 additions & 0 deletions examples/ExampleSubscriber/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{

"agentid": "ExampleSubscriber",
"message": "hello",
"oat_point": "devices/Building/LAB/Device/OutsideAirTemperature",
"mixed_point": "devices/Building/LAB/Device/MixedAirTemperature",
"damper_point": "devices/Building/LAB/Device/DamperSignal",
"all_topic": "devices/Building/LAB/Device/all",
"query_point": "Building/LAB/Device/OutsideAirTemperature"

}
6 changes: 6 additions & 0 deletions examples/ExampleSubscriber/execreqs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"requirements":{
"cpu.bogomips": 100,
"memory.soft_limit_in_bytes": 200
}
}
76 changes: 76 additions & 0 deletions examples/ExampleSubscriber/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# -*- coding: utf-8 -*- {{{
# vim: set fenc=utf-8 ft=python sw=4 ts=4 sts=4 et:

# Copyright (c) 2015, Battelle Memorial Institute
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and documentation
# are those of the authors and should not be interpreted as representing
# official policies, either expressed or implied, of the FreeBSD
# Project.
#
# This material was prepared as an account of work sponsored by an
# agency of the United States Government. Neither the United States
# Government nor the United States Department of Energy, nor Battelle,
# nor any of their employees, nor any jurisdiction or organization that
# has cooperated in the development of these materials, makes any
# warranty, express or implied, or assumes any legal liability or
# responsibility for the accuracy, completeness, or usefulness or any
# information, apparatus, product, software, or process disclosed, or
# represents that its use would not infringe privately owned rights.
#
# Reference herein to any specific commercial product, process, or
# service by trade name, trademark, manufacturer, or otherwise does not
# necessarily constitute or imply its endorsement, recommendation, or
# favoring by the United States Government or any agency thereof, or
# Battelle Memorial Institute. The views and opinions of authors
# expressed herein do not necessarily state or reflect those of the
# United States Government or any agency thereof.
#
# PACIFIC NORTHWEST NATIONAL LABORATORY
# operated by BATTELLE for the UNITED STATES DEPARTMENT OF ENERGY
# under Contract DE-AC05-76RL01830

#}}}

from setuptools import setup, find_packages

#get environ for agent name/identifier
packages = find_packages('.')
package = packages[0]

setup(
name = package,
version = "3.0",
install_requires = ['volttron'],
packages = packages,
entry_points = {
'setuptools.installation': [
'eggsecutable = ' + package + '.subscriber_agent:main',
]
}
)

Empty file.
1 change: 1 addition & 0 deletions examples/ExampleSubscriber/subscriber/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
HEARTBEAT_PERIOD = 5
245 changes: 245 additions & 0 deletions examples/ExampleSubscriber/subscriber/subscriber_agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
# -*- coding: utf-8 -*- {{{
# vim: set fenc=utf-8 ft=python sw=4 ts=4 sts=4 et:
#
# Copyright (c) 2015, Battelle Memorial Institute
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and documentation are those
# of the authors and should not be interpreted as representing official policies,
# either expressed or implied, of the FreeBSD Project.
#

# This material was prepared as an account of work sponsored by an
# agency of the United States Government. Neither the United States
# Government nor the United States Department of Energy, nor Battelle,
# nor any of their employees, nor any jurisdiction or organization
# that has cooperated in the development of these materials, makes
# any warranty, express or implied, or assumes any legal liability
# or responsibility for the accuracy, completeness, or usefulness or
# any information, apparatus, product, software, or process disclosed,
# or represents that its use would not infringe privately owned rights.
#
# Reference herein to any specific commercial product, process, or
# service by trade name, trademark, manufacturer, or otherwise does
# not necessarily constitute or imply its endorsement, recommendation,
# r favoring by the United States Government or any agency thereof,
# or Battelle Memorial Institute. The views and opinions of authors
# expressed herein do not necessarily state or reflect those of the
# United States Government or any agency thereof.
#
# PACIFIC NORTHWEST NATIONAL LABORATORY
# operated by BATTELLE for the UNITED STATES DEPARTMENT OF ENERGY
# under Contract DE-AC05-76RL01830

#}}}

from __future__ import absolute_import

from datetime import datetime
import logging
import random
import sys

from volttron.platform.vip.agent import Agent, Core, PubSub, compat
from volttron.platform.agent import utils
from volttron.platform.messaging import headers as headers_mod




utils.setup_logging()
_log = logging.getLogger(__name__)

'''
Structuring the agent this way allows us to grab config file settings
for use in subscriptions instead of hardcoding them.
'''

def subscriber_agent(config_path, **kwargs):
config = utils.load_config(config_path)
oat_point= config.get('oat_point',
'devices/Building/LAB/Device/OutsideAirTemperature')
mixed_point= config.get('mixed_point',
'devices/Building/LAB/Device/MixedAirTemperature')
damper_point= config.get('damper_point',
'devices/Building/LAB/Device/DamperSignal')
all_topic = config.get('all_topic',
'devices/Building/LAB/Device/all')
query_point= config.get('query_point',
'Building/LAB/Device/OutsideAirTemperature')


class ExampleSubscriber(Agent):
'''
This agent demonstrates usage of the 3.0 pubsub service as well as
interfacting with the historian. This agent is mostly self-contained,
but requires the histoiran be running to demonstrate the query feature.
'''

def __init__(self, **kwargs):
super(ExampleSubscriber, self).__init__(**kwargs)

@Core.receiver('onsetup')
def setup(self, sender, **kwargs):
# Demonstrate accessing a value from the config file
self._agent_id = config['agentid']



@PubSub.subscribe('pubsub', all_topic)
def match_device_all(self, peer, sender, bus, topic, headers, message):
'''
This method subscribes to all points under a device then pulls out
the specific point it needs.
The first element of the list in message is a dictionairy of points
under the device. The second element is a dictionary of metadata for points.
'''

print("Whole message", message)

#The time stamp is in the headers
print('Date', headers['Date'])

#Pull out the value for the point of interest
print("Value", message[0]['OutsideAirTemperature'])

#Pull out the metadata for the point
print('Unit', message[1]['OutsideAirTemperature']['units'])
print('Timezone', message[1]['OutsideAirTemperature']['tz'])
print('Type', message[1]['OutsideAirTemperature']['type'])



@PubSub.subscribe('pubsub', oat_point)
def on_match_OAT(self, peer, sender, bus, topic, headers, message):
'''
This method subscribes to the specific point topic.
For these topics, the value is the first element of the list
in message.
'''

print("Whole message", message)
print('Date', headers['Date'])
print("Value", message[0])
print("Units", message[1]['units'])
print("TimeZone", message[1]['tz'])
print("Type", message[1]['type'])


@PubSub.subscribe('pubsub', '')
def on_match_all(self, peer, sender, bus, topic, headers, message):
''' This method subscibes to all topics. It simply prints out the
topic seen.
'''

print(topic)
#
# Demonstrate periodic decorator and settings access
@Core.periodic(10)
def lookup_data(self):
'''
This method demonstrates how to query the platform historian for data
This will require that the historian is already running on the platform.
'''

try:

result = self.vip.rpc.call(
#Send this message to the platform historian
#Using the reserved ID
'platform.historian',
#Call the query method on this agent
'query',
#query takes the keyword arguments of:
#topic, then optional: start, end, count, order
# start= "2015-10-14T20:51:56",
topic=query_point,
count = 20,
#RPC uses gevent and we must call .get(timeout=10)
#to make it fetch the result and tell
#us if there is an error
order = "FIRST_TO_LAST").get(timeout=10)
print('Query Result', result)
except Exception as e:
print ("Could not contact historian. Is it running?")
print(e)

@Core.periodic(10)
def pub_fake_data(self):
''' This method publishes fake data for use by the rest of the agent.
The format mimics the format used by VOLTTRON drivers.
This method can be removed if you have real data to work against.
'''

#Make some random readings
oat_reading = random.uniform(30,100)
mixed_reading = oat_reading + random.uniform(-5,5)
damper_reading = random.uniform(0,100)

# Create a message for all points.
all_message = [{'OutsideAirTemperature': oat_reading, 'MixedAirTemperature': mixed_reading,
'DamperSignal': damper_reading},
{'OutsideAirTemperature': {'units': 'F', 'tz': 'UTC', 'type': 'float'},
'MixedAirTemperature': {'units': 'F', 'tz': 'UTC', 'type': 'float'},
'DamperSignal': {'units': '%', 'tz': 'UTC', 'type': 'float'}
}]

#Create messages for specific points
oat_message = [oat_reading,{'units': 'F', 'tz': 'UTC', 'type': 'float'}]
mixed_message = [mixed_reading,{'units': 'F', 'tz': 'UTC', 'type': 'float'}]
damper_message = [damper_reading,{'units': '%', 'tz': 'UTC', 'type': 'float'}]

#Create timestamp
now = datetime.utcnow().isoformat(' ') + 'Z'
headers = {
headers_mod.DATE: now
}

#Publish messages
self.vip.pubsub.publish(
'pubsub', all_topic, headers, all_message)

self.vip.pubsub.publish(
'pubsub', oat_point, headers, oat_message)

self.vip.pubsub.publish(
'pubsub', mixed_point, headers, mixed_message)

self.vip.pubsub.publish(
'pubsub', damper_point, headers, damper_message)



return ExampleSubscriber(**kwargs)
def main(argv=sys.argv):
'''Main method called by the eggsecutable.'''
try:
utils.vip_main(subscriber_agent)
except Exception as e:
_log.exception('unhandled exception')


if __name__ == '__main__':
# Entry point for script
sys.exit(main())
2 changes: 1 addition & 1 deletion examples/ListenerAgent/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@

setup(
name = package + 'agent',
version = "0.1",
version = "3.0",
install_requires = ['volttron'],
packages = packages,
entry_points = {
Expand Down
2 changes: 1 addition & 1 deletion examples/SimpleForwarder/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@

setup(
name = package + 'agent',
version = "0.1",
version = "3.0",
install_requires = ['volttron', 'ply'],
packages = packages,
entry_points = {
Expand Down
Loading

0 comments on commit 4546b0a

Please sign in to comment.