diff --git a/emqx-plugin/src/main/java/cc/iotkit/plugins/emqx/service/AuthVerticle.java b/emqx-plugin/src/main/java/cc/iotkit/plugins/emqx/service/AuthVerticle.java index d24ce80..24466e4 100644 --- a/emqx-plugin/src/main/java/cc/iotkit/plugins/emqx/service/AuthVerticle.java +++ b/emqx-plugin/src/main/java/cc/iotkit/plugins/emqx/service/AuthVerticle.java @@ -10,9 +10,14 @@ package cc.iotkit.plugins.emqx.service; */ import cc.iotkit.common.utils.CodecUtil; +import cc.iotkit.common.utils.UniqueIdUtil; import cc.iotkit.plugin.core.thing.IThingService; +import cc.iotkit.plugin.core.thing.actions.ActionResult; +import cc.iotkit.plugin.core.thing.actions.up.DeviceRegister; import cc.iotkit.plugin.core.thing.model.ThingProduct; import com.gitee.starblues.bootstrap.annotation.AutowiredType; +import com.gitee.starblues.core.PluginInfo; +import io.netty.handler.codec.mqtt.MqttConnectReturnCode; import io.vertx.core.AbstractVerticle; import io.vertx.core.http.HttpMethod; import io.vertx.core.http.HttpServer; @@ -43,6 +48,9 @@ public class AuthVerticle extends AbstractVerticle { @AutowiredType(AutowiredType.Type.MAIN_PLUGIN) private IThingService thingService; + @Autowired + private PluginInfo pluginInfo; + @Override public void start() { backendServer = vertx.createHttpServer(); @@ -80,6 +88,7 @@ public class AuthVerticle extends AbstractVerticle { String productKey = parts[0]; String deviceName = parts[1]; + String gwModel = parts[2]; if (!username.equals(deviceName)) { log.error("username:{}不正确", deviceName); httpResult(rc.response(), 403); @@ -100,9 +109,27 @@ public class AuthVerticle extends AbstractVerticle { return; } + //网关设备注册 + ActionResult result = thingService.post( + pluginInfo.getPluginId(), + DeviceRegister.builder() + .productKey(productKey) + .deviceName(deviceName) + .model(gwModel) + .version("1.0") + .id(UniqueIdUtil.newRequestId()) + .time(System.currentTimeMillis()) + .build() + ); + if (result.getCode() != 0) { + log.error("设备注册失败:{}", result); + httpResult(rc.response(), 403); + return; + } + Set devices = new HashSet<>(); - devices.add(parts[0] + "," + parts[1]); - EmqxPlugin.CLIENT_DEVICE_MAP.putIfAbsent(parts[0] + parts[1], devices); + devices.add(productKey + "," + deviceName); + EmqxPlugin.CLIENT_DEVICE_MAP.putIfAbsent(productKey + deviceName, devices); httpResult(rc.response(), 200); } catch (Throwable e) { diff --git a/emqx-plugin/src/main/java/cc/iotkit/plugins/emqx/service/EmqxPlugin.java b/emqx-plugin/src/main/java/cc/iotkit/plugins/emqx/service/EmqxPlugin.java index 5270722..014764f 100644 --- a/emqx-plugin/src/main/java/cc/iotkit/plugins/emqx/service/EmqxPlugin.java +++ b/emqx-plugin/src/main/java/cc/iotkit/plugins/emqx/service/EmqxPlugin.java @@ -200,16 +200,22 @@ public class EmqxPlugin implements PluginCloseListener, IPlugin, Runnable { //子设备注册 String subPk = params.getString("productKey"); String subDn = params.getString("deviceName"); + String subModel = params.getString("model"); ActionResult regResult = thingService.post( pluginInfo.getPluginId(), fillAction( - DeviceRegister.builder() - .productKey(subPk) - .deviceName(subDn) - .model(params.getString("model")) + SubDeviceRegister.builder() + .productKey(device.getProductKey()) + .deviceName(device.getDeviceName()) .version("1.0") + .subs(List.of( + DeviceRegister.builder() + .productKey(subPk) + .deviceName(subDn) + .model(subModel) + .build() + )) .build() - , subPk, subDn ) ); if (regResult.getCode() == 0) { @@ -289,9 +295,10 @@ public class EmqxPlugin implements PluginCloseListener, IPlugin, Runnable { thingService.post( pluginInfo.getPluginId(), fillAction(DeviceStateChange.builder() - .state(DeviceState.ONLINE) - .build() - , pk, dn + .productKey(pk) + .deviceName(dn) + .state(DeviceState.ONLINE) + .build() ) ); DEVICE_ONLINE.put(dn, true); @@ -306,19 +313,18 @@ public class EmqxPlugin implements PluginCloseListener, IPlugin, Runnable { thingService.post( pluginInfo.getPluginId(), fillAction(DeviceStateChange.builder() - .state(DeviceState.OFFLINE) - .build() - , pkDn[0], pkDn[1] + .productKey(pkDn[0]) + .deviceName(pkDn[1]) + .state(DeviceState.OFFLINE) + .build() ) ); DEVICE_ONLINE.remove(pkDn[1]); } } - private IDeviceAction fillAction(IDeviceAction action, String productKey, String deviceName) { + private IDeviceAction fillAction(IDeviceAction action) { action.setId(UniqueIdUtil.newRequestId()); - action.setProductKey(productKey); - action.setDeviceName(deviceName); action.setTime(System.currentTimeMillis()); return action; }