public abstract class LhingsDevice extends Object implements Runnable
Action
,
Event
and
StatusComponent
must be implemented to
define the actions,
events and status components of the device.
The frequency at which the loop()
method is executed can be set
using the method setLoopFrequency(float frequency)
.
This class also provides convenience methods to interact with and obtain
information about other devices that belong to the same Lhings account as
this one. See methods getDevices()
, requestAction()
, storeStatus()
, getStatus()
, and
statusRetrieve()
.Modifier and Type | Field and Description |
---|---|
protected static org.apache.log4j.Logger |
log |
protected static Properties |
uuids |
Constructor and Description |
---|
LhingsDevice(String username,
String password,
int port,
String deviceName)
Creates a new device in Lhings associated to the account with the given
username and password.
|
LhingsDevice(String username,
String password,
String deviceName)
Creates a new device in Lhings associated to the account with the given
username and password.
|
Modifier and Type | Method and Description |
---|---|
String |
apiKey()
Returns the Api-Key the device is using to connect with Lhings.
|
List<com.lhings.java.model.Device> |
getDevices()
This method returns a list of all the other devices that belong to the
same account as the calling device.
|
float |
getLoopFrequency()
Returns the number of times per second the method
loop() is
executed by the SDK. |
String |
getManufacturer()
Override this method if you want to specify your own manufacturer.
|
String |
getModelName()
Override this method if you want to specify your own model name.
|
String |
getName()
Returns the name of the device.
|
int |
getPort()
Used to retrieve the port used by this device to communicate with Lhings.
|
String |
getSerialNumber()
Override this method if you want to specify your own serial number
Default serial number is "000001"
|
Map<String,Object> |
getStatus(String uuid)
Retrieves the status of the given device.
|
String |
getType()
Override this method if you want to specify your own device type Default
device type is "javavirtualdevice"
|
String |
getUsername()
Returns the username of the account to which the device belongs.
|
String |
getVersion()
Override this method if you want to specify your own device version
Default device version is "1"
|
Boolean |
isDeviceConnected()
Returns true if the device was successful in setting up a permanent
communication channel with the Lhings server.
|
Boolean |
isLogged()
Returns true if the username and password combination provided was
correct and the device was able to start session in Lhings.
|
void |
logout()
This method is called to close the session with lhings.
|
abstract void |
loop()
Implementations must override this method and put in it the device logic.
|
String |
requestAction(String uuid,
String actionName,
Map<String,Object> arguments)
Request another device to perform one of its actions.Both the action name
and argument names must have been declared by the other device in its
descriptor file.
|
void |
run() |
protected void |
sendEvent(String name)
Sends an event without payload to Lhings.
|
protected void |
sendEvent(String name,
String payload)
Sends an event with payload to Lhings.
|
void |
setLoopFrequency(float loopFrequency)
Sets the number of times per second the method
loop() is
executed by the SDK. |
abstract void |
setup()
Implementation must override this method and put in it all the
initialization code required by device.
|
void |
start()
Start the device.
|
void |
stop()
Stops the device
|
void |
storeStatus()
Stores the value of all the status components of this device in Lhings.
|
String |
uuid()
Returns the unique UUID Lhings assigned to this device.
|
protected static final org.apache.log4j.Logger log
protected static final Properties uuids
public LhingsDevice(String username, String password, String deviceName) throws IOException, LhingsException
username
- Username of the Lhings account.password
- Password of the account.deviceName
- The name of the device.IOException
- If a network or connectivity error occurs.LhingsException
public LhingsDevice(String username, String password, int port, String deviceName) throws IOException, LhingsException
username
- Username of the Lhings account.password
- Password of the account.port
- The port the device will use to connect to the Internet for
non-http communications.deviceName
- The name of the device.IOException
- If a network or connectivity error occurs.LhingsException
public void start()
public void stop()
protected void sendEvent(String name)
name
- The name of the event. If no such event is defined for this
device no event is sent but a warning is shown in the log of the
application.protected void sendEvent(String name, String payload)
name
- The name of the event. If no such event is defined for this
device no event is sent but a warning is shown in the log of the
application.payload
- The payload of the event.public Boolean isLogged()
public Boolean isDeviceConnected()
public String getManufacturer()
public String getModelName()
public String getSerialNumber()
public String getType()
public String getVersion()
public void logout()
public String apiKey()
public String uuid()
public abstract void setup()
public abstract void loop()
public int getPort()
public String getName()
public String getUsername()
public float getLoopFrequency()
loop()
is
executed by the SDK.public void setLoopFrequency(float loopFrequency)
loop()
is
executed by the SDK. The SDK will do its best to execute the
loop()
at the frequency requested, but it does not guarantee
neither precision nor constant frequency. The default value is 10 Hz.public List<com.lhings.java.model.Device> getDevices() throws LhingsException, IOException
IOException
LhingsException
public Map<String,Object> getStatus(String uuid) throws DeviceDoesNotExistException, DeviceUnreachableException, UnauthorizedException, LhingsException, IOException
uuid
- The uuid of the device whose status is to be retrieved.DeviceDoesNotExistException
- If no device exists with that uuid.DeviceUnreachableException
- If the device could not be contacted
over the internet.UnauthorizedException
- If the device exists but the calling device
is not authorized to access its status.LhingsException
IOException
public void storeStatus() throws IOException, LhingsException
LhingsException
IOException
public String requestAction(String uuid, String actionName, Map<String,Object> arguments) throws IOException, LhingsException
uuid
- The uuid of the device that will perform the action.actionName
- The name of the action to be performed.arguments
- A Map with the name of the arguments of the action as
keys and the values of those arguments as values.IOException
LhingsException
Copyright © 2014. All rights reserved.