|
|
@@ -4,6 +4,9 @@ import cn.hutool.core.util.StrUtil;
|
|
|
import com.alibaba.fastjson2.JSON;
|
|
|
import com.alibaba.fastjson2.JSONArray;
|
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
|
+import com.tuoren.forward.netty.common.NetworkProtocol;
|
|
|
+import com.tuoren.forward.netty.common.NetworkType;
|
|
|
+import com.tuoren.forward.netty.common.Platform;
|
|
|
import com.tuoren.forward.entity.RegistLog;
|
|
|
import com.tuoren.forward.entity.User;
|
|
|
import com.tuoren.forward.entity.UserConfig;
|
|
|
@@ -11,9 +14,11 @@ import com.tuoren.forward.mapper.RegistLogMapper;
|
|
|
import com.tuoren.forward.mapper.UserConfigMapper;
|
|
|
import com.tuoren.forward.mapper.UserMapper;
|
|
|
import com.tuoren.forward.netty.aliutil.Config;
|
|
|
+import com.tuoren.forward.netty.excepetion.NExcCode;
|
|
|
import com.tuoren.forward.service.RegistLogService;
|
|
|
-import com.tuoren.forward.util.JsonExtractor;
|
|
|
+import com.tuoren.forward.netty.common.JsonExtractor;
|
|
|
import com.tuoren.forward.util.UUIDUtil;
|
|
|
+import com.tuoren.forward.netty.common.WifiDataTrans;
|
|
|
import io.netty.bootstrap.ServerBootstrap;
|
|
|
import io.netty.buffer.ByteBuf;
|
|
|
import io.netty.channel.*;
|
|
|
@@ -148,7 +153,7 @@ class MyServerHandler extends ChannelInboundHandlerAdapter {
|
|
|
registLog.setResult(jsonString);//保存结果
|
|
|
|
|
|
int code = jsonObject.getIntValue("code");
|
|
|
- if (code == 0){
|
|
|
+ if (code == 200){
|
|
|
registLog.setStatus("0");//成功
|
|
|
}else {
|
|
|
registLog.setStatus("1");//失败
|
|
|
@@ -189,78 +194,139 @@ class MyServerHandler extends ChannelInboundHandlerAdapter {
|
|
|
|
|
|
private String handleRegister(String msgStr) throws Exception {
|
|
|
JSONObject json = new JSONObject();
|
|
|
- json.put("code", 200);
|
|
|
+ json.put("code", NExcCode.SUCCESS.getCode());
|
|
|
|
|
|
if (StrUtil.isBlank(msgStr)) {
|
|
|
+ json.put("code", NExcCode.ERROR_PARAM.getCode());
|
|
|
+ json.put("msg", NExcCode.ERROR_PARAM.getMsg());
|
|
|
return json.toString();
|
|
|
}
|
|
|
+
|
|
|
JSONObject paramJson = JSONObject.parseObject(msgStr);
|
|
|
- String network = paramJson.getString("network");
|
|
|
- if (StrUtil.isEmpty(network)) {
|
|
|
- json.put("network", "NB-IoT");
|
|
|
- }
|
|
|
- String protocol = paramJson.getString("protocol");
|
|
|
- if (StrUtil.isEmpty(protocol)) {
|
|
|
- json.put("protocol", "CoAP");
|
|
|
- }
|
|
|
- String deviceName = paramJson.getString("deviceId"); //device表中的mac
|
|
|
- String userId = paramJson.getString("userId"); // user表中的code
|
|
|
- if (StrUtil.isEmpty(userId)) {
|
|
|
- json.put("userId", "1000");
|
|
|
- }
|
|
|
- String version = paramJson.getString("version");
|
|
|
- if (StrUtil.isEmpty(version)) {
|
|
|
- json.put("version", "1.0");
|
|
|
+
|
|
|
+ //网络类型
|
|
|
+ Integer networkCode = null;
|
|
|
+ try {
|
|
|
+ networkCode = paramJson.getIntValue("networkType");
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.warn("如果networkType为空或者不是整数, 使用默认值: NB-IoT", e);
|
|
|
}
|
|
|
- if ("WIFI".equalsIgnoreCase(network)) {
|
|
|
- JSONObject registerData = getRegisterData(deviceName, userId, network);
|
|
|
- //如果注册数据不为空,则构建响应消息
|
|
|
- if (!registerData.isEmpty()) {
|
|
|
- json.put("code", 0);
|
|
|
- json.put("type", 0x15);
|
|
|
- json.put("deviceId", deviceName);
|
|
|
-
|
|
|
- User user = userMapper.selectByCode(userId);
|
|
|
- String id = user.getId();
|
|
|
- UserConfig userConfig = userConfigMapper.selectByPrimaryKey(id);
|
|
|
- String wifi = userConfig.getWifi();
|
|
|
- Integer port = userConfig.getLocalPort();
|
|
|
- String address = userConfig.getLocalAddress();
|
|
|
-
|
|
|
- // 将WiFi字符串转换成JSON数组
|
|
|
- JSONArray wifiArray = JSON.parseArray(wifi);
|
|
|
- json.put("address", address);
|
|
|
- json.put("port", port);
|
|
|
- json.put("wifi", wifiArray);
|
|
|
- }
|
|
|
- } else {
|
|
|
+ NetworkType network = NetworkType.NetworkTypeName(networkCode);
|
|
|
+
|
|
|
+
|
|
|
+ //判断是否为老代码
|
|
|
+ boolean isOldCode = paramJson.containsKey("platform");
|
|
|
+ if (isOldCode) {
|
|
|
+ /**
|
|
|
+ * 老代码处理
|
|
|
+ */
|
|
|
+ String deviceName = paramJson.getString("deviceId"); //device表中的mac
|
|
|
+
|
|
|
//执行设备注册操作或查询操作
|
|
|
- JSONObject registerData = getRegisterData(deviceName, userId, network);
|
|
|
- //如果注册数据不为空,则构建响应消息
|
|
|
- if (!registerData.isEmpty()) {
|
|
|
- json.put("code", 0);
|
|
|
- json.put("deviceId", deviceName);
|
|
|
+ JSONObject registData = aliDeviceHttp.deviceRegist(deviceName);
|
|
|
+ if (registData.isEmpty()) {
|
|
|
+ registData = aliDeviceHttp.deviceQuery(deviceName);
|
|
|
+ }
|
|
|
+ if (!registData.isEmpty()) {
|
|
|
+ json.put("code", NExcCode.SUCCESS.getCode());
|
|
|
json.put("type", 0x15);
|
|
|
json.put("address", Config.COAP_ADDRESS);
|
|
|
json.put("port", Config.COAP_PORT);
|
|
|
- json.put("deviceSecret", registerData.getString("DeviceSecret"));
|
|
|
- json.put("productKey", registerData.getString("ProductKey"));
|
|
|
+ json.put("deviceName", deviceName);
|
|
|
+ json.put("deviceSecret", registData.getString("DeviceSecret"));
|
|
|
+ json.put("productKey", registData.getString("ProductKey"));
|
|
|
}
|
|
|
|
|
|
- }
|
|
|
- return json.toString();
|
|
|
+ }else {
|
|
|
+ /**
|
|
|
+ * 新代码处理
|
|
|
+ */
|
|
|
+ //平台
|
|
|
+ Platform platform = null;
|
|
|
+ //版本
|
|
|
+ int version = paramJson.getIntValue("version",1);//如果版本为空,则默认1
|
|
|
+
|
|
|
+ //网络协议
|
|
|
+ Integer protocolCode = null;
|
|
|
+ try {
|
|
|
+ protocolCode = paramJson.getIntValue("networkProtocol");
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 如果没有找到 networkType 或者它不是一个整数,则使用默认值
|
|
|
+ logger.warn("如果networkProtocol为空或者不是整数, 使用默认值: CoAP", e);
|
|
|
+ }
|
|
|
+ NetworkProtocol protocol = NetworkProtocol.NetworkProtocolName(protocolCode);
|
|
|
+
|
|
|
+ //产品编号
|
|
|
+ String productId = paramJson.getString("productId");
|
|
|
+ //如果为空,则默认为1dbfd476b7nm2
|
|
|
+ productId = StrUtil.isBlank(productId) ? "1dbfd476b7nm2" : productId;
|
|
|
+
|
|
|
+ //设备编号
|
|
|
+ String deviceName = paramJson.getString("deviceId"); //device表中的mac
|
|
|
+
|
|
|
+ //编码
|
|
|
+ String userId = paramJson.getString("userId"); // user表中的code
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 先判断UserId和DeviceId是否存在
|
|
|
+ * 如果存在进行注册,如果不存在返回code 401
|
|
|
+ *
|
|
|
+ */
|
|
|
+ User userIdCode = userMapper.selectByCode(userId);
|
|
|
+ if (userIdCode == null || StrUtil.isBlank(userIdCode.getCode())) {
|
|
|
+ json.put("code",NExcCode.ERROR_USERID.getCode());
|
|
|
+ json.put("msg", NExcCode.ERROR_USERID.getMsg());
|
|
|
+ logger.error("注册失败:userId不存在", json.toJSONString());
|
|
|
+ return json.toString();
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
|
|
|
- // 获取注册数据
|
|
|
- public JSONObject getRegisterData(String deviceName, String userId, String network) {
|
|
|
- JSONObject registData = aliDeviceHttp.deviceRegist(deviceName, userId, network);
|
|
|
- if (registData.isEmpty()) {
|
|
|
- registData = aliDeviceHttp.deviceQuery(deviceName, userId, network);
|
|
|
+ //根据网络类型进行判断
|
|
|
+ if (NetworkType.WIFI.equals(network)) {
|
|
|
+ /**
|
|
|
+ * wifi泵
|
|
|
+ */
|
|
|
+ platform = Platform.PlatformName(Platform.LOCAL.getCode());
|
|
|
+ // 处理WiFi注册
|
|
|
+ json = handleWifiRegistration(paramJson, network, platform, protocol, productId, deviceName, userId);
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 新NB,4G泵
|
|
|
+ */
|
|
|
+ platform = Platform.PlatformName(Platform.ALIYUN.getCode());
|
|
|
+
|
|
|
+ //执行设备注册操作或查询操作
|
|
|
+ JSONObject registData = aliDeviceHttp.deviceRegist(deviceName);
|
|
|
+ if (registData.isEmpty()) {
|
|
|
+ registData = aliDeviceHttp.deviceQuery(deviceName);
|
|
|
+ }
|
|
|
+ //如果注册数据不为空,则构建响应消息
|
|
|
+ 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());
|
|
|
+ json.put("networkProtocol", protocol.getCode());
|
|
|
+ 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("deviceSecret", registData.getString("DeviceSecret"));
|
|
|
+ json.put("productKey", registData.getString("ProductKey"));
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
}
|
|
|
- return registData;
|
|
|
+ return json.toString();
|
|
|
}
|
|
|
|
|
|
+
|
|
|
//获取参数中的deviceId
|
|
|
private String extractDeviceId(String msgStr) {
|
|
|
try {
|
|
|
@@ -271,7 +337,6 @@ class MyServerHandler extends ChannelInboundHandlerAdapter {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
//截取JSON数组中的数据
|
|
|
private JSONObject validateAndParseJson(String jsonString) throws Exception {
|
|
|
try {
|
|
|
@@ -283,6 +348,64 @@ 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();
|
|
|
+ int version = paramJson.getIntValue("version");
|
|
|
+ // 如果版本为空,则默认为1
|
|
|
+ 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());
|
|
|
+ json.put("networkProtocol", protocol.getCode());
|
|
|
+ json.put("productId", productId);
|
|
|
+ json.put("deviceId", deviceName);
|
|
|
+ json.put("userId", userId);
|
|
|
+
|
|
|
+ // 根据用户ID获取用户配置
|
|
|
+ User user = userMapper.selectByCode(userId);
|
|
|
+ String id = user.getId();
|
|
|
+ UserConfig userConfig = userConfigMapper.selectByPrimaryKey(id);
|
|
|
+ String wifi = userConfig.getWifi();
|
|
|
+ String addressArray = userConfig.getAddress();
|
|
|
+ String deviceSecret = userConfig.getDeviceSecret();
|
|
|
+
|
|
|
+ // 将地址数据转换成JSON数组
|
|
|
+ JSONArray addressArrayJson = JSON.parseArray(addressArray);
|
|
|
+
|
|
|
+ // 遍历循环, 查找与当前网络类型相同的地址
|
|
|
+ boolean found = false;
|
|
|
+ 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"));
|
|
|
+ found = true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!found) {
|
|
|
+ json.put("code", NExcCode.ERROR_NETWORK_TYPE.getCode());
|
|
|
+ json.put("msg", NExcCode.ERROR_NETWORK_TYPE.getMsg());
|
|
|
+ logger.error("注册失败:未找到匹配的网络类型:" + network.getName(), json.toJSONString());
|
|
|
+ return json;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 将WiFi字符串转换成JSON数组
|
|
|
+ JSONArray wifiArray = JSON.parseArray(wifi);
|
|
|
+ // 调用方法转换wifi数据
|
|
|
+ JSONArray transformWifiArray = WifiDataTrans.transformWifiArray(wifiArray);
|
|
|
+ json.put("deviceSecret",deviceSecret);
|
|
|
+ json.put("wifi", transformWifiArray);
|
|
|
+
|
|
|
+ return json;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|