SipUtils.java 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. package com.genersoft.iot.vmp.gb28181.utils;
  2. import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel;
  3. import com.genersoft.iot.vmp.gb28181.bean.RemoteAddressInfo;
  4. import com.genersoft.iot.vmp.utils.GitUtil;
  5. import com.genersoft.iot.vmp.vmanager.gb28181.ptz.PtzController;
  6. import gov.nist.javax.sip.address.AddressImpl;
  7. import gov.nist.javax.sip.address.SipUri;
  8. import gov.nist.javax.sip.header.Subject;
  9. import gov.nist.javax.sip.message.SIPRequest;
  10. import org.springframework.util.ObjectUtils;
  11. import javax.sip.PeerUnavailableException;
  12. import javax.sip.SipFactory;
  13. import javax.sip.header.FromHeader;
  14. import javax.sip.header.Header;
  15. import javax.sip.header.UserAgentHeader;
  16. import javax.sip.message.Request;
  17. import java.text.ParseException;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import java.util.UUID;
  21. //import java.util.logging.Logger;
  22. /**
  23. * @author panlinlin
  24. * @version 1.0.0
  25. * @description JAIN SIP的工具类
  26. * @createTime 2021年09月27日 15:12:00
  27. */
  28. public class SipUtils {
  29. // private final static Logger logger = LoggerFactory.getLogger(PtzController.class);
  30. public static String getUserIdFromFromHeader(Request request) {
  31. FromHeader fromHeader = (FromHeader)request.getHeader(FromHeader.NAME);
  32. return getUserIdFromFromHeader(fromHeader);
  33. }
  34. /**
  35. * 从subject读取channelId
  36. * */
  37. public static String getChannelIdFromRequest(Request request) {
  38. Header subject = request.getHeader("subject");
  39. if (subject == null) {
  40. // 如果缺失subject
  41. return null;
  42. }
  43. return ((Subject) subject).getSubject().split(":")[0];
  44. }
  45. public static String getUserIdFromFromHeader(FromHeader fromHeader) {
  46. AddressImpl address = (AddressImpl)fromHeader.getAddress();
  47. SipUri uri = (SipUri) address.getURI();
  48. return uri.getUser();
  49. }
  50. public static String getNewViaTag() {
  51. return "z9hG4bK" + System.currentTimeMillis();
  52. }
  53. public static UserAgentHeader createUserAgentHeader(SipFactory sipFactory, GitUtil gitUtil) throws PeerUnavailableException, ParseException {
  54. List<String> agentParam = new ArrayList<>();
  55. agentParam.add("Hfy-GB v1.1");
  56. if (gitUtil != null && gitUtil.getCommitTime() != null) {
  57. // agentParam.add(gitUtil.getBuildVersion() + ".");
  58. // agentParam.add(gitUtil.getCommitTime());
  59. }
  60. return sipFactory.createHeaderFactory().createUserAgentHeader(agentParam);
  61. }
  62. public static String getNewFromTag(){
  63. return UUID.randomUUID().toString().replace("-", "");
  64. // return getNewTag();
  65. }
  66. public static String getNewTag(){
  67. return String.valueOf(System.currentTimeMillis());
  68. }
  69. /**
  70. * 云台指令码计算
  71. *
  72. * @param leftRight 镜头左移右移 0:停止 1:左移 2:右移
  73. * @param upDown 镜头上移下移 0:停止 1:上移 2:下移
  74. * @param inOut 镜头放大缩小 0:停止 1:缩小 2:放大 3:聚焦
  75. * @param moveSpeed 镜头移动速度 默认 0XFF (0-255)
  76. * @param zoomSpeed 镜头缩放速度 默认 0X1 (0-255)
  77. */
  78. public static String cmdString(int leftRight, int upDown, int inOut, int moveSpeed, int zoomSpeed) {
  79. int cmdCode = 0;
  80. if (leftRight == 2) {
  81. cmdCode|=0x01; // 右移
  82. } else if(leftRight == 1) {
  83. cmdCode|=0x02; // 左移
  84. }
  85. if (upDown == 2) {
  86. cmdCode|=0x04; // 下移
  87. } else if(upDown == 1) {
  88. cmdCode|=0x08; // 上移
  89. }
  90. if (inOut == 2) {
  91. cmdCode |= 0x10; // 放大
  92. } else if(inOut == 1) {
  93. cmdCode |= 0x20; // 缩小
  94. }else if(inOut == 3){
  95. // TODO: 2022/12/13 聚焦code 待商榷
  96. cmdCode |= 0x30; // 聚焦
  97. }
  98. StringBuilder builder = new StringBuilder("A50F01");
  99. //A5 0F 01 01 ff ff
  100. String strTmp;
  101. strTmp = String.format("%02X", cmdCode);
  102. builder.append(strTmp, 0, 2);
  103. strTmp = String.format("%02X", moveSpeed);// 云台 +- => 15
  104. builder.append(strTmp, 0, 2);
  105. builder.append(strTmp, 0, 2);
  106. strTmp = String.format("%X", zoomSpeed);// 镜头 abcd => 00 15
  107. builder.append(strTmp, 0, 1).append("0");
  108. //计算校验码
  109. int checkCode = (0XA5 + 0X0F + 0X01 + cmdCode + moveSpeed + moveSpeed + (zoomSpeed /*<< 4*/ & 0XF0)) % 0X100;
  110. strTmp = String.format("%02X", checkCode);
  111. builder.append(strTmp, 0, 2);
  112. return builder.toString();
  113. }
  114. /**
  115. * 从请求中获取设备ip地址和端口号
  116. * @param request 请求
  117. * @param sipUseSourceIpAsRemoteAddress false 从via中获取地址, true 直接获取远程地址
  118. * @return 地址信息
  119. */
  120. public static RemoteAddressInfo getRemoteAddressFromRequest(SIPRequest request, boolean sipUseSourceIpAsRemoteAddress) {
  121. String remoteAddress;
  122. int remotePort;
  123. if (sipUseSourceIpAsRemoteAddress) {
  124. remoteAddress = request.getRemoteAddress().getHostAddress();
  125. remotePort = request.getRemotePort();
  126. }else {
  127. // 判断RPort是否改变,改变则说明路由nat信息变化,修改设备信息
  128. // 获取到通信地址等信息
  129. remoteAddress = request.getTopmostViaHeader().getReceived();
  130. remotePort = request.getTopmostViaHeader().getRPort();
  131. // 解析本地地址替代
  132. if (ObjectUtils.isEmpty(remoteAddress) || remotePort == -1) {
  133. remoteAddress = request.getTopmostViaHeader().getHost();
  134. remotePort = request.getTopmostViaHeader().getPort();
  135. }
  136. }
  137. return new RemoteAddressInfo(remoteAddress, remotePort);
  138. }
  139. public static DeviceChannel updateGps(DeviceChannel deviceChannel, String geoCoordSys) {
  140. if (deviceChannel.getLongitude()*deviceChannel.getLatitude() > 0) {
  141. if (geoCoordSys == null) {
  142. geoCoordSys = "WGS84";
  143. }
  144. if ("WGS84".equals(geoCoordSys)) {
  145. deviceChannel.setLongitudeWgs84(deviceChannel.getLongitude());
  146. deviceChannel.setLatitudeWgs84(deviceChannel.getLatitude());
  147. Double[] position = Coordtransform.WGS84ToGCJ02(deviceChannel.getLongitude(), deviceChannel.getLatitude());
  148. deviceChannel.setLongitudeGcj02(position[0]);
  149. deviceChannel.setLatitudeGcj02(position[1]);
  150. }else if ("GCJ02".equals(geoCoordSys)) {
  151. deviceChannel.setLongitudeGcj02(deviceChannel.getLongitude());
  152. deviceChannel.setLatitudeGcj02(deviceChannel.getLatitude());
  153. Double[] position = Coordtransform.GCJ02ToWGS84(deviceChannel.getLongitude(), deviceChannel.getLatitude());
  154. deviceChannel.setLongitudeWgs84(position[0]);
  155. deviceChannel.setLatitudeWgs84(position[1]);
  156. }else {
  157. deviceChannel.setLongitudeGcj02(0.00);
  158. deviceChannel.setLatitudeGcj02(0.00);
  159. deviceChannel.setLongitudeWgs84(0.00);
  160. deviceChannel.setLatitudeWgs84(0.00);
  161. }
  162. }else {
  163. deviceChannel.setLongitudeGcj02(deviceChannel.getLongitude());
  164. deviceChannel.setLatitudeGcj02(deviceChannel.getLatitude());
  165. deviceChannel.setLongitudeWgs84(deviceChannel.getLongitude());
  166. deviceChannel.setLatitudeWgs84(deviceChannel.getLatitude());
  167. }
  168. return deviceChannel;
  169. }
  170. public static String cmdPtzString(int direction,int speed){
  171. int cmdCode = 0;
  172. if (direction == 4) {
  173. cmdCode|=0x01; // 右移 0 03
  174. } else if(direction == 3) {
  175. cmdCode|=0x02; // 左移 04
  176. }else if(direction == 2) {
  177. cmdCode|=0x04; // 下移 05
  178. }else if(direction == 1) {
  179. cmdCode|=0x08; // 上移 06
  180. }else if(direction == 5) {
  181. cmdCode |= 0x10; // 放大 07
  182. }else if(direction == 6) {
  183. cmdCode |= 0x20; // 缩小 08
  184. }else {
  185. cmdCode |= 0x00; // 停止 00
  186. }
  187. int moveSpeed =0 ;
  188. moveSpeed |= 0x00;
  189. int zoomSpeed =0 ;
  190. zoomSpeed |= speed;
  191. StringBuilder builder = new StringBuilder("A50F01");
  192. //A5 0F 01 01 ff ff
  193. String strTmp;
  194. strTmp = String.format("%02X", cmdCode);
  195. builder.append(strTmp, 0, 2);
  196. strTmp = String.format("%02X", moveSpeed);// 云台 +- => 15
  197. builder.append(strTmp, 0, 2);
  198. builder.append(strTmp, 0, 2);
  199. strTmp = String.format("%02X", zoomSpeed);// 镜头 abcd => 00 15
  200. builder.append(strTmp, 0, 2);
  201. //计算校验码
  202. int checkCode = (0XA5 + 0X0F + 0X01 + cmdCode + moveSpeed + moveSpeed + (zoomSpeed /*<< 4*/ & 0XF0)) % 0X100;
  203. strTmp = String.format("%02X", checkCode);
  204. builder.append(strTmp, 0, 2);
  205. // logger.info("cmd code --------{}",strTmp);
  206. return builder.toString();
  207. }
  208. }