|
@@ -3,12 +3,15 @@ package cc.iotkit.comps.service;
|
|
import cc.iotkit.common.Constants;
|
|
import cc.iotkit.common.Constants;
|
|
import cc.iotkit.common.exception.BizException;
|
|
import cc.iotkit.common.exception.BizException;
|
|
import cc.iotkit.common.utils.JsonUtil;
|
|
import cc.iotkit.common.utils.JsonUtil;
|
|
-import cc.iotkit.comp.model.DeviceMessage;
|
|
|
|
|
|
+import cc.iotkit.common.utils.UniqueIdUtil;
|
|
|
|
+import cc.iotkit.comp.model.RegisterInfo;
|
|
|
|
+import cc.iotkit.comp.model.DeviceState;
|
|
import cc.iotkit.comps.config.ServerConfig;
|
|
import cc.iotkit.comps.config.ServerConfig;
|
|
-import cc.iotkit.comps.model.RegisterInfo;
|
|
|
|
|
|
+import cc.iotkit.dao.DeviceCache;
|
|
import cc.iotkit.dao.DeviceRepository;
|
|
import cc.iotkit.dao.DeviceRepository;
|
|
import cc.iotkit.dao.ProductRepository;
|
|
import cc.iotkit.dao.ProductRepository;
|
|
import cc.iotkit.model.device.DeviceInfo;
|
|
import cc.iotkit.model.device.DeviceInfo;
|
|
|
|
+import cc.iotkit.model.device.message.ThingModelMessage;
|
|
import cc.iotkit.model.product.Product;
|
|
import cc.iotkit.model.product.Product;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.commons.lang3.RandomUtils;
|
|
import org.apache.commons.lang3.RandomUtils;
|
|
@@ -23,7 +26,6 @@ import org.springframework.stereotype.Service;
|
|
import javax.annotation.PostConstruct;
|
|
import javax.annotation.PostConstruct;
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
-import java.util.Map;
|
|
|
|
import java.util.Optional;
|
|
import java.util.Optional;
|
|
|
|
|
|
@Slf4j
|
|
@Slf4j
|
|
@@ -36,8 +38,12 @@ public class DeviceBehaviourService {
|
|
private DeviceRepository deviceRepository;
|
|
private DeviceRepository deviceRepository;
|
|
@Autowired
|
|
@Autowired
|
|
private ServerConfig serverConfig;
|
|
private ServerConfig serverConfig;
|
|
|
|
+ @Autowired
|
|
|
|
+ private DeviceCache deviceCache;
|
|
|
|
+ @Autowired
|
|
|
|
+ private DeviceStateHolder deviceStateHolder;
|
|
|
|
|
|
- private Producer<DeviceMessage> deviceMessageProducer;
|
|
|
|
|
|
+ private Producer<ThingModelMessage> deviceMessageProducer;
|
|
|
|
|
|
@PostConstruct
|
|
@PostConstruct
|
|
public void init() throws PulsarClientException {
|
|
public void init() throws PulsarClientException {
|
|
@@ -45,13 +51,12 @@ public class DeviceBehaviourService {
|
|
PulsarClient client = PulsarClient.builder()
|
|
PulsarClient client = PulsarClient.builder()
|
|
.serviceUrl(serverConfig.getPulsarBrokerUrl())
|
|
.serviceUrl(serverConfig.getPulsarBrokerUrl())
|
|
.build();
|
|
.build();
|
|
- deviceMessageProducer = client.newProducer(JSONSchema.of(DeviceMessage.class))
|
|
|
|
- .topic("persistent://public/default/device_raw")
|
|
|
|
|
|
+ deviceMessageProducer = client.newProducer(JSONSchema.of(ThingModelMessage.class))
|
|
|
|
+ .topic("persistent://iotkit/default/device_thing")
|
|
.create();
|
|
.create();
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
public void register(RegisterInfo info) {
|
|
public void register(RegisterInfo info) {
|
|
try {
|
|
try {
|
|
DeviceInfo deviceInfo = register(null, info);
|
|
DeviceInfo deviceInfo = register(null, info);
|
|
@@ -66,7 +71,17 @@ public class DeviceBehaviourService {
|
|
subDevice.getTag(), null));
|
|
subDevice.getTag(), null));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- //todo 产生设备注册事件
|
|
|
|
|
|
+
|
|
|
|
+ //设备注册消息
|
|
|
|
+ ThingModelMessage modelMessage = new ThingModelMessage(
|
|
|
|
+ UniqueIdUtil.newRequestId(), "",
|
|
|
|
+ info.getProductKey(), info.getDeviceName(),
|
|
|
|
+ ThingModelMessage.TYPE_LIFETIME, "register",
|
|
|
|
+ 0, new HashMap<>(), System.currentTimeMillis(),
|
|
|
|
+ System.currentTimeMillis()
|
|
|
|
+ );
|
|
|
|
+
|
|
|
|
+ reportMessage(modelMessage);
|
|
} catch (BizException e) {
|
|
} catch (BizException e) {
|
|
log.error("register device error", e);
|
|
log.error("register device error", e);
|
|
throw e;
|
|
throw e;
|
|
@@ -86,34 +101,20 @@ public class DeviceBehaviourService {
|
|
DeviceInfo device = deviceRepository.findByProductKeyAndDeviceName(pk, info.getDeviceName());
|
|
DeviceInfo device = deviceRepository.findByProductKeyAndDeviceName(pk, info.getDeviceName());
|
|
|
|
|
|
if (device != null) {
|
|
if (device != null) {
|
|
- //更新设备信息
|
|
|
|
- device.setParentId(parentId);
|
|
|
|
- device.setUid(uid);
|
|
|
|
- Map<String, Object> tag = info.getTag();
|
|
|
|
- Map<String, Object> oldTag = device.getTag();
|
|
|
|
-
|
|
|
|
- if (oldTag == null) {
|
|
|
|
- oldTag = new HashMap<>();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if (tag != null) {
|
|
|
|
- oldTag.putAll(tag);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- device.setTag(oldTag);
|
|
|
|
- } else {
|
|
|
|
- //不存在,注册新设备
|
|
|
|
- device = new DeviceInfo();
|
|
|
|
- device.setId(newDeviceId(info.getDeviceName()));
|
|
|
|
- device.setParentId(parentId);
|
|
|
|
- device.setUid(uid);
|
|
|
|
- device.setDeviceId(device.getId());
|
|
|
|
- device.setProductKey(pk);
|
|
|
|
- device.setDeviceName(info.getDeviceName());
|
|
|
|
- device.setTag(info.getTag());
|
|
|
|
- device.setState(new DeviceInfo.State(false, null, null));
|
|
|
|
- device.setCreateAt(System.currentTimeMillis());
|
|
|
|
|
|
+ log.info("device already registered.");
|
|
|
|
+ return device;
|
|
}
|
|
}
|
|
|
|
+ //不存在,注册新设备
|
|
|
|
+ device = new DeviceInfo();
|
|
|
|
+ device.setId(newDeviceId(info.getDeviceName()));
|
|
|
|
+ device.setParentId(parentId);
|
|
|
|
+ device.setUid(uid);
|
|
|
|
+ device.setDeviceId(device.getId());
|
|
|
|
+ device.setProductKey(pk);
|
|
|
|
+ device.setDeviceName(info.getDeviceName());
|
|
|
|
+ device.setTag(info.getTag());
|
|
|
|
+ device.setState(new DeviceInfo.State(false, null, null));
|
|
|
|
+ device.setCreateAt(System.currentTimeMillis());
|
|
|
|
|
|
deviceRepository.save(device);
|
|
deviceRepository.save(device);
|
|
log.info("device registered:{}", JsonUtil.toJsonString(device));
|
|
log.info("device registered:{}", JsonUtil.toJsonString(device));
|
|
@@ -171,19 +172,60 @@ public class DeviceBehaviourService {
|
|
if (device == null) {
|
|
if (device == null) {
|
|
throw new BizException("device does not exist");
|
|
throw new BizException("device does not exist");
|
|
}
|
|
}
|
|
|
|
+ deviceStateChange(device, online);
|
|
|
|
|
|
|
|
+ //可能是父设备,父设备离线,子设备也要离线
|
|
|
|
+ if (!online && device.getParentId() == null) {
|
|
|
|
+ List<DeviceInfo> subDevices = deviceRepository.findByParentId(device.getDeviceId());
|
|
|
|
+ for (DeviceInfo subDevice : subDevices) {
|
|
|
|
+ deviceStateChange(subDevice, false);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void deviceStateChange(DeviceInfo device, boolean online) {
|
|
if (online) {
|
|
if (online) {
|
|
device.getState().setOnline(true);
|
|
device.getState().setOnline(true);
|
|
device.getState().setOnlineTime(System.currentTimeMillis());
|
|
device.getState().setOnlineTime(System.currentTimeMillis());
|
|
|
|
+ deviceStateHolder.online(device.getDeviceId());
|
|
} else {
|
|
} else {
|
|
device.getState().setOnline(false);
|
|
device.getState().setOnline(false);
|
|
device.getState().setOfflineTime(System.currentTimeMillis());
|
|
device.getState().setOfflineTime(System.currentTimeMillis());
|
|
|
|
+ deviceStateHolder.offline(device.getDeviceId());
|
|
}
|
|
}
|
|
deviceRepository.save(device);
|
|
deviceRepository.save(device);
|
|
- //todo 产生在离线事件
|
|
|
|
|
|
+
|
|
|
|
+ //设备状态变更消息
|
|
|
|
+ ThingModelMessage modelMessage = new ThingModelMessage(
|
|
|
|
+ UniqueIdUtil.newRequestId(), "",
|
|
|
|
+ device.getProductKey(), device.getDeviceName(),
|
|
|
|
+ ThingModelMessage.TYPE_STATE,
|
|
|
|
+ online ? DeviceState.STATE_ONLINE : DeviceState.STATE_OFFLINE,
|
|
|
|
+ 0,
|
|
|
|
+ new HashMap<>(), System.currentTimeMillis(),
|
|
|
|
+ System.currentTimeMillis()
|
|
|
|
+ );
|
|
|
|
+
|
|
|
|
+ reportMessage(modelMessage);
|
|
}
|
|
}
|
|
|
|
|
|
- public void reportMessage(DeviceMessage message) throws PulsarClientException {
|
|
|
|
- deviceMessageProducer.send(message);
|
|
|
|
|
|
+ public void reportMessage(ThingModelMessage message) {
|
|
|
|
+ try {
|
|
|
|
+ DeviceInfo device = deviceCache.findByProductKeyAndDeviceName(message.getProductKey(),
|
|
|
|
+ message.getDeviceName());
|
|
|
|
+ if (device == null) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ if (message.getOccurred() == null) {
|
|
|
|
+ message.setOccurred(System.currentTimeMillis());
|
|
|
|
+ }
|
|
|
|
+ if (message.getTime() == null) {
|
|
|
|
+ message.setTime(System.currentTimeMillis());
|
|
|
|
+ }
|
|
|
|
+ message.setDeviceId(device.getDeviceId());
|
|
|
|
+ deviceMessageProducer.send(message);
|
|
|
|
+ } catch (PulsarClientException e) {
|
|
|
|
+ log.error("send thing model message error", e);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|