zhouzeyu 1 год назад
Родитель
Сommit
c60c40d461

+ 3 - 0
src/main/java/com/tuoren/forward/entity/RegistLog.java

@@ -27,4 +27,7 @@ public class RegistLog {
 
     @Schema(description = "创建时间")
     private Date createtime;
+
+    @Schema(description = "返回错误信息")
+    private String msg;
 }

+ 19 - 20
src/main/java/com/tuoren/forward/netty/MyServer.java

@@ -70,6 +70,7 @@ class MyServerInitializer extends ChannelInitializer<SocketChannel> {
 
     @Autowired
     MyServerHandler myServerHandler;
+
     @Override
     protected void initChannel(SocketChannel ch) throws Exception {
         // TODO Auto-generated method stub
@@ -131,8 +132,7 @@ class MyServerHandler extends ChannelInboundHandlerAdapter {
         try {
             jsonString = handleRegister(msgStr);
 
-            //截取所需字段
-            jsonString = JsonExtractor.extractDeviceFields(jsonString);
+
             // 验证 JSON 格式
             JSONObject jsonObject = validateAndParseJson(jsonString);
 
@@ -140,9 +140,9 @@ class MyServerHandler extends ChannelInboundHandlerAdapter {
             long duration = endTime - startTime;
 
             registLog.setWaste(String.valueOf(duration));
-            registLog.setResult(jsonString);//保存结果
 
-            int maxLength = 1000;
+
+            int maxLength = 800;
             if (jsonString != null && !jsonString.isEmpty()) {
                 if (jsonString.length() > maxLength) {
                     jsonString = jsonString.substring(0, maxLength);
@@ -151,13 +151,14 @@ class MyServerHandler extends ChannelInboundHandlerAdapter {
                 logger.error("jsonString is empty");
                 throw new IllegalArgumentException("jsonString is empty");
             }
-
+            registLog.setResult(jsonString);//保存结果
 
             int code = jsonObject.getIntValue("code");
-            if (code == 200){
+            if (code == 200) {
                 registLog.setStatus("0");//成功
-            }else {
+            } else {
                 registLog.setStatus("1");//失败
+                registLog.setMsg(jsonObject.getString("msg"));
             }
 
             //保存日志
@@ -171,6 +172,7 @@ class MyServerHandler extends ChannelInboundHandlerAdapter {
             registLog.setWaste(String.valueOf(duration));
             registLog.setResult(JSON.toJSONString(e.getMessage()));//保存异常信息
             registLog.setStatus("2");//异常
+            registLog.setMsg(e.getMessage());
             registLogMapper.insert(registLog);
 
             logger.error("regist error:" + e);
@@ -230,7 +232,7 @@ class MyServerHandler extends ChannelInboundHandlerAdapter {
                 registData = aliDeviceHttp.deviceQuery(deviceName);
             }
             if (!registData.isEmpty()) {
-                json.put("code", NExcCode.SUCCESS.getCode());
+                json.put("code", 0);
                 json.put("type", 0x15);
                 json.put("address", Config.COAP_ADDRESS);
                 json.put("port", Config.COAP_PORT);
@@ -239,14 +241,14 @@ class MyServerHandler extends ChannelInboundHandlerAdapter {
                 json.put("productKey", registData.getString("ProductKey"));
             }
 
-        }else {
+        } else {
             /**
              * 新代码处理
              */
             //平台
             Platform platform = null;
             //版本
-            int version = paramJson.getIntValue("version",1);//如果版本为空,则默认1
+            int version = paramJson.getIntValue("version", 1);//如果版本为空,则默认1
 
             //网络协议
             Integer protocolCode = null;
@@ -276,7 +278,7 @@ class MyServerHandler extends ChannelInboundHandlerAdapter {
              */
             User userIdCode = userMapper.selectByCode(userId);
             if (userIdCode == null || StrUtil.isBlank(userIdCode.getCode())) {
-                json.put("code",NExcCode.ERROR_USERID.getCode());
+                json.put("code", NExcCode.ERROR_USERID.getCode());
                 json.put("msg", NExcCode.ERROR_USERID.getMsg());
                 logger.error("注册失败:userId不存在", json.toJSONString());
                 return json.toString();
@@ -308,7 +310,6 @@ class MyServerHandler extends ChannelInboundHandlerAdapter {
                 if (!registData.isEmpty()) {
 
                     json.put("code", NExcCode.SUCCESS.getCode());
-                    json.put("type", 0x15);
                     json.put("version", version);
                     json.put("platform", platform.getCode());
                     json.put("networkType", network.getCode());
@@ -316,8 +317,8 @@ class MyServerHandler extends ChannelInboundHandlerAdapter {
                     json.put("productId", productId);
                     json.put("deviceId", deviceName);
                     json.put("userId", userId);
-                    json.put("address", Config.COAP_ADDRESS);
-                    json.put("port", Config.COAP_PORT);
+                    json.put("remoteAddress", Config.COAP_ADDRESS);
+                    json.put("remotePort", Config.COAP_PORT);
                     json.put("deviceSecret", registData.getString("DeviceSecret"));
                     json.put("productKey", registData.getString("ProductKey"));
                 }
@@ -332,6 +333,7 @@ class MyServerHandler extends ChannelInboundHandlerAdapter {
     private String extractDeviceId(String msgStr) {
         try {
             JSONObject json = JSON.parseObject(msgStr);
+
             return json.getString("deviceId");
         } catch (Exception e) {
             return "";
@@ -349,8 +351,6 @@ class MyServerHandler extends ChannelInboundHandlerAdapter {
     }
 
 
-
-
     //新wifi注册信息
     private JSONObject handleWifiRegistration(JSONObject paramJson, NetworkType network, Platform platform, NetworkProtocol protocol, String productId, String deviceName, String userId) throws Exception {
         JSONObject json = new JSONObject();
@@ -359,7 +359,6 @@ class MyServerHandler extends ChannelInboundHandlerAdapter {
         version = version == 0 ? 1 : version;
 
         json.put("code", NExcCode.SUCCESS.getCode());
-        json.put("type", 0x15);
         json.put("version", version);
         json.put("platform", platform.getCode());
         json.put("networkType", network.getCode());
@@ -384,8 +383,8 @@ class MyServerHandler extends ChannelInboundHandlerAdapter {
         for (Object item : addressArrayJson) {
             JSONObject networkInfo = (JSONObject) item;
             if (network.getName().equalsIgnoreCase(networkInfo.getString("networkType"))) {
-                json.put("address", networkInfo.getString("address"));
-                json.put("port", networkInfo.getIntValue("port"));
+                json.put("remoteAddress", networkInfo.getString("address"));
+                json.put("remotePort", networkInfo.getIntValue("port"));
                 found = true;
                 break;
             }
@@ -402,7 +401,7 @@ class MyServerHandler extends ChannelInboundHandlerAdapter {
         JSONArray wifiArray = JSON.parseArray(wifi);
         // 调用方法转换wifi数据
         JSONArray transformWifiArray = WifiDataTrans.transformWifiArray(wifiArray);
-        json.put("deviceSecret",deviceSecret);
+        json.put("deviceSecret", deviceSecret);
         json.put("wifi", transformWifiArray);
 
         return json;

+ 13 - 4
src/main/resources/mapper/RegistLogMapper.xml

@@ -9,9 +9,10 @@
     <result column="status" jdbcType="CHAR" property="status" />
     <result column="waste" jdbcType="VARCHAR" property="waste" />
     <result column="createtime" jdbcType="TIMESTAMP" property="createtime" />
+    <result column="msg" jdbcType="VARCHAR" property="msg" />
   </resultMap>
   <sql id="Base_Column_List">
-    id, mac, param, result, status, waste, createtime
+    id, mac, param, result, status, waste, createtime,msg
   </sql>
   <select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
     select 
@@ -26,10 +27,10 @@
   <insert id="insert" parameterType="com.tuoren.forward.entity.RegistLog">
     insert into regist_log (id, mac, param, 
       result, status, waste, 
-      createtime)
+      createtime,msg)
     values (#{id,jdbcType=VARCHAR}, #{mac,jdbcType=VARCHAR}, #{param,jdbcType=VARCHAR}, 
       #{result,jdbcType=VARCHAR}, #{status,jdbcType=CHAR}, #{waste,jdbcType=VARCHAR}, 
-      #{createtime,jdbcType=TIMESTAMP})
+      #{createtime,jdbcType=TIMESTAMP},#{msg,jdbcType=VARCHAR})
   </insert>
   <insert id="insertSelective" parameterType="com.tuoren.forward.entity.RegistLog">
     insert into regist_log
@@ -55,6 +56,9 @@
       <if test="createtime != null">
         createtime,
       </if>
+      <if test="msg != null">
+        msg,
+      </if>
     </trim>
     <trim prefix="values (" suffix=")" suffixOverrides=",">
       <if test="id != null">
@@ -78,6 +82,9 @@
       <if test="createtime != null">
         #{createtime,jdbcType=TIMESTAMP},
       </if>
+      <if test="msg != null">
+        #{msg,jdbcType=VARCHAR},
+      </if>
     </trim>
   </insert>
   <update id="updateByPrimaryKeySelective" parameterType="com.tuoren.forward.entity.RegistLog">
@@ -101,6 +108,9 @@
       <if test="createtime != null">
         createtime = #{createtime,jdbcType=TIMESTAMP},
       </if>
+      <if test="msg != null">
+        #{msg,jdbcType=VARCHAR},
+      </if>
     </set>
     where id = #{id,jdbcType=VARCHAR}
   </update>
@@ -108,7 +118,6 @@
     update regist_log
     set mac = #{mac,jdbcType=VARCHAR},
       param = #{param,jdbcType=VARCHAR},
-      result = #{result,jdbcType=VARCHAR},
       status = #{status,jdbcType=CHAR},
       waste = #{waste,jdbcType=VARCHAR},
       createtime = #{createtime,jdbcType=TIMESTAMP}