Skip to content

Event Management Java 客户端

简介

Event Management Java 客户端允许您创建和更新与 assets 关联的事件。该服务还可管理用户创建的不同类型的事件。有关该服务的更多信息,请参见 Event Management

事件操作

事件控制器可管理标准和自定义事件。它基于 API 调用来创建、列出和更新事件。

客户端名称:EventClient

创建新事件

说明

实体 ID 缓存时可保留 1 分钟。实体 (asset) 创建完成后,必须刷新缓存才对 Event Management 可见。

// Construct the EventClient object
EventClient eventClient = EventClient.builder()
 .mindsphereCredentials(credentials)
 .restClientConfig(config)
 .build();

// Json string representing Mindsphere standard event or custom event
String eventJsonString = "{\r\n" +
 " \"typeId\": \"3cb8c2b1-f72e-43fa-ae39-ee9de14b8f26\",\r\n" +
 " \"correlationId\": \"3ecc3658-96bb-47ff-93b3-93afd640ee10\",\r\n" +
 " \"entityId\": \"50fd8ab5-23b3-45f7-aca1-12b0b186707b\",\r\n" +
 " \"timestamp\": \"2018-01-09T13:40:23.438Z\",\r\n" +
 " \"customStringField\": \"customValue\",\r\n" +
 " \"customIntegerField\":10\r\n" +
 "}"

BaseEvent createdEvent = null;
try {
 createdEvent = eventClient.createEvent(eventJsonString);
} catch (MindsphereException ex) {
 // Exception handling
}

创建新自定义事件

自定义事件是指分配了用户定义事件类型的事件。

// Construct the EventClient object as shown above

//Construct the CustomEvent object to be created
CustomEvent customEvent = new CustomEvent();
customEvent.setTypeId(eventTypeId);
customEvent.setEntityId(entityId);
customEvent.setTimestamp(timestamp);

Map<String, Object> fields = new HashMap<>();
fields.put("customField", "customValue");
customEvent.setFields(fields);

CustomEvent createdCustomEvent = null;
try {
 createdCustomEvent = eventClient.createCustomEvent(customEvent);
} catch (MindsphereException ex) {
 // Exception handling
}

创建新标准事件

标准事件使用了带有以下附加属性的预定义事件类型:

  • description
  • severity
  • code
  • source
  • acknowledged
// Construct the EventClient object as shown above

//Construct the StandardEvent object to be created
MindsphereStandardEvent standardEvent = new MindsphereStandardEvent();
standardEvent.setDescription(description);
standardEvent.setSeverity(severity);
standardEvent.setCode(code);
standardEvent.setAcknowledged(acknowledged);
standardEvent.setEntityId(entityId);
standardEvent.setTimestamp(timestamp);

MindsphereStandardEvent createdStandardEvent = null;
try {
 createdStandardEvent = eventClient.createStandardEvent(standardEvent);
} catch (MindsphereException ex) {
 // Exception handling
}

查询事件

根据查询参数返回事件。

说明

默认情况下,每页将返回 20 个元素。

// Construct the EventClient object as shown above

Events events = null;
try {
 events = eventClient.getEvents(size, page, filter, sort, ifNoneMatch, history);
} catch (MindsphereException ex) {
 // Exception handling
}

按 ID 查询事件

返回带有指定 ID 的事件。

// Construct the EventClient object as shown above

BaseEvent event = null;
try {
 event = eventClient.getEventById(eventId);
 // The retrieved event can be checked for type as follows
 if (event != null &amp;&amp; event instanceof MindsphereStandardEvent) {
 // Process the retrieved Standard event
 } else if (event != null &amp;&amp; event instanceof CustomEvent) {
 // Process the retrieved custom event
 }
} catch (MindsphereException ex) {
 // Exception handling
}

更新事件

更新带有指定 ID 的现有事件。

// Construct the EventClient object as shown above

BaseEvent event = null;
try {
 event = eventClient.updateEvents(eventId, eventJsonString, ifMatch);
 // The retrieved event can be checked for type as shown above for further processing
} catch (MindsphereException ex) {
 // Exception handling
}

更新标准事件

更新带有指定 ID 的现有标准事件。

// Construct the EventClient object as shown above

MindsphereStandardEvent updatedStandardEvent = null;
try {
 updatedStandardEvent = eventClient.updateEvents(eventId, standardEvent, ifMatch);
} catch (MindsphereException ex) {
 // Exception handling
}

更新自定义事件

更新带有指定 ID 的现有自定义事件。

// Construct the EventClient object as shown above

CustomEvent customEvent = null;
try {
 customEvent = eventClient.updateEvents(eventId, customEvent, ifMatch);
} catch (MindsphereException ex) {
 // Exception handling
}

事件类型操作

事件类型操作基于 API 调用来创建、列出和更新事件类型。

客户端名称:EventTypeClient

创建新事件类型

创建带有指定内容的新事件类型。

默认 ID 为生成的 UUID。如果已提供自定义 ID,必须以租户名称为前缀,并在该名称后面添加一个点。

// Construct the EventTypeClient object
EventTypeClient eventTypeClient = EventTypeClient.builder()
 .mindsphereCredentials(credentials)
 .restClientConfig(config)
 .build();

// Define the Event Type to be created
String eventTypeName = "OilTemperatureRise";
EventType eventType = new EventType();
eventType.setName(eventTypeName);
eventType.setTtl(10);
eventType.setScope(ScopeEnum.LOCAL);

// Define Field values to be associated with the Event Type
Field field = new Field();
field.setName("temperature");
field.setFilterable(Boolean.TRUE);
field.required(Boolean.TRUE);
field.setType(FieldTypeEnum.INTEGER);

// Associate Field values to the Event Type
List<Field> fieldList = new ArrayList<>();
fieldList.add(field);
eventType.setFields(fieldList);

EventType createdEventType = null;
try {
 createdEventType = eventTypeClient.createEventType(eventType);
} catch (MindsphereException e) {
 // Exception handling
}

查询事件类型

根据查询参数返回事件类型。

//Construct EventTypeClient object as shown above

EventTypes eventTypes = null;
try {
 eventTypes = eventTypeClient.getEventTypes(page, size, sort, filter, ifNoneMatch);
} catch (MindsphereException e) {
 // Exception handling
}

按 ID 读取事件类型

按 ID 返回事件类型。

//Construct EventTypeClient object as shown above

EventType eventType = null;
try {
 eventType = eventTypeClient.getEventTypesById(eventTypeId, ifNoneMatch);
} catch (MindsphereException e) {
 // Exception handling
}

更新事件类型

通过仅发送需要更改的内容来更新带有指定 ID 的事件类型。

//Construct EventTypeClient object as shown above

//Define the Event Tyoe Patch that will be used to update the event Type
EventTypePatch eventTypePatch = new EventTypePatch();
eventTypePatch.setOperation(OperationEnum.REPLACE);
eventTypePatch.setPath("/scope");
eventTypePatch.setValue(ScopeEnum.GLOBAL.getValue());

EventType updatedEventType = null;
try {
 updatedEventType = eventTypeClient.updateEventType(eventTypeId, eventType.getEtag().toString(), patch);
} catch (MindsphereException e) {
 // Exception handling
}

事件作业操作

事件作业控制器可用于创建作业以删除所查询的事件。

客户端名称:EventJobClient

创建作业以删除事件

创建新作业以删除满足查询参数的事件。用于选择待删除事件的查询参数在 DeleteEventsJobDto 实例中作为过滤器提及。

// Construct the EventJobClient object
EventJobClient eventJobClient = EventJobClient.builder()
 .mindsphereCredentials(credentials)
 .restClientConfig(config)
 .build();

//Create a DeleteEventsJobDto instance
DeleteEventsJobDto deleteEventsJobDto = new DeleteEventsJobDto();
deleteEventsJobDto.setFilter("typeId", "eventTypeId");

JobResource jobResource = null;
try {
 jobResource = eventJobClient.createDeleteEventsJob(deleteEventsJobDto);
} catch (MindsphereException e) {
 // Exception handling
}

可通过以下方式为 DeleteEventsJobDto 实例填充标准:

  1. 使用过滤器密钥和值
DeleteEventsJobDto deleteEventsJobDto = new DeleteEventsJobDto();
deleteEventsJobDto.setFilter("typeId", "eventTypeId");
deleteEventsJobDto.setFilter("id", "eventId");
  1. 使用 HashMap
DeleteEventsJobDto deleteEventsJobDto = new DeleteEventsJobDto();

HashMap<String, String> filterMap = new HashMap<>();
filterMap.put("typeId", "eventTypeId");
filterMap.put("id", "eventId");

deleteEventsJobDto.setFilter(filterMap);
  1. 使用字符串过滤器
DeleteEventsJobDto deleteEventsJobDto = new DeleteEventsJobDto();

String filter = "{\"filter\": { \"id\":\"eventId\"," +
\"typeId\":\"eventTypeId\"\r\n" +
" } " +
"}";

deleteEventsJobDto.setFilter(filter);

读取按 ID 删除事件的作业

返回按 ID 删除事件的作业。

//Construct EventJobClient object as shown above

String jobId = "deleteEventsJobId";
JobResource jobResource = null;
try {
 jobResource = eventJobClient.getDeleteEventsJobById(deleteEventsJobId);
} catch (MindsphereException e) {
 // Exception handling
}

Last update: March 22, 2023