How to use the data I receive from Azure IoT Hub

0 votes

So, this is my code for receiving data from the Azure Cloud:

public void accept(PartitionReceiver receiver)
{
    System.out.println("** Created receiver on partition " + partitionId);
    try {
        while (true) {
            Iterable<EventData> receivedEvents = receiver.receive(10).get();
            int batchSize = 0;
            if (receivedEvents != null)
            {
                for(EventData receivedEvent: receivedEvents)
                {                                    
                    System.out.println(String.format("| Time: %s", receivedEvent.getSystemProperties().getEnqueuedTime()));
                    System.out.println(String.format("| Device ID: %s", receivedEvent.getProperties().get("iothub-connection-device-id")));
                    System.out.println(String.format("| Message Payload: %s", new String(receivedEvent.getBody(), Charset.defaultCharset())));
                    batchSize++;
                }
            }
        }
    } catch (Exception e)
    {
        System.out.println("Failed to receive messages: " + e.getMessage());
    }
}

This is where I get the product name and price as a payload:
System.out.println(String.format("| Message Payload: %s", new String(receivedEvent.getBody(), Charset.defaultCharset())));

But now I need to take the Payload and store its product into a string object and the price into a double variable. How do I do that? Please suggest!

Aug 1, 2018 in IoT (Internet of Things) by Bharani
• 4,660 points
1,794 views

1 answer to this question.

0 votes

The payload you receive will be a JSON string. So, you could define a POJO class to package the data and then deserialize to the appropriate object and use it. Serilize and deserialize the data as event body between a POJO and JSON string by using a json library. You can choose any library you want from here: http://www.json.org/.

This is the code to receive :

public class Product {
public Product(){

}
    private String product;
    private Double price;

public Product(String json ){
    Gson gson=new Gson();
    try{
        Product product =gson.fromJson(json, Product.class);
        if (product!=null){
            System.out.println("Name: " +product.getProduct());
        }
    }catch (Exception e){
        System.out.println("failed: " +e.getMessage());
    }
}

    public String getProduct() {
        return product;
    }

    public Double getPrice() {
        return price;
    }
 }

And, this is how you can read the JSON String into a Object from the Class Product. Hence, the Product class will contain variables with product and price!

    public Product(String json ){
    Gson gson=new Gson();
    try{
        Product product =gson.fromJson(json, Product.class);
        if (product!=null){
            System.out.println("Name: " +product.getProduct());
        }
    }catch (Exception e){
        System.out.println("failed: " +e.getMessage());
    }
}

answered Aug 1, 2018 by DataKing99
• 8,250 points

Related Questions In IoT (Internet of Things)

0 votes
1 answer
0 votes
1 answer

Azure - What service to use for Arduino data (iot)

That's because the two services do completely ...READ MORE

answered Aug 1, 2018 in IoT (Internet of Things) by anonymous2
• 4,280 points
1,042 views
0 votes
1 answer

Azure IoT Hub : How to set Epoch Value of an Azure Function?

Try using Azure IoT Hub consumer groups, if ...READ MORE

answered Feb 21, 2019 in IoT (Internet of Things) by Shubham
• 13,490 points
1,251 views
0 votes
1 answer
0 votes
1 answer

Displaying Table Schema using Power BI with Azure IoT Hub

Answering your first question, Event Hubs are ...READ MORE

answered Aug 1, 2018 in IoT (Internet of Things) by nirvana
• 3,130 points
1,276 views
0 votes
2 answers

Azure IoT Hub Operations Monitoring

Hi, Can you tell which device you ...READ MORE

answered Jan 4, 2019 in IoT (Internet of Things) by Varul
• 140 points
1,631 views
0 votes
1 answer

Error when connecting Azure IoT Suite with Raspberry Pi

down vote I've verified the tutorial works on ...READ MORE

answered Sep 18, 2018 in IoT (Internet of Things) by anonymous2
• 4,280 points
879 views
0 votes
1 answer
0 votes
1 answer

Testing if messages from my Device is reaching the Azure IoT Hub?

Download the Device Explorer open source tool ...READ MORE

answered Aug 10, 2018 in IoT (Internet of Things) by DataKing99
• 8,250 points
3,835 views
0 votes
1 answer

TCP in Azure IoT Hub

The default Protocol Gateway samples are indeed ...READ MORE

answered Oct 11, 2018 in IoT (Internet of Things) by DataKing99
• 8,250 points
2,256 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP