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