Search in sources :

Example 1 with TimeInNanos

use of com.amazonaws.greengrass.streammanager.model.sitewise.TimeInNanos in project aws-iot-greengrass-edge-connector-for-kinesis-video-stream by awslabs.

the class StreamManager method pushData.

/**
 * Pushes data to an existing stream.
 *
 * @param assetId         - SiteWise asset id
 * @param propertyId      - SiteWise property id
 * @param val             - value to be pushed (Long, String, Double or Boolean)
 * @param updateTimeStamp - update time stamp
 * @return - sequence number
 */
public long pushData(@NonNull String assetId, @NonNull String propertyId, @NonNull Object val, Optional<Date> updateTimeStamp) {
    try {
        if (streamManagerClient == null || msgStreamName == null) {
            final String errorMsg = String.format("Error pushing data to Stream. Property Id: %s." + "Please create a message stream first.", propertyId);
            log.error(errorMsg);
            throw new EdgeConnectorForKVSException(errorMsg);
        }
        Variant variant = null;
        if (val instanceof Integer) {
            variant = new Variant().withIntegerValue(Long.valueOf((Integer) val));
        } else if (val instanceof String) {
            variant = new Variant().withStringValue((String) val);
        } else if (val instanceof Double) {
            variant = new Variant().withDoubleValue((Double) val);
        } else if (val instanceof Boolean) {
            variant = new Variant().withBooleanValue((Boolean) val);
        } else {
            final String errorMsg = String.format("Trying to push invalid val type", msgStreamName, propertyId);
            log.error(errorMsg);
            throw new EdgeConnectorForKVSException(errorMsg);
        }
        List<AssetPropertyValue> entries = new ArrayList<>();
        long epochSecond = Instant.now().getEpochSecond();
        if (updateTimeStamp.isPresent()) {
            epochSecond = updateTimeStamp.get().toInstant().getEpochSecond();
        }
        TimeInNanos timestamp = new TimeInNanos().withTimeInSeconds(epochSecond);
        AssetPropertyValue entry = new AssetPropertyValue().withValue(variant).withQuality(Quality.GOOD).withTimestamp(timestamp);
        entries.add(entry);
        PutAssetPropertyValueEntry putAssetPropertyValueEntry = new PutAssetPropertyValueEntry().withEntryId(UUID.randomUUID().toString()).withAssetId(assetId).withPropertyId(propertyId).withPropertyValues(entries);
        log.debug("Pushing data to Message Stream: " + msgStreamName);
        log.trace("For SiteWise Asset ID: " + assetId);
        log.trace("For SiteWise Property ID: " + propertyId);
        log.trace("Value: " + entries.get(0).toString());
        return streamManagerClient.appendMessage(msgStreamName, ValidateAndSerialize.validateAndSerializeToJsonBytes(putAssetPropertyValueEntry));
    } catch (StreamManagerException | JsonProcessingException ex) {
        final String errorMsg = String.format("Error pushing data to Stream: %s, Property Id: %s", msgStreamName, propertyId);
        log.error(errorMsg);
        return 0L;
    }
}
Also used : EdgeConnectorForKVSException(com.aws.iot.edgeconnectorforkvs.model.exceptions.EdgeConnectorForKVSException) PutAssetPropertyValueEntry(com.amazonaws.greengrass.streammanager.model.sitewise.PutAssetPropertyValueEntry) ArrayList(java.util.ArrayList) AssetPropertyValue(com.amazonaws.greengrass.streammanager.model.sitewise.AssetPropertyValue) StreamManagerException(com.amazonaws.greengrass.streammanager.client.exception.StreamManagerException) Variant(com.amazonaws.greengrass.streammanager.model.sitewise.Variant) TimeInNanos(com.amazonaws.greengrass.streammanager.model.sitewise.TimeInNanos) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

StreamManagerException (com.amazonaws.greengrass.streammanager.client.exception.StreamManagerException)1 AssetPropertyValue (com.amazonaws.greengrass.streammanager.model.sitewise.AssetPropertyValue)1 PutAssetPropertyValueEntry (com.amazonaws.greengrass.streammanager.model.sitewise.PutAssetPropertyValueEntry)1 TimeInNanos (com.amazonaws.greengrass.streammanager.model.sitewise.TimeInNanos)1 Variant (com.amazonaws.greengrass.streammanager.model.sitewise.Variant)1 EdgeConnectorForKVSException (com.aws.iot.edgeconnectorforkvs.model.exceptions.EdgeConnectorForKVSException)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ArrayList (java.util.ArrayList)1