|
@@ -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);
|