| | |
| | | package com.genersoft.iot.vmp.gb28181.utils; |
| | | |
| | | import com.genersoft.iot.vmp.gb28181.bean.RemoteAddressInfo; |
| | | import com.genersoft.iot.vmp.utils.GitUtil; |
| | | import gov.nist.javax.sip.address.AddressImpl; |
| | | import gov.nist.javax.sip.address.SipUri; |
| | | import gov.nist.javax.sip.header.Subject; |
| | | import gov.nist.javax.sip.message.SIPRequest; |
| | | import org.springframework.util.ObjectUtils; |
| | | |
| | | import javax.sip.PeerUnavailableException; |
| | | import javax.sip.SipFactory; |
| | |
| | | return "z9hG4bK" + System.currentTimeMillis(); |
| | | } |
| | | |
| | | public static UserAgentHeader createUserAgentHeader(SipFactory sipFactory) throws PeerUnavailableException, ParseException { |
| | | public static UserAgentHeader createUserAgentHeader(SipFactory sipFactory, GitUtil gitUtil) throws PeerUnavailableException, ParseException { |
| | | List<String> agentParam = new ArrayList<>(); |
| | | agentParam.add("WVP PRO"); |
| | | // TODO 添加版本信息以及日期 |
| | | agentParam.add("WVP-Pro "); |
| | | if (gitUtil != null ) { |
| | | if (!ObjectUtils.isEmpty(gitUtil.getBuildVersion())) { |
| | | agentParam.add("v"); |
| | | agentParam.add(gitUtil.getBuildVersion() + "."); |
| | | } |
| | | if (!ObjectUtils.isEmpty(gitUtil.getCommitTime())) { |
| | | agentParam.add(gitUtil.getCommitTime()); |
| | | } |
| | | } |
| | | return sipFactory.createHeaderFactory().createUserAgentHeader(agentParam); |
| | | } |
| | | |
| | |
| | | return String.valueOf(System.currentTimeMillis()); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 云台指令码计算 |
| | | * |
| | | * @param leftRight 镜头左移右移 0:停止 1:左移 2:右移 |
| | | * @param upDown 镜头上移下移 0:停止 1:上移 2:下移 |
| | | * @param inOut 镜头放大缩小 0:停止 1:缩小 2:放大 |
| | | * @param moveSpeed 镜头移动速度 默认 0XFF (0-255) |
| | | * @param zoomSpeed 镜头缩放速度 默认 0X1 (0-255) |
| | | */ |
| | | public static String cmdString(int leftRight, int upDown, int inOut, int moveSpeed, int zoomSpeed) { |
| | | int cmdCode = 0; |
| | | if (leftRight == 2) { |
| | | cmdCode|=0x01; // 右移 |
| | | } else if(leftRight == 1) { |
| | | cmdCode|=0x02; // 左移 |
| | | } |
| | | if (upDown == 2) { |
| | | cmdCode|=0x04; // 下移 |
| | | } else if(upDown == 1) { |
| | | cmdCode|=0x08; // 上移 |
| | | } |
| | | if (inOut == 2) { |
| | | cmdCode |= 0x10; // 放大 |
| | | } else if(inOut == 1) { |
| | | cmdCode |= 0x20; // 缩小 |
| | | } |
| | | StringBuilder builder = new StringBuilder("A50F01"); |
| | | String strTmp; |
| | | strTmp = String.format("%02X", cmdCode); |
| | | builder.append(strTmp, 0, 2); |
| | | strTmp = String.format("%02X", moveSpeed); |
| | | builder.append(strTmp, 0, 2); |
| | | builder.append(strTmp, 0, 2); |
| | | strTmp = String.format("%X", zoomSpeed); |
| | | builder.append(strTmp, 0, 1).append("0"); |
| | | //计算校验码 |
| | | int checkCode = (0XA5 + 0X0F + 0X01 + cmdCode + moveSpeed + moveSpeed + (zoomSpeed /*<< 4*/ & 0XF0)) % 0X100; |
| | | strTmp = String.format("%02X", checkCode); |
| | | builder.append(strTmp, 0, 2); |
| | | return builder.toString(); |
| | | } |
| | | |
| | | public static String getNewCallId() { |
| | | return (int) Math.floor(Math.random() * 10000) + ""; |
| | | } |
| | | |
| | | public static int getTypeCodeFromGbCode(String deviceId) { |
| | | if (ObjectUtils.isEmpty(deviceId)) { |
| | | return 0; |
| | | } |
| | | return Integer.parseInt(deviceId.substring(10, 13)); |
| | | } |
| | | |
| | | /** |
| | | * 判断是否是前端外围设备 |
| | | * @param deviceId |
| | | * @return |
| | | */ |
| | | public static boolean isFrontEnd(String deviceId) { |
| | | int typeCodeFromGbCode = getTypeCodeFromGbCode(deviceId); |
| | | return typeCodeFromGbCode > 130 && typeCodeFromGbCode < 199; |
| | | } |
| | | /** |
| | | * 从请求中获取设备ip地址和端口号 |
| | | * @param request 请求 |
| | | * @param sipUseSourceIpAsRemoteAddress false 从via中获取地址, true 直接获取远程地址 |
| | | * @return 地址信息 |
| | | */ |
| | | public static RemoteAddressInfo getRemoteAddressFromRequest(SIPRequest request, boolean sipUseSourceIpAsRemoteAddress) { |
| | | |
| | | String remoteAddress; |
| | | int remotePort; |
| | | if (sipUseSourceIpAsRemoteAddress) { |
| | | remoteAddress = request.getRemoteAddress().getHostAddress(); |
| | | remotePort = request.getRemotePort(); |
| | | }else { |
| | | // 判断RPort是否改变,改变则说明路由nat信息变化,修改设备信息 |
| | | // 获取到通信地址等信息 |
| | | remoteAddress = request.getTopmostViaHeader().getReceived(); |
| | | remotePort = request.getTopmostViaHeader().getRPort(); |
| | | // 解析本地地址替代 |
| | | if (ObjectUtils.isEmpty(remoteAddress) || remotePort == -1) { |
| | | remoteAddress = request.getTopmostViaHeader().getHost(); |
| | | remotePort = request.getTopmostViaHeader().getPort(); |
| | | } |
| | | } |
| | | |
| | | return new RemoteAddressInfo(remoteAddress, remotePort); |
| | | } |
| | | } |