Ver Fonte

语音经纬工牌

vinson há 5 anos atrás
pai
commit
4b0443bd2b

+ 6 - 0
pom.xml

@@ -131,6 +131,12 @@
             <artifactId>service-accept-client</artifactId>
             <version>1.0-SNAPSHOT</version>
         </dependency>
+        <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
+        <dependency>
+            <groupId>commons-io</groupId>
+            <artifactId>commons-io</artifactId>
+            <version>2.6</version>
+        </dependency>
     </dependencies>
 
     <build>

+ 79 - 29
src/main/java/com/tidecloud/dataacceptance/service/impl/JingWeiCardServerHandler.java

@@ -106,11 +106,11 @@ public class JingWeiCardServerHandler extends HexBinaryAcceptanceHandlerAdapter
                     voiceMsgClient.updateVoiceMsgSendFinish(advice.getDeviceId(), 1, 4);
                     break;
                 } else {
+
                     logger.info("微聊对讲数据[TK]:" + advice.toString());
-                    VoiceMsgVo msgVo = splitVoiceMsg(advice.getDeviceId(), req);
-//                    voiceMsgClient.insVoiceMsg(msgVo);
+                    VoiceMsgVo msgVo = splitVoiceMsg(msg, advice.getDeviceId(), req);
+                    voiceMsgClient.insVoiceMsg(msgVo);
                     normalReply(advice, channel, "TK,1");
-                    normalReply(channel, msgVo,msg);
                 }
                 break;
             case "UPLOAD": //数据上传间隔设置
@@ -128,7 +128,11 @@ public class JingWeiCardServerHandler extends HexBinaryAcceptanceHandlerAdapter
     }
     //[3G*YYYYYYYYYY*LEN*TK,AMR 格式音频数据]
 
