Cumulocity Microservice Tutorial Part 8 (Event handling)

Introduction

This tutorial will show you how to handle events e.g. event creation, deletion etc.

Precondition

The tutorial is based on Microservice Tutorial Part 1a, Part 1b, Part 2, Part 3, Part 4, Part 5, Part 6 & Part 7.

Source code

https://github.com/netperformance/cumulocity-microservice-part8

Microservice preparation

  1. Create a new microservice by using postman
  2. Get the bootstrap credentials by using postman
  3. Configure your application.properties
  4. Configure your cumulocity.json
  5. Subscribe to your microservice

Setup and create a new event

The EventApi is needed for event handling:

// you need the event API to handle measurements.

private final EventApi eventApi;
// Managed object representation will give you access to the ID of the managed object

ManagedObjectRepresentation managedObjectRepresentation = resolveManagedObject();

// Event representation object

EventRepresentation eventRepresentation = new EventRepresentation();

// set the event properties

eventRepresentation.setDateTime(new DateTime());

// add event to a managed object

eventRepresentation.setSource(managedObjectRepresentation);

eventRepresentation.setText("Door open");

eventRepresentation.setType("Event_type");

Get all events

// To get access to event collection representation

EventCollection eventCollection = eventApi.getEvents();

// To get access to e.g. all events (pages)

PagedEventCollectionRepresentation pagedEventCollectionRepresentation = eventCollection.get();   

// Representation of a series of event elements. Get all pages.

Iterable<EventRepresentation> iterable = pagedEventCollectionRepresentation.allPages();

// Usage of google guava to create an event list

List<EventRepresentation> eventRepresentationList = Lists.newArrayList(iterable);

Delete all events

List<EventRepresentation> eventRepresentationList = getAllEvents();        

// iterate over the event list and delete the events by using the event api

for(EventRepresentation eventRepresentation : eventRepresentationList) {

    eventApi.delete(eventRepresentation);

}

Get an event by given event id

// Use GId to transform the given id to a global c8y id

EventRepresentation eventRepresentation = eventApi.getEvent(GId.asGId(eventId));

Update of the required roles

We need to add the roles “ROLE_EVENT_READ” & “ROLE_EVENT_ADMIN” to the list of the required roles to get access to the events. 

{
  "requiredRoles": [
      "ROLE_INVENTORY_ADMIN",
      "ROLE_IDENTITY_ADMIN",
      "ROLE_MEASUREMENT_READ",
      "ROLE_EVENT_ADMIN",
      "ROLE_EVENT_READ"
  ]
}

List of all tutorials

Cumulocity Microservice Tutorial Part 1a (Hello World)
Cumulocity Microservice Tutorial Part 1b (local testing)
Cumulocity Microservice Tutorial Part 2 (Managed Object CRUD)
Cumulocity Microservice Tutorial Part 3 (Microservice/User-Context, external ID & measurement creation
Cumulocity Microservice Tutorial Part 4 (Read and display data from an external weather API & microservice settings/configuration)
Cumulocity Microservice Tutorial Part 5 (Creation of custom measurements)
Cumulocity Microservice Tutorial Part 6 (send a REST request via SDK to get the latest measurement)
Cumulocity Microservice Tutorial Part 7 (Alarm handling)
Cumulocity Microservice Tutorial Part 8 (Event handling)
Cumulocity Microservice Tutorial Part 9 (Operation handling)