diff --git a/src/main/java/com/zcloud/controller/keyProjects/VideoResourcesController.java b/src/main/java/com/zcloud/controller/keyProjects/VideoResourcesController.java index ccfb2cee..fb827612 100644 --- a/src/main/java/com/zcloud/controller/keyProjects/VideoResourcesController.java +++ b/src/main/java/com/zcloud/controller/keyProjects/VideoResourcesController.java @@ -61,6 +61,8 @@ public class VideoResourcesController extends BaseController { pd.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID()); //操作人 pd.put("ISDELETE", "0"); //是否删除 videoResourcesService.save(pd); + // 新增摄像头信息后更新最新的设备国标编号和通道国标编号 + videoResourcesService.updateAllNationalCode(); map.put("dockData", JSON.toJSONString(pd)); map.put("result", errInfo); }else { diff --git a/src/main/java/com/zcloud/mapper/dsno2/keyProjects/VideoResourcesMapper.java b/src/main/java/com/zcloud/mapper/dsno2/keyProjects/VideoResourcesMapper.java index 165eec31..1e6a0043 100644 --- a/src/main/java/com/zcloud/mapper/dsno2/keyProjects/VideoResourcesMapper.java +++ b/src/main/java/com/zcloud/mapper/dsno2/keyProjects/VideoResourcesMapper.java @@ -2,6 +2,7 @@ package com.zcloud.mapper.dsno2.keyProjects; import com.zcloud.entity.Page; import com.zcloud.entity.PageData; +import org.apache.ibatis.annotations.Param; import java.util.List; @@ -59,5 +60,32 @@ public interface VideoResourcesMapper { List listByOutsourced(Page page); List getPageOutSourceBySidlistPage(Page pd); + + /** + * 更新摄像头状态为在线 + * @param onlineDeviceList + */ + void updateOnlineStatus(@Param("onlineList") List onlineDeviceList); + + /** + * 更新摄像头状态为离线 + * @param offlineDeviceList + */ + void updateOfflineStatus(@Param("offlineList") List offlineDeviceList); + + /** + * 批量更新所有摄像头状态为离线 + */ + void updateOfflineStatusAll(); + + /** + * 更新所有设备的设备国标编号 + */ + void updateNationalCode(); + + /** + * 更新所有设备的通道国标编号 + */ + void updateTHONationalCode(); } diff --git a/src/main/java/com/zcloud/service/keyProjects/VideoResourcesService.java b/src/main/java/com/zcloud/service/keyProjects/VideoResourcesService.java index ff4f0307..f6c65716 100644 --- a/src/main/java/com/zcloud/service/keyProjects/VideoResourcesService.java +++ b/src/main/java/com/zcloud/service/keyProjects/VideoResourcesService.java @@ -53,5 +53,9 @@ public interface VideoResourcesService { List getPageOutSourceBySid(Page pd); + + void videoDevicesUpdateOnlineStatus(); + + void updateAllNationalCode(); } diff --git a/src/main/java/com/zcloud/service/keyProjects/impl/VideoResourcesServiceImpl.java b/src/main/java/com/zcloud/service/keyProjects/impl/VideoResourcesServiceImpl.java index 1565d9d3..babf277e 100644 --- a/src/main/java/com/zcloud/service/keyProjects/impl/VideoResourcesServiceImpl.java +++ b/src/main/java/com/zcloud/service/keyProjects/impl/VideoResourcesServiceImpl.java @@ -5,12 +5,17 @@ import com.zcloud.entity.PageData; import com.zcloud.mapper.dsno2.keyProjects.VideoManagerMapper; import com.zcloud.mapper.dsno2.keyProjects.VideoResourcesMapper; import com.zcloud.service.keyProjects.VideoResourcesService; +import com.zcloud.util.HeartBreakUtil; import com.zcloud.util.InitPageDataUtil; +import org.apache.commons.lang.StringUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * 说明:视频管理 @@ -107,5 +112,54 @@ public class VideoResourcesServiceImpl implements VideoResourcesService { return videoResourcesMapper.getPageOutSourceBySidlistPage(pd); } + @Override + public void videoDevicesUpdateOnlineStatus() { + try { + // 从摄像头接口中获取所有摄像头列表 + // 获取摄像头列表 + List channelList = HeartBreakUtil.getList(); + if (channelList != null && channelList.size() > 0) { + Map deviceMap = new HashMap<>(); + // 将设备列表的设备id和是否在线状态封装到map中 + for (HashMap device : channelList) { + // 判断设备状态是否在线,根据STATUS字段 + Boolean onlineStatus = false; + if (StringUtils.equals(device.get("Status").toString(),"ON")) { + onlineStatus = true; + } + // 将设备国标编号和通道国标编号拼在一起放到key里 + deviceMap.put(device.get("DeviceID").toString() + "-" + device.get("ID").toString(), onlineStatus); + } + // 将在线和不在线的设备id分开来 + List onlineDeviceList = new ArrayList<>(); // 在线设备id + // 将数据库中的所有设备查询出来 + List videoResourcesList = videoResourcesMapper.listAll(new PageData()); + for (PageData videoResources : videoResourcesList) { + // 将设备列表的设别国标编号和通道国标编号拼在一起 + String deviceCombId = videoResources.getString("NATIONAL_STANDARD_ID") + "-" + videoResources.getString("THO_NATIONAL_STANDARD_ID"); + if (deviceMap.containsKey(deviceCombId) && deviceMap.get(deviceCombId)) { + onlineDeviceList.add(videoResources.getString("VIDEO_RESOURCES_ID")); + } + } + // 批量更新摄像头在线状态 + videoResourcesMapper.updateOnlineStatus(onlineDeviceList); + videoResourcesMapper.updateOfflineStatus(onlineDeviceList); + } else { + // 获取摄像头列表为空 + System.out.println("获取摄像头列表为空"); + // 批量更新摄像头状态为离线 + videoResourcesMapper.updateOfflineStatusAll(); + } + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Override + public void updateAllNationalCode() { + videoResourcesMapper.updateNationalCode(); + videoResourcesMapper.updateTHONationalCode(); + } + } diff --git a/src/main/java/com/zcloud/util/HeartBreakUtil.java b/src/main/java/com/zcloud/util/HeartBreakUtil.java new file mode 100644 index 00000000..38d281cf --- /dev/null +++ b/src/main/java/com/zcloud/util/HeartBreakUtil.java @@ -0,0 +1,161 @@ +package com.zcloud.util; + +import com.alibaba.fastjson.JSONObject; +import com.zcloud.entity.PageData; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * 说明:常用工具 + * 作者:luoxiaobao + * 官网:www.qdkjchina.com + */ +public class HeartBreakUtil { + private static final String host = "https://arqsp.qhdsafety.com:10010"; + + // https://arqsp.qhdsafety.com:10010/api/v1/device/list?q=&start=10&limit=10&online=true&sort=ID&order=asc&_=1710834460090 +// https://arqsp.qhdsafety.com:10010/api/v1/device/list + private static String URLToken = "MvEXr3blIlazed1m8AfR22l7ZjLYXuF7WCAKL_wEwN7.oxNzExNTk0MjcyLCJwIjoiNTgwNjEwMGMyZDU2Y2UwMmIzZWYwYTZkZTg5NWQ5ZTliYjYwYTIxMGE4Yjc0NDE5OWE4YTEwZWM4OTYyMmMyOCIsInQiOjE3MTA5ODk0NzIsInUiOiIwYjUxNDkwMzcwIn0eyJlIj.GciOiJIUzI1NiIsInR5cCI6IkpXVCJ9eyJhb"; + + public static void main(String[] args) { +// getLogin(); +// getList(); + } + public static void logout (){ + + } + public static String getLogin (){ + String sasd = MD5.md5("Zykj@zcloud88888"); + PageData pd = new PageData(); + pd.put("username", "admin"); + pd.put("password", sasd); + pd.put("url_token_only", "true"); + Map returnMap = getGETTest(host + "/api/v1/login" , pd); + // 返回结果为空时将返回的token改为空 + if (Tools.isEmpty(returnMap)) { + return null; + } + String urlToken = returnMap.get("URLToken").toString(); + System.out.println(returnMap); + return urlToken; + } + public static List getList (){ + PageData pd = new PageData(); + pd.put("token", URLToken); + Map returnMap = getGETTest(host + "/api/v1/device/channellist" , pd); + if (Tools.isEmpty(returnMap)) { + return new ArrayList<>(); + } +// System.out.println(returnMap); + // System.out.println(deviceList); +// return (List) returnMap.get("DeviceList"); + return (List) returnMap.get("ChannelList"); + } + + /** + * 更新登录token并获取新的token + * @return + */ + public static boolean updateToken() { + String token = getLogin(); + // 如果返回token为空,则登录获取失败 + if (Tools.notEmpty(token)) { + URLToken = getLogin(); + return true; + } else { + return false; + } + } + + public static Map getGETTest (String httpUrl , PageData pd ){ + HttpURLConnection connection = null; + InputStream is = null; + BufferedReader br = null; + StringBuffer result = new StringBuffer(); +// ?username=admin&password=234f3424be5a75ad898a1b55f6e34d9e&url_token_only=true + StringBuffer nameValue = new StringBuffer(); + Map map = (Map)pd; + System.out.print("参数:{"); + for(Map.Entry entry : map.entrySet()){ + System.out.print(entry.getKey().toString() + ":" + entry.getValue().toString() + ","); + nameValue.append(entry.getKey().toString()+"=" + entry.getValue().toString() + "&"); + } + System.out.println("}"); + String parameter = ""; + if(nameValue.length()>0){ + parameter = "?"+nameValue.toString().substring(0,nameValue.length()-1); + } + + System.out.println( "地址:"+httpUrl + parameter); + try { + //创建连接 + URL url = new URL(httpUrl + parameter); + connection = (HttpURLConnection) url.openConnection(); + //设置请求方式 + connection.setRequestMethod("GET"); + //设置连接超时时间 + connection.setReadTimeout(15000); + //开始连接 + connection.connect(); + //获取响应数据 + if (connection.getResponseCode() == 200) { + //获取返回的数据 + is = connection.getInputStream(); + if (null != is) { + br = new BufferedReader(new InputStreamReader(is, "UTF-8")); + String temp = null; + while (null != (temp = br.readLine())) { + result.append(temp); + } + } + } + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (null != br) { + try { + br.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + if (null != is) { + try { + is.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + //关闭远程连接 + connection.disconnect(); + } +// return result.toString(); + + /** + * 转换成json,根据合法性返回json或者字符串 + */ + JSONObject jsonObject = null; + try{ + if (Tools.notEmpty(result.toString())) { + jsonObject = JSONObject.parseObject(result.toString()); + Map maps = new HashMap(); + maps = HttpClientService.parseJSON2Map(jsonObject); + return maps; + } else { + return null; + } + }catch (Exception e){ + e.printStackTrace(); + return null; + } + } +} diff --git a/src/main/java/com/zcloud/util/VideoHeartBreakScheduled.java b/src/main/java/com/zcloud/util/VideoHeartBreakScheduled.java new file mode 100644 index 00000000..36211bde --- /dev/null +++ b/src/main/java/com/zcloud/util/VideoHeartBreakScheduled.java @@ -0,0 +1,68 @@ +package com.zcloud.util; + +import com.zcloud.service.keyProjects.VideoResourcesService; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; +import org.springframework.core.annotation.Order; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; + +/** + * 查询摄像头是否在线定时任务 + * @author limingyu + * @date 2024-03-21 + */ +@Component +@Order(value = 2) +public class VideoHeartBreakScheduled implements ApplicationRunner { + + @Resource + private VideoResourcesService videoResourcesService; + + /** + * 定时更新摄像头在线状态 + */ + @Scheduled(cron ="0 0/5 * * * ?") //每5分钟执行一次 + private void videoEquipmentScheduled(){ + try { + System.out.println("============更新摄像头在线状态============"); + videoResourcesService.videoDevicesUpdateOnlineStatus(); + System.out.println("============更新摄像头状态成功============"); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + /** + * 每五天更新一次LiveGBS登录token(token有效期为7天) + */ + @Scheduled(cron ="0 0 0/5 * * ?") //测试 每5天执行一次 + private static void getLoginToken() throws Exception{ + try { + System.out.println("===============更新LiveGBS登录token============"); + boolean result = HeartBreakUtil.updateToken(); + if (result) { + System.out.println("===============更新LiveGBS登录token成功========="); + } else { + System.out.println("===============更新LiveGBS登录token失败========="); + } + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + /** + * 系统启动时执行一次 + * @throws Exception + */ + @Override + public void run(ApplicationArguments args) { + try{ + getLoginToken(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } +} diff --git a/src/main/resources/mybatis/dsno2/keyProjects/VideoManagerMapper.xml b/src/main/resources/mybatis/dsno2/keyProjects/VideoManagerMapper.xml index 735bf7d0..7a2afb83 100644 --- a/src/main/resources/mybatis/dsno2/keyProjects/VideoManagerMapper.xml +++ b/src/main/resources/mybatis/dsno2/keyProjects/VideoManagerMapper.xml @@ -175,10 +175,12 @@