|
|
@@ -5,7 +5,6 @@ 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.net.ResultContent;
|
|
|
-import com.github.microservice.types.deviceUse.OnLineDeviceType;
|
|
|
import com.github.microservice.types.deviceUse.OnLineState;
|
|
|
import com.zhongshu.iot.server.core.dao.mqtt.DeviceInfoDao;
|
|
|
import com.zhongshu.iot.server.core.dao.mqtt.DeviceOnLineInfoDao;
|
|
|
@@ -58,6 +57,25 @@ public class DevicePingInfoService extends SuperService {
|
|
|
@Autowired
|
|
|
private RedisService redisService;
|
|
|
|
|
|
+ public ResultContent<Object> ping(String dataId, String dataStr) {
|
|
|
+ PingResult pingResult = new PingResult();
|
|
|
+ GateWayPingInfoParam param = JSONUtil.toBean(dataStr, GateWayPingInfoParam.class);
|
|
|
+
|
|
|
+ if (StringUtils.isNotEmpty(param.getDeviceId())) {
|
|
|
+ ResultContent resultContent = devicePing(dataId, dataStr);
|
|
|
+ if (resultContent.isFailed()) {
|
|
|
+ pingResult.setFailed(resultContent.getMsg());
|
|
|
+ return ResultContent.buildSuccess(pingResult);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ pingResult.setFailed("ping设备不存在");
|
|
|
+ return ResultContent.buildSuccess(pingResult);
|
|
|
+ }
|
|
|
+ pingResult.setSuccess();
|
|
|
+ pingResult.setTime(System.currentTimeMillis());
|
|
|
+ return ResultContent.buildSuccess(pingResult);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 设备ping
|
|
|
*
|
|
|
@@ -71,20 +89,8 @@ public class DevicePingInfoService extends SuperService {
|
|
|
log.info("devicePing 设备在线处理:{}", deviceId);
|
|
|
DeviceInfo deviceInfo = deviceInfoDao.findTopByDeviceId(deviceId);
|
|
|
if (ObjectUtils.isNotEmpty(deviceInfo)) {
|
|
|
- Long time = System.currentTimeMillis();
|
|
|
- Map<String, Object> standardData = new HashMap<String, Object>();
|
|
|
- standardData.put("id", deviceInfo.getId());
|
|
|
- standardData.put("lastOnlineTime", time);
|
|
|
- standardData.put("lastOnlineTimeStr", DateUtils.paresTime(System.currentTimeMillis(), DateUtils.FORMAT_LONG));
|
|
|
- standardData.put("onLineState", OnLineState.OnLine);
|
|
|
- commonService.updateData(standardData, DeviceInfo.class.getSimpleName());
|
|
|
-
|
|
|
- Map<String, Object> whereMap = new HashMap<String, Object>();
|
|
|
- whereMap.put("deviceId", deviceId);
|
|
|
|
|
|
- Map<String, Object> dataMap = new HashMap<String, Object>();
|
|
|
- dataMap.put("state", OnLineState.OnLine);
|
|
|
- commonService.updateData(whereMap, dataMap, GateWay2Device.class.getSimpleName());
|
|
|
+ updateDeviceOnLine(deviceInfo, OnLineState.OnLine);
|
|
|
|
|
|
// ping记录
|
|
|
DevicePingInfo devicePingInfo = new DevicePingInfo();
|
|
|
@@ -100,8 +106,8 @@ public class DevicePingInfoService extends SuperService {
|
|
|
// 通知设备
|
|
|
deviceSyncFullCardService.noticeSyncDeviceOnlineTimeChange(deviceId);
|
|
|
|
|
|
- // 设备在线状态
|
|
|
- updateDeviceOnLine(deviceInfo.getDeviceId(), deviceInfo.getDeviceName());
|
|
|
+ // 设备在线状态 检查
|
|
|
+ updateDeviceOnLineCheck(deviceInfo.getDeviceId(), deviceInfo.getDeviceName());
|
|
|
} else {
|
|
|
log.info("心跳设备未找到: {}", deviceId);
|
|
|
return ResultContent.buildFail(String.format("设备不存在:%s", deviceId));
|
|
|
@@ -109,26 +115,13 @@ public class DevicePingInfoService extends SuperService {
|
|
|
return ResultContent.buildSuccess();
|
|
|
}
|
|
|
|
|
|
- public ResultContent<Object> ping(String dataId, String dataStr) {
|
|
|
- PingResult pingResult = new PingResult();
|
|
|
- GateWayPingInfoParam param = JSONUtil.toBean(dataStr, GateWayPingInfoParam.class);
|
|
|
-
|
|
|
- if (StringUtils.isNotEmpty(param.getDeviceId())) {
|
|
|
- ResultContent resultContent = devicePing(dataId, dataStr);
|
|
|
- if (resultContent.isFailed()) {
|
|
|
- pingResult.setFailed(resultContent.getMsg());
|
|
|
- return ResultContent.buildSuccess(pingResult);
|
|
|
- }
|
|
|
- } else {
|
|
|
- pingResult.setFailed("ping设备不存在");
|
|
|
- return ResultContent.buildSuccess(pingResult);
|
|
|
- }
|
|
|
- pingResult.setSuccess();
|
|
|
- pingResult.setTime(System.currentTimeMillis());
|
|
|
- return ResultContent.buildSuccess(pingResult);
|
|
|
- }
|
|
|
-
|
|
|
- private void updateDeviceOnLine(String deviceId, String deviceName) {
|
|
|
+ /**
|
|
|
+ * 设置状态状态检查
|
|
|
+ *
|
|
|
+ * @param deviceId
|
|
|
+ * @param deviceName
|
|
|
+ */
|
|
|
+ private void updateDeviceOnLineCheck(String deviceId, String deviceName) {
|
|
|
DeviceOnLineInfo entity = deviceOnLineInfoDao.findTopByDeviceId(deviceId);
|
|
|
if (ObjectUtils.isEmpty(entity)) {
|
|
|
entity = new DeviceOnLineInfo();
|
|
|
@@ -233,23 +226,32 @@ public class DevicePingInfoService extends SuperService {
|
|
|
}
|
|
|
if (onLineState != deviceInfo.getOnLineState()) {
|
|
|
log.info("设备在线状态改变:{} {}", deviceInfo.getDeviceName(), onLineState);
|
|
|
- updateDeviceOnLine(deviceInfo.getId(), deviceInfo.getDeviceId(), onLineState);
|
|
|
+ updateDeviceOnLine(deviceInfo, onLineState);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 更新设备在线状态
|
|
|
*
|
|
|
- * @param id
|
|
|
- * @param deviceId
|
|
|
+ * @param deviceInfo
|
|
|
* @param onLineState
|
|
|
*/
|
|
|
- private void updateDeviceOnLine(String id, String deviceId, OnLineState onLineState) {
|
|
|
+ private void updateDeviceOnLine(DeviceInfo deviceInfo, OnLineState onLineState) {
|
|
|
+ if (ObjectUtils.isEmpty(deviceInfo) || onLineState == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ String id = deviceInfo.getId();
|
|
|
+ String deviceId = deviceInfo.getDeviceId();
|
|
|
Map<String, Object> standardData = new HashMap<String, Object>();
|
|
|
standardData.put("id", id);
|
|
|
standardData.put("onLineState", onLineState);
|
|
|
if (onLineState == OnLineState.OffLine) {
|
|
|
+ // 离线
|
|
|
standardData.put("lastOfflineTime", System.currentTimeMillis());
|
|
|
+ } else if (onLineState == OnLineState.OnLine) {
|
|
|
+ // 在线
|
|
|
+ standardData.put("lastOnlineTime", System.currentTimeMillis());
|
|
|
+ standardData.put("lastOnlineTimeStr", DateUtils.paresTime(System.currentTimeMillis(), DateUtils.FORMAT_LONG));
|
|
|
}
|
|
|
commonService.updateData(standardData, DeviceInfo.class.getSimpleName());
|
|
|
|
|
|
@@ -257,7 +259,9 @@ public class DevicePingInfoService extends SuperService {
|
|
|
where.put("deviceId", deviceId);
|
|
|
Map<String, Object> updateData = new HashMap<>();
|
|
|
updateData.put("onLineState", onLineState);
|
|
|
+
|
|
|
commonService.updateData(where, updateData, IotThing2Device.class.getSimpleName());
|
|
|
+ commonService.updateData(where, updateData, GateWay2Device.class.getSimpleName());
|
|
|
|
|
|
// 通知业务平台
|
|
|
deviceSyncFullCardService.noticeSyncDeviceOnlineStateChange(deviceId);
|