Skip to content

Event Management Java 客户端

简介

Event Management Java 客户端允许您管理与 assets 和事件 types 相关联的事件。关于此服务请参考Event Management获取更多信息。

提示

以下示例中的占位符由尖括号 < > 表示。

事件类型操作

事件类型客户端创建、读取、更新和删除事件类型。

客户端名称: EventTypesClient

列出所有事件类型

// Construct EventTypesClient object
EventTypesClient eventTypesClient =  EventTypesClient.builder()
                                      .mindsphereCredentials(<credentials>)
                                      .restClientConfig(<config>)
                                      .build();

EventType event_types = null;
try {
    event_types = eventTypeClient.getEventTypes(<page>, <size>, "<sort>", "<filter>", "<ifNoneMatch>");
} catch (MindsphereException e) {
    // Exception Handling
}

创建事件类型

自定义事件类型 ID 必须在前缀前加上租户名称和小圆点。

// Construct the EventTypesClient object as shown above

// Define the event type to be created
String event_type_name = "<event_type_name>";
EventType event_type = new EventType();
event_type.setName(event_type_name);
event_type.setTtl(<ttl>);
event_type.setScope(EventType.ScopeEnum.LOCAL);

// Define fields for the event type
Field field = new Field();
field.setName("<field_name>");
field.setFilterable(Boolean.TRUE);
field.required(Boolean.TRUE);
field.setType(Field.TypeEnum.INTEGER);

// Add the fields to the event type
List<Field> field_list = new ArrayList<>();
field_list.add(field);
event_type.setFields(field_list);

EventType created_event_type = null;
try {
    created_event_type = event_type_client.createEventType(event_type);
} catch (MindsphereException e) {
    // Exception handling
}

更新事件类型

更新事件类型范围

// Construct EventTypesClient object as shown above

// Define the event type patch to update the event type with
EventTypePatch event_type_patch = new EventTypePatch();
event_type_patch.setOp(EventTypePatch.OperationEnum.REPLACE);
event_type_patch.setPath("/scope");
event_type_patch.setValue(ScopeEnum.GLOBAL.getValue());

EventType updated_event_type = null;
try {
    updated_event_type = event_type_client.updateEventType("<event_type_id>", "<etag>", event_type_patch);
} catch (MindsphereException e) {
    // Exception Handling
}

添加字符段到事件类型

// Construct EventTypesClient object as shown above

// Create the field to be added
Field new_field = new Field();
new_field.setName("<field_name>");
new_field.setUpdatable(Boolean.FALSE);
new_field.setType(Field.TypeEnum.STRING);

// Define the event type patch to update the event type with
EventTypePatch event_type_patch = new EventTypePatch();
event_type_patch.setOp(EventTypePatch.OperationEnum.ADD);
event_type_patch.setPath(EventManagementConstants.ADD_NEW_FIELD);

ObjectMapper object_mapper = new ObjectMapper();
String field_as_string = object_mapper.writeValueAsString(field)
event_type_patch.setValue(field_as_string);

EventType updated_event_type = null;
try {
    updated_event_type = event_type_client.updateEventType("<event_type_id>", "<etag>", event_type_patch);
} catch (MindsphereException e) {
    // Exception Handling
}

或者,使用 addNewFieldToEventType 方法。

//Construct EventTypesClient object as shown above

// Create the field to be added
Field new_field = new Field();
new_field.setName("<field_name>");
new_field.setUpdatable(Boolean.FALSE);
new_field.setType(Field.TypeEnum.STRING);

EventType updated_event_type = null;
try {
    updated_event_type = event_type_client.addNewFieldToEventType(eventType.getId(), "<etag>", new_field);
} catch (MindsphereException e) {
    // Exception Handling
}

强制设置事件类型的字符段

// Construct EventTypesClient object as shown above

// Define the event type patch to update the event type with
EventTypePatch patch = new EventTypePatch();
patch.setOp(EventTypePatch.OperationEnum.REPLACE);
patch.setPath("/fields/<fieldName>/required");
patch.setValue(Boolean.FALSE);

EventType updatedEventType = null;
try {
    updatedEventType = event_type_client.updateEventType("<event_type_id>", "<etag>", eventTypePatch);
} catch (MindsphereException e) {
    // Exception Handling
}

通过 ID 读取事件类型

//Construct EventTypesClient object as shown above

EventType event_type = null;
try {
    event_type = event_type_client.getEventTypesById("<event_type_id>", "<ifNoneMatch>");
} catch (MindsphereException e) {
    // Exception Handling
}

通过 ID 删除事件类型

// Construct the EventTypesClient object as shown above

try {
    event_type_client.deleteEventType("<event_type_id>", "<ifMatch>");
} catch (MindsphereException ex) {
    // Exception Handling
}

