瀏覽代碼

提交更新代码,修改视频token更新机制

wanxl 2 年之前
父節點
當前提交
e5ed119991

+ 9 - 3
src/main/java/com/template/controller/CompaniesController.java

@@ -17,6 +17,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
 
 import org.springframework.web.bind.annotation.RestController;
 
+import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
@@ -86,12 +88,16 @@ public class CompaniesController {
     @RequestMapping("/getComByTown")
     public CommonResult getComByTown() {
         List<Map<String,Object>> posList=postionsService.listMaps(new QueryWrapper<Postions>());
-
+        List<Map<String,Object>> result =new ArrayList<Map<String, Object>>();
         for(Map<String,Object> postions:posList){
             List<Companies> comList=companiesService.list(new QueryWrapper<Companies>().eq("postion_id",postions.get("id")));
-            postions.put("children",comList);
+            if(comList!=null &&comList.size()>0){
+                postions.put("children",comList);
+                result.add(postions);
+            }
+
         }
-        return CommonResult.ok(posList);
+        return CommonResult.ok(result);
     }
 }
 

+ 11 - 0
src/main/java/com/template/controller/DevicesController.java

@@ -6,6 +6,7 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.template.common.result.CommonResult;
 import com.template.model.Configs;
 import com.template.model.Devices;
+import com.template.scheduled.ScheduledService;
 import com.template.services.ConfigsService;
 import com.template.services.DevicesService;
 import org.apache.ibatis.annotations.Param;
@@ -36,6 +37,9 @@ public class DevicesController {
     @Autowired
     private ConfigsService configsService;
 
+    @Autowired
+    private ScheduledService scheduledService;
+
     @RequestMapping("/getCount")
     public CommonResult getCount() {
         int total=devicesService.count(new QueryWrapper<>());
@@ -63,6 +67,13 @@ public class DevicesController {
         Configs configs=configsService.getOne(new QueryWrapper<Configs>().eq("name","ys_access_token"));
         JSONObject jsonObject = JSONObject.parseObject(configs.getValue());
         if(jsonObject!=null) {
+            Long time =jsonObject.getLong("expireTime");
+            long l = System.currentTimeMillis();
+            if(time<l){
+                scheduledService.updateAccessToken();
+                configs=configsService.getOne(new QueryWrapper<Configs>().eq("name","ys_access_token"));
+                jsonObject = JSONObject.parseObject(configs.getValue());
+            }
             map.put("accessToken", jsonObject.getString("accessToken"));
         }else {
             map.put("accessToken", "");

+ 1 - 1
src/main/java/com/template/mapper/CompaniesMapper.java

@@ -22,7 +22,7 @@ import java.util.Map;
 public interface CompaniesMapper extends BaseMapper<Companies> {
 
     @Select("SELECT p.* ,\n" +
-            "(SELECT COUNT(1)  from companies cc where cc.postion_id=p.id ) tcount \n" +
+            "(SELECT COUNT(1)  from companies cc where cc.postion_id=p.id and cc.deleted=0) tcount \n" +
             "from\n" +
             "postions p ")
     List<Map<String, Object>> getAllCount();

+ 20 - 7
src/main/java/com/template/scheduled/ScheduledService.java

@@ -126,10 +126,11 @@ public class ScheduledService {
         return count;
     }
 
-    //定时保存设备传感器假数据 后续删除
-    @Scheduled(cron="0 0 1 1/5 * ?")
+    //定时更新token
+    @Scheduled(cron="0 30 0 * * ?")
     @RequestMapping("/updateAccessToken")
     public void updateAccessToken() {
+        Configs configs=configsService.getOne(new QueryWrapper<Configs>().eq("name","ys_access_token"));
         RestTemplate client = new RestTemplate();
         MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
         map.add("appKey", appKey);
@@ -142,14 +143,19 @@ public class ScheduledService {
         JSONObject jsonObject = JSONObject.parseObject(result);
         String status = String.valueOf(jsonObject.get("code"));
         if("200".equals(status)){
-            Configs configs=configsService.getOne(new QueryWrapper<Configs>().eq("name","ys_access_token"));
             configs.setValue(jsonObject.getString("data"));
-            boolean a=configsService.saveOrUpdate(configs);
-            logger.info("更新accesstoken返回结果:"+a);
+            if(configs.getId()!=null){
+                boolean a=configsService.updateById(configs);
+                logger.info("更新accesstoken返回结果:"+a);
+            }else{
+                boolean a=configsService.save(configs);
+                logger.info("插入accesstoken返回结果:"+a);
+            }
+
         }
     }
 
-    //定时保存设备传感器假数据 后续删除
+    //定时更新视频地址
     @Scheduled(cron="0 0 1 * * ?")
     @RequestMapping("/updatePicture")
     public void updatePicture() {
@@ -158,6 +164,13 @@ public class ScheduledService {
         JSONObject jsonObject1 = JSONObject.parseObject(configs.getValue());
         String accessToken =jsonObject1.getString("accessToken");
         logger.info("查询accesstoken返回结果:"+accessToken);
+        Long time =jsonObject1.getLong("expireTime");
+        long l = System.currentTimeMillis();
+        if (time<l){
+            this.updateAccessToken();
+            configs=configsService.getOne(new QueryWrapper<Configs>().eq("name","ys_access_token"));
+            accessToken=JSONObject.parseObject(configs.getValue()).getString("accessToken");
+        }
         List<Devices> devicesList = devicesService.list(new QueryWrapper<>());
         for (Devices devices:devicesList){
             String[] arr=devices.getDeviceSerial().split(",");
@@ -173,7 +186,7 @@ public class ScheduledService {
                 //header.setBearerAuth(request.getHeader("Authorization").replace("Bearer ",""));
                 HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(map, header);
                 String result = client.postForObject(url+"/v2/live/address/get", httpEntity, String.class);
-                logger.info("获取accesstoken返回结果"+result);
+                logger.info("获取视频链接返回结果"+result);
                 JSONObject jsonObject = JSONObject.parseObject(result);
                 String status = String.valueOf(jsonObject.get("code"));
                 if("200".equals(status)){

+ 3 - 3
src/main/resources/application-dev.yml

@@ -1,5 +1,5 @@
 server:
-  port: 3001
+  port: 13001
 #  servlet:
 #    context-path: /nayong
 
@@ -14,8 +14,8 @@ spring:
       max-request-size: 150MB
   datasource:
     username: root
-    password: Password123.
-    url: jdbc:mysql://172.16.40.109:3306/fangcang_new?useUnicode=tru&characterEncoding=UTF-8&useSSL=false&useAffectedRows=true&allowPublicKeyRetrieval=true&allowMultiQueries=true&rewriteBatchedStatements=true&serverTimezone=Asia/Shanghai
+    password: chuanghai@2022
+    url: jdbc:mysql://101.33.214.65:3306/fangcang_new?useUnicode=tru&characterEncoding=UTF-8&useSSL=false&useAffectedRows=true&allowPublicKeyRetrieval=true&allowMultiQueries=true&rewriteBatchedStatements=true&serverTimezone=Asia/Shanghai
     driver-class-name: com.mysql.cj.jdbc.Driver
     cache:
       type: redis