Action input types are defined when creating the thing itself. I’ve included an example thing below with two actions, one which accepts an object input, and another which accepts a numeric input.
from webthing import Action, SingleThing, Thing, WebThingServer
import json
import uuid
class ObjectAction(Action):
def __init__(self, thing, input_):
Action.__init__(self,
uuid.uuid4().hex,
thing,
'objectAction',
input_=input_)
def perform_action(self):
print('ObjectAction triggered: input={}'
.format(json.dumps(self.input)))
class ValueAction(Action):
def __init__(self, thing, input_):
Action.__init__(self,
uuid.uuid4().hex,
thing,
'valueAction',
input_=input_)
def perform_action(self):
print('ValueAction triggered: input={}'.format(self.input))
def make_thing():
thing = Thing('My Thing', [], 'A thing')
thing.add_available_action(
'objectAction',
{
'input': {
'type': 'object',
'required': [
'value1',
'value2',
],
'properties': {
'value1': {
'type': 'integer',
'minimum': 0,
'maximum': 100,
},
'value2': {
'type': 'boolean',
},
},
},
},
ObjectAction)
thing.add_available_action(
'valueAction',
{
'input': {
'type': 'number',
'minimum': -50,
'maximum': 50,
},
},
ValueAction)
return thing
def run_server():
thing = make_thing()
server = WebThingServer(SingleThing(thing), port=8888)
try:
server.start()
except KeyboardInterrupt:
server.stop()
if __name__ == '__main__':
run_server()
To trigger objectAction
:
curl \
-X POST \
--data '{"objectAction": {"input": {"value1": 25, "value2": false}}}' \
http://localhost:8888/actions
To trigger valueAction
:
curl \
-X POST \
--data '{"valueAction": {"input": 10}}' \
http://localhost:8888/actions