Browse Source

FUNC:设备上下线时,更新设备状态

AlexJeo 7 months ago
parent
commit
72ea30ddf3

+ 38 - 0
src/main/java/io/emqx/exhook/ExServer.java

@@ -19,6 +19,7 @@ package io.emqx.exhook;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import io.emqx.exhook.MongoDB.MongoUtils;
+import io.emqx.exhook.Redis.RedisUtil;
 import io.grpc.Server;
 import io.grpc.ServerBuilder;
 import io.grpc.stub.StreamObserver;
@@ -28,6 +29,7 @@ import java.io.UnsupportedEncodingException;
 import java.text.SimpleDateFormat;
 import java.util.Arrays;
 import java.util.Date;
+import java.util.List;
 import java.util.concurrent.TimeUnit;
 import java.util.logging.Logger;
 
@@ -36,6 +38,7 @@ import com.mongodb.client.MongoDatabase;
 import com.mongodb.client.MongoCollection;
 import com.mongodb.client.model.Filters;
 import org.bson.Document;
+import org.bson.conversions.Bson;
 
 public class ExServer {
     private static final Logger logger = Logger.getLogger(ExServer.class.getName());
@@ -170,6 +173,23 @@ public class ExServer {
             Thread MessageLogs = new Thread() {
                 public void run() {
                     try {
+                        //更新设备在线状态
+                        RedisUtil.set(request.getConninfo().getClientid()+"-status","1");
+                        //更新MongoDB中设备状态
+                        Bson filter = Filters.eq("sn",request.getConninfo().getClientid());
+                        List<Document> list = MongoUtils.find("device_online",filter);
+                        if(list.isEmpty()){
+                            JSONObject jsonObject = new JSONObject();
+                            jsonObject.put("sn",request.getConninfo().getClientid());
+                            jsonObject.put("online",1);
+                            MongoUtils.save(jsonObject,"device_online");
+                        }else{
+                            for(Document document : list){
+                                document.put("online",1);
+                                MongoUtils.update(document);
+                            }
+                        }
+
                         JSONObject parseObject = new JSONObject();
                         parseObject.put("node",request.getConninfo().getNode());
                         parseObject.put("clientid",request.getConninfo().getClientid());
@@ -207,6 +227,24 @@ public class ExServer {
             Thread MessageLogs = new Thread() {
                 public void run() {
                     try {
+                        //更新设备离线状态
+                        RedisUtil.set(request.getClientinfo().getClientid()+"-status","0");
+
+                        //更新MongoDB中设备状态
+                        Bson filter = Filters.eq("sn",request.getClientinfo().getClientid());
+                        List<Document> list = MongoUtils.find("device_online",filter);
+                        if(list.isEmpty()){
+                            JSONObject jsonObject = new JSONObject();
+                            jsonObject.put("sn",request.getClientinfo().getClientid());
+                            jsonObject.put("online",0);
+                            MongoUtils.save(jsonObject,"device_online");
+                        }else{
+                            for(Document document : list){
+                                document.put("online",0);
+                                MongoUtils.update(document);
+                            }
+                        }
+
                         JSONObject parseObject = new JSONObject();
                         parseObject.put("node",request.getClientinfo().getNode());
                         parseObject.put("clientid",request.getClientinfo().getClientid());

+ 7 - 0
src/main/java/io/emqx/exhook/Redis/RedisUtil.java

@@ -26,6 +26,13 @@ public class RedisUtil {
         jedis.close();
     }
 
+    public static void set(String key ,String Value){
+        JedisPool pool = getPool(IP,PORT);
+        Jedis jedis = pool.getResource();
+        jedis.set(key,Value);
+        jedis.close();
+    }
+
     public static String get(String key){
         JedisPool pool = getPool(IP,PORT);
         Jedis jedis = pool.getResource();