添加了ZLM SDK的内容
This commit is contained in:
parent
34ec3d9310
commit
48a01752f2
|
|
@ -23,61 +23,5 @@ public class App
|
||||||
{
|
{
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
// ThingsBoard REST API URL
|
|
||||||
String url = "http://iot.t-aaron.com:18080";
|
|
||||||
// Default Tenant Administrator credentials
|
|
||||||
String username = "tenant@thingsboard.org";
|
|
||||||
String password = "tuoheng2023";
|
|
||||||
// Creating new rest client and auth with credentials
|
|
||||||
RestClient client = new RestClient(url);
|
|
||||||
client.login(username, password);
|
|
||||||
|
|
||||||
PageData<Device> tenantDevices;
|
|
||||||
PageLink pageLink = new PageLink(10);
|
|
||||||
do {
|
|
||||||
// Fetch all tenant devices using current page link and print each of them
|
|
||||||
tenantDevices = client.getTenantDevices("", pageLink);
|
|
||||||
// tenantDevices.getData().forEach(System.out::println);
|
|
||||||
for (Device device : tenantDevices.getData()) {
|
|
||||||
System.out.println(device.getName());
|
|
||||||
System.out.println(device.getId().getEntityType().name());
|
|
||||||
|
|
||||||
ArrayList arrayList = new ArrayList();
|
|
||||||
arrayList.add("connectorType");
|
|
||||||
arrayList.add("humidity");
|
|
||||||
arrayList.add("data.job_number");
|
|
||||||
List<AttributeKvEntry> attributeKvEntries = client.getAttributeKvEntries(device.getId(), arrayList);
|
|
||||||
|
|
||||||
for (AttributeKvEntry attributeKvEntry : attributeKvEntries) {
|
|
||||||
System.out.println(attributeKvEntry.getKey() + ":" + attributeKvEntry.getValue());
|
|
||||||
}
|
|
||||||
|
|
||||||
if(device.getId().getEntityType().name().equals("DEVICE")) {
|
|
||||||
List<String> kvEntries = client.getTimeseriesKeys(device.getId());
|
|
||||||
if(!CollectionsUtil.isEmpty(kvEntries)){
|
|
||||||
|
|
||||||
List<TsKvEntry> latest = client.getLatestTimeseries(device.getId(),kvEntries);
|
|
||||||
if(!CollectionsUtil.isEmpty(latest)){
|
|
||||||
for(TsKvEntry latestKvEntry : latest){
|
|
||||||
System.out.println(latestKvEntry.getKey() + ":" + latestKvEntry.getValue());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
pageLink = pageLink.nextPageLink();
|
|
||||||
} while (tenantDevices.hasNext());
|
|
||||||
|
|
||||||
// Get information of current logged in user and print it
|
|
||||||
// client.getUser().ifPresent(System.out::println);
|
|
||||||
|
|
||||||
// Perform logout of current user and close the client
|
|
||||||
|
|
||||||
|
|
||||||
client.logout();
|
|
||||||
client.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,74 @@
|
||||||
|
package com.tuoheng;
|
||||||
|
|
||||||
|
import org.thingsboard.rest.client.RestClient;
|
||||||
|
import org.thingsboard.server.common.data.Device;
|
||||||
|
import org.thingsboard.server.common.data.kv.AttributeKvEntry;
|
||||||
|
import org.thingsboard.server.common.data.kv.TsKvEntry;
|
||||||
|
import org.thingsboard.server.common.data.page.PageData;
|
||||||
|
import org.thingsboard.server.common.data.page.PageLink;
|
||||||
|
import org.thingsboard.server.common.data.util.CollectionsUtil;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class IOTClient {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
// ThingsBoard REST API URL
|
||||||
|
String url = "http://iot.t-aaron.com:18080";
|
||||||
|
// Default Tenant Administrator credentials
|
||||||
|
String username = "tenant@thingsboard.org";
|
||||||
|
String password = "tuoheng2023";
|
||||||
|
// Creating new rest client and auth with credentials
|
||||||
|
RestClient client = new RestClient(url);
|
||||||
|
client.login(username, password);
|
||||||
|
|
||||||
|
PageData<Device> tenantDevices;
|
||||||
|
PageLink pageLink = new PageLink(10);
|
||||||
|
do {
|
||||||
|
// Fetch all tenant devices using current page link and print each of them
|
||||||
|
tenantDevices = client.getTenantDevices("", pageLink);
|
||||||
|
// tenantDevices.getData().forEach(System.out::println);
|
||||||
|
for (Device device : tenantDevices.getData()) {
|
||||||
|
System.out.println(device.getName());
|
||||||
|
System.out.println(device.getId().getEntityType().name());
|
||||||
|
|
||||||
|
ArrayList arrayList = new ArrayList();
|
||||||
|
arrayList.add("connectorType");
|
||||||
|
arrayList.add("humidity");
|
||||||
|
arrayList.add("data.job_number");
|
||||||
|
List<AttributeKvEntry> attributeKvEntries = client.getAttributeKvEntries(device.getId(), arrayList);
|
||||||
|
|
||||||
|
for (AttributeKvEntry attributeKvEntry : attributeKvEntries) {
|
||||||
|
System.out.println(attributeKvEntry.getKey() + ":" + attributeKvEntry.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
if(device.getId().getEntityType().name().equals("DEVICE")) {
|
||||||
|
List<String> kvEntries = client.getTimeseriesKeys(device.getId());
|
||||||
|
if(!CollectionsUtil.isEmpty(kvEntries)){
|
||||||
|
|
||||||
|
List<TsKvEntry> latest = client.getLatestTimeseries(device.getId(),kvEntries);
|
||||||
|
if(!CollectionsUtil.isEmpty(latest)){
|
||||||
|
for(TsKvEntry latestKvEntry : latest){
|
||||||
|
System.out.println(latestKvEntry.getKey() + ":" + latestKvEntry.getValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
pageLink = pageLink.nextPageLink();
|
||||||
|
} while (tenantDevices.hasNext());
|
||||||
|
|
||||||
|
// Get information of current logged in user and print it
|
||||||
|
// client.getUser().ifPresent(System.out::println);
|
||||||
|
|
||||||
|
// Perform logout of current user and close the client
|
||||||
|
|
||||||
|
|
||||||
|
client.logout();
|
||||||
|
client.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,271 @@
|
||||||
|
package com.tuoheng;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
|
public class ZlmClient {
|
||||||
|
|
||||||
|
private static final String ZLM_BASE_URL = "http://114.67.89.4:8778";
|
||||||
|
private static final String SECRET = "su6TiedN2rVAmBbIDX0aa0QTiBJLBdcf";
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
try {
|
||||||
|
// 调用 startRecord 方法
|
||||||
|
// String result = startRecord(1, "__defaultVhost__", "live", "prod", 86400);
|
||||||
|
// System.out.println("startRecord 响应结果:");
|
||||||
|
// System.out.println(result);
|
||||||
|
//
|
||||||
|
// System.out.println("\n" + "=".repeat(50) + "\n");
|
||||||
|
|
||||||
|
// 调用 isRecording 方法
|
||||||
|
// String recordingStatus = isRecording(1, "__defaultVhost__", "live", "prod");
|
||||||
|
// System.out.println("isRecording 响应结果:");
|
||||||
|
// System.out.println(recordingStatus);
|
||||||
|
//
|
||||||
|
// System.out.println("\n" + "=".repeat(50) + "\n");
|
||||||
|
|
||||||
|
// 调用 getMp4RecordFile 方法
|
||||||
|
String mp4Files = getMp4RecordFile("__defaultVhost__", "live", "prod", "2025-12-10");
|
||||||
|
System.out.println("getMp4RecordFile 响应结果:");
|
||||||
|
System.out.println(mp4Files);
|
||||||
|
|
||||||
|
System.out.println("\n" + "=".repeat(50) + "\n");
|
||||||
|
|
||||||
|
// 调用 deleteRecordDirectory 方法
|
||||||
|
String deleteResult = deleteRecordDirectory("__defaultVhost__", "live", "prod", "2025-12-10");
|
||||||
|
System.out.println("deleteRecordDirectory 响应结果:");
|
||||||
|
System.out.println(deleteResult);
|
||||||
|
|
||||||
|
System.out.println("\n" + "=".repeat(50) + "\n");
|
||||||
|
|
||||||
|
// 调用 stopRecord 方法
|
||||||
|
// String stopResult = stopRecord(1, "__defaultVhost__", "live", "prod");
|
||||||
|
// System.out.println("stopRecord 响应结果:");
|
||||||
|
// System.out.println(stopResult);
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.err.println("调用失败: " + e.getMessage());
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 开始录制
|
||||||
|
*
|
||||||
|
* @param type 类型 (0 或 1)
|
||||||
|
* @param vhost 虚拟主机
|
||||||
|
* @param app 应用名
|
||||||
|
* @param stream 流ID
|
||||||
|
* @param maxSecond 最大录制时长(秒)
|
||||||
|
* @return 响应结果 JSON 字符串
|
||||||
|
* @throws Exception 如果请求失败
|
||||||
|
*/
|
||||||
|
public static String startRecord(int type, String vhost, String app, String stream, int maxSecond) throws Exception {
|
||||||
|
// 构建 URL 参数
|
||||||
|
StringBuilder urlBuilder = new StringBuilder(ZLM_BASE_URL);
|
||||||
|
urlBuilder.append("/index/api/startRecord?");
|
||||||
|
urlBuilder.append("secret=").append(URLEncoder.encode(SECRET, StandardCharsets.UTF_8.name()));
|
||||||
|
urlBuilder.append("&type=").append(type);
|
||||||
|
urlBuilder.append("&vhost=").append(URLEncoder.encode(vhost, StandardCharsets.UTF_8.name()));
|
||||||
|
urlBuilder.append("&app=").append(URLEncoder.encode(app, StandardCharsets.UTF_8.name()));
|
||||||
|
urlBuilder.append("&stream=").append(URLEncoder.encode(stream, StandardCharsets.UTF_8.name()));
|
||||||
|
urlBuilder.append("&max_second=").append(maxSecond);
|
||||||
|
|
||||||
|
// 创建 HTTP 连接
|
||||||
|
URL url = new URL(urlBuilder.toString());
|
||||||
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||||
|
connection.setRequestMethod("GET");
|
||||||
|
connection.setConnectTimeout(10000);
|
||||||
|
connection.setReadTimeout(10000);
|
||||||
|
|
||||||
|
// 获取响应
|
||||||
|
int responseCode = connection.getResponseCode();
|
||||||
|
if (responseCode == HttpURLConnection.HTTP_OK) {
|
||||||
|
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
|
||||||
|
StringBuilder response = new StringBuilder();
|
||||||
|
String line;
|
||||||
|
while ((line = reader.readLine()) != null) {
|
||||||
|
response.append(line);
|
||||||
|
}
|
||||||
|
reader.close();
|
||||||
|
return response.toString();
|
||||||
|
} else {
|
||||||
|
throw new Exception("HTTP 请求失败,状态码: " + responseCode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查录制状态
|
||||||
|
*
|
||||||
|
* @param type 类型 (0 或 1)
|
||||||
|
* @param vhost 虚拟主机
|
||||||
|
* @param app 应用名
|
||||||
|
* @param stream 流ID
|
||||||
|
* @return 响应结果 JSON 字符串
|
||||||
|
* @throws Exception 如果请求失败
|
||||||
|
*/
|
||||||
|
public static String isRecording(int type, String vhost, String app, String stream) throws Exception {
|
||||||
|
// 构建 URL 参数
|
||||||
|
StringBuilder urlBuilder = new StringBuilder(ZLM_BASE_URL);
|
||||||
|
urlBuilder.append("/index/api/isRecording?");
|
||||||
|
urlBuilder.append("secret=").append(URLEncoder.encode(SECRET, StandardCharsets.UTF_8.name()));
|
||||||
|
urlBuilder.append("&type=").append(type);
|
||||||
|
urlBuilder.append("&vhost=").append(URLEncoder.encode(vhost, StandardCharsets.UTF_8.name()));
|
||||||
|
urlBuilder.append("&app=").append(URLEncoder.encode(app, StandardCharsets.UTF_8.name()));
|
||||||
|
urlBuilder.append("&stream=").append(URLEncoder.encode(stream, StandardCharsets.UTF_8.name()));
|
||||||
|
|
||||||
|
// 创建 HTTP 连接
|
||||||
|
URL url = new URL(urlBuilder.toString());
|
||||||
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||||
|
connection.setRequestMethod("GET");
|
||||||
|
connection.setConnectTimeout(10000);
|
||||||
|
connection.setReadTimeout(10000);
|
||||||
|
|
||||||
|
// 获取响应
|
||||||
|
int responseCode = connection.getResponseCode();
|
||||||
|
if (responseCode == HttpURLConnection.HTTP_OK) {
|
||||||
|
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
|
||||||
|
StringBuilder response = new StringBuilder();
|
||||||
|
String line;
|
||||||
|
while ((line = reader.readLine()) != null) {
|
||||||
|
response.append(line);
|
||||||
|
}
|
||||||
|
reader.close();
|
||||||
|
return response.toString();
|
||||||
|
} else {
|
||||||
|
throw new Exception("HTTP 请求失败,状态码: " + responseCode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 停止录制
|
||||||
|
*
|
||||||
|
* @param type 类型 (0 或 1)
|
||||||
|
* @param vhost 虚拟主机
|
||||||
|
* @param app 应用名
|
||||||
|
* @param stream 流ID
|
||||||
|
* @return 响应结果 JSON 字符串
|
||||||
|
* @throws Exception 如果请求失败
|
||||||
|
*/
|
||||||
|
public static String stopRecord(int type, String vhost, String app, String stream) throws Exception {
|
||||||
|
// 构建 URL 参数
|
||||||
|
StringBuilder urlBuilder = new StringBuilder(ZLM_BASE_URL);
|
||||||
|
urlBuilder.append("/index/api/stopRecord?");
|
||||||
|
urlBuilder.append("secret=").append(URLEncoder.encode(SECRET, StandardCharsets.UTF_8.name()));
|
||||||
|
urlBuilder.append("&type=").append(type);
|
||||||
|
urlBuilder.append("&vhost=").append(URLEncoder.encode(vhost, StandardCharsets.UTF_8.name()));
|
||||||
|
urlBuilder.append("&app=").append(URLEncoder.encode(app, StandardCharsets.UTF_8.name()));
|
||||||
|
urlBuilder.append("&stream=").append(URLEncoder.encode(stream, StandardCharsets.UTF_8.name()));
|
||||||
|
|
||||||
|
// 创建 HTTP 连接
|
||||||
|
URL url = new URL(urlBuilder.toString());
|
||||||
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||||
|
connection.setRequestMethod("GET");
|
||||||
|
connection.setConnectTimeout(10000);
|
||||||
|
connection.setReadTimeout(10000);
|
||||||
|
|
||||||
|
// 获取响应
|
||||||
|
int responseCode = connection.getResponseCode();
|
||||||
|
if (responseCode == HttpURLConnection.HTTP_OK) {
|
||||||
|
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
|
||||||
|
StringBuilder response = new StringBuilder();
|
||||||
|
String line;
|
||||||
|
while ((line = reader.readLine()) != null) {
|
||||||
|
response.append(line);
|
||||||
|
}
|
||||||
|
reader.close();
|
||||||
|
return response.toString();
|
||||||
|
} else {
|
||||||
|
throw new Exception("HTTP 请求失败,状态码: " + responseCode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取MP4录制文件列表
|
||||||
|
*
|
||||||
|
* @param vhost 虚拟主机
|
||||||
|
* @param app 应用名
|
||||||
|
* @param stream 流ID
|
||||||
|
* @param period 日期 (格式: YYYY-MM-DD)
|
||||||
|
* @return 响应结果 JSON 字符串
|
||||||
|
* @throws Exception 如果请求失败
|
||||||
|
*/
|
||||||
|
public static String getMp4RecordFile(String vhost, String app, String stream, String period) throws Exception {
|
||||||
|
// 构建 URL 参数
|
||||||
|
StringBuilder urlBuilder = new StringBuilder(ZLM_BASE_URL);
|
||||||
|
urlBuilder.append("/index/api/getMp4RecordFile?");
|
||||||
|
urlBuilder.append("secret=").append(URLEncoder.encode(SECRET, StandardCharsets.UTF_8.name()));
|
||||||
|
urlBuilder.append("&vhost=").append(URLEncoder.encode(vhost, StandardCharsets.UTF_8.name()));
|
||||||
|
urlBuilder.append("&app=").append(URLEncoder.encode(app, StandardCharsets.UTF_8.name()));
|
||||||
|
urlBuilder.append("&stream=").append(URLEncoder.encode(stream, StandardCharsets.UTF_8.name()));
|
||||||
|
urlBuilder.append("&period=").append(URLEncoder.encode(period, StandardCharsets.UTF_8.name()));
|
||||||
|
|
||||||
|
// 创建 HTTP 连接
|
||||||
|
URL url = new URL(urlBuilder.toString());
|
||||||
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||||
|
connection.setRequestMethod("GET");
|
||||||
|
connection.setConnectTimeout(10000);
|
||||||
|
connection.setReadTimeout(10000);
|
||||||
|
|
||||||
|
// 获取响应
|
||||||
|
int responseCode = connection.getResponseCode();
|
||||||
|
if (responseCode == HttpURLConnection.HTTP_OK) {
|
||||||
|
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
|
||||||
|
StringBuilder response = new StringBuilder();
|
||||||
|
String line;
|
||||||
|
while ((line = reader.readLine()) != null) {
|
||||||
|
response.append(line);
|
||||||
|
}
|
||||||
|
reader.close();
|
||||||
|
return response.toString();
|
||||||
|
} else {
|
||||||
|
throw new Exception("HTTP 请求失败,状态码: " + responseCode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除录制目录
|
||||||
|
*
|
||||||
|
* @param vhost 虚拟主机
|
||||||
|
* @param app 应用名
|
||||||
|
* @param stream 流ID
|
||||||
|
* @param period 日期 (格式: YYYY-MM-DD)
|
||||||
|
* @return 响应结果 JSON 字符串
|
||||||
|
* @throws Exception 如果请求失败
|
||||||
|
*/
|
||||||
|
public static String deleteRecordDirectory(String vhost, String app, String stream, String period) throws Exception {
|
||||||
|
// 构建 URL 参数
|
||||||
|
StringBuilder urlBuilder = new StringBuilder(ZLM_BASE_URL);
|
||||||
|
urlBuilder.append("/index/api/deleteRecordDirectory?");
|
||||||
|
urlBuilder.append("secret=").append(URLEncoder.encode(SECRET, StandardCharsets.UTF_8.name()));
|
||||||
|
urlBuilder.append("&vhost=").append(URLEncoder.encode(vhost, StandardCharsets.UTF_8.name()));
|
||||||
|
urlBuilder.append("&app=").append(URLEncoder.encode(app, StandardCharsets.UTF_8.name()));
|
||||||
|
urlBuilder.append("&stream=").append(URLEncoder.encode(stream, StandardCharsets.UTF_8.name()));
|
||||||
|
urlBuilder.append("&period=").append(URLEncoder.encode(period, StandardCharsets.UTF_8.name()));
|
||||||
|
|
||||||
|
// 创建 HTTP 连接
|
||||||
|
URL url = new URL(urlBuilder.toString());
|
||||||
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||||
|
connection.setRequestMethod("GET");
|
||||||
|
connection.setConnectTimeout(10000);
|
||||||
|
connection.setReadTimeout(10000);
|
||||||
|
|
||||||
|
// 获取响应
|
||||||
|
int responseCode = connection.getResponseCode();
|
||||||
|
if (responseCode == HttpURLConnection.HTTP_OK) {
|
||||||
|
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
|
||||||
|
StringBuilder response = new StringBuilder();
|
||||||
|
String line;
|
||||||
|
while ((line = reader.readLine()) != null) {
|
||||||
|
response.append(line);
|
||||||
|
}
|
||||||
|
reader.close();
|
||||||
|
return response.toString();
|
||||||
|
} else {
|
||||||
|
throw new Exception("HTTP 请求失败,状态码: " + responseCode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue