Package com.gears42.iot.webthing
Class Thing
- java.lang.Object
-
- com.gears42.iot.webthing.Thing
-
public abstract class Thing extends java.lang.Object
Thing class provides a Thing Description for a device, which includes thing's property, action and events. Property describes the available properties of a thing. Event is the kind of event that can be emitted by a device. To create a Thing: 1. Add the required properties of a thing. Example code snippet to create and add a property to a thing: Consider a light bulb, here the property indicates whether the light on or off
Map<String, Object> onDescription = new HashMap<String, Object>(); onDescription.put("@type", "OnOffProperty"); onDescription.put("title", "On/Off"); onDescription.put("type", "boolean"); onDescription.put("description", "Whether the light is turned on"); thing.addProperty(new Property(thing,"on",new Value(true),onDescription));
2. Add the available actions. 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
3. Add the available events. Example code snippet to create and add an event to a thing: Consider a light bulb which can emit an event of overheatingMap<String, Object> overheatedMetadata = new HashMap<String, Object>(); overheatedMetadata.put("description","The bulb has exceeded its safe operating temperature"); overheatedMetadata.put("type", "number"); overheatedMetadata.put("unit", "degree celsius"); thing.addAvailableEvent("overheated", overheatedMetadata);
-
-
Constructor Summary
Constructors Constructor Description Thing(java.lang.String name, java.lang.String description, java.lang.String deviceId, java.lang.String model)
Initializes the Thing object.Thing(java.lang.String name, java.util.List<java.lang.String> type, java.lang.String description, java.lang.String deviceId, java.lang.String model)
Initialize the Thing object.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
actionNotify(Action action)
Notify all subscribers of an action status change.void
addAvailableAction(java.lang.String name, java.util.Map<java.lang.String,java.lang.Object> metadata, java.lang.Class cls)
Add an available action.void
addAvailableEvent(java.lang.String name, java.util.Map<java.lang.String,java.lang.Object> metadata)
Add an available event.void
addEvent(Event event)
Add a new event and notify subscribers.void
addEventSubscriber(java.lang.String name, WebThingServer.ThingHandler.ThingWebSocket ws)
Add a new websocket subscriber to an event.void
addProperty(Property property)
Add a property to this thing.void
addSubscriber(WebThingServer.ThingHandler.ThingWebSocket ws)
Add a new websocket subscriber.com.google.gson.JsonObject
asThingDescription()
Return the thing state as a Thing Description.void
cleanAvailableCompliance(java.util.List<java.lang.String> compliances)
void
eventNotify(Event event)
Notify all subscribers of an event.Property
findProperty(java.lang.String propertyName)
Find a property by name.Action
getAction(java.lang.String actionName, java.lang.String actionId)
Get an action.com.google.gson.JsonArray
getActionDescriptions(java.lang.String actionName)
Get the thing's actions as a JSONArray.ComplianceEvent
getComplianceEvent(java.lang.String requestId)
java.lang.String
getContext()
Get the type context of the thing.java.lang.String
getDescription()
Get the description of the thing.java.lang.String
getDeviceId()
gives the id of the device/thing.com.google.gson.JsonArray
getEventDescriptions(java.lang.String eventName)
Get the thing's events as a JSONArray.java.lang.String
getHref()
Get this thing's href.java.lang.String
getModel()
Get the model name of the thing.java.lang.String
getName()
Get the name of the thing.com.google.gson.JsonObject
getProperties()
Get a mapping of all properties and their values.<T> T
getProperty(java.lang.String propertyName)
Get a property's value.com.google.gson.JsonObject
getPropertyDescriptions()
Get the thing's properties as a JSONObject.Thing
getThing()
Gives the current instance of the Thing.java.util.List<java.lang.String>
getType()
Get the type(s) of the thing.java.lang.String
getUiHref()
Get this thing's UI href.java.lang.String
getWsHref()
Get this thing's websocket href.boolean
hasProperty(java.lang.String propertyName)
Determine whether or not this thing has a given property.abstract boolean
isThingOnline()
Override with appropriate definition to update the Thing's property 'Thing in contact'.Action
performAction(java.lang.String actionName, com.google.gson.JsonObject input)
Perform an action on the thing.void
propertyNotify(Property property)
Notify all subscribers of a property change.boolean
removeAction(java.lang.String actionName, java.lang.String actionId)
Remove an existing action.void
removeAvailableCompliances()
void
removeComplianceEvent(java.lang.String requestId)
void
removeComplianceEventFromMain(java.lang.String requestId)
void
removeEventSubscriber(java.lang.String name, WebThingServer.ThingHandler.ThingWebSocket ws)
Remove a websocket subscriber from an event.void
removeProperty(Property property)
Remove a property from this thing.void
removeSubscriber(WebThingServer.ThingHandler.ThingWebSocket ws)
Remove a websocket subscriber.void
setDeviceId(java.lang.String deviceId)
Sets the deviceId.void
setHrefPrefix(java.lang.String prefix)
Set the prefix of any hrefs associated with this thing.void
setModel(java.lang.String model)
Set the model name of the thing.void
setName(java.lang.String name)
Set the name of the thing.<T> void
setProperty(java.lang.String propertyName, T value)
Set a property value.void
setUiHref(java.lang.String href)
Set the href of this thing's custom UI.void
setWsHref(java.lang.String href)
Set the href of this thing's websocket.abstract void
update(java.util.List<java.lang.String> properties)
Override with appropriate definition to update the Thing's properties with new values.
-
-
-
Constructor Detail
-
Thing
public Thing(java.lang.String name, java.lang.String description, java.lang.String deviceId, java.lang.String model)
Initializes the Thing object.- Parameters:
name
- The Thing's namedescription
- The Thing's descriptiondeviceId
- The deviceIdmodel
- Model name of Thing
-
Thing
public Thing(java.lang.String name, java.util.List<java.lang.String> type, java.lang.String description, java.lang.String deviceId, java.lang.String model)
Initialize the Thing object.- Parameters:
name
- The thing's nametype
- The thing's type(s)description
- Description of the thingdeviceId
- The deviceIdmodel
- Model name of Thing
-
-
Method Detail
-
getDeviceId
public java.lang.String getDeviceId()
gives the id of the device/thing.- Returns:
- The deviceId
-
setDeviceId
public void setDeviceId(java.lang.String deviceId)
Sets the deviceId.- Parameters:
deviceId
- Id of the device
-
asThingDescription
public com.google.gson.JsonObject asThingDescription()
Return the thing state as a Thing Description.- Returns:
- Current thing state.
-
getHref
public java.lang.String getHref()
Get this thing's href.- Returns:
- The href
-
getWsHref
public java.lang.String getWsHref()
Get this thing's websocket href.- Returns:
- The href.
-
setWsHref
public void setWsHref(java.lang.String href)
Set the href of this thing's websocket.- Parameters:
href
- The href
-
getUiHref
public java.lang.String getUiHref()
Get this thing's UI href.- Returns:
- The href.
-
setUiHref
public void setUiHref(java.lang.String href)
Set the href of this thing's custom UI.- Parameters:
href
- The href
-
setHrefPrefix
public void setHrefPrefix(java.lang.String prefix)
Set the prefix of any hrefs associated with this thing.- Parameters:
prefix
- The prefix
-
getName
public java.lang.String getName()
Get the name of the thing.- Returns:
- The name.
-
getModel
public java.lang.String getModel()
Get the model name of the thing.- Returns:
- The name.
-
getContext
public java.lang.String getContext()
Get the type context of the thing.- Returns:
- The context.
-
getType
public java.util.List<java.lang.String> getType()
Get the type(s) of the thing.- Returns:
- The type(s).
-
getDescription
public java.lang.String getDescription()
Get the description of the thing.- Returns:
- The description.
-
setName
public void setName(java.lang.String name)
Set the name of the thing.
-
setModel
public void setModel(java.lang.String model)
Set the model name of the thing.
-
getPropertyDescriptions
public com.google.gson.JsonObject getPropertyDescriptions()
Get the thing's properties as a JSONObject.- Returns:
- Properties, i.e. name: description.
-
getActionDescriptions
public com.google.gson.JsonArray getActionDescriptions(java.lang.String actionName)
Get the thing's actions as a JSONArray.- Parameters:
actionName
- Optional action name to get descriptions for- Returns:
- Action descriptions.
-
getEventDescriptions
public com.google.gson.JsonArray getEventDescriptions(java.lang.String eventName)
Get the thing's events as a JSONArray.- Parameters:
eventName
- Optional event name to get descriptions for- Returns:
- Event descriptions.
-
addProperty
public void addProperty(Property property)
Add a property to this thing.- Parameters:
property
- Property to add.
-
removeProperty
public void removeProperty(Property property)
Remove a property from this thing.- Parameters:
property
- Property to remove.
-
findProperty
public Property findProperty(java.lang.String propertyName)
Find a property by name.- Parameters:
propertyName
- Name of the property to find- Returns:
- Property if found, else null.
-
getProperty
public <T> T getProperty(java.lang.String propertyName)
Get a property's value.- Type Parameters:
T
- Type of the property value- Parameters:
propertyName
- Name of the property to get the value of- Returns:
- Current property value if found, else null.
-
getProperties
public com.google.gson.JsonObject getProperties()
Get a mapping of all properties and their values.- Returns:
- JSON object of propertyName -> value.
-
hasProperty
public boolean hasProperty(java.lang.String propertyName)
Determine whether or not this thing has a given property.- Parameters:
propertyName
- The property to look for- Returns:
- Indication of property presence.
-
setProperty
public <T> void setProperty(java.lang.String propertyName, T value) throws PropertyError
Set a property value.- Type Parameters:
T
- Type of the property value- Parameters:
propertyName
- Name of the property to setvalue
- Value to set- Throws:
PropertyError
- If value could not be set.
-
getAction
public Action getAction(java.lang.String actionName, java.lang.String actionId)
Get an action.- Parameters:
actionName
- Name of the actionactionId
- ID of the action- Returns:
- The requested action if found, else null.
-
addEvent
public void addEvent(Event event)
Add a new event and notify subscribers.- Parameters:
event
- The event that occurred.
-
addAvailableEvent
public void addAvailableEvent(java.lang.String name, java.util.Map<java.lang.String,java.lang.Object> metadata)
Add an available event.- Parameters:
name
- Name of the eventmetadata
- Event metadata, i.e. type, description, etc., as a Map
-
performAction
public Action performAction(java.lang.String actionName, com.google.gson.JsonObject input)
Perform an action on the thing.- Parameters:
actionName
- Name of the actioninput
- Any action inputs- Returns:
- The action that was created.
-
removeAction
public boolean removeAction(java.lang.String actionName, java.lang.String actionId)
Remove an existing action.- Parameters:
actionName
- name of the actionactionId
- ID of the action- Returns:
- Boolean indicating the presence of the action.
-
addAvailableAction
public void addAvailableAction(java.lang.String name, java.util.Map<java.lang.String,java.lang.Object> metadata, java.lang.Class cls)
Add an available action.- Parameters:
name
- Name of the actionmetadata
- Action metadata, i.e. type, description, etc., as a Mapcls
- Class to instantiate for this action
-
addSubscriber
public void addSubscriber(WebThingServer.ThingHandler.ThingWebSocket ws)
Add a new websocket subscriber.- Parameters:
ws
- The websocket
-
removeSubscriber
public void removeSubscriber(WebThingServer.ThingHandler.ThingWebSocket ws)
Remove a websocket subscriber.- Parameters:
ws
- The websocket
-
addEventSubscriber
public void addEventSubscriber(java.lang.String name, WebThingServer.ThingHandler.ThingWebSocket ws)
Add a new websocket subscriber to an event.- Parameters:
name
- Name of the eventws
- The websocket
-
removeEventSubscriber
public void removeEventSubscriber(java.lang.String name, WebThingServer.ThingHandler.ThingWebSocket ws)
Remove a websocket subscriber from an event.- Parameters:
name
- Name of the eventws
- The websocket
-
removeAvailableCompliances
public void removeAvailableCompliances()
-
cleanAvailableCompliance
public void cleanAvailableCompliance(java.util.List<java.lang.String> compliances)
-
removeComplianceEventFromMain
public void removeComplianceEventFromMain(java.lang.String requestId)
-
removeComplianceEvent
public void removeComplianceEvent(java.lang.String requestId)
-
getComplianceEvent
public ComplianceEvent getComplianceEvent(java.lang.String requestId)
-
propertyNotify
public void propertyNotify(Property property)
Notify all subscribers of a property change.- Parameters:
property
- The property that changed
-
actionNotify
public void actionNotify(Action action)
Notify all subscribers of an action status change.- Parameters:
action
- The action whose status changed
-
eventNotify
public void eventNotify(Event event)
Notify all subscribers of an event.- Parameters:
event
- The event that occurred
-
update
public abstract void update(java.util.List<java.lang.String> properties)
Override with appropriate definition to update the Thing's properties with new values.- Parameters:
properties
- list of properties that needs to be updated.
-
isThingOnline
public abstract boolean isThingOnline()
Override with appropriate definition to update the Thing's property 'Thing in contact'. The property 'Thing in contact' specifies whether the connector is able to contact the thing currently.'
-
getThing
public Thing getThing()
Gives the current instance of the Thing.- Returns:
- The current instance of the Thing
-
-