@ -1,5 +1,6 @@
package com.cjy.devicedocking.utils ;
import com.cjy.devicedocking.controller.UdpController ;
import org.springframework.beans.factory.annotation.Value ;
import org.springframework.stereotype.Component ;
@ -55,6 +56,7 @@ public class UDPUtil {
private String uploudUrl ;
private static DatagramSocket socket ;
private static int j = 1 ;
/ * *
* 中间件测试命令 message固定
@ -67,46 +69,6 @@ public class UDPUtil {
return sendAndReceive ( message ) ;
}
/ * *
* @param virtualDeviceId 中间件id
* @param terminalEquipmentId 终端设备id
* @param volume 音量取值范围 0 ~ 127
* @return
* @throws Exception
* /
public String updateDeviceVolume ( String virtualDeviceId , String terminalEquipmentId , int volume ) throws Exception {
String message = requestHeader + deviceIdConvert ( virtualDeviceId ) + "00000800" + "0C" + "0000" ;
//十六进制字符串到字节数组
byte [ ] buffer = hexStringToByteArray ( message ) ;
//计算 ChkSum 即前15位的和
message + = sumChkSum ( buffer ) ;
message + = deviceIdConvert ( terminalEquipmentId ) + convert ( volume ) + "000000" ;
return sendAndReceive ( message ) ;
}
/ * *
* @param virtualDeviceId 中间件id
* @param fileName 文件名称
* @return
* @throws Exception
* /
public String uploadFile ( String virtualDeviceId , String fileName ) throws Exception {
int i = ( uploudUrl + fileName ) . length ( ) + 3 ;
String file = digestConversion ( uploudUrl . replace ( "\\" , "/" ) + fileName ) + "00" ;
String message = requestHeader + deviceIdConvert ( virtualDeviceId ) + "0000" + convert ( i ) + "000F" + "0000" ;
//十六进制字符串到字节数组
byte [ ] buffer = hexStringToByteArray ( message ) ;
//计算 ChkSum 即前15位的和
message + = sumChkSum ( buffer ) ;
message + = "0000" + file ;
// return sendAndReceive(message);
return message ;
}
/ * *
* Cmd = 0x01 / Cmd = 0x11
* Len = 52 ,
@ -120,8 +82,8 @@ public class UDPUtil {
public String initializationDevice ( String virtualDeviceId , String describe ) throws Exception {
//参数拼接顺序为 //|--Header ---|------DID -----|--SN=0--|--Len=52--|--Cmd=0--|---------------| ChkSum |DID|管理码|设备描述|服务器的IP地址|服务器连接端口|服务器控制端口|服务器采播端口|保留
String message = requestHeader + "000000000000340001000044" + deviceIdConvert ( virtualDeviceId ) + deviceIdConvert ( managementCode ) + digestConversion ( describe ) + ipToHexadecimal ( ipAddress ) + severConnectPort + serverControlPort + serverCapturePort + "0000" ;
// return sendAndReceive(message);
return message ;
return sendAndReceive ( message ) ;
// return message;
}
/ * *
@ -138,8 +100,8 @@ public class UDPUtil {
byte [ ] buffer = hexStringToByteArray ( message ) ;
//计算 ChkSum 即前15位的和
message + = sumChkSum ( buffer ) ;
// return sendAndReceive(message);
return message ;
return sendAndReceive ( message ) ;
// return message;
}
/ * *
@ -159,7 +121,7 @@ public class UDPUtil {
//计算 ChkSum 即前15位的和
message + = sumChkSum ( buffer ) ;
return sendAndReceive ( message ) ;
// return message;
// return message;
}
/ * *
@ -185,56 +147,57 @@ public class UDPUtil {
/ * *
* 可编辑任务
*
* @param virtualDeviceId 中间件id
* @param fileId 文件id或者播放列表 文件是4为 : 0001 列表是 1 2
* @param playMethod 播放方式
* @param deviceCode 终端设备id或者终端设备分组id
* @param taskName 任务名称
* @param fileId 文件id或者播放列表 文件是4为 : 0001 列表是 1
* @param playMethod 播放方式
* @param deviceCode 终端设备id或者终端设备分组id
* @param taskName 任务名称
* @return
* @throws Exception
* /
public String editableTasks ( String virtualDeviceId , List < String > fileId , Integer playMethod , List < String > deviceCode , String taskName ) throws Exception {
// fe e0 a7 8a/ 15 11 00 00 /00 00 40 00/ 07 00 00 7c/ 00 00 00 20 /00 01 01 00 00 00/ 00 01 01 00 00 00 /00 02/ 02 01 /03 00 00 00 /08 54 41 53 4b 36 37 38 39 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00/ 01 00 00 00/ bc 1a 00 00
//(从第16个开始的)任务编号/保留/类型/定时类型、定时模式/起始时间,年月日时分秒,这里无效,但必须是合法值/结束时间,年月日时分秒,这里无效,但必须是合法值/播放内容数量F=2(奇数个文件加1改为偶数),播放终端数量D=1/音量/任务名称/文件名称/设备名称
String newFileId = "" ;
String newDeviceCode = "" ;
for ( int i = 0 ; i < fileId . size ( ) ; i + + ) {
if ( fileId . get ( i ) . length ( ) = = 4 ) {
if ( i = = fileId . size ( ) - 1 & & fileId . size ( ) % 2 = = 1 ) {
newFileId + = deviceIdConvert ( fileId . get ( i ) ) + "0000" ;
} else {
newFileId + = deviceIdConvert ( fileId . get ( i ) ) ;
public String editableTasks ( String virtualDeviceId , List < String > fileId , Integer playMethod , List < String > deviceCode , String taskName ) throws Exception {
//(从第16个开始的)任务编号/保留/类型/定时类型、定时模式/起始时间,年月日时分秒,这里无效,但必须是合法值/结束时间,年月日时分秒,这里无效,但必须是合法值/播放内容数量F=2(奇数个文件加1改为偶数),播放终端数量D=1/音量/任务名称/文件名称/设备名称
String newFileId = "" ;
String newDeviceCode = "" ;
for ( int i = 0 ; i < fileId . size ( ) ; i + + ) {
if ( fileId . get ( i ) . length ( ) = = 4 ) {
if ( i = = fileId . size ( ) - 1 & & fileId . size ( ) % 2 = = 1 ) {
newFileId + = deviceIdConvert ( fileId . get ( i ) ) + "0000" ;
} else {
newFileId + = deviceIdConvert ( fileId . get ( i ) ) ;
}
} else {
if ( i = = fileId . size ( ) - 1 & & fileId . size ( ) % 2 = = 1 ) {
newFileId + = convertTheListId ( Integer . parseInt ( fileId . get ( i ) ) ) ;
} else {
newFileId + = convertTheListIdeven ( Integer . parseInt ( fileId . get ( i ) ) ) ;
} else {
if ( i = = fileId . size ( ) - 1 & & fileId . size ( ) % 2 = = 1 ) {
newFileId + = convertTheListId ( Integer . parseInt ( fileId . get ( i ) ) ) ;
} else {
newFileId + = convertTheListIdeven ( Integer . parseInt ( fileId . get ( i ) ) ) ;
}
}
}
for ( String deviceCodeItem : deviceCode ) {
if ( deviceCodeItem . length ( ) > 3 ) {
newDeviceCode + = deviceIdConvert ( deviceCodeItem ) ;
} else {
newDeviceCode + = convertGroupId ( Integer . parseInt ( deviceCodeItem ) ) ;
for ( String deviceCodeItem : deviceCode ) {
if ( deviceCodeItem . length ( ) > 3 ) {
newDeviceCode + = deviceIdConvert ( deviceCodeItem ) ;
} else {
newDeviceCode + = convertGroupId ( Integer . parseInt ( deviceCodeItem ) ) ;
}
}
String dataInfo = "00" + "00" + "00" + "20" + "000101000000" + "000101000000" + "00" + convert ( playMethod ) + "0201" + "03000000" + digestConversion ( taskName ) + newFileId + newDeviceCode ;
String dataInfo = "00" + "00" + "00" + "20" + "000101000000" + "000101000000" + "00" + convert ( playMethod ) + "0201" + "03000000" + digestConversion ( taskName ) + newFileId + newDeviceCode ;
//这个值 应该是 64=56+F*2+D*4 (注:F是文件数量 D是设备个数)
int len = 56 + ( ( fileId . size ( ) % 2 = = 1 ? ( fileId . size ( ) + 1 ) : fileId . size ( ) ) * 2 ) + ( deviceCode . size ( ) * 4 ) ;
int len = 56 + ( ( fileId . size ( ) % 2 = = 1 ? ( fileId . size ( ) + 1 ) : fileId . size ( ) ) * 2 ) + ( deviceCode . size ( ) * 4 ) ;
// 20暂时不动
String message = requestHeader + deviceIdConvert ( virtualDeviceId ) + "0000" + convert ( len ) + "0007" + "0000" ;
String message = requestHeader + deviceIdConvert ( virtualDeviceId ) + "0000" + convert ( len ) + "0007" + "0000" ;
byte [ ] buffer = hexStringToByteArray ( message ) ;
message + = sumChkSum ( buffer ) ;
message + = dataInfo ;
// return sendAndReceive(message);
return message ;
message + = sumChkSum ( buffer ) ;
message + = dataInfo ;
return sendAndReceive ( message ) ;
// return message;
}
//FE E0 A7 8A 16 11 00 00 00 00 00 00 05 01 00 3c
/ * *
* 通过中间件获取设备状态信息
*
@ -242,17 +205,17 @@ public class UDPUtil {
* @return
* @throws Exception
* /
public String getDeviceState ( String virtualDeviceId ) throws Exception {
public String getDeviceState ( String virtualDeviceId , String para ) throws Exception {
// |固定请求头|虚拟设备id(中间件id)| |Cmd| |
// String message = requestHeader + deviceIdConvert(virtualDeviceId) + "00000000" + "01" + "04 00";
String message = requestHeader + deviceIdConvert ( virtualDeviceId ) + "00000000" + "05" + "04 00" ;
// String message = requestHeader + deviceIdConvert(virtualDeviceId) + "00000000" + "05" + para+" 00";
String message = requestHeader + deviceIdConvert ( virtualDeviceId ) + "00000000" + "05" + para + "00" ;
//十六进制字符串到字节数组
byte [ ] buffer = hexStringToByteArray ( message ) ;
//计算 ChkSum 即前15位的和
message + = sumChkSum ( buffer ) ;
System . out . println ( message ) ;
// return sendAndReceive(message);
return message ;
return sendAndReceive ( message ) ;
// return message;
}
public String getSendNextData ( String virtualDeviceId , Integer sn ) throws Exception {
@ -273,6 +236,7 @@ public class UDPUtil {
message + = sumChkSum ( buffer ) ;
System . out . println ( message ) ;
return sendAndReceive ( message ) ;
// return message;
}
/ * *
@ -306,6 +270,62 @@ public class UDPUtil {
return hexString ;
}
/ * *
* @param virtualDeviceId 中间件id
* @param terminalEquipmentId 终端设备id
* @param volume 音量取值范围 0 ~ 127
* @return
* @throws Exception
* /
public String updateDeviceVolume ( String virtualDeviceId , String terminalEquipmentId , int volume ) throws Exception {
String message = requestHeader + deviceIdConvert ( virtualDeviceId ) + "00000800" + "0C" + "0000" ;
//十六进制字符串到字节数组
byte [ ] buffer = hexStringToByteArray ( message ) ;
//计算 ChkSum 即前15位的和
message + = sumChkSum ( buffer ) ;
message + = deviceIdConvert ( terminalEquipmentId ) + convert ( volume ) + "000000" ;
return sendAndReceive ( message ) ;
}
/ * *
* @param virtualDeviceId 中间件id
* @param fileName 文件名称
* @return
* @throws Exception
* /
public String uploadFile ( String virtualDeviceId , String fileName ) throws Exception {
int i = ( uploudUrl + fileName ) . length ( ) + 3 ;
String file = digestConversion ( uploudUrl . replace ( "\\" , "/" ) + fileName ) + "00" ;
String message = requestHeader + deviceIdConvert ( virtualDeviceId ) + "0000" + convert ( i ) + "000F" + "0000" ;
//十六进制字符串到字节数组
byte [ ] buffer = hexStringToByteArray ( message ) ;
//计算 ChkSum 即前15位的和
message + = sumChkSum ( buffer ) ;
message + = "0000" + file ;
String returnString = sendAndReceive ( message ) ;
//下载最好调用一下
// String response = this.getDeviceState(virtualDeviceId,"04");
// Boolean iscycle = true;
// String responseData = "";
// if (response.substring(28, 30).equals("00")) {
// while (iscycle) {
// responseData = this.getSendNextData(virtualDeviceId, j++);
// //16-20是01的话 表示已经是最后页了
//
// if ("80".equals(responseData.substring(18, 20))) {
// //如果是01表示最后一页结束循环
// iscycle = false;
// String lastDataString =responseData.substring(responseData.length()-272,responseData.length());
// System.out.println(responseData);
// }
// }
//
// }
return returnString ;
// return message;
}
//字符串转byte
public static byte [ ] hexToByteArray ( String hexString ) {
int len = hexString . length ( ) ;
@ -368,7 +388,6 @@ public class UDPUtil {
}
//这个是 备注转换的逻辑
public static String digestConversion ( String str ) {
byte [ ] bytes = str . getBytes ( Charset . forName ( "GBK" ) ) ;
@ -437,17 +456,24 @@ public class UDPUtil {
// System.out.println(convertGroupId(1));
// System.out.println(convertTheListId(1));
System . out . println ( deviceIdConvert ( "0002" ) ) ;
System . out . println ( convertTheListId ( 256 ) ) ;
// System.out.println(deviceIdConvert("0002"));
// System.out.println(convertTheListId(256));
// String responseData = "FEE0A78B151100000280D8008D040021";
// System.out.println(responseData.substring(18, 20));
String responseData = "FEE0A78B151100000280D8008D0400210000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000150000FF100100000631382E6D703300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" ;
String lastDataString = responseData . substring ( responseData . length ( ) - 272 , responseData . length ( ) ) ;
System . out . println ( lastDataString ) ;
}
public static String convertGroupId ( int decimal ) {
public static String convertGroupId ( int decimal ) {
String output = String . format ( "%02XFFFFFF" , decimal ) ;
return output ;
}
public static String convertTheListId ( int decimal ) {
public static String convertTheListId ( int decimal ) {
String output ;
if ( decimal < 256 ) {
output = String . format ( "%02Xff0000" , decimal ) ;
@ -458,8 +484,9 @@ public class UDPUtil {
}
return output ;
}
//偶数的时候
public static String convertTheListIdeven ( int decimal ) {
public static String convertTheListIdeven ( int decimal ) {
String output ;
if ( decimal < 256 ) {
output = String . format ( "%02Xff" , decimal ) ;