From a9098372191b3c51995d41ee28404d1b71244d98 Mon Sep 17 00:00:00 2001
From: jiazx0107@163.com <jiazx0107@163.com>
Date: 星期二, 12 十二月 2023 18:41:04 +0800
Subject: [PATCH] 提交网关心跳和设备状态2

---
 src/main/java/com/fzzy/gateway/GatewayTimerScheduled.java |  118 +++++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 107 insertions(+), 11 deletions(-)

diff --git a/src/main/java/com/fzzy/gateway/GatewayTimerScheduled.java b/src/main/java/com/fzzy/gateway/GatewayTimerScheduled.java
index b26ff83..b29de93 100644
--- a/src/main/java/com/fzzy/gateway/GatewayTimerScheduled.java
+++ b/src/main/java/com/fzzy/gateway/GatewayTimerScheduled.java
@@ -1,21 +1,22 @@
 package com.fzzy.gateway;
 
-import com.fzzy.api.data.ApiParam;
-import com.fzzy.api.entity.ApiConfs;
-import com.fzzy.api.service.*;
-import com.fzzy.api.utils.ContextUtil;
-import com.fzzy.api.utils.RedisUtil;
+import com.alibaba.fastjson.JSON;
+import com.fzzy.api.data.GatewayDeviceProtocol;
+import com.fzzy.data.ConfigData;
 import com.fzzy.gateway.api.GatewayRemoteManager;
+import com.fzzy.gateway.data.BaseReqData;
+import com.fzzy.gateway.data.WeatherWebDto;
 import com.fzzy.gateway.entity.GatewayConf;
+import com.fzzy.gateway.entity.GatewayDevice;
 import com.fzzy.gateway.service.GatewayConfService;
+import com.fzzy.gateway.util.GatewayHttpUtil;
 import lombok.extern.slf4j.Slf4j;
-import org.apache.commons.lang.time.DateUtils;
-import org.springframework.beans.factory.annotation.Autowired;
+import org.apache.commons.lang.StringUtils;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;
 
 import javax.annotation.Resource;
-import java.util.Date;
+import java.util.Collection;
 import java.util.List;
 
 /**
@@ -25,25 +26,94 @@
 @Component(GatewayTimerScheduled.BEAN_ID)
 public class GatewayTimerScheduled {
 
+    public static String DEFAULT_URL = "https://v1.yiketianqi.com/api?unescape=1&version=v61&appid={appId}&appsecret={appsecret}&cityid={cityid}";
+    public static String DEFAULT_APP_ID = "49421971";
+    public static String DEFAULT_APP_SECRET = "JmJE48Fv";
+    public static String DEFAULT_CITYID = "101270101";//鎴愰兘
+
+
     public static final String BEAN_ID = "gateway.timerScheduled";
 
     @Resource
     private GatewayConfService confService;
-
     @Resource
     private GatewayRemoteManager gatewayRemoteManager;
+    @Resource
+    private ConfigData configData;
 
 
     /**
      * <p>
      * 鍥哄畾鏃堕棿锛氭瘡闂撮殧10鍒嗛挓鎵ц涓�娆�
      */
-    @Scheduled(cron = "0 0/10 * * * ? ")
+    @Scheduled(cron = "0 0/10 * * * ?")
     public void scheduled() {
 
         //缃戝叧鐨勫績璺虫墽琛�
         doHeartbeat();
 
+    }
+
+    /**
+     * <p>
+     * 姣忛棿闅�3绉掓墽琛屼竴娆′簨浠�
+     */
+    @Scheduled(cron = "0/3 * * * * ?")
+    public void scheduled3() {
+
+        //鎵цHTTP鍦扮璇锋眰
+        exeHttpWeightReq();
+    }
+
+
+    /**
+     * 姣忛棿闅�30鍒嗛挓鎵ц涓�娆�
+     */
+    @Scheduled(cron = "0 0/30 * * * ?")
+    public void scheduled30() {
+
+        //瀹氭椂鑾峰彇姘旇薄淇℃伅
+        doWeatherExe();
+    }
+
+    public void doWeatherExe() {
+
+        try {
+            String url = configData.getWeatherPath();
+
+            if (null == url) {
+                url = DEFAULT_URL;
+            }
+
+            url = url.replace("{appId}", DEFAULT_APP_ID).replace("{appsecret}", DEFAULT_APP_SECRET);
+
+            if (StringUtils.isNotEmpty(configData.getCityId())) {
+                url = url.replace("{cityid}", configData.getCityId());
+            } else {
+                url = url.replace("{cityid}", DEFAULT_CITYID);
+            }
+
+            log.debug("------姘旇薄璇锋眰淇℃伅URL---{}", url);
+            String result = GatewayHttpUtil.doGet(url);
+
+            if (null == result) {
+                log.error("褰撳墠澶栫綉鑾峰彇姘旇薄淇℃伅澶辫触鈥︹��");
+                return;
+            }
+            WeatherWebDto dto = JSON.parseObject(result, WeatherWebDto.class);
+            if (StringUtils.isNotEmpty(dto.getErrcode())) {
+                log.error("褰撳墠澶栫綉鑾峰彇姘旇薄淇℃伅寮傚父:{}", dto.getErrmsg());
+                return;
+            }
+
+            WeatherWebDto.contextMap.put("default", dto);
+
+
+            log.info("===========================绯荤粺瀹氭椂鑾疯幏鍙栨皵璞′俊鎭�===={}==================", dto);
+
+        } catch (Exception e) {
+
+        }
     }
 
 
@@ -63,5 +133,31 @@
             gatewayRemoteManager.getRemoteService(conf.getPushProtocol()).heartbeat(conf);
         }
     }
-    
+
+
+    /**
+     * 鎵ц鍦扮HTTP璇锋眰鍗忚
+     */
+    private void exeHttpWeightReq() {
+        //鑾峰彇璁惧
+        Collection<GatewayDevice> list = GatewayUtils.listDeviceWeight();
+
+        if (null == list || list.isEmpty()) return;
+
+        BaseReqData reqData;
+        for (GatewayDevice device : list) {
+
+            if (!GatewayDeviceProtocol.DEVICE_WEIGHT_HTTP.getCode().equals(device.getSyncProtocol())) {
+                continue;
+            }
+
+            reqData = new BaseReqData();
+            reqData.setDevice(device);
+            reqData.setDeviceName(device.getDeviceName());
+            reqData.setProductId(device.getProductId());
+            reqData.setDeviceId(device.getDeviceId());
+
+            gatewayRemoteManager.getSyncWeightService(device.getSyncProtocol()).syncWeightInfo(reqData);
+        }
+    }
 }

--
Gitblit v1.9.3