Cumulocity Microservice Tutorial Part 7 (Alarm handling)

Introduction

This tutorial will show you how to handle alarms e.g. alarm 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.

Source code

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

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 alarm 

The EventApi is needed for event handling:

// you need the alarm API to handle measurements.

private final AlarmApi alarmApi;

// alarm reprentation object

AlarmRepresentation alarmRepresentation = new AlarmRepresentation();


// set the needed alarm properties

alarmRepresentation.setDateTime(new DateTime());

alarmRepresentation.setSource(managedObjectRepresentation);

alarmRepresentation.setText("Temperature limit exceeded!");

alarmRepresentation.setType("Temperature_Alarm");

alarmRepresentation.setSeverity("CRITICAL");


// create an alarm    

alarmApi.create(alarmRepresentation);

Delete all alarms by given alarm filter

// Alarm filter definition

AlarmFilter alarmFilter = new AlarmFilter();

alarmFilter.bySeverity(CumulocitySeverities.CRITICAL);



// Usage of the alarm api to delete all the alarms by given  alarm filter

alarmApi.deleteAlarmsByFilter(alarmFilter);

Get alarm by given alarm id

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

AlarmRepresentation alarmRepresentation = alarmApi.getAlarm(GId.asGId(alarmId));

return alarmRepresentation.toJSON();

Get all alarms

// Get an alarm collection object by using the alarm api

AlarmCollection alarmCollection = alarmApi.getAlarms();

// Get a paged alarm collection representation. Insert the number e.g. alarmCollection.get(100) for restriction of number of results to 100.

PagedAlarmCollectionRepresentation pagedAlarmCollectionRepresentation = alarmCollection.get();

// Use the paged alarm collection representation to get the list of alarms

List<AlarmRepresentation> alarmRepresentations = pagedAlarmCollectionRepresentation.getAlarms();

Update of the required roles

We need to add the roles “ROLE_ALARM_READ” & “ROLE_ALARM_ADMIN” to the list of the required roles to get access to the alarms. 

{
  "requiredRoles": [
      "ROLE_INVENTORY_ADMIN",
      "ROLE_IDENTITY_ADMIN",
      "ROLE_MEASUREMENT_READ",
      "ROLE_ALARM_ADMIN",
      "ROLE_ALARM_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)