Search in sources :

Example 1 with TimestampElement

use of org.jivesoftware.smackx.iot.data.element.TimestampElement in project Smack by igniterealtime.

the class IoTFieldsExtensionProvider method parseNode.

public NodeElement parseNode(XmlPullParser parser) throws XmlPullParserException, IOException, ParseException {
    final int initialDepth = parser.getDepth();
    final NodeInfo nodeInfo = NodeInfoParser.parse(parser);
    List<TimestampElement> timestampElements = new ArrayList<>();
    outerloop: while (true) {
        final XmlPullParser.Event eventType = parser.next();
        final String name = parser.getName();
        switch(eventType) {
            case START_ELEMENT:
                switch(name) {
                    case TimestampElement.ELEMENT:
                        TimestampElement timestampElement = parseTimestampElement(parser);
                        timestampElements.add(timestampElement);
                        break;
                }
                break;
            case END_ELEMENT:
                if (parser.getDepth() == initialDepth) {
                    break outerloop;
                }
                break;
            default:
                // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
                break;
        }
    }
    return new NodeElement(nodeInfo, timestampElements);
}
Also used : NodeInfo(org.jivesoftware.smackx.iot.element.NodeInfo) TimestampElement(org.jivesoftware.smackx.iot.data.element.TimestampElement) ArrayList(java.util.ArrayList) NodeElement(org.jivesoftware.smackx.iot.data.element.NodeElement)

Example 2 with TimestampElement

use of org.jivesoftware.smackx.iot.data.element.TimestampElement 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 if an exception occurs.
 * @throws TimeoutException if there was a timeout.
 */
@SmackIntegrationTest
public void dataTest() throws 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 {
        IntegrationTestRosterUtil.ensureBothAccountsAreSubscribedToEachOther(conOne, conTwo, timeout);
        values = iotDataManagerTwo.requestMomentaryValuesReadOut(conOne.getUser());
    } finally {
        iotDataManagerOne.uninstallThing(dataThing);
        IntegrationTestRosterUtil.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.annotations.SmackIntegrationTest)

Example 3 with TimestampElement

use of org.jivesoftware.smackx.iot.data.element.TimestampElement in project Smack by igniterealtime.

the class IoTFieldsExtensionProvider method parseTimestampElement.

public TimestampElement parseTimestampElement(XmlPullParser parser) throws XmlPullParserException, IOException, ParseException {
    final int initialDepth = parser.getDepth();
    final String dateString = parser.getAttributeValue(null, "value");
    Date date = XmppDateTime.parseDate(dateString);
    List<IoTDataField> fields = new ArrayList<>();
    outerloop: while (true) {
        final XmlPullParser.Event eventType = parser.next();
        switch(eventType) {
            case START_ELEMENT:
                final String name = parser.getName();
                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 END_ELEMENT:
                if (parser.getDepth() == initialDepth) {
                    break outerloop;
                }
                break;
            default:
                // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
                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

TimestampElement (org.jivesoftware.smackx.iot.data.element.TimestampElement)3 ArrayList (java.util.ArrayList)2 IoTDataField (org.jivesoftware.smackx.iot.data.element.IoTDataField)2 NodeElement (org.jivesoftware.smackx.iot.data.element.NodeElement)2 Date (java.util.Date)1 AbstractSmackIntegrationTest (org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest)1 SmackIntegrationTest (org.igniterealtime.smack.inttest.annotations.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 NodeInfo (org.jivesoftware.smackx.iot.element.NodeInfo)1