public class LegoService extends java.lang.Object implements IOCallbackListener
ServiceCallbackListener
using registerCallbackListener(dk.lego.devicesdk.services.ServiceCallbackListener)
to be notified when a service receives an updated value.Modifier and Type | Field and Description |
---|---|
protected dk.lego.devicesdk.services.LegoServiceCallbackHelper |
callbackHelper |
protected ConnectInfo |
connectInfo
General info about the connected service
|
protected InputFormat |
inputFormat |
protected IO |
io |
Modifier | Constructor and Description |
---|---|
protected |
LegoService(ConnectInfo connectInfo,
IO io) |
Modifier and Type | Method and Description |
---|---|
void |
addValidDataFormat(DataFormat dataFormat)
Add a new valid data format
|
static LegoService |
createService(ConnectInfo connectInfo,
IO io) |
void |
didReceiveInputFormat(IO io,
InputFormat inputFormat) |
void |
didReceiveValueData(IO io,
byte[] valueData) |
ConnectInfo |
didRequestConnectInfo(IO io) |
boolean |
equals(java.lang.Object o) |
ConnectInfo |
getConnectInfo() |
InputFormat |
getDefaultInputFormat()
The default input format that will be uploaded to the device for this service upon discovery of the service.
|
LegoDevice |
getDevice() |
float |
getFloatFromData(byte[] data)
The value data representation from the service as a floating-point number.
|
InputFormat |
getInputFormat()
The current input format for this service.
|
int |
getInputFormatMode()
Convenience method that will return the mode of the current inputFormat.
|
java.lang.Integer |
getIntegerFromData(byte[] data)
The value data representation from the service as an integer.
|
IO |
getIo() |
java.lang.Number |
getNumberFromValueData()
The latest received value from the service as a
Number . |
java.lang.Number |
getNumberFromValueData(byte[] data)
The value representation of data from the service as an
Number . |
java.util.List<java.lang.Number> |
getNumbersFromValueDataSet()
The latest received value from the service as an array of
Number . |
java.util.List<java.lang.Number> |
getNumbersFromValueDataSet(byte[] dataSet)
The value data representation from the service as an array of
Number . |
java.lang.String |
getServiceName() |
java.util.HashSet<DataFormat> |
getValidDataFormats()
The data formats that this service may use to parse received data.
|
float |
getValueAsFloat()
The latest received value from the service as an integer.
|
long |
getValueAsInteger()
The latest received value from the service as an integer.
|
byte[] |
getValueData()
The latest received value from the service as raw data
|
void |
handleUpdatedInputFormat(InputFormat inputFormat) |
void |
handleUpdatedValueData(byte[] valueData)
Handle new value data from device (may be overwritten in sub-classes)
|
int |
hashCode() |
boolean |
isInternalService() |
void |
registerCallbackListener(ServiceCallbackListener listener)
Add a callback listener to receive service updates.
|
void |
removeValidDataFormat(DataFormat dataFormat)
Remove a valid data format
|
void |
sendReadValueRequest()
If the notifications is disabled for the service in the inputFormat through
InputFormat.notificationsEnabled
you will have to use this method to request an updated value for the service. |
void |
sendResetStateRequest()
This will send a reset command to the Device for this service.
|
void |
setDevice(LegoDevice device) |
void |
unregisterCallbackListener(ServiceCallbackListener listener)
Remove a callback listener
|
void |
updateCurrentInputFormatWithNewMode(int newMode)
Sends and updates input format with newMode for this service to the device.
|
void |
updateInputFormat(InputFormat newFormat)
Send an updated input format for this service to the device.
|
void |
writeData(byte[] data)
Will send data to the IO backed by this service.
|
protected ConnectInfo connectInfo
protected IO io
protected InputFormat inputFormat
protected dk.lego.devicesdk.services.LegoServiceCallbackHelper callbackHelper
protected LegoService(ConnectInfo connectInfo, IO io)
public static LegoService createService(ConnectInfo connectInfo, IO io)
public void setDevice(LegoDevice device)
public LegoDevice getDevice()
public IO getIo()
public boolean isInternalService()
public ConnectInfo didRequestConnectInfo(IO io)
didRequestConnectInfo
in interface IOCallbackListener
public ConnectInfo getConnectInfo()
public java.lang.String getServiceName()
public InputFormat getDefaultInputFormat()
InputFormat
of the servicepublic InputFormat getInputFormat()
public void updateInputFormat(InputFormat newFormat)
ServiceCallbackListener.didUpdateInputFormat(dk.lego.devicesdk.services.LegoService, dk.lego.devicesdk.input_output.InputFormat, dk.lego.devicesdk.input_output.InputFormat)
.newFormat
- The input format to send to the device.public int getInputFormatMode()
public void updateCurrentInputFormatWithNewMode(int newMode)
newMode
- The new service modepublic java.util.HashSet<DataFormat> getValidDataFormats()
validDataFormats
that matches the
InputFormat.unit
and InputFormat.unit
.
If a match is found the DataFormat is used to parse the received data correctly (as a float, integer).
The known service types such as TiltSensor
and MotionSensor
etc. comes with a set
of predefined validDataFormats
and you do not need to add valid data formats yourself.
However, if you wish to use the LegoService with a service type unknown to the SDK
you must add the valid data formats using the addValidDataFormat(dk.lego.devicesdk.input_output.DataFormat)
method.public void addValidDataFormat(DataFormat dataFormat)
dataFormat
- The data format to add (see getValidDataFormats()
)public void removeValidDataFormat(DataFormat dataFormat)
dataFormat
- The data format to remove (see getValidDataFormats()
)public byte[] getValueData()
public java.lang.Number getNumberFromValueData()
Number
.
If the inputFormat
specifies a data set with more than one element, the first element is returned.
If no valid data format is found to parse the data nil is returned, see getValidDataFormats()
.public java.lang.Number getNumberFromValueData(byte[] data)
Number
.
If the inputFormat
specifies a data set with more than one element, the first element is returned.
If no valid data format is found to parse the data nil is returned, see getValidDataFormats()
.data
- The data to parse.public java.util.List<java.lang.Number> getNumbersFromValueDataSet()
Number
.
This makes sense if the data set count of the inputFormat
is higher than 1. For example, if the data set
count is two the valueData
consists of two numbers.
If no valid data format is found to parse the data nil is returned, see getValidDataFormats()
.public java.util.List<java.lang.Number> getNumbersFromValueDataSet(byte[] dataSet)
Number
.
This makes sense if the data set count of the inputFormat
is higher than 1. For example, if the data set
count is two the valueData
consists of two numbers.
If no valid data format is found to parse the data nil is returned, see getValidDataFormats()
.dataSet
- The data to parse.public float getFloatFromData(byte[] data)
data
- The data to parse.public java.lang.Integer getIntegerFromData(byte[] data)
data
- The data to parse.public long getValueAsInteger()
public float getValueAsFloat()
public void sendReadValueRequest()
InputFormat.notificationsEnabled
you will have to use this method to request an updated value for the service.
The updated value will be delivered through the delegate method ServiceCallbackListener.didUpdateValueData(dk.lego.devicesdk.services.LegoService, byte[], byte[])
.public void sendResetStateRequest()
public void writeData(byte[] data)
data
- The data to write.public void didReceiveInputFormat(IO io, InputFormat inputFormat)
didReceiveInputFormat
in interface IOCallbackListener
public void handleUpdatedInputFormat(InputFormat inputFormat)
public void didReceiveValueData(IO io, byte[] valueData)
didReceiveValueData
in interface IOCallbackListener
public void handleUpdatedValueData(byte[] valueData) throws LDSDKError
valueData
- received value dataLDSDKError
- if received data could not be verifiedpublic void registerCallbackListener(ServiceCallbackListener listener)
listener
- The delegate to addpublic void unregisterCallbackListener(ServiceCallbackListener listener)
listener
- The delegate to removepublic boolean equals(java.lang.Object o)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object