T-Res API demo

Create processing function


In [11]:
import django
django.setup()
from pyot.models import *
from pyot.tres.tresApp import *

In [12]:
halve = """
from tres_pymite import *
i = getInputList()
print "list:",
print len(i), i
if len(i) != 0:
  a = sum(i) / len(i)
  setOutput(a)
  print a
"""
t1 = TResPF.fromSource(halve, 'mmm')

rin = Resource.objects.filter(uri='/light', host__network__hostname='cooja@pyot')
rout = Resource.objects.filter(uri='/actuators/leds', host__active=True)
print rin, rout

tresTask = TResTask(TresPf=t1, inputS=rin, output=rout, period=10)
print tresTask


r = Resource.objects.get(uri='/tasks')
print r.host, '    is a t-res node'
resp = tresTask.deploy(r)
print resp
#tresTask.emulate(120)


Validating /media/andrea/ssd1/tmpPyotMerge/pyot/pyot/media/scripts/mmm.py ...
[<Resource: aaaa::200:0:0:4 - /light>, <Resource: aaaa::200:0:0:3 - /light>] [<Resource: aaaa::200:0:0:4 - /actuators/leds>, <Resource: aaaa::200:0:0:3 - /actuators/leds>]
Pf=mmm, inputs=[<Resource: aaaa::200:0:0:4 - /light>, <Resource: aaaa::200:0:0:3 - /light>], output=[<Resource: aaaa::200:0:0:4 - /actuators/leds>, <Resource: aaaa::200:0:0:3 - /actuators/leds>]
aaaa::200:0:0:2     is a t-res node
first put result = 2.01
PF put result = 2.04
OD put result = 2.04 coap://[aaaa::200:0:0:4]/actuators/leds
OD put result = 2.04 coap://[aaaa::200:0:0:3]/actuators/leds
IS put result = 2.04 coap://[aaaa::200:0:0:4]/light
IS put result = 2.04 coap://[aaaa::200:0:0:3]/light
1 - Tres Task Pf=mmm, inputs=[<Resource: aaaa::200:0:0:4 - /light>, <Resource: aaaa::200:0:0:3 - /light>], output=[<Resource: aaaa::200:0:0:4 - /actuators/leds>, <Resource: aaaa::200:0:0:3 - /actuators/leds>] Installed

Start Tres Task


In [13]:
r = tresTask.start()
print r


aaaa::200:0:0:2 - /tasks Task now running 2.05
2.05 - Task now running

Get the last output


In [14]:
lo = tresTask.getLastOutput()
r = lo.GET()
print r


---------------------------------------------------------------------------
MultipleObjectsReturned                   Traceback (most recent call last)
<ipython-input-14-29387f2f2849> in <module>()
----> 1 lo = tresTask.getLastOutput()
      2 r = lo.GET()
      3 print r

/media/andrea/ssd1/tmpPyotMerge/pyot/pyot/tres/tresApp.py in getLastOutput(self)
    110     def getLastOutput(self):
    111         t = self.getTaskObject()
--> 112         return t.getLastOutput()
    113 
    114     def getInputResource(self):

/media/andrea/ssd1/tmpPyotMerge/pyot/pyot/models/tres.pyc in getLastOutput(self)
    344         last_output = Resource.objects.get(host=self.TResResource.host,
    345                                            uri='/tasks/' +
--> 346                                            self.pf.name + '/lo')
    347         return last_output
    348 

/media/andrea/ssd1/tmpPyotMerge/pyot/venv/local/lib/python2.7/site-packages/django/db/models/manager.pyc in manager_method(self, *args, **kwargs)
    125         def create_method(name, method):
    126             def manager_method(self, *args, **kwargs):
--> 127                 return getattr(self.get_queryset(), name)(*args, **kwargs)
    128             manager_method.__name__ = method.__name__
    129             manager_method.__doc__ = method.__doc__

/media/andrea/ssd1/tmpPyotMerge/pyot/venv/local/lib/python2.7/site-packages/django/db/models/query.pyc in get(self, *args, **kwargs)
    336         raise self.model.MultipleObjectsReturned(
    337             "get() returned more than one %s -- it returned %s!" %
--> 338             (self.model._meta.object_name, num)
    339         )
    340 

MultipleObjectsReturned: get() returned more than one Resource -- it returned 2!

Check the content of is and od


In [15]:
Is = tresTask.getInputSource()
r = Is.GET()
print r

Od = tresTask.getOutputDestination()
r = Od.GET()
print r


---------------------------------------------------------------------------
MultipleObjectsReturned                   Traceback (most recent call last)
<ipython-input-15-d42dfca51605> in <module>()
----> 1 Is = tresTask.getInputSource()
      2 r = Is.GET()
      3 print r
      4 
      5 Od = tresTask.getOutputDestination()

/media/andrea/ssd1/tmpPyotMerge/pyot/pyot/tres/tresApp.py in getInputSource(self)
    118     def getInputSource(self):
    119         t = self.getTaskObject()
--> 120         return t.getInputSource()
    121 
    122     def getOutputDestination(self):

/media/andrea/ssd1/tmpPyotMerge/pyot/pyot/models/tres.pyc in getInputSource(self)
    362         """
    363         _is = Resource.objects.get(host=self.TResResource.host,
--> 364                                    uri='/tasks/' + self.pf.name + '/is')
    365         return _is
    366 

/media/andrea/ssd1/tmpPyotMerge/pyot/venv/local/lib/python2.7/site-packages/django/db/models/manager.pyc in manager_method(self, *args, **kwargs)
    125         def create_method(name, method):
    126             def manager_method(self, *args, **kwargs):
--> 127                 return getattr(self.get_queryset(), name)(*args, **kwargs)
    128             manager_method.__name__ = method.__name__
    129             manager_method.__doc__ = method.__doc__

/media/andrea/ssd1/tmpPyotMerge/pyot/venv/local/lib/python2.7/site-packages/django/db/models/query.pyc in get(self, *args, **kwargs)
    336         raise self.model.MultipleObjectsReturned(
    337             "get() returned more than one %s -- it returned %s!" %
--> 338             (self.model._meta.object_name, num)
    339         )
    340 

MultipleObjectsReturned: get() returned more than one Resource -- it returned 2!

Start Tres Task


In [16]:
r = tresTask.stop()
print r


aaaa::200:0:0:2 - /tasks Task now halted 2.05
2.05 - Task now halted

Uninstall the task


In [17]:
r = tresTask.uninstall()
print r


1 - Task mmm uninstalled

In [ ]: