|
@@ -0,0 +1,111 @@
|
|
|
|
|
+package com.zswl.dataservice.service.mqtt;
|
|
|
|
|
+
|
|
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
|
|
+import com.github.microservice.models.hxz.ConsumTransactionsModel;
|
|
|
|
|
+import com.github.microservice.models.hxz.DevicePingInfoParam;
|
|
|
|
|
+import com.zswl.dataservice.dao.mqtt.DeviceInfoDao;
|
|
|
|
|
+import com.zswl.dataservice.dao.mqtt.DevicePingInfoDao;
|
|
|
|
|
+import com.zswl.dataservice.domain.ExecuteAnnotationService;
|
|
|
|
|
+import com.zswl.dataservice.domain.ExecuteAnnotationServiceMethod;
|
|
|
|
|
+import com.zswl.dataservice.domain.mqtt.DeviceInfo;
|
|
|
|
|
+import com.zswl.dataservice.domain.mqtt.DevicePingInfo;
|
|
|
|
|
+import com.zswl.dataservice.service.base.CommonService;
|
|
|
|
|
+import com.zswl.dataservice.service.base.SuperService;
|
|
|
|
|
+import com.zswl.dataservice.service.sync.DeviceSyncFullCardService;
|
|
|
|
|
+import com.zswl.dataservice.type.DataState;
|
|
|
|
|
+import com.zswl.dataservice.utils.DateUtils;
|
|
|
|
|
+import com.zswl.dataservice.utils.bean.BeanUtils;
|
|
|
|
|
+import com.zswl.dataservice.utils.mqtt.type.OnLineState;
|
|
|
|
|
+import com.zswl.dataservice.utils.result.ResultContent;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.apache.commons.lang3.ObjectUtils;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.Date;
|
|
|
|
|
+import java.util.HashMap;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * @author TRX
|
|
|
|
|
+ * @date 2024/7/3
|
|
|
|
|
+ */
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+@Service
|
|
|
|
|
+@ExecuteAnnotationService
|
|
|
|
|
+public class DevicePingInfoService extends SuperService {
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ DevicePingInfoDao devicePingInfoDao;
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ DeviceInfoDao deviceInfoDao;
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ CommonService commonService;
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ DeviceSyncFullCardService deviceSyncFullCardService;
|
|
|
|
|
+
|
|
|
|
|
+ @ExecuteAnnotationServiceMethod(value = "devicePing", remark = "设备心跳")
|
|
|
|
|
+ public ResultContent<Object> devicePing(String dataStr) {
|
|
|
|
|
+ DevicePingInfoParam param = JSONUtil.toBean(dataStr, DevicePingInfoParam.class);
|
|
|
|
|
+ String deviceId = param.getDeviceId();
|
|
|
|
|
+ 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);
|
|
|
|
|
+ commonService.updateData(standardData, DeviceInfo.class.getSimpleName());
|
|
|
|
|
+
|
|
|
|
|
+ // ping记录
|
|
|
|
|
+ DevicePingInfo devicePingInfo = new DevicePingInfo();
|
|
|
|
|
+ devicePingInfo.setDeviceId(deviceId);
|
|
|
|
|
+ devicePingInfo.setDeviceName(deviceInfo.getDeviceName());
|
|
|
|
|
+ devicePingInfo.setGateWayId(param.getGateWayId());
|
|
|
|
|
+ devicePingInfo.setProjectInfoCode(deviceInfo.getProjectInfoCode());
|
|
|
|
|
+ devicePingInfo.setTTL(new Date(System.currentTimeMillis() + 7 * 24L * 60 * 60 * 1000L));
|
|
|
|
|
+ devicePingInfo.setTimeStr(DateUtils.paresTime(System.currentTimeMillis(), DateUtils.FORMAT_LONG));
|
|
|
|
|
+ devicePingInfoDao.save(devicePingInfo);
|
|
|
|
|
+ deviceSyncFullCardService.noticeSyncDeviceOnlineTimeChange(deviceId);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ log.info("心跳设备未找到: {}", deviceId);
|
|
|
|
|
+ }
|
|
|
|
|
+ return ResultContent.buildSuccess();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 检查
|
|
|
|
|
+ */
|
|
|
|
|
+ public void checkDeviceState() {
|
|
|
|
|
+ log.info("checkDeviceState: {}", DateUtils.paresTime(System.currentTimeMillis(), DateUtils.FORMAT_LONG));
|
|
|
|
|
+ List<DeviceInfo> list = deviceInfoDao.findAll();
|
|
|
|
|
+ if (ObjectUtils.isNotEmpty(list)) {
|
|
|
|
|
+ long time = System.currentTimeMillis();
|
|
|
|
|
+ for (DeviceInfo deviceInfo : list) {
|
|
|
|
|
+ OnLineState onLineState = OnLineState.OnLine;
|
|
|
|
|
+ List<DevicePingInfo> _list = devicePingInfoDao.findTop5ByDeviceIdOrderByCreateTimeDesc(deviceInfo.getDeviceId());
|
|
|
|
|
+ if (_list != null && _list.size() > 2) {
|
|
|
|
|
+ Long firstTime = _list.get(0).getCreateTime();
|
|
|
|
|
+ Long lastTime = _list.get(_list.size() - 1).getCreateTime();
|
|
|
|
|
+ long avgTime = (firstTime - lastTime) / _list.size();
|
|
|
|
|
+ log.info("avgTime: {} {}", (firstTime - lastTime), avgTime);
|
|
|
|
|
+ if ((time - firstTime) > avgTime) {
|
|
|
|
|
+ onLineState = OnLineState.OffLine;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (onLineState != deviceInfo.getOnLineState()) {
|
|
|
|
|
+ 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());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+}
|