EmqxDeviceComponent 判断设备是否存在,从数据库读取。

V0.5.x
七步才子 2022-05-21 20:43:56 +08:00
parent beeb1014a9
commit 0d60b00235
1 changed files with 39 additions and 18 deletions

View File

@ -10,6 +10,8 @@ import cc.iotkit.comp.model.DeviceState;
import cc.iotkit.comp.utils.SpringUtils; import cc.iotkit.comp.utils.SpringUtils;
import cc.iotkit.converter.DeviceMessage; import cc.iotkit.converter.DeviceMessage;
import cc.iotkit.converter.ThingService; import cc.iotkit.converter.ThingService;
import cc.iotkit.dao.DeviceRepository;
import cc.iotkit.model.device.DeviceInfo;
import cc.iotkit.model.device.message.ThingModelMessage; import cc.iotkit.model.device.message.ThingModelMessage;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import io.netty.handler.codec.mqtt.MqttQoS; import io.netty.handler.codec.mqtt.MqttQoS;
@ -36,7 +38,6 @@ public class EmqxDeviceComponent extends AbstractDeviceComponent {
private static final Logger log = LoggerFactory.getLogger(EmqxDeviceComponent.class); private static final Logger log = LoggerFactory.getLogger(EmqxDeviceComponent.class);
private Vertx vertx; private Vertx vertx;
private AuthVerticle authVerticle; private AuthVerticle authVerticle;
//private MqttVerticle mqttVerticle;
private CountDownLatch countDownLatch; private CountDownLatch countDownLatch;
private String deployedId; private String deployedId;
private EmqxConfig mqttConfig; private EmqxConfig mqttConfig;
@ -45,9 +46,6 @@ public class EmqxDeviceComponent extends AbstractDeviceComponent {
//组件mqtt clientId默认通过mqtt auth验证。 //组件mqtt clientId默认通过mqtt auth验证。
private Set<String> compMqttClientIdList = new HashSet<>(); private Set<String> compMqttClientIdList = new HashSet<>();
private final Map<String, Device> deviceChildToParent = new HashMap<>();
private TransparentConverter transparentConverter = new TransparentConverter(); private TransparentConverter transparentConverter = new TransparentConverter();
public void create(CompConfig config) { public void create(CompConfig config) {
@ -170,9 +168,23 @@ public class EmqxDeviceComponent extends AbstractDeviceComponent {
if (parent == null) { if (parent == null) {
return; return;
} }
Device device = new Device(state.getProductKey(), state.getDeviceName()); //Device device = new Device(state.getProductKey(), state.getDeviceName());
DeviceRepository deviceRepository = SpringUtils.getBean(DeviceRepository.class);
if (DeviceState.STATE_ONLINE.equals(state.getState())) { DeviceInfo deviceInfo = deviceRepository.findByProductKeyAndDeviceName(state.getProductKey(), state.getDeviceName());
if (deviceInfo != null) {
boolean isOnline = DeviceState.STATE_ONLINE.equals(state.getState());
deviceInfo.getState().setOnline(isOnline);
if(!isOnline) {
deviceInfo.getState().setOfflineTime(System.currentTimeMillis());
}
if(isOnline) {
deviceInfo.getState().setOnlineTime(System.currentTimeMillis());
}
deviceRepository.save(deviceInfo);
}
/*if (DeviceState.STATE_ONLINE.equals(state.getState())) {
//保存子设备所属父设备 //保存子设备所属父设备
deviceChildToParent.put(device.toString(), deviceChildToParent.put(device.toString(),
new Device(parent.getProductKey(), parent.getDeviceName()) new Device(parent.getProductKey(), parent.getDeviceName())
@ -180,17 +192,20 @@ public class EmqxDeviceComponent extends AbstractDeviceComponent {
} else { } else {
//删除关系 //删除关系
deviceChildToParent.remove(device.toString()); deviceChildToParent.remove(device.toString());
} }*/
} }
@Override @Override
public void send(DeviceMessage message) { public void send(DeviceMessage message) {
Device child = new Device(message.getProductKey(), message.getDeviceName()); /*DeviceRepository deviceRepository = SpringUtils.getBean(DeviceRepository.class);
DeviceInfo child = deviceRepository.findByProductKeyAndDeviceName(message.getProductKey(), message.getDeviceName());
//作为子设备查找父设备 //作为子设备查找父设备
Device parent = deviceChildToParent.get(child.toString()); DeviceInfo parent = deviceRepository.findByDeviceId(child.getParentId());
if (parent == null) { if (parent == null) {
parent = child; parent = child;
} }*/
Object obj = message.getContent(); Object obj = message.getContent();
if (!(obj instanceof Map)) { if (!(obj instanceof Map)) {
@ -215,15 +230,21 @@ public class EmqxDeviceComponent extends AbstractDeviceComponent {
@Override @Override
public boolean exist(String productKey, String deviceName) { public boolean exist(String productKey, String deviceName) {
return true;
/*//先作为子设备查找是否存在父设备 DeviceRepository deviceRepository = SpringUtils.getBean(DeviceRepository.class);
Device device = deviceChildToParent.get(new Device(productKey, deviceName).toString());
if (device != null) { DeviceInfo child = deviceRepository.findByProductKeyAndDeviceName(productKey, deviceName);
if (child != null) {
return true; return true;
}*/ }
//return mqttVerticle.exist(productKey, deviceName); //作为子设备查找父设备
DeviceInfo parent = deviceRepository.findByDeviceId(child.getParentId());
if (parent != null) {
return true;
}
return false;
} }
@ -243,8 +264,8 @@ public class EmqxDeviceComponent extends AbstractDeviceComponent {
return transparentConverter.encode(service, device); return transparentConverter.encode(service, device);
} }
public Object getCompMqttClientIdList(){ public Object getCompMqttClientIdList() {
String[] result = compMqttClientIdList.toArray(new String[0]); String[] result = compMqttClientIdList.toArray(new String[0]);
return JsonUtil.toJsonString(result); return JsonUtil.toJsonString(result);
} }