I wanted to call an microservice in epl how can it be done ?
Hi @rushikesh_n ,
when you open a new or existing EPL app you can see the “Samples” button on the top right.
The first example shows how to “Call another microservice”.
Call another microservice - EPL app example
/**
* This application queries a separate microservice (machine learning)
* to make predictions based on the Anomaly Detection use case in the
* documentation.
*/
using com.apama.cumulocity.CumulocityRequestInterface;
using com.apama.correlator.Component;
using com.apama.cumulocity.Alarm;
using com.apama.cumulocity.Measurement;
using com.apama.cumulocity.FindManagedObjectResponse;
using com.apama.cumulocity.FindManagedObjectResponseAck;
using com.apama.cumulocity.FindManagedObject;
using com.softwareag.connectivity.httpclient.HttpOptions;
using com.softwareag.connectivity.httpclient.HttpTransport;
using com.softwareag.connectivity.httpclient.Request;
using com.softwareag.connectivity.httpclient.Response;
using com.apama.json.JSONPlugin;
/**
* Call another microservice
*/
monitor DetectAnomalies {
CumulocityRequestInterface requestIface;
action onload() {
requestIface := CumulocityRequestInterface.connectToCumulocity();
// Replace yourDeviceId with the value of your device id
string yourDeviceId:= "device_id";
listenAndActOnMeasurements(yourDeviceId, "IsolationForests");
}
action listenAndActOnMeasurements(string deviceId, string modelName)
{
monitor.subscribe(Measurement.SUBSCRIBE_CHANNEL);
on all Measurement(source = deviceId) as m {
if (m.measurements.hasKey("c8y_Acceleration")) {
log "Received measurement" at INFO;
string RECORD:= convertMeasurementToRecord(m);
log "Sending record to zementis - " + RECORD at INFO;
string REQUEST_ROOT:= "/service/zementis/apply/" + modelName;
string QUERY_PARAM:= "record";
Request zementisRequest:=
requestIface.createRequest("GET", REQUEST_ROOT, any());
zementisRequest.setQueryParameter(QUERY_PARAM, RECORD);
zementisRequest.execute(ZementisHandler(deviceId).requestHandler);
log "EPL execution completed." at INFO;
}
}
}
action convertMeasurementToRecord(com.apama.cumulocity.Measurement m)
returns string
{
dictionary < string, any > json := { };
json["accelerationX"] := m.measurements.
getOrDefault("c8y_Acceleration").
getOrDefault("accelerationX").value;
json["accelerationY"] := m.measurements.
getOrDefault("c8y_Acceleration").
getOrDefault("accelerationY").value;
json["accelerationZ"] := m.measurements.
getOrDefault("c8y_Acceleration").
getOrDefault("accelerationZ").value;
json["gyroX"] := m.measurements.
getOrDefault("c8y_Gyroscope").
getOrDefault("gyroX").value;
json["gyroY"] := m.measurements.
getOrDefault("c8y_Gyroscope").
getOrDefault("gyroY").value;
json["gyroZ"] := m.measurements.
getOrDefault("c8y_Gyroscope").
getOrDefault("gyroZ").value;
return JSONPlugin.toJSON(json);
}
event ZementisHandler
{
string deviceId;
action requestHandler(Response zementisResponse)
{
integer statusCode:= zementisResponse.statusCode;
if (statusCode = 200 and
zementisResponse.payload.data.hasEntry("outputs") = true)
{
if (<boolean> zementisResponse.payload.
getSequence("outputs")[0].
getEntry("outlier") = true )
{
send Alarm("",
"AnomalyDetectionAlarm",
deviceId,
currentTime,
"Anomaly detected",
"ACTIVE",
"CRITICAL",
1,
new dictionary<string, any>) to Alarm.SEND_CHANNEL;
}
}
else
{
log "Zementis response :" +
zementisResponse.payload.data.toString()
at INFO;
}
}
}
}
Regards
Kai
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.