事件操作

事件客户端创建、读取、更新标准和自定义事件。

租户名称: EventsClient

列出所有事件

说明

默认情况下,响应的每一页有20个元素。

注意

getEvents 方法返回 BaseEvent 对象的分页列表。这些对象必须类型化,才能用作 MindsphereStandardEventCustomEvent,如下所示。

列出所有标准事件

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


Events events = null;
try {
    events = eventClient.getEvents(<size>, <page>, "<filter>", "<sort>", "<ifNoneMatch>", history);
    if (events != null && events.getEmbedded() != null
            && events.getEmbedded().getEvents() != null && !events.getEmbedded().getEvents().isEmpty())
    {
        List<BaseEvent> eventList = events.getEmbedded().getEvents();

        // Typecast the list object to obtain a MindsphereStandardEvent object
        MindsphereStandardEvent standard_event = eventList.get(0);
    }
} catch (MindsphereException ex) {
    // Exception Handling
}

列出特定事件类型的所有事件

要获取特定(自定义)事件类型的事件,必须定义针对各自事件类型 ID 的过滤。例如,对于事件类型 ID e5e6460e-60da-4cc9-b4b7-a7af3cc9fca6

String filter = "{\"typeId\":\"e5e6460e-60da-4cc9-b4b7-a7af3cc9fca6\"}";

示例实现:

// Construct the EventsClient object as shown above

Events events = null;
try {
    String filter = "{\"type_id\":\"<event_type_id>\"}";
    events = eventClient.getEvents(<size>, <page>, filter, "<sort>", "<ifNoneMatch>", history);

    if (events != null && events.getEmbedded() != null
            && events.getEmbedded().getEvents() != null && !events.getEmbedded().getEvents().isEmpty())
    {
        List<BaseEvent> eventList = events.getEmbedded().getEvents();
        CustomEvent custom_event = (CustomEvent)eventList.get(0);
    }
} catch (MindsphereException ex) {
    // Exception Handling
}

创建标准事件

// Construct the EventsClient object as shown above

// Construct the MindSphereStandardEvent object to be created
MindSphereStandardEvent standard_event = new MindSphereStandardEventt();

// Set the base properties
standard_event.setCorrelationId("<correlation_id>");
standard_event.setEntityId("<entity_id>");
standard_event.setTimestamp("<timestamp>");

// Set the standard event properties
standard_event.setDescription("<description>");
standard_event.setSeverity(<severity>);
standard_event.setCode("<code>");
standard_event.setAcknowledged(<acknowledged>);

MindSphereStandardEvent created_standard_event = null;
try {
    created_standard_event = eventClient.createStandardEvent(standard_event);
} catch (MindsphereException ex) {
    // Exception Handling
}

或者,以 JSON 格式提供标准事件。

String standard_event_as_json = "{" +
                                "\"correlationId\":\"<correlation_id>\"," +
                                "\"timestamp\":\"<timestamp\"," +
                                "\"entityId\":\"<entity_id>\"," +
                                "\"severity\":<severity>," +
                                "\"description\":\"<description>\"," +
                                "\"code":\"<code>\"," +
                                "\"source\":\"source\"," +
                                "\"acknowledged\":\"<acknowledged>" +
                             "}";

MindSphereStandardEvent created_standard_event = null;
try {
    created_standard_event = (MindSphereStandardEvent) eventClient.createEvent(standard_event_as_json);
} catch (MindsphereException ex) {
    // Exception Handling
}

创建自定义事件

自定义事件是从用户定义的事件类型实例化的。

// Construct the EventsClient object as shown above

// Construct the CustomEvent object to be created
CustomEvent custom_event = new CustomEvent();

// Set the base properties
custom_event.setCorrelationId("<correlation_id>");
custom_event.setEntityId("<entity_id>");
custom_event.setTimestamp("<timestamp>");
custom_event.setTypeId("<event_type_id>");

// Set values for custom fields as hash maps using a key and value pair
custom_event.getFields().put("<custom_field_1>", "<custom_field_value1>");
custom_event.getFields().put("<custom_field_2>", "<custom_field_value2>");

CustomEvent created_custom_event = null;
try {
    created_custom_event = eventClient.createCustomEvent(custom_event);
} catch (MindsphereException ex) {
    // Exception Handling
}

或者,以 JSON 格式提供标自定义事件。

String custom_event_as_json = "{" +
                              "\"typeId\":\"<event_type_id>\"," +
                              "\"correlationId\":\"<correlation_id>\"," +
                              "\"timestamp\":\"<timestamp>\"," +
                              "\"entityId\":\"<entity_id>\"," +
                              "\"<custom_field_1>\":\"custom_field_value1\"," +
                              "\"<custom_field_2>\":\"custom_field_value2\"," +
                           "}";

