# huaweicloud-dris-samples-traffic-events-java
**Repository Path**: HuaweiCloudDeveloper/huaweicloud-dris-samples-traffic-events-java
## Basic Information
- **Project Name**: huaweicloud-dris-samples-traffic-events-java
- **Description**: 华为云提供了IoT设备接入SDK,您可以直接集成服务端SDK来调用DRIS的相关API,从而实现长期交通事件管理功能。该示例展示了如何通过java版SDK来管理长期交通事件。
- **Primary Language**: Unknown
- **License**: Apache-2.0
- **Default Branch**: master-dev
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2023-01-03
- **Last Updated**: 2025-06-16
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
## 版本说明
本示例基于华为云SDK V3.0版本开发
## 功能介绍
华为云提供了DRIS服务端SDK,您可以直接集成服务端SDK来调用DRIS的相关API,对道路设备和感知事件等资源进行相关操作,该示例展示了如何通过java版SDK来管理长期交通事件。
## 前置条件
- 1、获取华为云开发工具包(SDK),您也可以查看安装JAVA SDK。
- 2、要使用华为云 Java SDK,您需要拥有华为云账号以及该账号对应的 Access Key(AK)和 Secret Access Key(SK)。
- 3、华为云 Java SDK 支持 **Java JDK 1.8** 及其以上版本。
### SDK获取和安装
您可以通过Maven配置所依赖的主机迁移服务SDK。
```xml
com.huaweicloud.sdk
huaweicloud-sdk-dris
3.1.22
```
## 示例代码
```java
public class TrafficEventsSolutionDemo {
private static final Logger logger = LoggerFactory.getLogger(TrafficEventsSolutionDemo.class);
public static void main(String[] args) {
String ak = "";
String sk = "";
// 创建DrisClient实例。
ICredential auth = new BasicCredentials()
.withAk(ak)
.withSk(sk);
DrisClient client = DrisClient.newBuilder()
.withCredential(auth)
.withRegion(DrisRegion.valueOf("cn-north-4"))
.build();
try {
String trafficEventId = createTrafficEvent(client);
updateTrafficEvent(client, trafficEventId);
listTrafficEvents(client);
showTrafficEvent(client, trafficEventId);
deleteTrafficEvent(client, trafficEventId);
} catch (ConnectionException e) {
logger.error("ConnectionException", e);
} catch (RequestTimeoutException e) {
logger.error("RequestTimeoutException", e);
} catch (ServiceResponseException e) {
logger.error("ServiceResponseException", e);
System.out.println(e.getHttpStatusCode());
System.out.println(e.getErrorCode());
System.out.println(e.getErrorMsg());
}
}
private static String createTrafficEvent(DrisClient client) {
CreateTrafficEventRequest request = new CreateTrafficEventRequest();
AddTrafficEventDTO body = new AddTrafficEventDTO();
// 设置事件来源类型,取值范围:police:警方数据、government:政府数据、rsu:RSU上报数据等
body.setEventSourceType("rsu");
// 参考接口说明 https://support.huaweicloud.com/api-v2x/v2x_04_0101.html
body.setEventClass("AbnormalTraffic");
body.setEventType(100);
// 参数说明:区域码,参考区域码查询。
body.setAreaCode(518051);
// 设置事件位置中心坐标
EventLocation eventLocation = new EventLocation();
eventLocation.setLat(BigDecimal.valueOf(35.01));
eventLocation.setLon(BigDecimal.valueOf(113.02));
body.setEventPosition(eventLocation);
// 设置事件位置参考路径
EventReferencePath referencePath = new EventReferencePath();
referencePath.setPathRadius(100);
List path = new ArrayList<>();
EventLocation pathPoint01 = new EventLocation();
pathPoint01.setLat(BigDecimal.valueOf(35.0101));
pathPoint01.setLon(BigDecimal.valueOf(113.002));
path.add(pathPoint01);
EventLocation pathPoint02 = new EventLocation();
pathPoint02.setLat(BigDecimal.valueOf(35.0102));
pathPoint02.setLon(BigDecimal.valueOf(113.002));
path.add(pathPoint02);
referencePath.setActivePath(path);
List referencePaths = new ArrayList<>();
referencePaths.add(referencePath);
body.setReferencePaths(referencePaths);
// 设置事件所在位置说明
body.setEventPositionName("北环大道1010");
// 设置事件影响开始时间和结束时间
body.setStartTime("2023-01-01T01:37:01Z");
body.setEndTime("2023-01-02T01:37:01Z");
request.withBody(body);
CreateTrafficEventResponse response = client.createTrafficEvent(request);
System.out.println(response.toString());
return response.getEventId();
}
private static void updateTrafficEvent(DrisClient client, String trafficEventId) {
UpdateTrafficEventRequest request = new UpdateTrafficEventRequest();
request.setEventId(trafficEventId);
UpdateTrafficEventDTO body = new UpdateTrafficEventDTO();
body.setEventPositionName("北环大道1011");
request.withBody(body);
UpdateTrafficEventResponse response = client.updateTrafficEvent(request);
System.out.println(response.toString());
}
private static void listTrafficEvents(DrisClient client) {
BatchShowTrafficEventsRequest request = new BatchShowTrafficEventsRequest();
BatchShowTrafficEventsResponse response = client.batchShowTrafficEvents(request);
System.out.println(response.toString());
}
private static void showTrafficEvent(DrisClient client, String trafficEventId) {
ShowTrafficEventRequest request = new ShowTrafficEventRequest();
request.setEventId(trafficEventId);
ShowTrafficEventResponse response = client.showTrafficEvent(request);
System.out.println(response.toString());
}
private static void deleteTrafficEvent(DrisClient client, String trafficEventId) {
DeleteTrafficEventRequest request = new DeleteTrafficEventRequest();
request.setEventId(trafficEventId);
DeleteTrafficEventResponse response = client.deleteTrafficEvent(request);
System.out.println(response.toString());
}
}
```
相关参数说明如下所示:
- ak:华为云账号Access Key
- sk:华为云账号Secret Access Key
- region:服务所在区域,当前支持北京四,即"cn-north-4"
## 参考
更多信息请参考[API Explorer](https://console.ulanqab.huawei.com/apiexplorer/#/apidoc/DRIS)
## 修订记录
| 发布日期 | 文档版本 | 修订说明 |
| :--------: | :------: | :----------: |
| 2023-01-16 | 1.0 | 文档首次发布 |