Package com.gears42.iot.webthing
Class Action
- java.lang.Object
-
- com.gears42.iot.webthing.Action
-
public class Action extends java.lang.ObjectAn action is a function which can be performed on a device/thing. An action includes: 1. A title (A name for the action) 2. A description (A short description on the action) 3. A links array (An array of links linking to one or more representations of an Action resource, each with an implied default rel=action) 4. An input object with: a. A primitive type (Type of the input data) b. A semantic @type (a string identifying a type from the linked context) c. A unit (SI unit of the input data) d. A minimum and maximum (Range of the input data) e. multipleOf (If input data is of numeric type it should be the multiple of this) Example code snippet to create and add an action to a thing: Consider a light bulb, here the action is to turn on/off the bulbMap<String, Object> actionMetadata= new HashMap<String, Object>(); Map<String, Object> actionInput= new HashMap<String, Object>(); Map<String, Object> actionProperties= new HashMap<String, Object>(); Map<String, Object> actionStatus= new HashMap<String, Object>(); actionStatus.put("type", "boolean"); actionProperties.put("on", actionStatus); actionInput.put("type", "object"); actionInput.put("required", Arrays.asList(new String[]{"on"})); actionInput.put("properties", actionProperties); actionMetadata.put("label", "on/off"); actionMetadata.put("description", "on/off a bulb"); actionMetadata.put("input", actionInput); this.addAvailableAction("on", actionMetadata,OnAction.class);//The code to perform the action can be defined in OnAction class
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description com.google.gson.JsonObjectasActionDescription()Get the action description.voidcancel()Override this with the code necessary to cancel the action.voidfinish()Finish performing the action.java.lang.StringgetHref()Get this action's href.java.lang.StringgetId()Get this action's ID.com.google.gson.JsonObjectgetInput()Get the inputs for this action.java.lang.StringgetName()Get this action's name.ActionResponse.ACTION_STATUSgetStatus()Get this action's status.ThinggetThing()Get the thing associated with this action.java.lang.StringgetTimeCompleted()Get the time the action was completed.java.lang.StringgetTimeRequested()Get the time the action was requested.voidnotifyActionStatus(ActionResponse.ACTION_STATUS status, java.lang.String reason)Notify to all subscribers with custom status and reasonActionResponseperformAction(ActionResponse actionResponse)Override this with the code necessary to perform the action.voidsetHrefPrefix(java.lang.String prefix)Set the prefix of any hrefs associated with this action.voidstart()Start performing the action.
-
-
-
Constructor Detail
-
Action
public Action(java.lang.String id, Thing thing, java.lang.String name)Initialize the object.- Parameters:
id- ID of this actionthing- Thing this action belongs toname- Name of the action
-
Action
public Action(java.lang.String id, Thing thing, java.lang.String name, com.google.gson.JsonObject input)Initialize the object.- Parameters:
id- ID of this actionthing- Thing this action belongs toname- Name of the actioninput- Any action inputs
-
-
Method Detail
-
asActionDescription
public com.google.gson.JsonObject asActionDescription()
Get the action description.- Returns:
- Description of the action as a JSONObject.
-
setHrefPrefix
public void setHrefPrefix(java.lang.String prefix)
Set the prefix of any hrefs associated with this action.- Parameters:
prefix- The prefix
-
getId
public java.lang.String getId()
Get this action's ID.- Returns:
- The ID.
-
getName
public java.lang.String getName()
Get this action's name.- Returns:
- The name.
-
getHref
public java.lang.String getHref()
Get this action's href.- Returns:
- The href.
-
getStatus
public ActionResponse.ACTION_STATUS getStatus()
Get this action's status.- Returns:
- The status.
-
getThing
public Thing getThing()
Get the thing associated with this action.- Returns:
- The thing.
-
getTimeRequested
public java.lang.String getTimeRequested()
Get the time the action was requested.- Returns:
- The time.
-
getTimeCompleted
public java.lang.String getTimeCompleted()
Get the time the action was completed.- Returns:
- The time.
-
getInput
public com.google.gson.JsonObject getInput()
Get the inputs for this action.- Returns:
- The inputs.
-
start
public void start()
Start performing the action.
-
performAction
public ActionResponse performAction(ActionResponse actionResponse)
Override this with the code necessary to perform the action.- Parameters:
actionResponse- ActionResponse have current status and reason of action. Developer have to return this object with updated status of action.
-
cancel
public void cancel()
Override this with the code necessary to cancel the action.
-
finish
public void finish()
Finish performing the action.
-
notifyActionStatus
public void notifyActionStatus(ActionResponse.ACTION_STATUS status, java.lang.String reason)
Notify to all subscribers with custom status and reason- Parameters:
status- Custom status(status except created, pending and completed) to notify all subscribersreason- Custom reason to notify all subscribers
-
-