feat:emqx插件增加子设备注册

master
xiwa 2024-02-25 00:18:39 +08:00
parent 9124e5be37
commit b6256a2123
2 changed files with 49 additions and 16 deletions

View File

@ -10,9 +10,14 @@ package cc.iotkit.plugins.emqx.service;
*/ */
import cc.iotkit.common.utils.CodecUtil; 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.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 cc.iotkit.plugin.core.thing.model.ThingProduct;
import com.gitee.starblues.bootstrap.annotation.AutowiredType; 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.AbstractVerticle;
import io.vertx.core.http.HttpMethod; import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServer; import io.vertx.core.http.HttpServer;
@ -43,6 +48,9 @@ public class AuthVerticle extends AbstractVerticle {
@AutowiredType(AutowiredType.Type.MAIN_PLUGIN) @AutowiredType(AutowiredType.Type.MAIN_PLUGIN)
private IThingService thingService; private IThingService thingService;
@Autowired
private PluginInfo pluginInfo;
@Override @Override
public void start() { public void start() {
backendServer = vertx.createHttpServer(); backendServer = vertx.createHttpServer();
@ -80,6 +88,7 @@ public class AuthVerticle extends AbstractVerticle {
String productKey = parts[0]; String productKey = parts[0];
String deviceName = parts[1]; String deviceName = parts[1];
String gwModel = parts[2];
if (!username.equals(deviceName)) { if (!username.equals(deviceName)) {
log.error("username:{}不正确", deviceName); log.error("username:{}不正确", deviceName);
httpResult(rc.response(), 403); httpResult(rc.response(), 403);
@ -100,9 +109,27 @@ public class AuthVerticle extends AbstractVerticle {
return; 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<String> devices = new HashSet<>(); Set<String> devices = new HashSet<>();
devices.add(parts[0] + "," + parts[1]); devices.add(productKey + "," + deviceName);
EmqxPlugin.CLIENT_DEVICE_MAP.putIfAbsent(parts[0] + parts[1], devices); EmqxPlugin.CLIENT_DEVICE_MAP.putIfAbsent(productKey + deviceName, devices);
httpResult(rc.response(), 200); httpResult(rc.response(), 200);
} catch (Throwable e) { } catch (Throwable e) {

View File

@ -200,16 +200,22 @@ public class EmqxPlugin implements PluginCloseListener, IPlugin, Runnable {
//子设备注册 //子设备注册
String subPk = params.getString("productKey"); String subPk = params.getString("productKey");
String subDn = params.getString("deviceName"); String subDn = params.getString("deviceName");
String subModel = params.getString("model");
ActionResult regResult = thingService.post( ActionResult regResult = thingService.post(
pluginInfo.getPluginId(), pluginInfo.getPluginId(),
fillAction( fillAction(
SubDeviceRegister.builder()
.productKey(device.getProductKey())
.deviceName(device.getDeviceName())
.version("1.0")
.subs(List.of(
DeviceRegister.builder() DeviceRegister.builder()
.productKey(subPk) .productKey(subPk)
.deviceName(subDn) .deviceName(subDn)
.model(params.getString("model")) .model(subModel)
.version("1.0") .build()
))
.build() .build()
, subPk, subDn
) )
); );
if (regResult.getCode() == 0) { if (regResult.getCode() == 0) {
@ -289,9 +295,10 @@ public class EmqxPlugin implements PluginCloseListener, IPlugin, Runnable {
thingService.post( thingService.post(
pluginInfo.getPluginId(), pluginInfo.getPluginId(),
fillAction(DeviceStateChange.builder() fillAction(DeviceStateChange.builder()
.productKey(pk)
.deviceName(dn)
.state(DeviceState.ONLINE) .state(DeviceState.ONLINE)
.build() .build()
, pk, dn
) )
); );
DEVICE_ONLINE.put(dn, true); DEVICE_ONLINE.put(dn, true);
@ -306,19 +313,18 @@ public class EmqxPlugin implements PluginCloseListener, IPlugin, Runnable {
thingService.post( thingService.post(
pluginInfo.getPluginId(), pluginInfo.getPluginId(),
fillAction(DeviceStateChange.builder() fillAction(DeviceStateChange.builder()
.productKey(pkDn[0])
.deviceName(pkDn[1])
.state(DeviceState.OFFLINE) .state(DeviceState.OFFLINE)
.build() .build()
, pkDn[0], pkDn[1]
) )
); );
DEVICE_ONLINE.remove(pkDn[1]); DEVICE_ONLINE.remove(pkDn[1]);
} }
} }
private IDeviceAction fillAction(IDeviceAction action, String productKey, String deviceName) { private IDeviceAction fillAction(IDeviceAction action) {
action.setId(UniqueIdUtil.newRequestId()); action.setId(UniqueIdUtil.newRequestId());
action.setProductKey(productKey);
action.setDeviceName(deviceName);
action.setTime(System.currentTimeMillis()); action.setTime(System.currentTimeMillis());
return action; return action;
} }