Class Action


  • public class Action
    extends java.lang.Object
     An 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 bulb
     
    	Map<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 
    
    
    • Constructor Summary

      Constructors 
      Constructor Description
      Action​(java.lang.String id, Thing thing, java.lang.String name)
      Initialize the object.
      Action​(java.lang.String id, Thing thing, java.lang.String name, com.google.gson.JsonObject input)
      Initialize the object.
    • Constructor Detail

      • Action

        public Action​(java.lang.String id,
                      Thing thing,
                      java.lang.String name)
        Initialize the object.
        Parameters:
        id - ID of this action
        thing - Thing this action belongs to
        name - 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 action
        thing - Thing this action belongs to
        name - Name of the action
        input - 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.
      • 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 subscribers
        reason - Custom reason to notify all subscribers