-    protected VoiceMsgVo splitVoiceMsg(String deviceId, byte[] req) {
+    protected VoiceMsgVo splitVoiceMsg(String msg, String deviceId, byte[] req) {
+        int length = req.length - 24;
+        byte[] bs = new byte[length];
+        System.arraycopy(req, 23, bs, 0, length);
+
         VoiceMsgVo voiceMsg = new VoiceMsgVo();
         SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
         String time = df.format(new Date());
@@ -136,27 +140,10 @@ public class JingWeiCardServerHandler extends HexBinaryAcceptanceHandlerAdapter
         voiceMsg.setTotal(1);
         voiceMsg.setNu(1);
         voiceMsg.setVoiceTime(time);
-        try {
-            byte[] bytes = subByte(req, 23, req.length - 24);
-            voiceMsg.setMsg(bytes);
-        } catch (Exception e) {
-            logger.warn("语音解析错误" + JSON.toJSONString(voiceMsg) + ":" + e.getStackTrace());
-        }
+        voiceMsg.setMsg(bs);
+        voiceMsg.setMsg(bs);
         return voiceMsg;
     }
-    /**
-     * 截取byte数组   不改变原数组
-     * @param b 原数组
-     * @param off 偏差值(索引)
-     * @param length 长度
-     * @return 截取后的数组
-     */
-    public byte[] subByte(byte[] b, int off, int length){
-        byte[] b1 = new byte[length];
-        System.arraycopy(b, off, b1, 0, length);
-        return b1;
-    }
-
 
 
     /**
@@ -169,14 +156,69 @@ public class JingWeiCardServerHandler extends HexBinaryAcceptanceHandlerAdapter
         try {
             VoiceMsgVo voiceF = voiceMsgClient.queryVoiceMsg(deviceId, 1);
             if (voiceF.getLag() == 1) {
-                normalReply(channel, voiceF,"");
+                normalReply2(channel, voiceF,"");
                 //发送中的数据
-                voiceMsgClient.updateVoiceMsgSendFinish(deviceId, 1, 3);
+                voiceMsgClient.updateVoiceMsgSendFinish(deviceId, voiceF.getMsgId(), 4);
             }
         } catch (Exception e) {
             logger.error("语音下行发送异常!!!!! deviceId=" + deviceId);
         }
     }
+    private void normalReply2(Channel channel, VoiceMsgVo voiceMsg, String msg) {
+        ByteBuf buffer = buildVoiceToClientBytebuf(voiceMsg, msg);
+        Integer msgId = voiceMsg.getMsgId();
+        String deviceId = voiceMsg.getDeviceId();
+
+        sendVoiceToDevice(channel, buffer, msgId, deviceId);
+    }
+
+    public boolean sendVoiceToDevice(String deviceId, byte[] bytes) {
+        Channel channel = deviceChannelMap.get(deviceId);
+        if (channel == null) {
+            logger.warn("the device[{}] is offline and send cancled", deviceId);
+            return false;
+        }
+        ByteBuf byteBuf = buildVoiceToClientBytebuf(deviceId, bytes);
+        sendVoiceToDevice(channel, byteBuf,0, deviceId);
+        return true;
+    }
+
+    private void sendVoiceToDevice(Channel channel, ByteBuf buffer, Integer msgId, String deviceId) {
+        ChannelFuture channelFuture = channel.writeAndFlush(buffer);
+        channelFuture.addListener(future ->
+                {
+                    logger.info("send voice[{}] to client[{}]", msgId, deviceId);
+                }
+        );
+    }
+
+    private ByteBuf buildVoiceToClientBytebuf(VoiceMsgVo voiceMsg, String msg) {
+        String deviceId = voiceMsg.getDeviceId();
+        byte[] bytes = voiceMsg.getMsg();
+        return buildVoiceToClientBytebuf(deviceId, bytes);
+    }
+
+    private ByteBuf buildVoiceToClientBytebuf(String deviceId, byte[] bytes) {
+        StringBuilder replyCommand = new StringBuilder();
+        replyCommand.append("[");
+        replyCommand.append("3G").append("*");
+        replyCommand.append(deviceId).append("*");
+        replyCommand
+                .append(numToHex16(bytes.length + 3))
+                .append("*");
+        replyCommand.append("TK").append(",");
+        String replyCommandStr = replyCommand.toString();
+        StringBuilder replyCommand2 = new StringBuilder();
+        replyCommand2.append("]");
+        String replyCommandStr2 = replyCommand2.toString();
+        ByteBuf buffer = Unpooled.buffer(replyCommandStr.getBytes().length + bytes.length + replyCommandStr2.getBytes().length);
+        buffer.writeBytes(replyCommandStr.getBytes());
+        buffer.writeBytes(bytes);
+        buffer.writeBytes(replyCommandStr2.getBytes());
+
+        buffer.readerIndex(0);
+        return buffer;
+    }
 
     /**
      *  主动回复
@@ -195,11 +237,14 @@ public class JingWeiCardServerHandler extends HexBinaryAcceptanceHandlerAdapter
         replyCommand.append("[");
         replyCommand.append("3G").append("*");
         String deviceId = voiceMsg.getDeviceId();
-        if (deviceId.equals("9513532727")) {
-            deviceId = "9513532780";
-        }
+//        if (deviceId.equals("9513532727")) {
+//            deviceId = "9513532780";
+//        }
+//        deviceId = "9513532727";
         replyCommand.append(deviceId).append("*");
-        replyCommand.append(bodys[2]).append("*");
+        replyCommand
+                .append(bodys[2])
+                .append("*");
         replyCommand.append("TK").append(",");
         String replyCommandStr = replyCommand.toString();
         StringBuilder replyCommand2 = new StringBuilder();
@@ -209,6 +254,11 @@ public class JingWeiCardServerHandler extends HexBinaryAcceptanceHandlerAdapter
         buffer.writeBytes(replyCommandStr.getBytes());
         buffer.writeBytes(voiceMsg.getMsg());
         buffer.writeBytes(replyCommandStr2.getBytes());
+
+        buffer.readerIndex(0);
+        byte[] bs = new byte[buffer.readableBytes()];
+        buffer.readBytes(bs);
+
         buffer.readerIndex(0);
 
         channel = deviceChannelMap.get(deviceId);

+ 150 - 0
src/main/java/com/tidecloud/dataacceptance/web/JIngweiCarController.java

@@ -0,0 +1,150 @@
+//package com.tidecloud.dataacceptance.web;
+//
+//import com.tidecloud.dataacceptance.common.HexStringUtils;
+//import com.tidecloud.dataacceptance.service.impl.JingWeiCardServerHandler;
+//import org.apache.commons.io.IOUtils;
+//import org.springframework.beans.factory.annotation.Autowired;
+//import org.springframework.web.bind.annotation.PathVariable;
+//import org.springframework.web.bind.annotation.RequestMapping;
+//import org.springframework.web.bind.annotation.RestController;
+//
+//import java.io.IOException;
+//import java.io.InputStream;
+//
+//@RestController
+//public class JIngweiCarController {
+//
+//    @Autowired
+//    private JingWeiCardServerHandler jingWeiCardServerHandler;
+//
+//    @RequestMapping("device/{deviceId}/sendVoice")
+//    public String sendVoice(@PathVariable String deviceId) throws IOException {
+//        try {
+//
+//            String name = "jingwei_zhuangyi.amr";
+////            String name = "voice.amr";
+//
+//
+//            InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(name);
+//            byte[] bs = IOUtils.toByteArray(resourceAsStream);
+//
+//
+////            String name = "/tmp/voice.amr";
+////            File f = new File(name);
+////            FileOutputStream fileOutputStream = new FileOutputStream(f);
+////            byte[] bs = new byte[(int) f.length()];
+////            fileOutputStream.write(bs);
+//            String s = HexStringUtils.toHexString(bs);
+//            String replace = s.toUpperCase()
+//                    .replace("7D", "7D01")
+//                    .replace("5B", "7D02")
+//                    .replace("5D", "7D03")
+//                    .replace("2C", "7D04")
+//                    .replace("2A", "7D05");
+////            byte[] s1 = BitOperator.hexToBytes(replace, replace.length());
+//            byte[] s1 = hexToByteArray(replace);
+////            byte[] s1 = convert2(bs);
+//
+//            jingWeiCardServerHandler.sendVoiceToDevice(deviceId, bs);
+//
+//            return "success";
+//        } catch (Exception e) {
+//            e.printStackTrace();
+//            return "faile";
+//        }
+//    }
+//
+//    public static byte[] convert(byte[] bs) {
+//        byte[] resultHolder = new byte[bs.length * 2];
+//        int index = 0;
+//        for (int i = 0; i < bs.length; i = i + 2) {
+//            byte firstBit = bs[i];
+//            if (i == bs.length -1) {
+//                resultHolder[index++] = firstBit;
+//            } else {
+//                byte secondBit = bs[i + 1];
+//                if (firstBit == 0X7D) {
+//                    if (secondBit == 0X01) {
+//                        resultHolder[index++] = 0X7D;
+//                    } else if (secondBit == 0X02) {
+//                        resultHolder[index++] = 0X5B;
+//                    } else if (secondBit == 0X03) {
+//                        resultHolder[index++] = 0X5D;
+//                    } else if (secondBit == 0X04) {
+//                        resultHolder[index++] = 0X2C;
+//                    } else if (secondBit == 0X05) {
+//                        resultHolder[index++] = 0X2A;
+//                    } else {
+//                        resultHolder[index++] = firstBit;
+//                        resultHolder[index++] = secondBit;
+//                    }
+//                } else {
+//                    resultHolder[index++] = firstBit;
+//                    resultHolder[index++] = secondBit;
+//                }
+//            }
+//        }
+//
+//        byte[] result = new byte[index];
+//        for (int i = 0; i < index; i++) {
+//            result[i] = resultHolder[i];
+//        }
+//        return result;
+//    }
+//
+//    public static byte[] convert2(byte[] bs) {
+//        byte[] resultHolder = new byte[bs.length * 2];
+//        int index = 0;
+//        for (int i = 0; i < bs.length; i++) {
+//            byte b = bs[i];
+//            if (b == 0X7D) {
+//                resultHolder[index++] = 0X7D;
+//                resultHolder[index++] = 0X01;
+//            } else if (b == 0X5B) {
+//                resultHolder[index++] = 0X7D;
+//                resultHolder[index++] = 0X02;
+//            } else if (b == 0X5D) {
+//                resultHolder[index++] = 0X7D;
+//                resultHolder[index++] = 0X03;
+//            } else if (b == 0X2C) {
+//                resultHolder[index++] = 0X7D;
+//                resultHolder[index++] = 0X04;
+//            } else if (b == 0X2A) {
+//                resultHolder[index++] = 0X7D;
+//                resultHolder[index++] = 0X05;
+//            } else {
+//                resultHolder[index++] = b;
+//            }
+//        }
+//
+//        byte[] result = new byte[index];
+//        for (int i = 0; i < index; i++) {
+//            result[i] = resultHolder[i];
+//        }
+//        return result;
+//    }
+//
+//
+//    public static byte[] hexToByteArray(String inHex){
+//        int hexlen = inHex.length();
+//        byte[] result;
+//        if (hexlen % 2 == 1){
+//            //奇数
+//            hexlen++;
+//            result = new byte[(hexlen/2)];
+//            inHex="0"+inHex;
+//        }else {
+//            //偶数
+//            result = new byte[(hexlen/2)];
+//        }
+//        int j=0;
+//        for (int i = 0; i < hexlen; i+=2){
+//            result[j]=hexToByte(inHex.substring(i,i+2));
+//            j++;
+//        }
+//        return result;
+//    }
+//    public static byte hexToByte(String inHex){
+//        return (byte)Integer.parseInt(inHex,16);
+//    }
+//}