|
|
@@ -135,7 +135,10 @@ class MyServerHandler extends ChannelInboundHandlerAdapter {
|
|
|
byte[] resultByte = new byte[result.readableBytes()];
|
|
|
result.readBytes(resultByte);
|
|
|
result.release();
|
|
|
-
|
|
|
+ for (byte a : resultByte) {
|
|
|
+ System.out.printf("%02x", a);
|
|
|
+ }
|
|
|
+ System.out.println();
|
|
|
String msgStr = AES.decrypt(resultByte, key);
|
|
|
System.out.println("aes decrypt:" + msgStr);
|
|
|
logger.info("aes decrypt:" + msgStr);
|
|
|
@@ -186,7 +189,7 @@ class MyServerHandler extends ChannelInboundHandlerAdapter {
|
|
|
registLog.setStatus("2");//异常
|
|
|
registLog.setMsg(e.getMessage());
|
|
|
registLogMapper.insert(registLog);
|
|
|
- logger.error("regist error:" + e);
|
|
|
+ logger.error("regist error" , e);
|
|
|
}
|
|
|
//执行加密操作
|
|
|
byte[] resultRegister = AES.encrypt(jsonString, key);
|
|
|
@@ -216,8 +219,7 @@ class MyServerHandler extends ChannelInboundHandlerAdapter {
|
|
|
//判断传入的version是0或者为空
|
|
|
if (version == 0 || StrUtil.isBlank(String.valueOf(version))) {
|
|
|
processVersionZero(paramJson, json);
|
|
|
- }
|
|
|
- else if (version == 1) {
|
|
|
+ } else if (version == 1) {
|
|
|
//网络类型
|
|
|
Integer networkCode = paramJson.getIntValue("networkType");
|
|
|
NetworkType network = NetworkType.NetworkTypeName(networkCode);
|
|
|
@@ -225,7 +227,7 @@ class MyServerHandler extends ChannelInboundHandlerAdapter {
|
|
|
String userId = paramJson.getString("userId");//1000
|
|
|
//查询userId是否存在
|
|
|
User userIdCode = userMapper.selectByCode(userId);
|
|
|
- if ( userIdCode == null || StrUtil.isBlank(userIdCode.getCode())) {
|
|
|
+ if (userIdCode == null || StrUtil.isBlank(userIdCode.getCode())) {
|
|
|
return buildErrorResponse(NExcCode.ERROR_USERID, json);
|
|
|
}
|
|
|
//产品编号
|
|
|
@@ -240,6 +242,17 @@ class MyServerHandler extends ChannelInboundHandlerAdapter {
|
|
|
String deviceName = paramJson.getString("deviceId");
|
|
|
//SIM卡号
|
|
|
String sim = paramJson.getString("sim");
|
|
|
+ //通信模块mac(这是传给你的字段,然后插入到数据库中)
|
|
|
+ String netMac = paramJson.getString("netMac");
|
|
|
+ //查询出来返回给他ip范围(需要就注册用,不需要就传"")
|
|
|
+ Device deviceMac = deviceMapper.selectByMac(deviceName);
|
|
|
+ //ip范围
|
|
|
+ String ipAddress = deviceMac == null?"":deviceMac.getIpAddress();
|
|
|
+ //网关
|
|
|
+ String gateWay = deviceMac == null?"":deviceMac.getGateWay();
|
|
|
+ //子网掩码
|
|
|
+ String netMask = deviceMac == null?"":deviceMac.getNetMask();
|
|
|
+
|
|
|
//通过查询获取到参数
|
|
|
String id = userIdCode.getId();
|
|
|
//查找用户id
|
|
|
@@ -273,16 +286,19 @@ class MyServerHandler extends ChannelInboundHandlerAdapter {
|
|
|
return buildErrorResponse(NExcCode.ERROR_PLATFORM_NULL, json);
|
|
|
}
|
|
|
//添加修改设备
|
|
|
- handleDevice(deviceName, sim, productId, id);
|
|
|
+ handleDevice(platform, deviceName, netMac, ipAddress, sim, productId, id);
|
|
|
+
|
|
|
//通过平台进行设备注册
|
|
|
switch (platform) {
|
|
|
case 1:
|
|
|
- handleAliYunRegistration(json, version, network, userId, productId, protocol, deviceName, platform, productKey);
|
|
|
+ handleAliYunRegistration(json, version, network, userId, productId, protocol, deviceName, platform, productKey);//走的阿里云
|
|
|
break;
|
|
|
case 2:
|
|
|
return buildErrorResponse(NExcCode.ERROR_PLATFORM_TYPE, json);
|
|
|
case 3:
|
|
|
- handleLocalRegistration(json, paramJson, network, userId, productId, protocol, deviceName, wifi, deviceSecret, remoteAddress, remotePort);
|
|
|
+ handleLocalRegistration(json, paramJson, network,
|
|
|
+ userId, productId, protocol, deviceName,
|
|
|
+ ipAddress,gateWay,netMask, wifi, deviceSecret, remoteAddress, remotePort);
|
|
|
break;
|
|
|
default:
|
|
|
return buildErrorResponse(NExcCode.ERROR_VERSION_TYPE, json);
|
|
|
@@ -291,22 +307,28 @@ class MyServerHandler extends ChannelInboundHandlerAdapter {
|
|
|
return json.toString();
|
|
|
}
|
|
|
|
|
|
+
|
|
|
//处理版本1的local设备注册
|
|
|
- private void handleLocalRegistration(JSONObject json, JSONObject paramJson, NetworkType network, String userId, String productId, NetworkProtocol protocol, String deviceName, String wifi, String deviceSecret, String remoteAddress, int remotePort) {
|
|
|
+ private void handleLocalRegistration(JSONObject json, JSONObject paramJson, NetworkType network, String userId,
|
|
|
+ String productId, NetworkProtocol protocol, String deviceName,
|
|
|
+ String ipAddress,String gateWay,String netMask, String wifi, String deviceSecret, String remoteAddress, int remotePort) {
|
|
|
//WiFi注册
|
|
|
if (NetworkType.WIFI.equals(network)) {
|
|
|
- LocalReturnParam(paramJson, json, network, protocol, deviceName, userId, productId, remoteAddress, remotePort, deviceSecret);
|
|
|
+ LocalReturnParam(paramJson, json, network, protocol, deviceName,
|
|
|
+ ipAddress,gateWay,netMask, userId, productId, remoteAddress, remotePort, deviceSecret);
|
|
|
JSONArray wifiArray = JSON.parseArray(wifi);
|
|
|
JSONArray transformWifiArray = WifiDataTrans.transformWifiArray(wifiArray);
|
|
|
json.put("wifi", transformWifiArray);
|
|
|
}
|
|
|
// 4G注册
|
|
|
else if (NetworkType.LTE.equals(network)) {
|
|
|
- LocalReturnParam(paramJson, json, network, protocol, deviceName, userId, productId, remoteAddress, remotePort, deviceSecret);
|
|
|
+ LocalReturnParam(paramJson, json, network, protocol, deviceName,
|
|
|
+ ipAddress,gateWay,netMask, userId, productId, remoteAddress, remotePort, deviceSecret);
|
|
|
}
|
|
|
// NB注册
|
|
|
else if (NetworkType.NB_IOT.equals(network)) {
|
|
|
- LocalReturnParam(paramJson, json, network, protocol, deviceName, userId, productId, remoteAddress, remotePort, deviceSecret);
|
|
|
+ LocalReturnParam(paramJson, json, network, protocol, deviceName,
|
|
|
+ ipAddress,gateWay,netMask, userId, productId, remoteAddress, remotePort, deviceSecret);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -334,6 +356,7 @@ class MyServerHandler extends ChannelInboundHandlerAdapter {
|
|
|
json.put("productKey", productKey);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
//处理版本0的设备注册
|
|
|
private void processVersionZero(JSONObject paramJson, JSONObject json) {
|
|
|
String deviceName = paramJson.getString("deviceId"); //device表中的mac
|
|
|
@@ -350,32 +373,32 @@ class MyServerHandler extends ChannelInboundHandlerAdapter {
|
|
|
json.put("deviceSecret", registData.getString("DeviceSecret"));
|
|
|
json.put("productKey", registData.getString("ProductKey"));
|
|
|
}
|
|
|
-
|
|
|
- Device device = deviceMapper.selectByMac(deviceName);
|
|
|
- if(device != null) {
|
|
|
- device.setSim(sim);
|
|
|
- if(StringUtils.isEmpty(userCode)) {
|
|
|
- User user = userMapper.selectByCode(userCode);
|
|
|
- if(user != null) {
|
|
|
- device.setTenantId(user.getId());
|
|
|
- }
|
|
|
- deviceMapper.updateByPrimaryKeySelective2(device);
|
|
|
+
|
|
|
+ Device device = deviceMapper.selectByMac(deviceName);
|
|
|
+ if (device != null) {
|
|
|
+ device.setSim(sim);
|
|
|
+ if (StringUtils.isEmpty(userCode)) {
|
|
|
+ User user = userMapper.selectByCode(userCode);
|
|
|
+ if (user != null) {
|
|
|
+ device.setTenantId(user.getId());
|
|
|
+ }
|
|
|
+ deviceMapper.updateByPrimaryKeySelective2(device);
|
|
|
}
|
|
|
- }else {
|
|
|
- device = new Device();
|
|
|
- device.setId(UUIDUtil.get32UUID());
|
|
|
- device.setMac(deviceName);
|
|
|
- device.setSim(sim);
|
|
|
- device.setCreatetime(new Date());
|
|
|
- if(StringUtils.isEmpty(userCode)) {
|
|
|
- User user = userMapper.selectByCode(userCode);
|
|
|
- if(user != null) {
|
|
|
- device.setTenantId(user.getId());
|
|
|
- }
|
|
|
+ } else {
|
|
|
+ device = new Device();
|
|
|
+ device.setId(UUIDUtil.get32UUID());
|
|
|
+ device.setMac(deviceName);
|
|
|
+ device.setSim(sim);
|
|
|
+ device.setCreatetime(new Date());
|
|
|
+ if (StringUtils.isEmpty(userCode)) {
|
|
|
+ User user = userMapper.selectByCode(userCode);
|
|
|
+ if (user != null) {
|
|
|
+ device.setTenantId(user.getId());
|
|
|
+ }
|
|
|
}
|
|
|
- deviceMapper.insertSelective(device);
|
|
|
+ deviceMapper.insertSelective(device);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
|
|
|
//截取deviceId
|
|
|
@@ -390,20 +413,23 @@ class MyServerHandler extends ChannelInboundHandlerAdapter {
|
|
|
}
|
|
|
|
|
|
//新增修改设备信息
|
|
|
- private void handleDevice(String deviceName, String sim, String productId, String id) {
|
|
|
+ private void handleDevice(Integer platform, String deviceName, String netMac, String ipAddress, String sim, String productId, String id) {
|
|
|
//查询数据库是否存在该设备
|
|
|
Device deviceMac = deviceMapper.selectByMac(deviceName);
|
|
|
if (deviceMac == null) {
|
|
|
- //不存在,则新增设备
|
|
|
- //添加操作
|
|
|
- Device NewDevice = new Device();
|
|
|
- NewDevice.setId(UUIDUtil.get32UUID());
|
|
|
- NewDevice.setMac(deviceName);
|
|
|
- NewDevice.setSim(sim);
|
|
|
- NewDevice.setProductId(productId);
|
|
|
- NewDevice.setCreatetime(new Date());
|
|
|
- NewDevice.setTenantId(id);
|
|
|
- deviceMapper.insertSelective(NewDevice);
|
|
|
+ Device newDevice = new Device();
|
|
|
+ newDevice.setId(UUIDUtil.get32UUID());
|
|
|
+ newDevice.setMac(deviceName);
|
|
|
+ newDevice.setSim(sim);
|
|
|
+ newDevice.setProductId(productId);
|
|
|
+ newDevice.setCreatetime(new Date());
|
|
|
+ newDevice.setTenantId(id);
|
|
|
+
|
|
|
+ if (platform == 3) {
|
|
|
+ newDevice.setNetMac(netMac);
|
|
|
+ }
|
|
|
+
|
|
|
+ deviceMapper.insertSelective(newDevice);
|
|
|
} else {
|
|
|
//如果存在,检查productId是否一致
|
|
|
boolean needUpdate = false;
|
|
|
@@ -428,6 +454,16 @@ class MyServerHandler extends ChannelInboundHandlerAdapter {
|
|
|
deviceMac.setModifytime(new Date());
|
|
|
needUpdate = true;
|
|
|
}
|
|
|
+
|
|
|
+ if (platform == 3) {
|
|
|
+ String deviceMacNetMac = deviceMac.getNetMac();
|
|
|
+ if (StrUtil.isBlank(deviceMacNetMac) || !netMac.equals(deviceMacNetMac)) {
|
|
|
+ deviceMac.setNetMac(netMac);
|
|
|
+ deviceMac.setModifytime(new Date());
|
|
|
+ needUpdate = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
if (needUpdate) {
|
|
|
deviceMapper.updateByMac(deviceMac);
|
|
|
logger.debug("Device updated: {}", deviceMac);
|
|
|
@@ -436,15 +472,33 @@ class MyServerHandler extends ChannelInboundHandlerAdapter {
|
|
|
}
|
|
|
|
|
|
//Local 平台返回参数
|
|
|
- private void LocalReturnParam(JSONObject paramJson, JSONObject json, NetworkType network, NetworkProtocol protocol, String deviceName, String userId, String productId, String remoteAddress, int remotePort, String deviceSecret) {
|
|
|
+ private void LocalReturnParam(JSONObject paramJson, JSONObject json, NetworkType network,
|
|
|
+ NetworkProtocol protocol, String deviceName,
|
|
|
+ String ipAddress,String gateWay,String netMask, String userId, String productId,
|
|
|
+ String remoteAddress, int remotePort, String deviceSecret) {
|
|
|
json.put("code", NExcCode.SUCCESS.getCode());
|
|
|
json.put("version", paramJson.getString("version"));
|
|
|
json.put("platform", Platform.LOCAL.getCode());
|
|
|
json.put("networkType", network.getCode());
|
|
|
json.put("networkProtocol", protocol.getCode());
|
|
|
- json.put("productId", paramJson.getString("productId"));
|
|
|
+ json.put("productId", productId);
|
|
|
json.put("deviceId", deviceName);
|
|
|
- json.put("userId", paramJson.getString("userId"));
|
|
|
+ if (StrUtil.isBlank(ipAddress)) {
|
|
|
+ json.put("ipAddress", "");
|
|
|
+ } else {
|
|
|
+ json.put("ipAddress", ipAddress);
|
|
|
+ }
|
|
|
+ if (StrUtil.isBlank(gateWay)) {
|
|
|
+ json.put("gateWay", "");
|
|
|
+ } else {
|
|
|
+ json.put("gateWay", gateWay);
|
|
|
+ }
|
|
|
+ if (StrUtil.isBlank(netMask)) {
|
|
|
+ json.put("netMask", "");
|
|
|
+ } else {
|
|
|
+ json.put("netMask", netMask);
|
|
|
+ }
|
|
|
+ json.put("userId", userId);
|
|
|
json.put("remoteAddress", remoteAddress);
|
|
|
json.put("remotePort", remotePort);
|
|
|
json.put("deviceSecret", deviceSecret);
|