public class GenericService extends LegoService
GenericService
is a 'tagging' interface and does not offer any extra methods or properties other than those available
from its parent class LegoService
.
As opposed to the known service types (tilt, motion, etc) a generic service does not have a predefined default input format (@see LegoService.getDefaultInputFormat()).
Therefore, when the SDK discovers a service with a type it does not recognize it does not automatically send an InputFormat
to the LegoDevice
. Without a configured input format the Device will not send any updates for the SDK when the value readings of the sensor
value changes for that service.
To receive IO value reading updates you must create and send an InputFormat to the device to be used for the service. You may look for
inspiration on how to do this in one of the concrete service classes, like the MotionSensor
or VoltageSensor
. Below an example
is given on how you could configure and use a new Temperature Sensor yet unknown to the SDK.
if (service instanceof GenericService && service.getConnectInfo().getType() == TEMPERATURE_SENSOR_TYPE_NUMBER) {
GenericService temperatureSensor = (GenericService) service;
// The temperature sensor is yet unknown to the SDK (there is no TemperatureSensor class) so we need to configure
// the service ourselves.
// As a generic sensor does not how a defaultInputFormat is defined
// we must create one and send it to the device.
// Look in the documentation for the temperature sensor to see which modes it supports.
InputFormat inputFormat = InputFormat.inputFormat(
getConnectInfo().getConnectId(),
getConnectInfo().getType(),
0, // See the documentation for the sensor for supported modes
1, // Receive updates when the value changes with delta 1
InputFormat.InputFormatUnit.INPUT_FORMAT_UNIT_SI,
true); // Notifications are enabled
// Tell the device to configure the sensor with the input format
temperatureSensor.updateInputFormat(inputFormat);
// We know from the documentation that the temperature sensor produces readings in
// Kelvin as 4 byte floats when in mode 0 with unit set to SI.
DataFormat dataFormat = DataFormat.create(
"Kelvin",
0, // must match the mode for the inputFormat
InputFormat.InputFormatUnit.INPUT_FORMAT_UNIT_SI, // must match the unit for the inputFormat
4, // a 4 byte float
1); //only one value in the data set (the temperature)
temperatureSensor.addValidDataFormat(dataFormat);
// Now, add a callback listener to receive notifications when the service has a new temperature reading
temperatureSensor.registerCallbackListener(this);
}
Now, when the service receives an updated value from the temperature sensor you will receive
a notification through the listener.
public void didUpdateValueData(LegoService service, byte[] oldValue, byte[] newValue) {
//As we have defined a valid data format stating that the received value can be parsed as a 4 byte
//float we can use the convenience method to retrieve the value as a float.
Float32 temperatureReading = service.valueAsFloat;
}
It is not required to add a valid data format to the GenericService
, but it is recommended to
do so as this will also help the SDK validate all received data according to the defined valid data formats
and write any inconsistencies to the LDSDKLogger
.callbackHelper, connectInfo, inputFormat, io
Modifier and Type | Method and Description |
---|---|
static GenericService |
createService(ConnectInfo connectInfo,
IO io) |
InputFormat |
getDefaultInputFormat()
The default input format that will be uploaded to the device for this service upon discovery of the service.
|
java.lang.String |
getServiceName() |
addValidDataFormat, didReceiveInputFormat, didReceiveValueData, didRequestConnectInfo, equals, getConnectInfo, getDevice, getFloatFromData, getInputFormat, getInputFormatMode, getIntegerFromData, getIo, getNumberFromValueData, getNumberFromValueData, getNumbersFromValueDataSet, getNumbersFromValueDataSet, getValidDataFormats, getValueAsFloat, getValueAsInteger, getValueData, handleUpdatedInputFormat, handleUpdatedValueData, hashCode, isInternalService, registerCallbackListener, removeValidDataFormat, sendReadValueRequest, sendResetStateRequest, setDevice, unregisterCallbackListener, updateCurrentInputFormatWithNewMode, updateInputFormat, writeData
public java.lang.String getServiceName()
getServiceName
in class LegoService
public InputFormat getDefaultInputFormat()
LegoService
getDefaultInputFormat
in class LegoService
InputFormat
of the servicepublic static GenericService createService(ConnectInfo connectInfo, IO io)