|
|
@@ -5,21 +5,20 @@ import com.github.microservice.models.hxz.DevicePingInfoParam;
|
|
|
import com.github.microservice.models.hxz.GateWayPingInfoParam;
|
|
|
import com.github.microservice.models.hxz.PingResult;
|
|
|
import com.github.microservice.models.type.DeviceType;
|
|
|
+import com.github.microservice.models.type.OnLineDeviceType;
|
|
|
import com.github.microservice.models.type.OnLineState;
|
|
|
+import com.github.microservice.net.ResultContent;
|
|
|
import com.zhongshu.iot.server.core.dao.mqtt.DeviceInfoDao;
|
|
|
+import com.zhongshu.iot.server.core.dao.mqtt.DeviceOnLineInfoDao;
|
|
|
import com.zhongshu.iot.server.core.dao.mqtt.DevicePingInfoDao;
|
|
|
import com.zhongshu.iot.server.core.dao.mqtt.GateWayInfoDao;
|
|
|
import com.zhongshu.iot.server.core.domain.ExecuteAnnotationService;
|
|
|
import com.zhongshu.iot.server.core.domain.ExecuteAnnotationServiceMethod;
|
|
|
-import com.zhongshu.iot.server.core.domain.iot.mqtt.DeviceInfo;
|
|
|
-import com.zhongshu.iot.server.core.domain.iot.mqtt.DevicePingInfo;
|
|
|
-import com.zhongshu.iot.server.core.domain.iot.mqtt.GateWay2Device;
|
|
|
-import com.zhongshu.iot.server.core.domain.iot.mqtt.GateWayInfo;
|
|
|
+import com.zhongshu.iot.server.core.domain.iot.mqtt.*;
|
|
|
import com.zhongshu.iot.server.core.service.base.CommonService;
|
|
|
import com.zhongshu.iot.server.core.service.base.SuperService;
|
|
|
import com.zhongshu.iot.server.core.service.sync.DeviceSyncFullCardService;
|
|
|
import com.zhongshu.iot.server.core.util.DateUtils;
|
|
|
-import com.github.microservice.net.ResultContent;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.ObjectUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
@@ -41,21 +40,24 @@ import java.util.Map;
|
|
|
public class DevicePingInfoService extends SuperService {
|
|
|
|
|
|
@Autowired
|
|
|
- DevicePingInfoDao devicePingInfoDao;
|
|
|
+ private DevicePingInfoDao devicePingInfoDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private DeviceInfoDao deviceInfoDao;
|
|
|
|
|
|
@Autowired
|
|
|
- DeviceInfoDao deviceInfoDao;
|
|
|
+ private CommonService commonService;
|
|
|
|
|
|
@Autowired
|
|
|
- CommonService commonService;
|
|
|
+ private DeviceSyncFullCardService deviceSyncFullCardService;
|
|
|
|
|
|
@Autowired
|
|
|
- DeviceSyncFullCardService deviceSyncFullCardService;
|
|
|
+ private GateWayInfoDao gateWayInfoDao;
|
|
|
|
|
|
@Autowired
|
|
|
- GateWayInfoDao gateWayInfoDao;
|
|
|
+ private DeviceOnLineInfoDao deviceOnLineInfoDao;
|
|
|
|
|
|
- // ping记录保存时间
|
|
|
+ // ping记录保存时间 1天
|
|
|
private Long pingTTl = 1 * 24L * 60 * 60 * 1000L;
|
|
|
|
|
|
//最大的心跳时间间隔 5分钟
|
|
|
@@ -95,6 +97,9 @@ public class DevicePingInfoService extends SuperService {
|
|
|
|
|
|
// 通知设备
|
|
|
deviceSyncFullCardService.noticeSyncDeviceOnlineTimeChange(deviceId);
|
|
|
+
|
|
|
+ // 设备在线状态
|
|
|
+ updateDeviceOnLine(deviceInfo.getDeviceId(), deviceInfo.getDeviceName(), OnLineDeviceType.Device);
|
|
|
} else {
|
|
|
log.info("心跳设备未找到: {}", deviceId);
|
|
|
}
|
|
|
@@ -131,6 +136,9 @@ public class DevicePingInfoService extends SuperService {
|
|
|
devicePingInfo.setTTL(new Date(System.currentTimeMillis() + pingTTl));
|
|
|
devicePingInfo.setTimeStr(DateUtils.paresTime(System.currentTimeMillis(), DateUtils.FORMAT_LONG));
|
|
|
devicePingInfoDao.save(devicePingInfo);
|
|
|
+
|
|
|
+ // 网关在线状态
|
|
|
+ updateDeviceOnLine(gateWayInfo.getGateWayId(), gateWayInfo.getGateWayName(), OnLineDeviceType.GateWay);
|
|
|
}
|
|
|
}
|
|
|
pingResult.setSuccess();
|
|
|
@@ -138,6 +146,45 @@ public class DevicePingInfoService extends SuperService {
|
|
|
return ResultContent.buildSuccess(pingResult);
|
|
|
}
|
|
|
|
|
|
+ private void updateDeviceOnLine(String deviceId, String deviceName, OnLineDeviceType onLineDeviceType) {
|
|
|
+ DeviceOnLineInfo entity = deviceOnLineInfoDao.findTopByDeviceId(deviceId);
|
|
|
+ if (ObjectUtils.isEmpty(entity)) {
|
|
|
+ entity = new DeviceOnLineInfo();
|
|
|
+ }
|
|
|
+ entity.setDeviceId(deviceId);
|
|
|
+ entity.setDeviceName(deviceName);
|
|
|
+ entity.setTTL(new Date(System.currentTimeMillis() + 5000));
|
|
|
+ entity.setTimeStr(DateUtils.paresTime(System.currentTimeMillis(), DateUtils.FORMAT_LONG));
|
|
|
+ entity.setOnLineDeviceType(onLineDeviceType);
|
|
|
+ deviceOnLineInfoDao.save(entity);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * TTL 到期,设备下线
|
|
|
+ *
|
|
|
+ * @param dataId
|
|
|
+ */
|
|
|
+ public void deviceTTLUnLine(String dataId) {
|
|
|
+ DeviceOnLineInfo entity = deviceOnLineInfoDao.findTopById(dataId);
|
|
|
+ if (ObjectUtils.isNotEmpty(entity)) {
|
|
|
+ if (entity.getOnLineDeviceType() == OnLineDeviceType.GateWay) {
|
|
|
+ GateWayInfo gateWayInfo = gateWayInfoDao.findTopByGateWayId(entity.getDeviceId());
|
|
|
+ OnLineState onLineState = OnLineState.OffLine;
|
|
|
+ Map<String, Object> standardData = new HashMap<String, Object>();
|
|
|
+ standardData.put("id", gateWayInfo.getId());
|
|
|
+ standardData.put("onLineState", onLineState);
|
|
|
+ commonService.updateData(standardData, GateWayInfo.class.getSimpleName());
|
|
|
+ } else if (entity.getOnLineDeviceType() == OnLineDeviceType.Device) {
|
|
|
+ DeviceInfo deviceInfo = deviceInfoDao.findTopByDeviceId(entity.getDeviceId());
|
|
|
+ OnLineState onLineState = OnLineState.OffLine;
|
|
|
+ Map<String, Object> standardData = new HashMap<String, Object>();
|
|
|
+ standardData.put("id", deviceInfo.getId());
|
|
|
+ standardData.put("onLineState", onLineState);
|
|
|
+ commonService.updateData(standardData, DeviceInfo.class.getSimpleName());
|
|
|
+ deviceSyncFullCardService.noticeSyncDeviceOnlineStateChange(deviceInfo.getDeviceId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 检查设备的状态
|