From 4d207cda87d3685a7edfbb4c62887807d2c6ccf1 Mon Sep 17 00:00:00 2001
From: lizhanwei <Dana_Lee1016@126.com>
Date: 星期三, 19 二月 2020 15:37:39 +0800
Subject: [PATCH] 消息转义后发送;NettyTcp发送消息进行日志记录

---
 app/src/main/java/safeluck/drive/evaluation/platformMessage/AttachInfo.java        |    6 
 im_lib/src/main/java/com/anyun/im_lib/util/MyLog.java                              |  359 +++++++++++++++++++++++++++++++++++++++++++++++++++
 app/src/main/java/safeluck/drive/evaluation/util/Utils.java                        |   12 +
 im_lib/src/main/java/com/anyun/im_lib/netty/NettyTcpClient.java                    |    3 
 app/src/main/java/safeluck/drive/evaluation/fragment/TcpFragment.java              |    9 
 app/src/main/java/safeluck/drive/evaluation/platformMessage/JK2019MessageBase.java |    1 
 app/src/main/java/safeluck/drive/evaluation/im/MessageProcessor.java               |    2 
 7 files changed, 383 insertions(+), 9 deletions(-)

diff --git a/app/src/main/java/safeluck/drive/evaluation/fragment/TcpFragment.java b/app/src/main/java/safeluck/drive/evaluation/fragment/TcpFragment.java
index b5009a7..58c6177 100644
--- a/app/src/main/java/safeluck/drive/evaluation/fragment/TcpFragment.java
+++ b/app/src/main/java/safeluck/drive/evaluation/fragment/TcpFragment.java
@@ -233,12 +233,15 @@
                 break;
             case R.id.btn_keepalive:
                 ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
-                scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
+                scheduledExecutorService.scheduleWithFixedDelay(new Runnable() {
                     @Override
                     public void run() {
-                        MessageProcessor.getInstance().sendMessage(new JKMessage0002());
+                        JKMessage0002 jkMessage0002 = new JKMessage0002();
+                        jkMessage0002.checksum = 0x7e;
+                        MessageProcessor.getInstance().sendMessage(jkMessage0002);
+                        Log.i(TAG, "run: 5s涓�娆�");
                     }
-                },1000,1000, TimeUnit.MICROSECONDS);
+                },1,5, TimeUnit.SECONDS);
 
                 break;
         }
diff --git a/app/src/main/java/safeluck/drive/evaluation/im/MessageProcessor.java b/app/src/main/java/safeluck/drive/evaluation/im/MessageProcessor.java
index 84272be..cedcb9d 100644
--- a/app/src/main/java/safeluck/drive/evaluation/im/MessageProcessor.java
+++ b/app/src/main/java/safeluck/drive/evaluation/im/MessageProcessor.java
@@ -94,7 +94,7 @@
             public void run() {
 
                 if (IMSClientBootstrap.getInstance().isActive()){
-                    IMSClientBootstrap.getInstance().sendMessage(msg.toBytes());
+                    IMSClientBootstrap.getInstance().sendMessage(messageEscaper.escape(msg.toBytes()));
                 }else{
                     Log.e(TAG, "run: 鍙戦�佹秷鎭け璐ワ紝鏈垵濮嬪寲杩炴帴NettyTcp");
                 }
diff --git a/app/src/main/java/safeluck/drive/evaluation/platformMessage/AttachInfo.java b/app/src/main/java/safeluck/drive/evaluation/platformMessage/AttachInfo.java
index 97d16df..ef0c3f5 100644
--- a/app/src/main/java/safeluck/drive/evaluation/platformMessage/AttachInfo.java
+++ b/app/src/main/java/safeluck/drive/evaluation/platformMessage/AttachInfo.java
@@ -9,12 +9,12 @@
 public class AttachInfo extends BinMessageBase {
     @Order(1)
     @Int8
-    public int attach_id;
+    public int attach_message_id;
     @Order(2)
     @Int8
-    public int attach_length;
+    public int attach_message_length;
     @Order(3)
-    @Length(lengthField="attach_length")
+    @Length(lengthField="attach_message_length")
     @Hex
     public String attach_data;
 }
diff --git a/app/src/main/java/safeluck/drive/evaluation/platformMessage/JK2019MessageBase.java b/app/src/main/java/safeluck/drive/evaluation/platformMessage/JK2019MessageBase.java
index f10dc01..0845036 100644
--- a/app/src/main/java/safeluck/drive/evaluation/platformMessage/JK2019MessageBase.java
+++ b/app/src/main/java/safeluck/drive/evaluation/platformMessage/JK2019MessageBase.java
@@ -78,7 +78,6 @@
         byte[] bytes = BytesUtils.hexStringToBytes(hex);
         bytes[bytes.length - 2] = BytesUtils.xor(bytes,1,bytes.length -2);
 
-
         return BytesUtils.bytesToHexString(bytes);
     }
     @Order(1)
