TRX 1 jaar geleden
bovenliggende
commit
d209a84c2a

+ 2 - 1
src/main/java/com/zswl/dataservice/service/artemis/OperationMessageService.java

@@ -206,7 +206,7 @@ public class OperationMessageService {
             Long time = jsonObject.getLong("time");
             Long ttl = jsonObject.getLong("ttl");
             String event = jsonObject.getStr("event");
-            String gateWayId = jsonObject.getStr("gateWayId");
+            String gateWayId = jsonObject.getStr("gatewayId");
             boolean isTimeOut = false;
             if (System.currentTimeMillis() > (time + ttl)) {
                 isTimeOut = true;
@@ -292,6 +292,7 @@ public class OperationMessageService {
             String handleMsg = "处理成功";
             JSONObject jsonObject1 = (JSONObject) json.get("data");
             jsonObject1.put("mqttDataId", entity.getDataId());
+            jsonObject1.put("GateWayId", entity.getGateWayId());
             String DeviceId = jsonObject1.getStr("DeviceId");
             entity.setDeviceId(DeviceId);
             try {

+ 19 - 0
src/main/java/com/zswl/dataservice/service/base/CommonService.java

@@ -44,4 +44,23 @@ public class CommonService {
         return updateResult.getUpsertedId();
     }
 
+    public Object updateData(Map<String, Object> where, Map<String, Object> standardData, String collectionName) {
+        collectionName = CommonUtil.getCollectionName(collectionName);
+        Criteria criteria = new Criteria();
+        if (where != null) {
+            where.forEach((key, value) -> {
+                criteria.and(key).is(value);
+            });
+        }
+        Query query = new Query(criteria);
+        Update update = new Update();
+        standardData.forEach((key, value) -> {
+            if (!"id".equals(key)) {
+                update.set(key, value);
+            }
+        });
+        UpdateResult updateResult = mongoTemplate.upsert(query, update, collectionName);
+        return updateResult.getUpsertedId();
+    }
+
 }

+ 35 - 0
src/main/java/com/zswl/dataservice/service/mqtt/DevicePingInfoService.java

@@ -3,12 +3,16 @@ 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.github.microservice.models.hxz.GateWayPingInfoParam;
+import com.github.microservice.models.hxz.PingResult;
 import com.zswl.dataservice.dao.mqtt.DeviceInfoDao;
 import com.zswl.dataservice.dao.mqtt.DevicePingInfoDao;
+import com.zswl.dataservice.dao.mqtt.GateWayInfoDao;
 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.domain.mqtt.GateWayInfo;
 import com.zswl.dataservice.service.base.CommonService;
 import com.zswl.dataservice.service.base.SuperService;
 import com.zswl.dataservice.service.sync.DeviceSyncFullCardService;
@@ -48,6 +52,9 @@ public class DevicePingInfoService extends SuperService {
     @Autowired
     DeviceSyncFullCardService deviceSyncFullCardService;
 
+    @Autowired
+    GateWayInfoDao gateWayInfoDao;
+
     @ExecuteAnnotationServiceMethod(value = "devicePing", remark = "设备心跳")
     public ResultContent<Object> devicePing(String dataStr) {
         DevicePingInfoParam param = JSONUtil.toBean(dataStr, DevicePingInfoParam.class);
@@ -77,6 +84,34 @@ public class DevicePingInfoService extends SuperService {
         return ResultContent.buildSuccess();
     }
 
+    @ExecuteAnnotationServiceMethod(value = "ping", remark = "设备心跳")
+    public ResultContent<Object> ping(String dataStr) {
+        log.info("dataStr: {}", dataStr);
+        PingResult pingResult = new PingResult();
+        GateWayPingInfoParam param = JSONUtil.toBean(dataStr, GateWayPingInfoParam.class);
+        GateWayInfo gateWayInfo = gateWayInfoDao.findTopByGateWayId(param.getGateWayId());
+        if (ObjectUtils.isNotEmpty(gateWayInfo)) {
+            Map<String, Object> where = new HashMap<>();
+            where.put("gateWayId", gateWayInfo.getGateWayId());
+            Map<String, Object> standardData = new HashMap<>();
+            standardData.put("onLineState", OnLineState.OnLine);
+            standardData.put("lastOnlineTime", System.currentTimeMillis());
+            commonService.updateData(where, standardData, GateWayInfo.class.getSimpleName());
+
+            // ping记录
+            DevicePingInfo devicePingInfo = new DevicePingInfo();
+            devicePingInfo.setGateWayId(param.getGateWayId());
+            devicePingInfo.setProjectInfoCode(gateWayInfo.getProjectInfoCode());
+            devicePingInfo.setTTL(new Date(System.currentTimeMillis() + 1 * 24L * 60 * 60 * 1000L));
+            devicePingInfo.setTimeStr(DateUtils.paresTime(System.currentTimeMillis(), DateUtils.FORMAT_LONG));
+            devicePingInfoDao.save(devicePingInfo);
+        }
+        pingResult.setSuccess();
+        pingResult.setTime(System.currentTimeMillis());
+        return ResultContent.buildSuccess(pingResult);
+    }
+
+
     /**
      * 检查
      */