Search in sources :

Example 1 with IoTDataField

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());
}
Also used : IoTFieldsExtension(org.jivesoftware.smackx.iot.data.element.IoTFieldsExtension) TimestampElement(org.jivesoftware.smackx.iot.data.element.TimestampElement) IntField(org.jivesoftware.smackx.iot.data.element.IoTDataField.IntField) IntField(org.jivesoftware.smackx.iot.data.element.IoTDataField.IntField) IoTDataField(org.jivesoftware.smackx.iot.data.element.IoTDataField) NodeElement(org.jivesoftware.smackx.iot.data.element.NodeElement) ThingMomentaryReadOutResult(org.jivesoftware.smackx.iot.data.ThingMomentaryReadOutResult) ThingMomentaryReadOutRequest(org.jivesoftware.smackx.iot.data.ThingMomentaryReadOutRequest) AbstractSmackIntegrationTest(org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest) SmackIntegrationTest(org.igniterealtime.smack.inttest.SmackIntegrationTest)

Example 2 with IoTDataField

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);
}
Also used : TimestampElement(org.jivesoftware.smackx.iot.data.element.TimestampElement) ArrayList(java.util.ArrayList) IoTDataField(org.jivesoftware.smackx.iot.data.element.IoTDataField) Date(java.util.Date)

Aggregations

IoTDataField (org.jivesoftware.smackx.iot.data.element.IoTDataField)2 TimestampElement (org.jivesoftware.smackx.iot.data.element.TimestampElement)2 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 AbstractSmackIntegrationTest (org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest)1 SmackIntegrationTest (org.igniterealtime.smack.inttest.SmackIntegrationTest)1 ThingMomentaryReadOutRequest (org.jivesoftware.smackx.iot.data.ThingMomentaryReadOutRequest)1 ThingMomentaryReadOutResult (org.jivesoftware.smackx.iot.data.ThingMomentaryReadOutResult)1 IntField (org.jivesoftware.smackx.iot.data.element.IoTDataField.IntField)1 IoTFieldsExtension (org.jivesoftware.smackx.iot.data.element.IoTFieldsExtension)1 NodeElement (org.jivesoftware.smackx.iot.data.element.NodeElement)1