diff --git a/app/src/main/java/safeluck/drive/evaluation/util/Utils.java b/app/src/main/java/safeluck/drive/evaluation/util/Utils.java
index 6231097..7b178a3 100644
--- a/app/src/main/java/safeluck/drive/evaluation/util/Utils.java
+++ b/app/src/main/java/safeluck/drive/evaluation/util/Utils.java
@@ -6,6 +6,7 @@
 
 import com.anyun.exam.lib.MyLog;
 import com.anyun.exam.lib.util.ByteUtil;
+import com.safeluck.aykj.utils.BytesUtils;
 
 /**
  * MyApplication2
@@ -104,4 +105,15 @@
         String regx= "^\\d+";
         return str.matches(regx);
     }
+
+    public static void main(String []args){
+        String str   = "EB00020000031420010000000400A5";
+//        String str   = "7EEB00020000031420010000000400A5007E";
+        String str1  = "EB000200000314200100000004030D00";
+//        String str1  = "7EEB000200000314200100000004030D00D57E";
+        String str2  = "EB000200000314200100000004035000";
+        byte [] dtas=BytesUtils.hexStringToBytes(str);
+        byte checkcode = calCheckCode(dtas);
+        System.out.println(BytesUtils.toHexString(checkcode));
+    }
 }
diff --git a/im_lib/src/main/java/com/anyun/im_lib/netty/NettyTcpClient.java b/im_lib/src/main/java/com/anyun/im_lib/netty/NettyTcpClient.java
index 11be1ce..ac26ebc 100644
--- a/im_lib/src/main/java/com/anyun/im_lib/netty/NettyTcpClient.java
+++ b/im_lib/src/main/java/com/anyun/im_lib/netty/NettyTcpClient.java
@@ -12,6 +12,7 @@
 import com.anyun.im_lib.listener.IMSConnectStatusCallback;
 import com.anyun.im_lib.listener.OnEventListener;
 import com.anyun.im_lib.util.ByteUtil;
+import com.anyun.im_lib.util.MyLog;
 import com.safeluck.aykj.utils.BytesUtils;
 
 import java.nio.ByteBuffer;
@@ -332,7 +333,7 @@
             Log.i(TAG, "sendMsg fail,channel涓虹┖"+msg);
         }
         try {
-            Log.i(TAG, "sendMsg: "+ BytesUtils.bytesToHexString(msg));
+            MyLog.i("PlatformMessage", "sendMsg: "+ BytesUtils.bytesToHexString(msg));
             ByteBuf byteBuf = ByteBufAllocator.DEFAULT.ioBuffer();
             byteBuf.writeBytes(msg);
             channel.writeAndFlush(byteBuf);
diff --git a/im_lib/src/main/java/com/anyun/im_lib/util/MyLog.java b/im_lib/src/main/java/com/anyun/im_lib/util/MyLog.java
new file mode 100644
index 0000000..c2ad1ff
--- /dev/null
+++ b/im_lib/src/main/java/com/anyun/im_lib/util/MyLog.java
@@ -0,0 +1,359 @@
+package com.anyun.im_lib.util;
+
+
+
+
+
+
+/**
+ * anyunProject(20190906)
+ * Created by lzw on 2019/9/6. 17:23:17
+ * 閭锛�632393724@qq.com
+ * All Rights Saved! Chongqing AnYun Tech co. LTD
+ */
+
+
+import android.content.Context;
+import android.os.Environment;
+import android.text.TextUtils;
+import android.util.Log;
+
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public class MyLog {
+
+     private static final String TAG = "AYJiaKao";
+
+
+    private static Boolean MYLOG_SWITCH = true; // 鏃ュ織鏂囦欢鎬诲紑鍏�
+
+    private static Boolean MYLOG_WRITE_TO_FILE = true;// 鏃ュ織鍐欏叆鏂囦欢寮�鍏�
+    private static File file;
+
+    // 杈撳叆鏃ュ織绫诲瀷锛寃浠h〃鍙緭鍑哄憡璀︿俊鎭瓑锛寁浠h〃杈撳嚭鎵�鏈変俊鎭�
+    private static char MYLOG_TYPE = 'v';
+
+    // 鏃ュ織鏂囦欢鍦╯dcard涓殑璺緞
+    private static String MYLOG_PATH_SDCARD_DIR = "/sdcard/JiaKaolog";
+
+    private static int SDCARD_LOG_FILE_SAVE_DAYS = 3;// sd鍗′腑鏃ュ織鏂囦欢鐨勬渶澶氫繚瀛樺ぉ鏁�
+
+    private static String MYLOGFILEName = "Log.txt";// 鏈被杈撳嚭鐨勬棩蹇楁枃浠跺悕绉�
+
+    private static SimpleDateFormat myLogSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 鏃ュ織鐨勮緭鍑烘牸寮�
+
+    private static SimpleDateFormat logfile = new SimpleDateFormat("yyyy-MM-dd");// 鏃ュ織鏂囦欢鏍煎紡
+
+    public Context context;
+
+
+    public static void w(String tag, Object msg) { // 璀﹀憡淇℃伅
+
+        log(tag, msg.toString(), 'w');
+
+    }
+
+
+    public static void e(String tag, Object msg) { // 閿欒淇℃伅
+
+        log(tag, msg.toString(), 'e');
+
+    }
+
+
+    public static void d(String tag, Object msg) {// 璋冭瘯淇℃伅
+
+        log(tag, msg.toString(), 'd');
+
+    }
+
+
+    public static void i(String tag, Object msg) {//
+
+        log(tag, msg.toString(), 'i');
+
+    }
+
+    public static void i(Object msg) {//
+
+        log(TAG, msg.toString(), 'i');
+
+    }
+
+
+    public static void v(String tag, Object msg) {
+
+        log(tag, msg.toString(), 'v');
+
+    }
+
+
+    public static void w(String tag, String text) {
+
+        log(tag, text, 'w');
+
+    }
+
+
+    public static void e(String tag, String text) {
+
+        log(tag, text, 'e');
+
+    }
+
+
+    public static void d(String tag, String text) {
+
+        log(tag, text, 'd');
+
+    }
+
+
+    public static void i(String tag, String text) {
+
+        log(tag, text, 'i');
+
+    }
+
+    public static void i(String text) {
+
+        log(TAG, text, 'i');
+
+    }
+
+
+    public static void v(String tag, String text) {
+
+        log(tag, text, 'v');
+
+    }
+
+
+    /**
+     * 鏍规嵁tag, msg鍜岀瓑绾э紝杈撳嚭鏃ュ織
+     *
+     * @param tag
+     * @param msg
+     * @param level
+     */
+
+    private static void log(final String tag, final String msg, final char level) {
+
+        if (MYLOG_SWITCH) {//鏃ュ織鏂囦欢鎬诲紑鍏�
+
+            if ('e' == level && ('e' == MYLOG_TYPE || 'v' == MYLOG_TYPE)) { // 杈撳嚭閿欒淇℃伅
+
+                Log.e(tag, msg);
+
+            } else if ('w' == level && ('w' == MYLOG_TYPE || 'v' == MYLOG_TYPE)) {
+
+                Log.w(tag, msg);
+
+            } else if ('d' == level && ('d' == MYLOG_TYPE || 'v' == MYLOG_TYPE)) {
+
+                Log.d(tag, msg);
+
+            } else if ('i' == level && ('d' == MYLOG_TYPE || 'v' == MYLOG_TYPE)) {
+
+                Log.i(tag, msg);
+
+            } else {
+
+                Log.v(tag, msg);
+
+            }
+
+            if (MYLOG_WRITE_TO_FILE)//鏃ュ織鍐欏叆鏂囦欢寮�鍏�
+                new Thread(new Runnable() {
+                    @Override
+                    public void run() {
+                        writeLogtoFile(String.valueOf(level), tag, msg);
+                    }
+                }
+                ).start();
+
+
+        }
+
+    }
+
+    public static void createIfNotExist() {
+
+
+        String needWriteFiel = logfile.format(System.currentTimeMillis());
+
+
+        Log.i(TAG, "createIfNotExist: " + needWriteFiel);
+
+
+        File dirsFile = new File(MYLOG_PATH_SDCARD_DIR);
+
+        if (!dirsFile.exists()) {
+
+            dirsFile.mkdirs();
+
+        }
+
+        //Log.i("鍒涘缓鏂囦欢","鍒涘缓鏂囦欢");
+
+        file = new File(dirsFile.toString(), needWriteFiel + MYLOGFILEName);// MYLOG_PATH_SDCARD_DIR
+
+        if (!file.exists()) {
+
+            try {
+
+                //鍦ㄦ寚瀹氱殑鏂囦欢澶逛腑鍒涘缓鏂囦欢
+
+                file.createNewFile();
+
+            } catch (Exception e) {
+
+            }
+
+        }
+    }
+
+    /**
+     * 鎵撳紑鏃ュ織鏂囦欢骞跺啓鍏ユ棩蹇�
+     *
+     * @param mylogtype
+     * @param tag
+     * @param text
+     */
+
+    private static void writeLogtoFile(String mylogtype, final String tag, final String text) {// 鏂板缓鎴栨墦寮�鏃ュ織鏂囦欢
+
+        String needWriteMessage = myLogSdf.format(System.currentTimeMillis()) + " " + tag + "  " + text;
+
+        try {
+/**鍚庨潰杩欎釜鍙傛暟浠h〃鏄笉鏄鎺ヤ笂鏂囦欢涓師鏉ョ殑鏁版嵁锛屼笉杩涜瑕嗙洊**/
+
+            if (file == null) {
+                Log.i(TAG, "writeLogtoFile: file==null");
+                createIfNotExist();
+            }
+            FileWriter filerWriter = new FileWriter(file, true);
+
+            BufferedWriter bufWriter = new BufferedWriter(filerWriter);
+
+            bufWriter.write(needWriteMessage);
+
+            bufWriter.newLine();
+
+            bufWriter.close();
+
+            filerWriter.close();
+
+        } catch (IOException e) {
+
+            e.printStackTrace();
+
+        }
+    }
+
+
+    /**
+     * 鍒犻櫎鍒跺畾鐨勬棩蹇楁枃浠�
+     */
+
+    public static void delFile() {// 鍒犻櫎鏃ュ織鏂囦欢
+
+        String needDelFiel = logfile.format(getDateBefore());
+
+        String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/nvlog";
+
+        File file = new File(dirPath, needDelFiel + MYLOGFILEName);// MYLOG_PATH_SDCARD_DIR
+
+        if (file.exists()) {
+
+            file.delete();
+
+        }
+
+    }
+
+    public static void delSubDirLogs() {
+        String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/jiakaolog";
+        String regx = "^[0-9]{4}-";
+        Pattern pattern = Pattern.compile(regx);
+        File file = new File(dirPath);
+        boolean needDel = false;
+        if (file.isDirectory()) {
+            Log.i(TAG, "subDirLogs: is a directory");
+            File[] subFiles = file.listFiles();
+            for (File f :
+                    subFiles) {
+                String fName = f.getName();
+                Log.i(TAG, "subDirLogs: 鏂囦欢鍚嶇О锛�" + fName);
+                if (!TextUtils.isEmpty(fName)) {
+                    Matcher matcher = pattern.matcher(fName);
+                    if (matcher.find()) {
+
+                        Log.i(TAG, "subDirLogs: 绗﹀悎瑙勫垯^[0-9]{4}- : " + matcher.group(0));
+                        for (int i = 0; i < SDCARD_LOG_FILE_SAVE_DAYS; i++) {
+                            String needSaveFileName = logfile.format(getDateBefore(i));
+                            Log.i(TAG,"needsaveFileName="+needSaveFileName);
+                            if (fName.contains(needSaveFileName)) {
+                                Log.i(TAG, "delSubDirLogs: 姝og闇�瑕佷繚鐣欙細" + fName + " 鏃ユ湡锛�" + needSaveFileName);
+                                needDel = false;
+                                break;
+                            } else {
+                                needDel = true;
+                            }
+                        }
+                        if (needDel) {
+                            Log.i(TAG, "delSubDirLogs: 鍒犻櫎璇ユ枃浠讹細" + fName);
+                            f.delete();
+                        }
+
+
+                    } else {
+                        Log.i(TAG, "delSubDirLogs: 涓嶇鍚堝垹闄よ鍒欙紝鍒欎笉鍒犻櫎锛�" + fName);
+                    }
+                }
+            }
+        } else {
+            Log.i(TAG, "subDirLogs: is not a directory");
+        }
+    }
+
+
+    /**
+     * 寰楀埌鐜板湪鏃堕棿鍓嶇殑鍑犲ぉ鏃ユ湡锛岀敤鏉ュ緱鍒伴渶瑕佸垹闄ょ殑鏃ュ織鏂囦欢鍚�
+     */
+
+    private static Date getDateBefore() {
+
+        Date nowtime = new Date();
+
+        Calendar now = Calendar.getInstance();
+
+        now.setTime(nowtime);
+
+        now.set(Calendar.DATE, now.get(Calendar.DATE) - SDCARD_LOG_FILE_SAVE_DAYS);
+
+        return now.getTime();
+
+    }
+
+    private static Date getDateBefore(int i) {
+        Date nowtime = new Date();
+
+        Calendar now = Calendar.getInstance();
+
+        now.setTime(nowtime);
+
+        now.set(Calendar.DATE, now.get(Calendar.DATE) - i);
+
+        return now.getTime();
+    }
+
+}
\ No newline at end of file

--
Gitblit v1.8.0