Search in sources :

Example 1 with IoTFieldsExtension

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

the class IoTDataManager method requestMomentaryValuesReadOut.

/**
     * Try to read out a things momentary values.
     *
     * @param jid the full JID of the thing to read data from.
     * @return a list with the read out data.
     * @throws NoResponseException
     * @throws XMPPErrorException
     * @throws NotConnectedException
     * @throws InterruptedException
     */
public List<IoTFieldsExtension> requestMomentaryValuesReadOut(EntityFullJid jid) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
    final XMPPConnection connection = connection();
    final int seqNr = nextSeqNr.incrementAndGet();
    IoTDataRequest iotDataRequest = new IoTDataRequest(seqNr, true);
    iotDataRequest.setTo(jid);
    StanzaFilter doneFilter = new IoTFieldsExtensionFilter(seqNr, true);
    StanzaFilter dataFilter = new IoTFieldsExtensionFilter(seqNr, false);
    // Setup the IoTFieldsExtension message collectors before sending the IQ to avoid a data race.
    StanzaCollector doneCollector = connection.createStanzaCollector(doneFilter);
    StanzaCollector.Configuration dataCollectorConfiguration = StanzaCollector.newConfiguration().setStanzaFilter(dataFilter).setCollectorToReset(doneCollector);
    StanzaCollector dataCollector = connection.createStanzaCollector(dataCollectorConfiguration);
    try {
        connection.createStanzaCollectorAndSend(iotDataRequest).nextResultOrThrow();
        // Wait until a message with an IoTFieldsExtension and the done flag comes in.
        doneCollector.nextResult();
    } finally {
        // Ensure that the two collectors are canceled in any case.
        dataCollector.cancel();
        doneCollector.cancel();
    }
    int collectedCount = dataCollector.getCollectedCount();
    List<IoTFieldsExtension> res = new ArrayList<>(collectedCount);
    for (int i = 0; i < collectedCount; i++) {
        Message message = dataCollector.pollResult();
        IoTFieldsExtension iotFieldsExtension = IoTFieldsExtension.from(message);
        res.add(iotFieldsExtension);
    }
    return res;
}
Also used : IoTDataRequest(org.jivesoftware.smackx.iot.data.element.IoTDataRequest) IoTFieldsExtension(org.jivesoftware.smackx.iot.data.element.IoTFieldsExtension) StanzaFilter(org.jivesoftware.smack.filter.StanzaFilter) Message(org.jivesoftware.smack.packet.Message) ArrayList(java.util.ArrayList) XMPPConnection(org.jivesoftware.smack.XMPPConnection) StanzaCollector(org.jivesoftware.smack.StanzaCollector) IoTFieldsExtensionFilter(org.jivesoftware.smackx.iot.data.filter.IoTFieldsExtensionFilter)

Example 2 with IoTFieldsExtension

use of org.jivesoftware.smackx.iot.data.element.IoTFieldsExtension 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)

Aggregations

IoTFieldsExtension (org.jivesoftware.smackx.iot.data.element.IoTFieldsExtension)2 ArrayList (java.util.ArrayList)1 AbstractSmackIntegrationTest (org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest)1 SmackIntegrationTest (org.igniterealtime.smack.inttest.SmackIntegrationTest)1 StanzaCollector (org.jivesoftware.smack.StanzaCollector)1 XMPPConnection (org.jivesoftware.smack.XMPPConnection)1 StanzaFilter (org.jivesoftware.smack.filter.StanzaFilter)1 Message (org.jivesoftware.smack.packet.Message)1 ThingMomentaryReadOutRequest (org.jivesoftware.smackx.iot.data.ThingMomentaryReadOutRequest)1 ThingMomentaryReadOutResult (org.jivesoftware.smackx.iot.data.ThingMomentaryReadOutResult)1 IoTDataField (org.jivesoftware.smackx.iot.data.element.IoTDataField)1 IntField (org.jivesoftware.smackx.iot.data.element.IoTDataField.IntField)1 IoTDataRequest (org.jivesoftware.smackx.iot.data.element.IoTDataRequest)1 NodeElement (org.jivesoftware.smackx.iot.data.element.NodeElement)1 TimestampElement (org.jivesoftware.smackx.iot.data.element.TimestampElement)1 IoTFieldsExtensionFilter (org.jivesoftware.smackx.iot.data.filter.IoTFieldsExtensionFilter)1