use of org.jivesoftware.smackx.iot.data.element.IoTDataField in project Smack by igniterealtime.
the class IoTDataIntegrationTest method dataTest.
/**
* Connection one provides a thing, which momentary value is read out by connection two.
*
* @throws Exception
* @throws TimeoutException
*/
@SmackIntegrationTest
public void dataTest() throws TimeoutException, Exception {
final String key = StringUtils.randomString(12);
final String sn = StringUtils.randomString(12);
final int value = INSECURE_RANDOM.nextInt();
Thing dataThing = Thing.builder().setKey(key).setSerialNumber(sn).setMomentaryReadOutRequestHandler(new ThingMomentaryReadOutRequest() {
@Override
public void momentaryReadOutRequest(ThingMomentaryReadOutResult callback) {
IoTDataField.IntField field = new IntField(testRunId, value);
callback.momentaryReadOut(Collections.singletonList(field));
}
}).build();
iotDataManagerOne.installThing(dataThing);
List<IoTFieldsExtension> values;
try {
RosterIntegrationTest.ensureBothAccountsAreSubscribedToEachOther(conOne, conTwo, timeout);
values = iotDataManagerTwo.requestMomentaryValuesReadOut(conOne.getUser());
} finally {
iotDataManagerOne.uninstallThing(dataThing);
RosterIntegrationTest.ensureBothAccountsAreNotInEachOthersRoster(conOne, conTwo);
}
assertEquals(1, values.size());
IoTFieldsExtension iotFieldsExtension = values.get(0);
List<NodeElement> nodes = iotFieldsExtension.getNodes();
assertEquals(1, nodes.size());
NodeElement node = nodes.get(0);
List<TimestampElement> timestamps = node.getTimestampElements();
assertEquals(1, timestamps.size());
TimestampElement timestamp = timestamps.get(0);
List<? extends IoTDataField> fields = timestamp.getDataFields();
assertEquals(1, fields.size());
IoTDataField dataField = fields.get(0);
assertTrue(dataField instanceof IoTDataField.IntField);
IoTDataField.IntField intDataField = (IoTDataField.IntField) dataField;
assertEquals(testRunId, intDataField.getName());
assertEquals(value, intDataField.getValue());
}
use of org.jivesoftware.smackx.iot.data.element.IoTDataField in project Smack by igniterealtime.
the class IoTFieldsExtensionProvider method parseTimestampElement.
public TimestampElement parseTimestampElement(XmlPullParser parser) throws ParseException, XmlPullParserException, IOException {
final int initialDepth = parser.getDepth();
final String dateString = parser.getAttributeValue(null, "value");
final Date date = XmppDateTime.parseDate(dateString);
List<IoTDataField> fields = new ArrayList<>();
outerloop: while (true) {
final int eventType = parser.next();
final String name = parser.getName();
switch(eventType) {
case XmlPullParser.START_TAG:
IoTDataField field = null;
final String fieldName = parser.getAttributeValue(null, "name");
final String fieldValue = parser.getAttributeValue(null, "value");
switch(name) {
case "int":
{
int value = Integer.parseInt(fieldValue);
field = new IoTDataField.IntField(fieldName, value);
}
break;
case "boolean":
{
boolean value = Boolean.parseBoolean(fieldValue);
field = new IoTDataField.BooleanField(fieldName, value);
}
break;
default:
LOGGER.warning("IoT Data field type '" + name + "' not implement yet. Ignoring.");
break;
}
if (field != null) {
fields.add(field);
}
break;
case XmlPullParser.END_TAG:
if (parser.getDepth() == initialDepth) {
break outerloop;
}
break;
}
}
return new TimestampElement(date, fields);
}
Aggregations