CustomEvent created_custom_event = null;
try {
    created_custom_event = (CustomEvent)eventClient.createEvent(custom_event_as_json);
} catch (MindsphereException ex) {
    // Exception Handling
}

通过 ID 获取事件

// Construct the EventsClient object as shown above

BaseEvent event = null;
try {
    event = eventClient.getEventById("<event_id>");
    // Check the type of the retrieved event
    if (event != null && event instanceof MindsphereStandardEvent) {
        // Process the retrieved standard event
        MindsphereStandardEvent standard_event = (MindsphereStandardEvent) event;
    } else if (event != null && event instanceof CustomEvent) {
        // Process the retrieved custom event
        CustomEvent custom_event = (CustomEvent) event;
    }
} catch (MindsphereException ex) {
    // Exception Handling
}

更新事件

更新带有特定 ID 的事件。

// Construct the EventsClient object as shown above

BaseEvent event = null;
try {
    event = eventClient.updateEvents("<event_id>", "<event_json_string>", "<ifMatch>");
    // Check the type of the retrieved event as shown above for further processing
} catch (MindsphereException ex) {
    // Exception Handling
}

更新标准事件

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

// Construct the EventsClient object as shown above

MindSphereStandardEvent updated_standard_event = null;
try {
    updated_standard_event = eventClient.updateStandardEvent("<event_id>", "<standard_event>", "<ifMatch>");
} catch (MindsphereException ex) {
    // Exception Handling
}

更新自定义事件

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

// Construct the EventsClient object as shown above

CustomEvent custom_event = null;
try {
    custom_event = eventClient.updateCustomEvent("<event_id>", "<customEvent>", "<ifMatch>");
} catch (MindsphereException ex) {
    // Exception Handling
}

事件作业操作

作业客户端创建和检索作业以此来创建和删除事件。

客户端名称:JobsClient

创建作业以此创建事件

创建作业以此创建自定义事件

// Construct a JobsClient object
JobsClient jobs_client = JobsClient.builder()
                                 .restClientConfig(<config>)
                                 .build();

CustomEvent custom_event = new CustomEvent();
custom_event.setTypeId(eventTypeId1);

CustomEvent custom_event2 = new CustomEvent();
custom_event2.setTypeId(eventTypeId2);

List<BaseEvent> event_list = new ArrayList<>();
event_list.add(custom_event);
event_list.add(custom_event2);

CreateEventsJob create_events_job = new CreateEventsJob();
create_events_job.setEvents(event_list);

JobResource job_resource = null;

try {
    job_resource = jobs_client.createCreateEventsJob(create_events_job);
} catch (MindsphereException e) {
    // Exception handling
}

创建作业以此创建标准事件

MindSphereStandardEvent standard_event = new MindSphereStandardEvent();
MindSphereStandardEvent standard_event2 = new MindSphereStandardEvent();

List<BaseEvent> eventList = new ArrayList<>();
eventList.add(standard_event);
eventList.add(standard_event2);

CreateEventsJob create_events_job = new CreateEventsJob();
create_events_job.setEvents(eventList);

JobResource job_resource = null;

try {
    job_resource = jobs_client.createCreateEventsJob(create_events_job);
} catch (MindsphereException e) {
    // Exception handling
}

通过 ID 获取作业以此创建事件

//Construct JobsClient object as shown above

String job_id = "<createEventsJobId>";
CreateJobResource create_job_resource = null;
try {
    create_job_resource = jobs_client.getCreateEventsJobById(job_id);
} catch (MindsphereException e) {
    // Exception handling
}

创建作业以此删除事件

创建作业来删除满足查询参数的事件。查询参数在DeleteEventsJob类的过滤器方法中列出。

// Create a map of events to be deleted
HashMap<String, String> filter = new HashMap<>();
filter.put("typeId", "com.siemens.mindsphere.eventmgmt.event.type.MindSphereStandardEvent");

// Create the DeleteEventsJob instance
DeleteEventsJob delete_events_job = new DeleteEventsJob();
delete_events_job.setFilter(filter);

JobResource job_event = null;
try {
    job_event = jobs_client.createDeleteEventsJob(delete_events_job);
} catch (MindsphereException e) {
    // Exception handling
}

通过 ID 获取作业以此删除事件

//Construct JobsClient object as shown above

String job_id = "<delete_events_job_id>";
DeleteJobResource delete_job_resource = null;
try {
    delete_job_resource = jobs_client.getDeleteEventsJobById(job_id);
} catch (MindsphereException e) {
    // Exception handling
}

还有问题?

向社区提问


除非另行声明,该网站内容遵循MindSphere开发许可协议.


Last update: July 11, 2019